summaryrefslogtreecommitdiff
path: root/acorn/src/malloc_wrapper.c
blob: 3b5f9dfab2896115247213939d803d437a30b2bc (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
#include <malloc.h>
#include <FreeRTOS.h>
#include <semphr.h>

static xSemaphoreHandle malloc_sem = NULL;
static malloc_t old_malloc;
static realloc_t old_realloc;
static free_t old_free;

static void * malloc_wrap(size_t s) {
    void * r;
    xSemaphoreTakeRecursive(malloc_sem, portMAX_DELAY);
    r = old_malloc(s);
    xSemaphoreGiveRecursive(malloc_sem);
    return r;
}

static void * realloc_wrap(void * p, size_t s) {
    void * r;
    xSemaphoreTakeRecursive(malloc_sem, portMAX_DELAY);
    r = old_realloc(p, s);
    xSemaphoreGiveRecursive(malloc_sem);
    return r;
}

static void free_wrap(void * p) {
    xSemaphoreTakeRecursive(malloc_sem, portMAX_DELAY);
    old_free(p);
    xSemaphoreGiveRecursive(malloc_sem);
}

void init_malloc_wrapper() {
    malloc_sem = xSemaphoreCreateRecursiveMutex();
    old_malloc = malloc;
    old_realloc = realloc;
    old_free = free;
    malloc = malloc_wrap;
    realloc = realloc_wrap;
    free = free_wrap;
}