[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua list crashes with a linear allocator
- From: andre@...
- Date: Fri, 19 Dec 2014 18:51:24 +0000
> If ptr is NULL, osize is the type of the thing being allocated.
The manual says that "ptr is NULL if and only if osize is zero." Besides, my implementation closely follows the sample implementation in the user manual: http://www.lua.org/manual/5.1/manual.html#lua_Alloc
I tried to write an allocator that respects all the rules in the user manual, one by one:
static void* LuaAllocator( void* ud, void* ptr, size_t osize, size_t nsize )
{
linear_allocator_t* lalloc = (linear_allocator_t*)ud;
void* nptr;
if ( osize == 0 && nsize == 0 ) /* this actually happens */
{
/* When nsize is zero, the allocator must return NULL. */
nptr = 0;
}
else if ( osize == 0 && nsize != 0 )
{
/* When nsize is not zero and osize is zero, the allocator should behave like malloc. */
nptr = linear_allocator_malloc( lalloc, nsize );
}
else if ( osize != 0 && nsize == 0 )
{
/* If osize is not zero, it should free the block pointed to by ptr. */
nptr = 0; /* When nsize is zero, the allocator must return NULL. */
}
else /* osize != 0 && nsize != 0 */
{
/* When nsize and osize are not zero, the allocator behaves like realloc. */
if ( osize < nsize )
{
/* only allocate memory if the new size is greater than the old size */
nptr = linear_allocator_malloc( lalloc, nsize );
}
else
{
/* otherwise we recycle the memory */
nptr = ptr;
}
}
printf( "ptr=%p osize=%4u nsize=%4u nptr=%p used=%.8u\n", ptr, osize, nsize, nptr, lalloc->current_offset );
fflush( stdout );
return nptr;
}
But I got the same crash. Even if I try *not* to recycle the memory the crash is the same.
Thanks,
Andre Leiradella