uc-sdk
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
malloc_wrapper.c
Go to the documentation of this file.
1 #include <malloc.h>
2 #include <FreeRTOS.h>
3 #include <semphr.h>
4 
5 static xSemaphoreHandle malloc_sem = NULL;
6 static malloc_t old_malloc;
7 static realloc_t old_realloc;
8 static free_t old_free;
9 
10 static void * malloc_wrap(size_t s) {
11  void * r;
13  r = old_malloc(s);
14  xSemaphoreGiveRecursive(malloc_sem);
15  return r;
16 }
17 
18 static void * realloc_wrap(void * p, size_t s) {
19  void * r;
21  r = old_realloc(p, s);
22  xSemaphoreGiveRecursive(malloc_sem);
23  return r;
24 }
25 
26 static void free_wrap(void * p) {
28  old_free(p);
29  xSemaphoreGiveRecursive(malloc_sem);
30 }
31 
33  malloc_sem = xSemaphoreCreateRecursiveMutex();
34  old_malloc = malloc;
35  old_realloc = realloc;
36  old_free = free;
37  malloc = malloc_wrap;
38  realloc = realloc_wrap;
39  free = free_wrap;
40 }
41