diff options
author | William Wilgus <me.theuser@yahoo.com> | 2019-07-12 05:23:52 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2019-07-28 15:17:48 +0200 |
commit | 45bd14b392622cb58e967a24e4652c510b3d43e4 (patch) | |
tree | 22bd2e5cafc2d82ecc4773f83de7f86515b4db43 /apps/plugins/lua/lauxlib.c | |
parent | 4beafe16fafc2e5c59734ef065a6f8d23766520d (diff) | |
download | rockbox-45bd14b392622cb58e967a24e4652c510b3d43e4.tar.gz rockbox-45bd14b392622cb58e967a24e4652c510b3d43e4.zip |
Lua Add Emergency Garbage Collector
Derivative of work by RobertGabrielJakabosky
http://lua-users.org/wiki/EmergencyGarbageCollector
I've only implemented the not enough memory part and
expanded this idea to adding a mechanism to signal
the OOM condition of the plugin buffer which allows us to only
grab the playback buffer after garbage collection fails
(SO THE MUSIC KEEPS PLAYING AS LONG AS POSSIBLE)
Change-Id: I684fb98b540ffc01f7ba324ab5b761ceb59b9f9b
Diffstat (limited to 'apps/plugins/lua/lauxlib.c')
-rw-r--r-- | apps/plugins/lua/lauxlib.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c index 2e4b3b1e3c..acd7e0e636 100644 --- a/apps/plugins/lua/lauxlib.c +++ b/apps/plugins/lua/lauxlib.c | |||
@@ -13,7 +13,6 @@ | |||
13 | #include <string.h> | 13 | #include <string.h> |
14 | #include "lstring.h" /* ROCKLUA ADDED */ | 14 | #include "lstring.h" /* ROCKLUA ADDED */ |
15 | 15 | ||
16 | |||
17 | /* This file uses only the official API of Lua. | 16 | /* This file uses only the official API of Lua. |
18 | ** Any function declared here could be written as an application function. | 17 | ** Any function declared here could be written as an application function. |
19 | ** Note ** luaS_newlloc breaks this guarantee ROCKLUA ADDED | 18 | ** Note ** luaS_newlloc breaks this guarantee ROCKLUA ADDED |
@@ -34,7 +33,9 @@ | |||
34 | #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ | 33 | #define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \ |
35 | lua_gettop(L) + (i) + 1) | 34 | lua_gettop(L) + (i) + 1) |
36 | 35 | ||
37 | 36 | #ifndef LUA_OOM | |
37 | #define LUA_OOM(L) {} | ||
38 | #endif | ||
38 | /* | 39 | /* |
39 | ** {====================================================== | 40 | ** {====================================================== |
40 | ** Error-report functions | 41 | ** Error-report functions |
@@ -756,14 +757,30 @@ LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) { | |||
756 | 757 | ||
757 | 758 | ||
758 | static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { | 759 | static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { |
759 | (void)ud; | 760 | (void) osize; |
760 | (void)osize; | 761 | lua_State *L = (lua_State *)ud; |
762 | void *nptr; | ||
763 | |||
761 | if (nsize == 0) { | 764 | if (nsize == 0) { |
762 | free(ptr); | 765 | free(ptr); |
763 | return NULL; | 766 | return NULL; |
764 | } | 767 | } |
765 | else | 768 | |
766 | return realloc(ptr, nsize); | 769 | nptr = realloc(ptr, nsize); |
770 | if (nptr == NULL) { | ||
771 | if(L != NULL) | ||
772 | { | ||
773 | luaC_fullgc(L); /* emergency full collection. */ | ||
774 | nptr = realloc(ptr, nsize); /* try allocation again */ | ||
775 | } | ||
776 | |||
777 | if (nptr == NULL) { | ||
778 | LUA_OOM(L); /* if defined.. signal OOM condition */ | ||
779 | nptr = realloc(ptr, nsize); /* try allocation again */ | ||
780 | } | ||
781 | } | ||
782 | |||
783 | return nptr; | ||
767 | } | 784 | } |
768 | 785 | ||
769 | 786 | ||
@@ -779,6 +796,7 @@ static int panic (lua_State *L) { | |||
779 | 796 | ||
780 | LUALIB_API lua_State *luaL_newstate (void) { | 797 | LUALIB_API lua_State *luaL_newstate (void) { |
781 | lua_State *L = lua_newstate(l_alloc, NULL); | 798 | lua_State *L = lua_newstate(l_alloc, NULL); |
799 | lua_setallocf(L, l_alloc, L); /* allocator needs lua_State. */ | ||
782 | if (L) lua_atpanic(L, &panic); | 800 | if (L) lua_atpanic(L, &panic); |
783 | return L; | 801 | return L; |
784 | } | 802 | } |