1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdlib.h>
8 #include "pico.h"
9 #include "pico/malloc.h"
10 
11 #if PICO_USE_MALLOC_MUTEX
12 #include "pico/mutex.h"
13 auto_init_mutex(malloc_mutex);
14 #endif
15 
16 #if PICO_DEBUG_MALLOC
17 #include <stdio.h>
18 #endif
19 
20 extern void *REAL_FUNC(malloc)(size_t size);
21 extern void *REAL_FUNC(calloc)(size_t count, size_t size);
22 extern void *REAL_FUNC(realloc)(void *mem, size_t size);
23 extern void REAL_FUNC(free)(void *mem);
24 
25 extern char __StackLimit; /* Set by linker.  */
26 
27 #if !PICO_USE_MALLOC_MUTEX
28 #define MALLOC_ENTER(outer) ((void)0);
29 #define MALLOC_EXIT(outer) ((void)0);
30 #elif !__PICOLIBC__
31 #define MALLOC_ENTER(outer) mutex_enter_blocking(&malloc_mutex);
32 #define MALLOC_EXIT(outer) mutex_exit(&malloc_mutex);
33 #else
34 static uint8_t mutex_exception_level_plus_one[NUM_CORES];
35 // PICOLIBC implementations of calloc and realloc may call malloc and free,
36 // so we need to cope with re-entrant calls. We don't want to use a recursive
37 // mutex as that won't catch usage within an ISR; instead we record the exception
38 // nesting as of acquiring the mutex
39 #define MALLOC_ENTER(outer) \
40     uint exception = __get_current_exception(); \
41     uint core_num = get_core_num(); \
42     /* we skip the locking on outer == false if we are in the same irq nesting */ \
43     /* note: the `+ 1` is to distinguish no malloc nesting vs no-exception/irq level */ \
44     bool do_lock = outer || exception + 1 != mutex_exception_level_plus_one[core_num]; \
45     if (do_lock) { \
46         mutex_enter_blocking(&malloc_mutex); \
47         if (outer) mutex_exception_level_plus_one[core_num] = (uint8_t)(exception + 1); \
48     }
49 
50 #define MALLOC_EXIT(outer) \
51     if (outer) { \
52         mutex_exception_level_plus_one[core_num] = 0; \
53     } \
54     if (do_lock) { \
55         mutex_exit(&malloc_mutex); \
56     }
57 
58 #endif
59 
check_alloc(__unused void * mem,__unused uint size)60 static inline void check_alloc(__unused void *mem, __unused uint size) {
61 #if PICO_MALLOC_PANIC
62     if (!mem || (((char *)mem) + size) > &__StackLimit) {
63         panic("Out of memory");
64     }
65 #endif
66 }
67 
WRAPPER_FUNC(malloc)68 void *WRAPPER_FUNC(malloc)(size_t size) {
69     MALLOC_ENTER(false)
70     void *rc = REAL_FUNC(malloc)(size);
71     MALLOC_EXIT(false)
72 #if PICO_DEBUG_MALLOC
73     if (!rc) {
74         printf("malloc %d failed to allocate memory\n", (uint) size);
75     } else if (((uint8_t *)rc) + size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) {
76         printf("malloc %d %p->%p\n", (uint) size, rc, ((uint8_t *) rc) + size);
77     }
78 #endif
79     check_alloc(rc, size);
80     return rc;
81 }
82 
WRAPPER_FUNC(calloc)83 void *WRAPPER_FUNC(calloc)(size_t count, size_t size) {
84     MALLOC_ENTER(true)
85     void *rc = REAL_FUNC(calloc)(count, size);
86     MALLOC_EXIT(true)
87 #if PICO_DEBUG_MALLOC
88     if (!rc) {
89         printf("calloc %d failed to allocate memory\n", (uint) (count * size));
90     } else if (((uint8_t *)rc) + count * size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) {
91         printf("calloc %d %p->%p\n", (uint) (count * size), rc, ((uint8_t *) rc) + size);
92     }
93 #endif
94     check_alloc(rc, count * size);
95     return rc;
96 }
97 
WRAPPER_FUNC(realloc)98 void *WRAPPER_FUNC(realloc)(void *mem, size_t size) {
99     MALLOC_ENTER(true)
100     void *rc = REAL_FUNC(realloc)(mem, size);
101     MALLOC_EXIT(true)
102 #if PICO_DEBUG_MALLOC
103     if (!rc) {
104         printf("realloc %d failed to allocate memory\n", (uint) size);
105     } else if (((uint8_t *)rc) + size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) {
106         printf("realloc %p %d->%p\n", mem, (uint) size, rc);
107     }
108 #endif
109     check_alloc(rc, size);
110     return rc;
111 }
112 
WRAPPER_FUNC(free)113 void WRAPPER_FUNC(free)(void *mem) {
114     MALLOC_ENTER(false)
115     REAL_FUNC(free)(mem);
116     MALLOC_EXIT(false)
117 }
118