在lua中设置不同的内存分配函数

我正在完成 Roberto Ierusalimschy 的《Programming in Lua》第三版中的练习。我在解决练习 32.1 中的一个 bug 时遇到了问题。该语句以代码注释的形式提供。

/*
  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 <lauxlib.h>

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, &ud);
  ud->mem_limit = mem_limit;

  return 0;
}

static int l_getlimit(lua_State *L)
{
  MemLimitUData *ud;

  lua_getallocf(L, &ud);
  lua_pushnumber(L, ud->mem_limit);

  return 1;
}

static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
{
  MemLimitUData *udata = (MemLimitUData*)ud;

  if (udata->mem_limit != 0 &&
      udata->mem_limit < udata->currently_used - osize + nsize)
  {
    return NULL;
  }

  udata->currently_used += nsize - osize;
  return udata->original_alloc(udata->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->mem_limit = 0;
  ud->currently_used =
    lua_gc(L, LUA_GCCOUNT, 0) * 1024 + lua_gc(L, LUA_GCCOUNTB, 0);
  ud->original_alloc = lua_getallocf(L, &ud->original_ud);

  lua_setallocf(L, l_alloc, ud);
  luaL_newlib(L, memlimit);

  return 1;
}

当我将源代码构建为 memlimit.dll 并从 Lua 脚本中使用它时,

local memlimit = require"memlimit"

当脚本结束时程序会崩溃。当我使用调试器查找问题时,问题似乎出现在 Lua 内部。文件为 lmem.c 的第 84 行:

newblock = (*g->frealloc)(g->ud, block, osize, nsize);

使用的 Lua 版本为 5.2.3。

我做错了什么来破坏 Lua 的内存管理?

点赞
用户107090
用户107090

我没有尝试过你的代码,但以下是当我阅读它时引起我注意的事情:

luaopen_memlimit 中的 ud 被创建为用户数据,但在 Lua 中没有被天价。将其传递给 lua_getallocf 不算是天价。当程序结束时,ud 可能会被 lua_close 搜集,使用你的 l_alloc 释放所有数据。你应该使用普通的 malloc 或原始的 allocf 来创建 ud

2013-12-20 23:33:25