title: set different memory allocation function in lua
body: <p>I'm doing the exercises from the third edition of "Programming in Lua" book by Roberto Ierusalimschy. I have a problem with a bug in my solution to exercise 32.1. The statement is provided as comment in the code.</p>
  
  <pre><code>/*
    Exercise 32.1:
      Write a library that allows a script to limit the total amount of memory
  used by its Lua state. It may offer a single function, setlimit, to set that
  limit.
      The library should set its own allocation funciton. This function, before
  calling the original allocator, checks the total memory in use and returns
  NULL if the requested memory exeeds the limit.
      (Hint: the library can use lua_gc to initialize its byte count when it
  starts. It also can use the user data of the allocation function to keep its
  state: the byte count, the current memory limit, etc.; remember to use the
  original user data when calling the original allocation function.)
  */
  
  #ifdef WIN32
    #define LUA_EXPORT __declspec(dllexport)
  #else
    #define LUA_EXPORT
  #endif
  
  #include &lt;lauxlib.h&gt;
  
  typedef struct MemLimitUData
  {
    size_t mem_limit;
    size_t currently_used;
    lua_Alloc original_alloc;
    void *original_ud;
  }
  MemLimitUData;
  
  static int l_setlimit(lua_State *L)
  {
    MemLimitUData *ud;
    size_t mem_limit = luaL_checkinteger(L, 1);
  
    lua_getallocf(L, &amp;ud);
    ud-&gt;mem_limit = mem_limit;
  
    return 0;
  }
  
  static int l_getlimit(lua_State *L)
  {
    MemLimitUData *ud;
  
    lua_getallocf(L, &amp;ud);
    lua_pushnumber(L, ud-&gt;mem_limit);
  
    return 1;
  }
  
  static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
  {
    MemLimitUData *udata = (MemLimitUData*)ud;
  
    if (udata-&gt;mem_limit != 0 &amp;&amp;
        udata-&gt;mem_limit &lt; udata-&gt;currently_used - osize + nsize)
    {
      return NULL;
    }
  
    udata-&gt;currently_used += nsize - osize;
    return udata-&gt;original_alloc(udata-&gt;original_ud, ptr, osize, nsize);
  }
  
  static const luaL_Reg memlimit[] =
  {
    { "setlimit", l_setlimit },
    { "getlimit", l_getlimit },
    { NULL, NULL }
  };
  
  int LUA_EXPORT luaopen_memlimit(lua_State *L)
  {
    MemLimitUData *ud =
      (MemLimitUData*)lua_newuserdata(L, sizeof(MemLimitUData));
  
    ud-&gt;mem_limit = 0;
    ud-&gt;currently_used =
      lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
    ud-&gt;original_alloc = lua_getallocf(L, &amp;ud-&gt;original_ud);
  
    lua_setallocf(L, l_alloc, ud);
    luaL_newlib(L, memlimit);
  
    return 1;
  }
  </code></pre>
  
  <p>When I build the source as <i>memlimit.dll</i> and use it from Lua script,
  <pre><code>local memlimit = require"memlimit"</code></pre> the program crashes when the script ends. When I use debugger to look for the problem, the problematic statement seems to be in Lua internals. The file is <i>lmem.c</i> line <b>84</b>:</p>
  
  <pre><code>newblock = (*g->frealloc)(g->ud, block, osize, nsize);</code></pre>
  
  <p>The used version of Lua is 5.2.3.</p>
  
  <p>What wrong I do to break the Lua memory management ?</p>