Compare commits

..

No commits in common. "8d67e4400906cdef2a33fb948dbbe50a0fb549b7" and "e8b02570a29d8df0094e096983e31be451b56748" have entirely different histories.

2 changed files with 15 additions and 16 deletions
apps/twnlua

View File

@ -8,6 +8,9 @@ with open(sys.argv[1], 'r') if sys.argv[1] != "-" else sys.stdin as f:
api = json.loads(api_source)
# TODO: use lua_to--x functions for defaults, so that fewer function calls are made
# TODO: use lua_createtable for preallocation
def default(parameter):
basetype = parameter["type"].rsplit(' *', 1)[0]
if parameter["type"] == "float":
@ -69,18 +72,13 @@ for procedure, procedure_desc in api["procedures"].items():
binding += " %s %s;\n" % (parameter["type"], parameter["name"])
binding += " lua_getfield(L, 1, \"%s\");\n" % parameter["name"]
if "default" in parameter and parameter["type"] != "float":
if "default" in parameter:
binding += " if (lua_isnoneornil(L, -1))\n"
binding += " %s = %s;\n" % (parameter["name"], default(parameter))
binding += " else\n "
if parameter["type"] == "float":
if "default" in parameter:
binding += " int is_%s_num;\n" % parameter["name"]
binding += " %s = (float)lua_tonumberx(L, -1, &is_%s_num);\n" % (parameter["name"], parameter["name"]);
binding += " if (!is_%s_num) %s = %s;\n" % (parameter["name"], parameter["name"], default(parameter))
else:
binding += " %s = (float)lua_tonumber(L, -1);\n" % (parameter["name"]);
binding += " %s = (float)lua_tonumber(L, -1);\n" % (parameter["name"]);
elif parameter["type"] == "bool":
binding += " %s = lua_toboolean(L, -1);\n" % (parameter["name"]);
elif parameter["type"] == "char *":

View File

@ -44,12 +44,13 @@ static int physfs_loader(lua_State *L) {
/* WARN! experimental and will probably be removed */
/* it is an attempt to ease memory usage problems posed by using lua, partially successful */
/* 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 */
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][128];
static char slots[1024][64];
static int16_t free_slots[1024] = { [0] = -1 };
static size_t free_slot_count = 1024;
@ -58,26 +59,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][128-1])
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 128);
else if (osize)
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
SDL_free(ptr);
return NULL;
} else {
if (!ptr && nsize <= 128 && free_slot_count > 0) {
if (!ptr && nsize <= 64 && 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][128-1]) {
if ((char *)ptr >= &slots[0][0] && (char *)ptr <= &slots[1024-1][64-1]) {
/* still fits */
if (nsize <= 128)
if (nsize <= 64)
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) / 128);
free_slots[free_slot_count++] = (int16_t)(((uintptr_t)ptr - (uintptr_t)slots) / 64);
return mem;
}