-
Type: New Feature
-
Resolution: Unresolved
-
Priority: Trivial - P5
-
None
-
Affects Version/s: None
-
Component/s: libbson
-
Fully Compatible
I would suggest adding a new API function, bson_init_static_from_buffer which is similar in most ways to bson_new_from_buffer, and in others to bson_init_static, but differs from the latter in that the buffer supplied is not read-only, and from the former in that it does not actually allocate the bson, but instead takes a pointer to one as an extra initial parameter and returns with the bson initialized so that appending to it will utilize the realloc function supplied with the allocation context as a parameter.
To that end, the prototype would probably look something like this:
void bson_init_static_from_buffer (bson_t *bson, uint8_t **buf, size_t *buf_len, bson_realloc_func realloc_func, void *realloc_func_ctx)
Behaviour for this function would be virtually identical to that of bson_new_from_buffer except that it does not allocate the bson_t, and it sets up the bson so that calling bson_destroy does not attempt to free it, nor does it try to free the buffer it points to. It would, however, if `realloc_func` was not null, call the reallocation function with the current buffer and a size of 0, which bears the responsibility of freeing the memory used.
The most obvious use case for such a function would be to allow each bson to have its own memory allocator (presumably accessed via the realloc_func_ctx pointer) without imposing the requirement that all bsons obtain their memory from a single pool, as bson_set_mem_vtable does currently.