src/main/c/jnlua.c
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
	jint total, used;
	(void)ud; /* not used, always NULL */
	getluamemory(&total, &used);
	if (nsize == 0) {
		free(ptr);
		setluamemoryused(used - osize);
	}
	else {
		if (ptr == NULL) {
			if (total >= 0 && total - nsize >= used) {
				setluamemoryused(used + nsize);
				return malloc(nsize);
			}
		}
		else {
			/* Lua expects this to not fail if nsize <= osize, so we must allow
			   that even if it exceeds our current max memory. */
			if (nsize <= osize || (total - (nsize - osize) >= used)) {
				setluamemoryused(used + (nsize - osize));
				return realloc(ptr, nsize);
			}
		}
	}
	return NULL;
}
/* This custom allocator ensures a VM won't exceed its allowed memory use. */
This custom allocator ensures a VM won't exceed its allowed memory use.
This custom allocator ensures a VM won ' t exceed its allowed memory use .
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
	jint total, used;
	(void)ud; 
	getluamemory(&total, &used);
	if (nsize == 0) {
		free(ptr);
		setluamemoryused(used - osize);
	}
	else {
		if (ptr == NULL) {
			if (total >= 0 && total - nsize >= used) {
				setluamemoryused(used + nsize);
				return malloc(nsize);
			}
		}
		else {
			if (nsize <= osize || (total - (nsize - osize) >= used)) {
				setluamemoryused(used + (nsize - osize));
				return realloc(ptr, nsize);
			}
		}
	}
	return NULL;
}
static void * l_alloc ( void * ud , void * ptr , size_t osize , size_t nsize ) { jint total , used ; ( void ) ud ; getluamemory ( & total , & used ) ; if ( nsize == 0 ) { free ( ptr ) ; setluamemoryused ( used - osize ) ; } else { if ( ptr == NULL ) { if ( total >= 0 && total - nsize >= used ) { setluamemoryused ( used + nsize ) ; return malloc ( nsize ) ; } } else { if ( nsize <= osize || ( total - ( nsize - osize ) >= used ) ) { setluamemoryused ( used + ( nsize - osize ) ) ; return realloc ( ptr , nsize ) ; } } } return NULL ; }
This custom allocator ensures a VM won't exceed its allowed memory use.
This custom allocator ensures a VM won ' t exceed its allowed memory use .
/* not used, always NULL */ /* Lua expects this to not fail if nsize <= osize, so we must allow
			   that even if it exceeds our current max memory. */