fluent / cfl

Tiny library for data structures management, call it c:\ floppy
Apache License 2.0
0 stars 6 forks source link

variant: extended API to support (optional) referenced strings and bytes #43

Closed edsiper closed 7 months ago

edsiper commented 7 months ago

Note: this PR bumps CFL to v0.5.0

This PR extends the variants functionality to support referenced values. When adding strings or bytes as variants, the current API creates a copy of those, however there are cases where the caller control its own buffer only wants to have a reference but not a new copy to avoid memory duplication; this is the case of an optimization we are doing in Fluent Bit.

This is a refactor that affects variants, kvlist and arrays, the following is a summary of the new APIs that changed (these are the latest versions):

variants

struct cfl_variant *cfl_variant_create_from_string_s(char *value, size_t value_size, int referenced);
struct cfl_variant *cfl_variant_create_from_bytes(char *value, size_t length, int referenced);

This PR also adds two new functions to set and retrieve the size of such buffers:

void cfl_variant_size_set(struct cfl_variant *var, size_t size);
size_t cfl_variant_size_get(struct cfl_variant *var);

kvlist

int cfl_kvlist_insert_bytes(struct cfl_kvlist *list, char *key, char *value,
                            char *value, size_t value_length,
                            int referenced);

int cfl_kvlist_insert_bytes_s(struct cfl_kvlist *list,
                              char *key, size_t key_size,
                              char *value, size_t value_length,
                              int referenced);

int cfl_kvlist_insert_string_s(struct cfl_kvlist *list,
                              char *key, size_t key_size,
                              char *value, size_t value_size,
                              int referenced);

array

int cfl_array_append_string_s(struct cfl_array *array, char *str, size_t str_len, int referenced);
int cfl_array_append_bytes(struct cfl_array *array, char *value, size_t length, int referenced);

In addition to the changes in array, I have added a new unit test for the array API.


These are breaking changes that once this library is updated, we will need changes also in CMetrics and CTraces