diff options
Diffstat (limited to 'apps/codecs/libtremor/oggmalloc.c')
-rw-r--r-- | apps/codecs/libtremor/oggmalloc.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/apps/codecs/libtremor/oggmalloc.c b/apps/codecs/libtremor/oggmalloc.c new file mode 100644 index 0000000000..b11eaa54f5 --- /dev/null +++ b/apps/codecs/libtremor/oggmalloc.c | |||
@@ -0,0 +1,75 @@ | |||
1 | #include <os_types.h> | ||
2 | |||
3 | static unsigned char *mallocbuf; | ||
4 | static size_t bufsize, tmp_ptr, mem_ptr; | ||
5 | |||
6 | void ogg_malloc_init(void) | ||
7 | { | ||
8 | mallocbuf = ci->codec_get_buffer(&bufsize); | ||
9 | tmp_ptr = bufsize & ~3; | ||
10 | mem_ptr = 0; | ||
11 | } | ||
12 | |||
13 | void *ogg_malloc(size_t size) | ||
14 | { | ||
15 | void* x; | ||
16 | |||
17 | size = (size + 3) & ~3; | ||
18 | |||
19 | if (mem_ptr + size > tmp_ptr) | ||
20 | return NULL; | ||
21 | |||
22 | x = &mallocbuf[mem_ptr]; | ||
23 | mem_ptr += size; /* Keep memory 32-bit aligned */ | ||
24 | |||
25 | return x; | ||
26 | } | ||
27 | |||
28 | void *ogg_tmpmalloc(size_t size) | ||
29 | { | ||
30 | size = (size + 3) & ~3; | ||
31 | |||
32 | if (mem_ptr + size > tmp_ptr) | ||
33 | return NULL; | ||
34 | |||
35 | tmp_ptr -= size; | ||
36 | return &mallocbuf[tmp_ptr]; | ||
37 | } | ||
38 | |||
39 | void *ogg_calloc(size_t nmemb, size_t size) | ||
40 | { | ||
41 | void *x; | ||
42 | x = ogg_malloc(nmemb * size); | ||
43 | if (x == NULL) | ||
44 | return NULL; | ||
45 | ci->memset(x, 0, nmemb * size); | ||
46 | return x; | ||
47 | } | ||
48 | |||
49 | void *ogg_tmpcalloc(size_t nmemb, size_t size) | ||
50 | { | ||
51 | void *x; | ||
52 | x = ogg_tmpmalloc(nmemb * size); | ||
53 | if (x == NULL) | ||
54 | return NULL; | ||
55 | ci->memset(x, 0, nmemb * size); | ||
56 | return x; | ||
57 | } | ||
58 | |||
59 | void *ogg_realloc(void *ptr, size_t size) | ||
60 | { | ||
61 | void *x; | ||
62 | (void)ptr; | ||
63 | x = ogg_malloc(size); | ||
64 | return x; | ||
65 | } | ||
66 | |||
67 | long ogg_tmpmalloc_pos(void) | ||
68 | { | ||
69 | return tmp_ptr; | ||
70 | } | ||
71 | |||
72 | void ogg_tmpmalloc_free(long pos) | ||
73 | { | ||
74 | tmp_ptr = pos; | ||
75 | } | ||