use slot size of 128 for twnlua allocator

This commit is contained in:
veclavtalica 2025-01-12 03:20:27 +03:00
parent e8b02570a2
commit 192907a0db

View File

@ -44,13 +44,12 @@ static int physfs_loader(lua_State *L) {
/* WARN! experimental and will probably be removed */
/* it was an attempt to ease memory usage problems posed by using lua */
/* it saved ~100MiB, but it still hogs ~500MiB for no reason on my PC */
/* it is an attempt to ease memory usage problems posed by using lua, partially successful */
static void *custom_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud;
/* small allocations are placed in slots, as there's a big chance they will not need to be resized */
static char slots[1024][64];
static char slots[1024][128];
static int16_t free_slots[1024] = { [0] = -1 };
static size_t free_slot_count = 1024;
@ -59,26 +58,26 @@ static void *custom_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
free_slots[i] = (int16_t)i;
if (nsize == 0) {
if (ptr && (char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][64-1])
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 64);
else
if (ptr && (char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][128-1])
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 128);
else if (osize)
SDL_free(ptr);
return NULL;
} else {
if (!ptr && nsize <= 64 && free_slot_count > 0) {
if (!ptr && nsize <= 128 && free_slot_count > 0) {
/* use a slot */
return slots[free_slots[--free_slot_count]];
}
if ((char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][64-1]) {
if ((char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][128-1]) {
/* still fits */
if (nsize <= 64)
if (nsize <= 128)
return ptr;
/* move from slot to dynamic memory */
void *mem = SDL_malloc(nsize);
SDL_memcpy(mem, ptr, osize);
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 64);
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 128);
return mem;
}