How to properly load and read a binn object from a data buffer without too many allocations?
hydexon opened this issue · comments
Hi, having troubles about figuring out how to use binn without wasting too much memory since i'm into a resource-limited environment (retrocomputing, DOS), and i want to know if opening and reading data also makes additional allocations, since i'm a bit confused how the inner works in binn.
this is my code for example:
int mapsize = 0;
char* mapdata = vfs_load(mapfile, &mapsize);
if(mapdata == NULL)
return -1;
binn* maproot = binn_open(mapdata);
binn_release(maproot);
free(mapdata);
binn_open
just allocates just the binn structure and nothing else and fetches the data directly from the buffer without copying?, and if is for string (i assume i have to copy them with memcpy, before i free the data buffer) and objects too?
i use mostly binn for reading map data, not writing.
As stated here:
binn_open
will allocate memory for a binn structure.
binn_load
accepts a pointer to a pre-allocated structure, like in the stack.
Example usage of binn_load
:
binn map;
BOOL result = binn_load(data, &map);
If you use binn_free
with the above map
variable it will just clear the memory (in this case). You don't need to use it.
Do not use binn_release
. It is intended to be used with fully allocated items.
binn_open
makes just a single allocation of size = sizeof(binn)
, that can be released with binn_free
When reading a string/blob/container, it will return a pointer to a region inside the the given data
. So if you release memory of data
then the pointers will point to invalid memory. You can either postpone the memory release or copy the string/blob to somewhere.
so binn_release()
is only used for binn_open()
and only deallocates the binn structure that was allocated dynamically?,and i assmue it leaves the original raw data buffer intact which must be free'd manually.
I wrote above:
Do not use
binn_release
!!!!