summaryrefslogtreecommitdiff
path: root/apps/codecs/libtremor/oggmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/libtremor/oggmalloc.c')
-rw-r--r--apps/codecs/libtremor/oggmalloc.c80
1 files changed, 29 insertions, 51 deletions
diff --git a/apps/codecs/libtremor/oggmalloc.c b/apps/codecs/libtremor/oggmalloc.c
index 6da7cfcedc..3d60370641 100644
--- a/apps/codecs/libtremor/oggmalloc.c
+++ b/apps/codecs/libtremor/oggmalloc.c
@@ -1,85 +1,63 @@
1#include "os_types.h" 1#include "os_types.h"
2#include "../lib/tlsf/src/tlsf.h"
2 3
3#if defined(CPU_ARM) || defined(CPU_COLDFIRE) 4#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS)
4#include <setjmp.h> 5#include <setjmp.h>
5extern jmp_buf rb_jump_buf; 6extern 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
6#endif 12#endif
7 13
8static size_t tmp_ptr;
9
10void ogg_malloc_init(void) 14void ogg_malloc_init(void)
11{ 15{
12 mallocbuf = ci->codec_get_buffer(&bufsize); 16 size_t bufsize;
13 tmp_ptr = bufsize & ~3; 17 void* buf = ci->codec_get_buffer(&bufsize);
14 mem_ptr = 0; 18 init_memory_pool(bufsize, buf);
15} 19}
16 20
17void *ogg_malloc(size_t size) 21void ogg_malloc_destroy()
18{ 22{
19 void* x; 23 size_t bufsize;
20 24 void* buf = ci->codec_get_buffer(&bufsize);
21 size = (size + 3) & ~3; 25 destroy_memory_pool(buf);
22
23 if (mem_ptr + size > tmp_ptr)
24#if defined(CPU_ARM) || defined(CPU_COLDFIRE)
25 longjmp(rb_jump_buf, 1);
26#else
27 return NULL;
28#endif
29
30 x = &mallocbuf[mem_ptr];
31 mem_ptr += size; /* Keep memory 32-bit aligned */
32
33 return x;
34} 26}
35 27
36void *ogg_tmpmalloc(size_t size) 28void *ogg_malloc(size_t size)
37{ 29{
38 size = (size + 3) & ~3; 30 void* x = tlsf_malloc(size);
39 31
40 if (mem_ptr + size > tmp_ptr) 32 if (x == NULL)
41 return NULL; 33 LONGJMP(1);
42 34
43 tmp_ptr -= size; 35 return x;
44 return &mallocbuf[tmp_ptr];
45} 36}
46 37
47void *ogg_calloc(size_t nmemb, size_t size) 38void *ogg_calloc(size_t nmemb, size_t size)
48{ 39{
49 void *x; 40 void *x = tlsf_calloc(nmemb, size);
50 x = ogg_malloc(nmemb * size);
51 if (x == NULL)
52 return NULL;
53 ci->memset(x, 0, nmemb * size);
54 return x;
55}
56 41
57void *ogg_tmpcalloc(size_t nmemb, size_t size)
58{
59 void *x;
60 x = ogg_tmpmalloc(nmemb * size);
61 if (x == NULL) 42 if (x == NULL)
62 return NULL; 43 LONGJMP(1);
63 ci->memset(x, 0, nmemb * size); 44
64 return x; 45 return x;
65} 46}
66 47
67void *ogg_realloc(void *ptr, size_t size) 48void *ogg_realloc(void *ptr, size_t size)
68{ 49{
69 void *x; 50 void *x = tlsf_realloc(ptr, size);
70 (void)ptr;
71 x = ogg_malloc(size);
72 return x;
73}
74 51
75long ogg_tmpmalloc_pos(void) 52 if (x == NULL)
76{ 53 LONGJMP(1);
77 return tmp_ptr; 54
55 return x;
78} 56}
79 57
80void ogg_tmpmalloc_free(long pos) 58void ogg_free(void* ptr)
81{ 59{
82 tmp_ptr = pos; 60 tlsf_free(ptr);
83} 61}
84 62
85/* Allocate IRAM buffer */ 63/* Allocate IRAM buffer */