Skip to content

Commit

Permalink
iio.h: Update iio_context_get_xml()
Browse files Browse the repository at this point in the history
Instead of returning a string allocated alongside the IIO context, it
will now return a string allocated on the heap, that has to be freed
with free() afterwards. The function can now also return errors.

The advantage of this change is multiple:
- IIO contexts now don't have a huge XML string allocated during their
  whole lifetime;
- If the application does not call iio_context_get_xml(), then all the
  internal functions used to generate the XML string in the first place
  are dead code, and when building statically, can be garbage-collected.

Signed-off-by: Paul Cercueil <[email protected]>
  • Loading branch information
pcercuei committed Jan 31, 2024
1 parent 26d5f6e commit 6f43ba0
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 17 deletions.
15 changes: 2 additions & 13 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ static ssize_t iio_snprintf_context_xml(char *ptr, ssize_t len,
}

/* Returns a string containing the XML representation of this context */
static char * iio_context_create_xml(const struct iio_context *ctx)
char * iio_context_get_xml(const struct iio_context *ctx)
{
ssize_t len;
char *str;
Expand Down Expand Up @@ -251,11 +251,6 @@ void iio_context_set_pdata(struct iio_context *ctx, struct iio_context_pdata *d)
ctx->pdata = d;
}

const char * iio_context_get_xml(const struct iio_context *ctx)
{
return ctx->xml;
}

const char * iio_context_get_name(const struct iio_context *ctx)
{
return ctx->name;
Expand Down Expand Up @@ -284,7 +279,6 @@ void iio_context_destroy(struct iio_context *ctx)
for (i = 0; i < ctx->nb_devices; i++)
free_device(ctx->devices[i]);
free(ctx->devices);
free(ctx->xml);
free(ctx->description);
free(ctx->git_tag);
free(ctx->pdata);
Expand Down Expand Up @@ -340,12 +334,7 @@ int iio_context_init(struct iio_context *ctx)
for (i = 0; i < ctx->nb_devices; i++)
reorder_channels(ctx->devices[i]);

if (ctx->xml)
return 0;

ctx->xml = iio_context_create_xml(ctx);

return iio_err(ctx->xml);
return 0;
}

unsigned int iio_context_get_version_major(const struct iio_context *ctx)
Expand Down
2 changes: 0 additions & 2 deletions iio-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ struct iio_context {
struct iio_device **devices;
unsigned int nb_devices;

char *xml;

char **values;
struct iio_attr_list attrlist;

Expand Down
5 changes: 3 additions & 2 deletions include/iio/iio.h
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,9 @@ __api __pure const char * iio_context_get_version_tag(const struct iio_context *

/** @brief Obtain a XML representation of the given context
* @param ctx A pointer to an iio_context structure
* @return A pointer to a static NULL-terminated string */
__api __check_ret __pure const char * iio_context_get_xml(const struct iio_context *ctx);
* @return On success, an allocated string. Must be deallocated with free().
* @return On failure, a pointer-encoded error is returned */
__api __check_ret char * iio_context_get_xml(const struct iio_context *ctx);


/** @brief Get the name of the given context
Expand Down

0 comments on commit 6f43ba0

Please sign in to comment.