aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--api.c19
-rw-r--r--api_stash.c6
-rw-r--r--api_stash.h2
-rw-r--r--api_vm.c107
-rw-r--r--moony.h10
5 files changed, 90 insertions, 54 deletions
diff --git a/api.c b/api.c
index e871b8b..b79a001 100644
--- a/api.c
+++ b/api.c
@@ -325,9 +325,9 @@ _sink(LV2_Atom_Forge_Sink_Handle handle, const void *buf, uint32_t size)
if(ser->offset + size > ser->size)
{
const uint32_t new_size = ser->size * 2;
- if(ser->tlsf)
+ if(ser->moony)
{
- if(!(ser->buf = tlsf_realloc(ser->tlsf, ser->buf, new_size)))
+ if(!(ser->buf = moony_realloc(ser->moony, ser->buf, ser->size, new_size)))
return 0; // realloc failed
}
else
@@ -369,11 +369,11 @@ _stash(lua_State *L)
lframe->forge = &moony->stash_forge;
atom_ser_t ser = {
- .tlsf = moony->vm.tlsf, // use tlsf_realloc
+ .moony = moony,
.size = 1024,
.offset = 0
};
- ser.buf = tlsf_malloc(moony->vm.tlsf, ser.size);
+ ser.buf = moony_alloc(moony, ser.size);
if(ser.buf)
{
@@ -385,8 +385,9 @@ _stash(lua_State *L)
lua_call(L, 1, 0);
if(moony->stash_atom)
- tlsf_free(moony->vm.tlsf, moony->stash_atom);
+ moony_free(moony, moony->stash_atom, moony->stash_size);
moony->stash_atom = atom;
+ moony->stash_size = ser.size;
}
}
else
@@ -457,7 +458,7 @@ _state_save(LV2_Handle instance, LV2_State_Store_Function store,
}
atom_ser_t ser = {
- .tlsf = NULL,
+ .moony = NULL,
.size = 1024,
.offset = 0
};
@@ -893,8 +894,9 @@ moony_deinit(moony_t *moony)
moony->state_atom = NULL;
if(moony->stash_atom)
- tlsf_free(moony->vm.tlsf, moony->stash_atom);
+ moony_free(moony, moony->stash_atom, moony->stash_size);
moony->stash_atom = NULL;
+ moony->stash_size = 0;
moony_vm_deinit(&moony->vm);
}
@@ -1542,8 +1544,9 @@ moony_in(moony_t *moony, const LV2_Atom_Sequence *control, LV2_Atom_Sequence *no
moony_error(moony);
lua_gc(L, LUA_GCSTEP, 0);
- tlsf_free(moony->vm.tlsf, moony->stash_atom);
+ moony_free(moony, moony->stash_atom, moony->stash_size);
moony->stash_atom = NULL;
+ moony->stash_size = 0;
}
}
else if(property->body == moony->uris.moony_selection)
diff --git a/api_stash.c b/api_stash.c
index cc95c4b..506acec 100644
--- a/api_stash.c
+++ b/api_stash.c
@@ -24,7 +24,7 @@ _lstash__gc(lua_State *L)
atom_ser_t *ser = &lstash->ser;
if(ser->buf)
- tlsf_free(ser->tlsf, ser->buf);
+ moony_free(ser->moony, ser->buf, ser->size);
return 0;
}
@@ -81,10 +81,10 @@ _lstash(lua_State *L)
// initialize memory pool
atom_ser_t *ser = &lstash->ser;
- ser->tlsf = moony->vm.tlsf;
+ ser->moony = moony;
ser->size = 1024;
ser->offset = 0; // reset stash pointer
- ser->buf = tlsf_malloc(moony->vm.tlsf, ser->size);
+ ser->buf = moony_alloc(moony, ser->size);
if(!ser->buf)
lua_pushnil(L); // memory allocation failed
diff --git a/api_stash.h b/api_stash.h
index 1305c67..96e7750 100644
--- a/api_stash.h
+++ b/api_stash.h
@@ -27,7 +27,7 @@ typedef struct _atom_ser_t atom_ser_t;
typedef struct _lstash_t lstash_t;
struct _atom_ser_t {
- tlsf_t tlsf;
+ moony_t *moony; // use rt-memory pool?
uint32_t size;
uint8_t *buf;
uint32_t offset;
diff --git a/api_vm.c b/api_vm.c
index 825496e..dbbe20a 100644
--- a/api_vm.c
+++ b/api_vm.c
@@ -30,40 +30,12 @@
#define MEM_SIZE 0x40000UL // 256KB
-// rt
-static inline void *
-rt_alloc(moony_vm_t *vm, size_t len)
-{
- return tlsf_malloc(vm->tlsf, len);
-}
-
-// rt
-static inline void *
-rt_realloc(moony_vm_t *vm, size_t len, void *buf)
-{
- return tlsf_realloc(vm->tlsf, buf, len);
-}
-
-// rt
+//#define MOONY_LOG_MEM
+#ifdef MOONY_LOG_MEM
static inline void
-rt_free(moony_vm_t *vm, void *buf)
+_log_mem(moony_t *moony, void *ptr, size_t osize, size_t nsize)
{
- tlsf_free(vm->tlsf, buf);
-}
-
-// rt
-static void *
-lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
-{
- moony_vm_t *vm = ud;
- (void)osize;
-
- vm->used += nsize - (ptr ? osize : 0);
- if(vm->used > (vm->space >> 1))
- moony_vm_mem_extend(vm);
-
-#if 0
- moony_t *moony = (void *)vm - offsetof(moony_t, vm);
+ moony_vm_t *vm = &moony->vm;
if(moony->log)
{
char suffix = ' ';
@@ -78,27 +50,77 @@ lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
suffix = 'M';
used >>= 10;
}
- if(!ptr)
- lv2_log_trace(&moony->logger, "used: %4zu%c, old: %4zu, new: %4zu, type: '%s'",
- used, suffix, osize, nsize, lua_typename(vm->L, osize));
- else
- lv2_log_trace(&moony->logger, "used: %4zu%c, old: %4zu, new: %4zu, data: %p",
- used, suffix, osize, nsize, ptr);
+ lv2_log_trace(&moony->logger, "used: %4zu%c, old: %4zu, new: %4zu, data: %p",
+ used, suffix, osize, nsize, ptr);
}
+}
+#endif
+
+inline void *
+moony_alloc(moony_t *moony, size_t nsize)
+{
+ moony_vm_t *vm = &moony->vm;
+ vm->used += nsize;
+ if(vm->used > (vm->space >> 1))
+ moony_vm_mem_extend(vm);
+
+#ifdef MOONY_LOG_MEM
+ _log_mem(moony, NULL, 0, nsize);
+#endif
+
+ return tlsf_malloc(vm->tlsf, nsize);
+}
+
+inline void *
+moony_realloc(moony_t *moony, void *buf, size_t osize, size_t nsize)
+{
+ moony_vm_t *vm = &moony->vm;
+ vm->used -= osize;
+ vm->used += nsize;
+ if(vm->used > (vm->space >> 1))
+ moony_vm_mem_extend(vm);
+
+#ifdef MOONY_LOG_MEM
+ _log_mem(moony, buf, osize, nsize);
#endif
+ return tlsf_realloc(vm->tlsf, buf, nsize);
+}
+
+inline void
+moony_free(moony_t *moony, void *buf, size_t osize)
+{
+ moony_vm_t *vm = &moony->vm;
+ vm->used -= osize;
+ if(vm->used > (vm->space >> 1))
+ moony_vm_mem_extend(vm);
+
+#ifdef MOONY_LOG_MEM
+ _log_mem(moony, buf, osize, 0);
+#endif
+
+ tlsf_free(vm->tlsf, buf);
+}
+
+// rt
+static void *
+lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
+{
+ moony_t *moony = ud;
+ moony_vm_t *vm = &moony->vm;
+
if(nsize == 0)
{
if(ptr)
- rt_free(vm, ptr);
+ moony_free(moony, ptr, osize);
return NULL;
}
else
{
if(ptr)
- return rt_realloc(vm, nsize, ptr);
+ return moony_realloc(moony, ptr, osize, nsize);
else
- return rt_alloc(vm, nsize);
+ return moony_alloc(moony, nsize);
}
}
@@ -106,6 +128,7 @@ lua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
int
moony_vm_init(moony_vm_t *vm)
{
+ moony_t *moony = (void *)vm - offsetof(moony_t, vm);
memset(vm, 0x0, sizeof(moony_vm_t));
// initialize array of increasing pool sizes
@@ -124,7 +147,7 @@ moony_vm_init(moony_vm_t *vm)
vm->pool[0] = tlsf_get_pool(vm->tlsf);
vm->space += vm->size[0];
- vm->L = lua_newstate(lua_alloc, vm);
+ vm->L = lua_newstate(lua_alloc, moony);
if(!vm->L)
return -1;
diff --git a/moony.h b/moony.h
index 8363dc9..9e74419 100644
--- a/moony.h
+++ b/moony.h
@@ -272,6 +272,7 @@ struct _moony_t {
LV2_Atom *state_atom;
LV2_Atom *stash_atom;
+ uint32_t stash_size;
latom_driver_hash_t atom_driver_hash [DRIVER_HASH_MAX];
@@ -362,6 +363,15 @@ _moony_patch(patch_t *patch, LV2_Atom_Forge *forge, LV2_URID key,
return 0; // overflow
}
+void *
+moony_alloc(moony_t *moony, size_t nsize);
+
+void *
+moony_realloc(moony_t *moony, void *buf, size_t osize, size_t nsize);
+
+void
+moony_free(moony_t *moony, void *buf, size_t osize);
+
LV2_Atom_Forge_Ref
_sink(LV2_Atom_Forge_Sink_Handle handle, const void *buf, uint32_t size);