#include "../include/regolith.h" #include "../include/regolith_lua.h" #include #include typedef struct counter { size_t live; size_t peak; } counter; static void track(counter *c, size_t add, size_t sub) { c->live = c->live + add - sub; if (c->live > c->peak) c->peak = c->live; } static void *c_alloc(size_t size, void *user) { void *p = malloc(size); if (p) track(user, size, 0); return p; } static void *c_realloc(void *ptr, size_t old_size, size_t new_size, void *user) { void *p = realloc(ptr, new_size); if (p) track(user, new_size, old_size); return p; } static void c_free(void *ptr, size_t size, void *user) { track(user, 0, size); free(ptr); } int main(void) { counter c = {0, 0}; rg_config cfg; rg_context *ctx; int rc; rg_config_init(&cfg); cfg.allocator.alloc = c_alloc; cfg.allocator.realloc = c_realloc; cfg.allocator.free = c_free; cfg.allocator.user = &c; ctx = rg_create(&cfg); if (!ctx) return 1; rc = rg_lua_open(ctx); if (rc == RG_OK) rc = rg_lua_dostring(ctx, "print('hello from ' .. _VERSION)", "=smoke"); if (rc == RG_OK) rc = rg_lua_dostring(ctx, "this is not lua", "=smoke_bad") == RG_ERR_LUA ? RG_OK : RG_ERR_GENERIC; printf("regolith %s, peak %zu bytes\n", rg_version(), c.peak); rg_destroy(ctx); if (c.live != 0) { printf("leak: %zu bytes still live\n", c.live); return 1; } return rc == RG_OK ? 0 : 1; }