diff options
Diffstat (limited to 'lib/rbcodec/codecs/libtremor/oggmalloc.c')
-rw-r--r-- | lib/rbcodec/codecs/libtremor/oggmalloc.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libtremor/oggmalloc.c b/lib/rbcodec/codecs/libtremor/oggmalloc.c new file mode 100644 index 0000000000..783e0f7240 --- /dev/null +++ b/lib/rbcodec/codecs/libtremor/oggmalloc.c | |||
@@ -0,0 +1,86 @@ | |||
1 | #include "os_types.h" | ||
2 | #include <tlsf.h> | ||
3 | |||
4 | #if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS) | ||
5 | #include <setjmp.h> | ||
6 | extern jmp_buf rb_jump_buf; | ||
7 | #define LONGJMP(x) longjmp(rb_jump_buf, x) | ||
8 | #elif defined(SIMULATOR) | ||
9 | #define LONGJMP(x) do { DEBUGF("Vorbis: allocation failed!\n"); return NULL; } while (false) | ||
10 | #else | ||
11 | #define LONGJMP(x) return NULL | ||
12 | #endif | ||
13 | |||
14 | void ogg_malloc_init(void) | ||
15 | { | ||
16 | size_t bufsize; | ||
17 | void* buf = ci->codec_get_buffer(&bufsize); | ||
18 | init_memory_pool(bufsize, buf); | ||
19 | } | ||
20 | |||
21 | void ogg_malloc_destroy() | ||
22 | { | ||
23 | size_t bufsize; | ||
24 | void* buf = ci->codec_get_buffer(&bufsize); | ||
25 | destroy_memory_pool(buf); | ||
26 | } | ||
27 | |||
28 | void *ogg_malloc(size_t size) | ||
29 | { | ||
30 | void* x = tlsf_malloc(size); | ||
31 | |||
32 | if (x == NULL) | ||
33 | LONGJMP(1); | ||
34 | |||
35 | return x; | ||
36 | } | ||
37 | |||
38 | void *ogg_calloc(size_t nmemb, size_t size) | ||
39 | { | ||
40 | void *x = tlsf_calloc(nmemb, size); | ||
41 | |||
42 | if (x == NULL) | ||
43 | LONGJMP(1); | ||
44 | |||
45 | return x; | ||
46 | } | ||
47 | |||
48 | void *ogg_realloc(void *ptr, size_t size) | ||
49 | { | ||
50 | void *x = tlsf_realloc(ptr, size); | ||
51 | |||
52 | if (x == NULL) | ||
53 | LONGJMP(1); | ||
54 | |||
55 | return x; | ||
56 | } | ||
57 | |||
58 | void ogg_free(void* ptr) | ||
59 | { | ||
60 | tlsf_free(ptr); | ||
61 | } | ||
62 | |||
63 | #ifdef TREMOR_USE_IRAM | ||
64 | /* Allocate IRAM buffer */ | ||
65 | static unsigned char iram_buff[IRAM_IBSS_SIZE] IBSS_ATTR MEM_ALIGN_ATTR; | ||
66 | static size_t iram_remain; | ||
67 | |||
68 | void iram_malloc_init(void){ | ||
69 | iram_remain=IRAM_IBSS_SIZE; | ||
70 | } | ||
71 | |||
72 | void *iram_malloc(size_t size){ | ||
73 | void* x; | ||
74 | |||
75 | /* always ensure alignment to CACHEALIGN_SIZE byte */ | ||
76 | size = (size + (CACHEALIGN_SIZE-1)) & ~(CACHEALIGN_SIZE-1); | ||
77 | |||
78 | if(size>iram_remain) | ||
79 | return NULL; | ||
80 | |||
81 | x = &iram_buff[IRAM_IBSS_SIZE-iram_remain]; | ||
82 | iram_remain-=size; | ||
83 | |||
84 | return x; | ||
85 | } | ||
86 | #endif | ||