lua-users home
lua-l archive

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


> About memory management, all calls are concentrated in a 
> single point, so it is quite easy to change. Is it really a 
> good idea to go a step further and implement that through a 
> "callback" function?

Actually, I take that last message back.  That's what I did in Amped.
What I do now is:

void *luaM_realloc (lua_State *L, void *block, lu_mem oldsize, lu_mem
size) {
  if (size == 0) {
//    l_free(block, oldsize);  /* block may be NULL; that is OK for free
*/
    (*G(L)->freeFunc)(block, G(L)->memData);
    luaM_setname(L, NULL);
    block = NULL;
  }
  else if (size >= MAX_SIZET)
    luaD_error(L, "memory allocation error: block too big");
  else {
//    block = l_realloc(block, oldsize, size);
    block = (*G(L)->reallocFunc)(block, size, G(L)->memData,
L->allocName, L->allocFlags);
    luaM_setname(L, NULL);
    if (block == NULL) {
      if (L)
        luaD_breakrun(L, LUA_ERRMEM);  /* break run without error
message */
      else return NULL;  /* error before creating state! */
    }
  }
  if (L && G(L)) {
    G(L)->nblocks -= oldsize;
    G(L)->nblocks += size;
  }
  return block;
}

Note the G(L)->freeFunc and G(L)->reallocFunc.  They are implemented in
lstate.h's global_State.

Josh