diff options
Diffstat (limited to 'apps/plugins/lua/tlsf_helper.c')
-rw-r--r-- | apps/plugins/lua/tlsf_helper.c | 55 |
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" |
24 | static const unsigned int sentinel = 0xBA5EFAC7; | ||
25 | #define SENTINEL(n) (sentinel ^ (n)) | ||
24 | 26 | ||
25 | void *get_new_area(size_t *size) | 27 | static char *pluginbuf_ptr = NULL; |
28 | static size_t pluginbuf_size = 0; | ||
29 | static char *audiobuf_ptr = NULL; | ||
30 | static size_t audiobuf_size = 0; | ||
31 | |||
32 | static 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 | |||
40 | static 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 | |||
56 | size_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 | |||
61 | size_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 | ||
71 | void *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 | ||