Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <assert.h>
- #include "lua.h"
- #include "lauxlib.h"
- static int num_allocated = 0;
- static int l_newthing(lua_State *l) {
- lua_newuserdata(l, sizeof(void*));
- lua_getglobal(l, "metatable");
- lua_setmetatable(l, -2);
- num_allocated++;
- printf(" alloc: %d objects\n", num_allocated);
- return 1;
- }
- static int l_gc(lua_State *l) {
- num_allocated--;
- printf("dealloc: %d objects\n", num_allocated);
- return 0;
- }
- int main(int argc, char **argv) {
- lua_State *l = luaL_newstate();
- lua_newtable(l);
- lua_pushstring(l, "__gc");
- lua_pushcfunction(l, l_gc);
- lua_rawset(l, -3);
- lua_setglobal(l, "metatable");
- lua_pushcfunction(l, l_newthing);
- lua_setglobal(l, "newthing");
- assert(!luaL_loadstring(l, "for i = 1,10000 do newthing() end"));
- lua_call(l, 0, 1);
- printf("done! inspect the process, then press enter to finish\n");
- getc(stdin);
- lua_close(l);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement