Skip to content

Instantly share code, notes, and snippets.

@ketralnis
Last active August 29, 2015 14:26
Show Gist options
  • Save ketralnis/4c177babb16ff77b6b7a to your computer and use it in GitHub Desktop.
Save ketralnis/4c177babb16ff77b6b7a to your computer and use it in GitHub Desktop.
void* l_alloc_restricted (void *ud, void *ptr, size_t osize, size_t nsize) {
_Executor* self = (_Executor*)ud;
if(ptr == NULL) {
/*
* <http://www.lua.org/manual/5.2/manual.html#lua_Alloc>:
* When ptr is NULL, osize encodes the kind of object that Lua is
* allocating.
*
* Since we don't care about that, just mark it as 0
*/
osize = 0;
}
if (nsize == 0) {
free(ptr);
self->memory_used -= osize; /* subtract old size from used memory */
return NULL;
}
if (self->memory_used + (nsize - osize) > self->memory_limit) {
/* too much memory in use */
return NULL;
}
ptr = realloc(ptr, nsize);
if (ptr) {
/* reallocation successful */
self->memory_used += (nsize - osize);
}
return ptr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment