lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You ignore osize.

As does the Lua default alloc function:

static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud;
(void)osize;
if (nsize == 0) {
  free(ptr);
  return NULL;
}
else
  return realloc(ptr, nsize);
}


Osize can imo be used to free allocated memory so the custom allocator doesn't need to remember the allocated size if it is lazy. Inside my simple test allocator I don't care about freeing. I just allocate and leak.
There must be another reason why it doesn't work.

Regards
Joerg

realloc implicitly "knows" osize, and has the additional effect of copying data from the new area to the old area. Calling a Lua allocator to change the size of a region must preserve the data in that region.