forked from len0rd/rockbox
lua add better memory stats
lua gives you a memory used number that only reflects the current allocations if fact it doesn't even give you a way to get the amount of ram free rb.mem_stats() seeks to fill this gap by marking the memory allocated for lua with a sentinel value which can later be checked to get a high water mark of the ram used by lua and a pretty good idea of how much ram is available Also includes an example script usage: used, allocd, free = rb.mem_stats() Change-Id: Ia282869f989848324d7d88c7df4827fdbce4fb4e
This commit is contained in:
parent
74258fca31
commit
ef34126913
3 changed files with 100 additions and 3 deletions
|
@ -21,16 +21,62 @@
|
|||
#include "plugin.h"
|
||||
#include <tlsf.h>
|
||||
#include "lua.h"
|
||||
static const unsigned int sentinel = 0xBA5EFAC7;
|
||||
#define SENTINEL(n) (sentinel ^ (n))
|
||||
|
||||
static char *pluginbuf_ptr = NULL;
|
||||
static size_t pluginbuf_size = 0;
|
||||
static char *audiobuf_ptr = NULL;
|
||||
static size_t audiobuf_size = 0;
|
||||
|
||||
static void set_sentinel(void* buf, size_t size)
|
||||
{
|
||||
size_t i;
|
||||
unsigned int *b = (int*) buf;
|
||||
for(i = 0; i < size / sizeof(sentinel); i++)
|
||||
*b++ = SENTINEL(i);
|
||||
}
|
||||
|
||||
static size_t check_sentinel(void* buf, size_t size)
|
||||
{
|
||||
const size_t sz = size / sizeof(sentinel);
|
||||
size_t unused = 0;
|
||||
size_t i;
|
||||
unsigned int *b = (int*) buf;
|
||||
for(i = 0; i < sz; i++)
|
||||
if (b[i] == SENTINEL(i))
|
||||
{
|
||||
unused++;
|
||||
while(++i < sz && b[i] == SENTINEL(i) && ++unused)
|
||||
;;
|
||||
}
|
||||
return unused * sizeof(sentinel);
|
||||
}
|
||||
|
||||
size_t rock_get_allocated_bytes(void)
|
||||
{
|
||||
return pluginbuf_size + audiobuf_size;
|
||||
}
|
||||
|
||||
size_t rock_get_unused_bytes(void)
|
||||
{
|
||||
size_t unused = 0;
|
||||
if (pluginbuf_size)
|
||||
unused += check_sentinel(pluginbuf_ptr, pluginbuf_size);
|
||||
if (audiobuf_size)
|
||||
unused += check_sentinel(audiobuf_ptr, audiobuf_size);
|
||||
return unused;
|
||||
}
|
||||
|
||||
void *get_new_area(size_t *size)
|
||||
{
|
||||
static char *pluginbuf_ptr = NULL;
|
||||
static char *audiobuf_ptr = NULL;
|
||||
|
||||
if (pluginbuf_ptr == NULL)
|
||||
{
|
||||
pluginbuf_ptr = rb->plugin_get_buffer(size);
|
||||
|
||||
pluginbuf_size = *size;
|
||||
set_sentinel(pluginbuf_ptr, pluginbuf_size);
|
||||
|
||||
/* kill tlsf signature if any */
|
||||
memset(pluginbuf_ptr, 0, 4);
|
||||
|
||||
|
@ -43,6 +89,9 @@ void *get_new_area(size_t *size)
|
|||
/* grab audiobuffer */
|
||||
audiobuf_ptr = rb->plugin_get_audio_buffer(size);
|
||||
|
||||
audiobuf_size = *size;
|
||||
set_sentinel(audiobuf_ptr, audiobuf_size);
|
||||
|
||||
return audiobuf_ptr;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue