lua-users home
lua-l archive

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


> > Unless, of course, the Lua authors consider using the 
> HeapAlloc memory
> > functions when compiled for Windows.
> 
> You can change that from the "outside" (in your project settings),
> defining the macros l_realloc/l_free.

Sorry, too quick on the send button last time.  Anyhow, here is what I had to do to luaM_realloc to get it to work properly:

/*
** generic allocation routine.
*/
void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem size) {
  lua_assert((oldsize == 0) == (block == NULL));
  if (size == 0) {
    if (block != NULL) {
      l_free(block, oldsize);
      block = NULL;
    }
    else return NULL;  /* avoid `nblocks' computations when oldsize==size==0 */
  }
  else if (size >= MAX_SIZET)
    luaG_runerror(L, "memory allocation error: block too big");
  else {
    // >>>>  Original code: block = l_realloc(block, oldsize, size);
	 if(block == NULL)
	{
		block = HeapAlloc(GetProcessHeap(),0,size);
	} else
	{
		block = HeapReAlloc(GetProcessHeap(),0,block,size);
	}
    if (block == NULL) {
      if (L)
        luaD_throw(L, LUA_ERRMEM);
      else return NULL;  /* error before creating state! */
    }
  }
  if (L) {
    lua_assert(G(L) != NULL && G(L)->nblocks > 0);
    G(L)->nblocks -= oldsize;
    G(L)->nblocks += size;
  }
  return block;
}