static void *l_alloc_restricted (void *ud, void *ptr, size_t osize, size_t nsize) { const int MAX_SIZE = 1024; int *used = (int *)ud; if(ptr == NULL) { osize = 0; } if (nsize == 0) { free(ptr); *used -= osize; return NULL; } else { if (*used + (nsize - osize) > MAX_SIZE) return NULL; ptr = realloc(ptr, nsize); if (ptr) *used += (nsize - osize); return ptr; } }
For Lua to use your dispenser, you can use
int *ud = malloc(sizeof(int)); *ud = 0; lua_State *L = lua_State *lua_newstate (l_alloc_restricted, ud);
Note. I have not tested the source, but it should work.
source share