summaryrefslogtreecommitdiffstats
path: root/apps/codecs/libtremor/oggmalloc.c
blob: 3d60370641665a59629147c026a2ab74091c44f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "os_types.h"
#include "../lib/tlsf/src/tlsf.h"

#if defined(CPU_ARM) || defined(CPU_COLDFIRE) || defined(CPU_MIPS)
#include <setjmp.h>
extern jmp_buf rb_jump_buf;
#define LONGJMP(x)  longjmp(rb_jump_buf, x)
#elif defined(SIMULATOR)
#define LONGJMP(x)  do { DEBUGF("Vorbis: allocation failed!\n"); return NULL; } while (false)
#else
#define LONGJMP(x)  return NULL
#endif

void ogg_malloc_init(void)
{
    size_t bufsize;
    void* buf = ci->codec_get_buffer(&bufsize);
    init_memory_pool(bufsize, buf);
}

void ogg_malloc_destroy()
{
    size_t bufsize;
    void* buf = ci->codec_get_buffer(&bufsize);
    destroy_memory_pool(buf);
}

void *ogg_malloc(size_t size)
{
    void* x = tlsf_malloc(size);

    if (x == NULL)
        LONGJMP(1);
    
    return x;
}

void *ogg_calloc(size_t nmemb, size_t size)
{
    void *x = tlsf_calloc(nmemb, size);

    if (x == NULL)
        LONGJMP(1);
    
    return x;
}

void *ogg_realloc(void *ptr, size_t size)
{
    void *x = tlsf_realloc(ptr, size);

    if (x == NULL)
        LONGJMP(1);
    
    return x;
}

void ogg_free(void* ptr)
{
    tlsf_free(ptr);
}

/* Allocate IRAM buffer */
static unsigned char iram_buff[IRAM_IBSS_SIZE] IBSS_ATTR __attribute__ ((aligned (16)));
static size_t iram_remain;

void iram_malloc_init(void){
    iram_remain=IRAM_IBSS_SIZE;
}

void *iram_malloc(size_t size){
    void* x;
    
    /* always ensure 16-byte aligned */
    if(size&0x0f)
      size=(size-(size&0x0f))+16;
      
    if(size>iram_remain)
      return NULL;
    
    x = &iram_buff[IRAM_IBSS_SIZE-iram_remain];
    iram_remain-=size;

    return x;
}