summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/tlsf_helper.c
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2020-10-05 03:28:02 -0400
committerWilliam Wilgus <wilgus.william@gmail.com>2020-10-05 11:53:27 -0400
commitef34126913978c7cd6e5b0831f78ac8355f053f0 (patch)
tree4a719e60a52d19031ed485d8c9e9fd64e446ef97 /apps/plugins/lua/tlsf_helper.c
parent74258fca311b2d7e9d834ab8607f2bd326f67807 (diff)
downloadrockbox-ef34126913978c7cd6e5b0831f78ac8355f053f0.tar.gz
rockbox-ef34126913978c7cd6e5b0831f78ac8355f053f0.zip
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
Diffstat (limited to 'apps/plugins/lua/tlsf_helper.c')
-rw-r--r--apps/plugins/lua/tlsf_helper.c55
1 files changed, 52 insertions, 3 deletions
diff --git a/apps/plugins/lua/tlsf_helper.c b/apps/plugins/lua/tlsf_helper.c
index 097d39c8e4..52ef269bcd 100644
--- a/apps/plugins/lua/tlsf_helper.c
+++ b/apps/plugins/lua/tlsf_helper.c
@@ -21,16 +21,62 @@
21#include "plugin.h" 21#include "plugin.h"
22#include <tlsf.h> 22#include <tlsf.h>
23#include "lua.h" 23#include "lua.h"
24static const unsigned int sentinel = 0xBA5EFAC7;
25#define SENTINEL(n) (sentinel ^ (n))
24 26
25void *get_new_area(size_t *size) 27static char *pluginbuf_ptr = NULL;
28static size_t pluginbuf_size = 0;
29static char *audiobuf_ptr = NULL;
30static size_t audiobuf_size = 0;
31
32static void set_sentinel(void* buf, size_t size)
33{
34 size_t i;
35 unsigned int *b = (int*) buf;
36 for(i = 0; i < size / sizeof(sentinel); i++)
37 *b++ = SENTINEL(i);
38}
39
40static size_t check_sentinel(void* buf, size_t size)
41{
42 const size_t sz = size / sizeof(sentinel);
43 size_t unused = 0;
44 size_t i;
45 unsigned int *b = (int*) buf;
46 for(i = 0; i < sz; i++)
47 if (b[i] == SENTINEL(i))
48 {
49 unused++;
50 while(++i < sz && b[i] == SENTINEL(i) && ++unused)
51 ;;
52 }
53 return unused * sizeof(sentinel);
54}
55
56size_t rock_get_allocated_bytes(void)
26{ 57{
27 static char *pluginbuf_ptr = NULL; 58 return pluginbuf_size + audiobuf_size;
28 static char *audiobuf_ptr = NULL; 59}
60
61size_t rock_get_unused_bytes(void)
62{
63 size_t unused = 0;
64 if (pluginbuf_size)
65 unused += check_sentinel(pluginbuf_ptr, pluginbuf_size);
66 if (audiobuf_size)
67 unused += check_sentinel(audiobuf_ptr, audiobuf_size);
68 return unused;
69}
29 70
71void *get_new_area(size_t *size)
72{
30 if (pluginbuf_ptr == NULL) 73 if (pluginbuf_ptr == NULL)
31 { 74 {
32 pluginbuf_ptr = rb->plugin_get_buffer(size); 75 pluginbuf_ptr = rb->plugin_get_buffer(size);
33 76
77 pluginbuf_size = *size;
78 set_sentinel(pluginbuf_ptr, pluginbuf_size);
79
34 /* kill tlsf signature if any */ 80 /* kill tlsf signature if any */
35 memset(pluginbuf_ptr, 0, 4); 81 memset(pluginbuf_ptr, 0, 4);
36 82
@@ -43,6 +89,9 @@ void *get_new_area(size_t *size)
43 /* grab audiobuffer */ 89 /* grab audiobuffer */
44 audiobuf_ptr = rb->plugin_get_audio_buffer(size); 90 audiobuf_ptr = rb->plugin_get_audio_buffer(size);
45 91
92 audiobuf_size = *size;
93 set_sentinel(audiobuf_ptr, audiobuf_size);
94
46 return audiobuf_ptr; 95 return audiobuf_ptr;
47 } 96 }
48 97