Select one of the symbols to view example projects that use it.
 
Outline
#include <stdlib.h>
#include "pico.h"
#include "pico/malloc.h"
#include "pico/mutex.h"
#include <stdio.h>
check_alloc(void *, uint)
__wrap_malloc(size_t)
__wrap_calloc(size_t, size_t)
__wrap_realloc(void *, size_t)
__wrap_free(void *)
Files
loading...
SourceVuRaspberry Pi Pico SDK and ExamplesPicoSDKsrc/rp2_common/pico_malloc/malloc.c
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. * * SPDX-License-Identifier: BSD-3-Clause *//* ... */ #include <stdlib.h> #include "pico.h" #include "pico/malloc.h" #if PICO_USE_MALLOC_MUTEX #include "pico/mutex.h" auto_init_mutex(malloc_mutex);/* ... */ #endif #if PICO_DEBUG_MALLOC #include <stdio.h> #endif extern void *REAL_FUNC(malloc)(size_t size); extern void *REAL_FUNC(calloc)(size_t count, size_t size); extern void *REAL_FUNC(realloc)(void *mem, size_t size); extern void REAL_FUNC(free)(void *mem); extern char __StackLimit; /* Set by linker. */ static inline void check_alloc(__unused void *mem, __unused uint size) { #if PICO_MALLOC_PANIC if (!mem || (((char *)mem) + size) > &__StackLimit) { panic("Out of memory"); }if (!mem || (((char *)mem) + size) > &__StackLimit) { ... } /* ... */#endif }{ ... } void *WRAPPER_FUNC(malloc)(size_t size) { #if PICO_USE_MALLOC_MUTEX mutex_enter_blocking(&malloc_mutex); #endif void *rc = REAL_FUNC(malloc)(size); #if PICO_USE_MALLOC_MUTEX mutex_exit(&malloc_mutex); #endif #if PICO_DEBUG_MALLOC if (!rc) { printf("malloc %d failed to allocate memory\n", (uint) size); }if (!rc) { ... } else if (((uint8_t *)rc) + size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) { printf("malloc %d %p->%p\n", (uint) size, rc, ((uint8_t *) rc) + size); }else if (((uint8_t *)rc) + size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) { ... } /* ... */#endif check_alloc(rc, size); return rc; ...} void *WRAPPER_FUNC(calloc)(size_t count, size_t size) { #if PICO_USE_MALLOC_MUTEX mutex_enter_blocking(&malloc_mutex); #endif void *rc = REAL_FUNC(calloc)(count, size); #if PICO_USE_MALLOC_MUTEX mutex_exit(&malloc_mutex); #endif #if PICO_DEBUG_MALLOC if (!rc) { printf("calloc %d failed to allocate memory\n", (uint) (count * size)); }if (!rc) { ... } else if (((uint8_t *)rc) + count * size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) { printf("calloc %d %p->%p\n", (uint) (count * size), rc, ((uint8_t *) rc) + size); }else if (((uint8_t *)rc) + count * size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) { ... } /* ... */#endif check_alloc(rc, count * size); return rc; ...} void *WRAPPER_FUNC(realloc)(void *mem, size_t size) { #if PICO_USE_MALLOC_MUTEX mutex_enter_blocking(&malloc_mutex); #endif void *rc = REAL_FUNC(realloc)(mem, size); #if PICO_USE_MALLOC_MUTEX mutex_exit(&malloc_mutex); #endif #if PICO_DEBUG_MALLOC if (!rc) { printf("realloc %d failed to allocate memory\n", (uint) size); }if (!rc) { ... } else if (((uint8_t *)rc) + size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) { printf("realloc %p %d->%p\n", mem, (uint) size, rc); }else if (((uint8_t *)rc) + size > (uint8_t*)PICO_DEBUG_MALLOC_LOW_WATER) { ... } /* ... */#endif check_alloc(rc, size); return rc; ...} void WRAPPER_FUNC(free)(void *mem) { #if PICO_USE_MALLOC_MUTEX mutex_enter_blocking(&malloc_mutex); #endif REAL_FUNC(free)(mem); #if PICO_USE_MALLOC_MUTEX mutex_exit(&malloc_mutex); #endif ...}
Details