9 #define RTL_TLSF_DEBUG 1 11 #define DEBUG_TLSF_ALLOC (1<<0) 12 #define DEBUG_TLSF_FREE (1<<1) 13 #define DEBUG_TLSF_TRACE (1<<20) 16 # define _DBG(x, mask, fmt, ...) do{ if (mask & x) printf("%s: " fmt "\n", __FUNCTION__, __VA_ARGS__); } while(0) 18 # define _DBG(x, mask, fmt, ...) do { } while(0) 21 #define TLSF_POOL_MIN_SIZE 1*1014*1024 24 int tlsf_rtt_init_mp(
struct lua_tlsf_info *tlsf_inf,
size_t sz)
28 tlsf_inf->pool2 = NULL;
29 tlsf_inf->total_mem = 0;
31 if(sz < TLSF_POOL_MIN_SIZE) {
32 fprintf(stderr,
"error: requested tlsf pool size (0x%lx) too small\n", (
unsigned long) sz);
36 tlsf_inf->pool = malloc(sz);
39 fprintf(stderr,
"error failed to allocate: 0x%lx bytes\n", (
unsigned long) sz);
43 tlsf_inf->total_mem = rtl_init_memory_pool(sz, tlsf_inf->pool);
52 rtl_destroy_memory_pool(tlsf_inf->pool);
56 free(tlsf_inf->pool2);
60 static void tlsf_trace_hook(lua_State *L,
lua_Debug *ar)
63 lua_sethook(L, tlsf_trace_hook, 0, 0);
64 luaL_error(L,
"memory allocation in TLSF trace mode");
68 void* tlsf_alloc (
void *ud,
void *ptr,
size_t osize,
size_t nsize)
74 _DBG(DEBUG_TLSF_FREE, tlsf_inf->mask,
"freeing 0x%lx, osize=%lu, nsize=%lu",
75 (
unsigned long) ptr, (
unsigned long) osize, (
unsigned long) nsize);
76 rtl_free_ex(ptr, tlsf_inf->pool);
79 if(DEBUG_TLSF_TRACE & tlsf_inf->mask) {
80 lua_sethook(tlsf_inf->L, tlsf_trace_hook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT, 1);
82 _DBG(DEBUG_TLSF_ALLOC, tlsf_inf->mask,
"allocating 0x%lx, osize=%lu, nsize=%lu",
83 (
unsigned long) ptr, (
unsigned long) osize, (
unsigned long) nsize);
84 return rtl_realloc_ex(ptr, nsize, tlsf_inf->pool);
88 int tlsf_rtt_incmem(
struct lua_tlsf_info *tlsf_inf,
size_t sz)
90 if(tlsf_inf->pool2 != NULL)
91 luaL_error(tlsf_inf->L,
"tlsf_rtt_incmem: region already increased, (increasing cur. only possible once)");
93 if((tlsf_inf->pool2 = malloc(sz)) == NULL)
94 luaL_error(tlsf_inf->L,
"tlsf_rtt_incmem: failed to increase memory by %d bytes. Out of mem.");
96 tlsf_inf->total_mem += rtl_add_new_area(tlsf_inf->pool2, sz, tlsf_inf->pool);
107 lua_pushstring(tlsf_inf->L,
"tlsf_info");
108 lua_pushlightuserdata(tlsf_inf->L, tlsf_inf);
109 lua_rawset(tlsf_inf->L, LUA_REGISTRYINDEX);
114 lua_pushstring(L,
"tlsf_info");
115 lua_rawget(L, LUA_REGISTRYINDEX);
119 static int tlsf_trace(lua_State *L)
121 int argc, enable, ret;
124 argc = lua_gettop(L);
127 lua_pushboolean(L, tlsf_inf->mask & DEBUG_TLSF_TRACE);
132 enable = lua_toboolean(L, 1);
135 tlsf_inf->mask |= DEBUG_TLSF_TRACE;
137 lua_sethook(L, tlsf_trace_hook, 0, 1);
138 tlsf_inf->mask &= ~DEBUG_TLSF_TRACE;
145 static int tlsf_warn(lua_State *L)
147 int argc, enable, ret;
150 argc = lua_gettop(L);
153 lua_pushboolean(L, tlsf_inf->mask & (DEBUG_TLSF_ALLOC | DEBUG_TLSF_FREE));
158 enable = lua_toboolean(L, 1);
161 tlsf_inf->mask |= DEBUG_TLSF_ALLOC | DEBUG_TLSF_FREE;
163 tlsf_inf->mask &= ~(DEBUG_TLSF_ALLOC | DEBUG_TLSF_FREE);
168 static int tlsf_stats(lua_State *L)
172 lua_pushinteger(L, rtl_get_used_size(tlsf_inf->pool));
173 lua_pushinteger(L, rtl_get_max_size(tlsf_inf->pool));
174 lua_pushinteger(L, tlsf_inf->total_mem);
178 static const struct luaL_Reg tlsf_f [] = {
179 {
"stats", tlsf_stats },
180 {
"warn", tlsf_warn },
181 {
"trace", tlsf_trace },
185 void register_tlsf_api(lua_State *L)
187 luaL_register(L,
"tlsf", tlsf_f);