Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <lua.h>
- #include <lauxlib.h>
- #include <lualib.h>
- #include <stdlib.h>
- #include <stdio.h>
- void bail(lua_State *L, const char *msg) {
- fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n", msg, lua_tostring(L, -1));
- lua_close(L);
- exit(1);
- }
- void print_gc_stats(lua_State *L, const char *msg) {
- int kb_in_use = lua_gc(L, LUA_GCCOUNT, 0);
- int kb_in_use_rem = lua_gc(L, LUA_GCCOUNTB, 0);
- printf("GC Stats (%s):\n", msg);
- for(int i = 0; i < 40; i++) {
- printf("-");
- }
- printf("\n");
- printf("Total memory in use: %d.%d KB\n", kb_in_use, kb_in_use_rem);
- printf("\n\n");
- }
- int main(void) {
- lua_State *L;
- L = luaL_newstate();
- luaL_openlibs(L);
- lua_gc(L, LUA_GCSETPAUSE, 150);
- lua_gc(L, LUA_GCSETSTEPMUL, 500);
- if (luaL_loadfile(L, "testbench.lua"))
- bail(L, "luaL_loadfile() failed");
- printf("In C, calling Lua with incremental GC\n\n");
- print_gc_stats(L, "Before running script");
- if (lua_pcall(L, 0, 0, 0))
- bail(L, "lua_pcall() failed");
- lua_getglobal(L, "test");
- if (lua_pcall(L, 0, 0, 0))
- bail(L, "lua_pcall() failed");
- print_gc_stats(L, "After running test function");
- // The GC does not work if we do not manually call the incremental steps below
- for(int i = 0; i < 2; i++) {
- lua_gc(L, LUA_GCSTEP, 0);
- }
- print_gc_stats(L, "After incremental GC cycle");
- lua_close(L);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement