liteserver / binn

Binary Serialization
Apache License 2.0
440 stars 58 forks source link

How to properly load and read a binn object from a data buffer without too many allocations? #42

Closed hydexon closed 2 years ago

hydexon commented 2 years ago

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.

kroggen commented 2 years ago

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.

hydexon commented 2 years ago

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.

kroggen commented 2 years ago

I wrote above:

Do not use binn_release !!!!