lua-users home
lua-l archive

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


David Kastrup wrote:
"kathrin_69@gmx.de" <kathrin_69@gmx.de> writes:

Hi,

I'm trying to replace the Lua default l_alloc function with my own
one. My aim is to have all memory Lua allocates inside a single array
of pre-allocated memory.

I've tried different implementations of a custom function with less
and less features. None of that worked (the library functions crashed
due to memory access error after a time).

Now I tried the very simplest version of a custom function I could
come up with. It just allocs and returns the memory asked for. It
doesn't care about freeing of memory, re-allocation, corruption
checks, fragmentation or anything else.


void* alloc_impl_manual( void* ptr, size_t osize, size_t nsize )
{
  if (nsize)
  {
      std::vector<unsigned char>* mem = new std::vector<unsigned
char>(nsize);
               //yes, it will leak. don't care for now

      return &(*mem)[0]; //returned memory position should be aligned
by new
  }
  return NULL;
}



The problems/crashes remains.
I must do some fundamental error or understand some detail wrong. Any
Ideas?

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