1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30
31#include "py/mpconfig.h"
32#include "py/misc.h"
33#include "py/mpstate.h"
34
35#if MICROPY_DEBUG_VERBOSE // print debugging info
36#define DEBUG_printf DEBUG_printf
37#else // don't print debugging info
38#define DEBUG_printf(...) (void)0
39#endif
40
41#if MICROPY_MEM_STATS
42#if !MICROPY_MALLOC_USES_ALLOCATED_SIZE
43#error MICROPY_MEM_STATS requires MICROPY_MALLOC_USES_ALLOCATED_SIZE
44#endif
45#define UPDATE_PEAK() { if (MP_STATE_MEM(current_bytes_allocated) > MP_STATE_MEM(peak_bytes_allocated)) MP_STATE_MEM(peak_bytes_allocated) = MP_STATE_MEM(current_bytes_allocated); }
46#endif
47
48#if MICROPY_ENABLE_GC
49#include "py/gc.h"
50
51// We redirect standard alloc functions to GC heap - just for the rest of
52// this module. In the rest of MicroPython source, system malloc can be
53// freely accessed - for interfacing with system and 3rd-party libs for
54// example. On the other hand, some (e.g. bare-metal) ports may use GC
55// heap as system heap, so, to avoid warnings, we do undef's first.
56#undef malloc
57#undef free
58#undef realloc
59#define malloc(b) gc_alloc((b), false)
60#define malloc_with_finaliser(b) gc_alloc((b), true)
61#define free gc_free
62#define realloc(ptr, n) gc_realloc(ptr, n, true)
63#define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv)
64#else
65
66// GC is disabled. Use system malloc/realloc/free.
67
68#if MICROPY_ENABLE_FINALISER
69#error MICROPY_ENABLE_FINALISER requires MICROPY_ENABLE_GC
70#endif
71
72STATIC void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
73 if (allow_move) {
74 return realloc(ptr, n_bytes);
75 } else {
76 // We are asked to resize, but without moving the memory region pointed to
77 // by ptr. Unless the underlying memory manager has special provision for
78 // this behaviour there is nothing we can do except fail to resize.
79 return NULL;
80 }
81}
82
83#endif // MICROPY_ENABLE_GC
84
85void *m_malloc(size_t num_bytes) {
86 void *ptr = malloc(num_bytes);
87 if (ptr == NULL && num_bytes != 0) {
88 m_malloc_fail(num_bytes);
89 }
90 #if MICROPY_MEM_STATS
91 MP_STATE_MEM(total_bytes_allocated) += num_bytes;
92 MP_STATE_MEM(current_bytes_allocated) += num_bytes;
93 UPDATE_PEAK();
94 #endif
95 DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
96 return ptr;
97}
98
99void *m_malloc_maybe(size_t num_bytes) {
100 void *ptr = malloc(num_bytes);
101 #if MICROPY_MEM_STATS
102 MP_STATE_MEM(total_bytes_allocated) += num_bytes;
103 MP_STATE_MEM(current_bytes_allocated) += num_bytes;
104 UPDATE_PEAK();
105 #endif
106 DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
107 return ptr;
108}
109
110#if MICROPY_ENABLE_FINALISER
111void *m_malloc_with_finaliser(size_t num_bytes) {
112 void *ptr = malloc_with_finaliser(num_bytes);
113 if (ptr == NULL && num_bytes != 0) {
114 m_malloc_fail(num_bytes);
115 }
116 #if MICROPY_MEM_STATS
117 MP_STATE_MEM(total_bytes_allocated) += num_bytes;
118 MP_STATE_MEM(current_bytes_allocated) += num_bytes;
119 UPDATE_PEAK();
120 #endif
121 DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
122 return ptr;
123}
124#endif
125
126void *m_malloc0(size_t num_bytes) {
127 void *ptr = m_malloc(num_bytes);
128 // If this config is set then the GC clears all memory, so we don't need to.
129 #if !MICROPY_GC_CONSERVATIVE_CLEAR
130 memset(ptr, 0, num_bytes);
131 #endif
132 return ptr;
133}
134
135#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
136void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes)
137#else
138void *m_realloc(void *ptr, size_t new_num_bytes)
139#endif
140{
141 void *new_ptr = realloc(ptr, new_num_bytes);
142 if (new_ptr == NULL && new_num_bytes != 0) {
143 m_malloc_fail(new_num_bytes);
144 }
145 #if MICROPY_MEM_STATS
146 // At first thought, "Total bytes allocated" should only grow,
147 // after all, it's *total*. But consider for example 2K block
148 // shrunk to 1K and then grown to 2K again. It's still 2K
149 // allocated total. If we process only positive increments,
150 // we'll count 3K.
151 size_t diff = new_num_bytes - old_num_bytes;
152 MP_STATE_MEM(total_bytes_allocated) += diff;
153 MP_STATE_MEM(current_bytes_allocated) += diff;
154 UPDATE_PEAK();
155 #endif
156 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
157 DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
158 #else
159 DEBUG_printf("realloc %p, %d : %p\n", ptr, new_num_bytes, new_ptr);
160 #endif
161 return new_ptr;
162}
163
164#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
165void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move)
166#else
167void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move)
168#endif
169{
170 void *new_ptr = realloc_ext(ptr, new_num_bytes, allow_move);
171 #if MICROPY_MEM_STATS
172 // At first thought, "Total bytes allocated" should only grow,
173 // after all, it's *total*. But consider for example 2K block
174 // shrunk to 1K and then grown to 2K again. It's still 2K
175 // allocated total. If we process only positive increments,
176 // we'll count 3K.
177 // Also, don't count failed reallocs.
178 if (!(new_ptr == NULL && new_num_bytes != 0)) {
179 size_t diff = new_num_bytes - old_num_bytes;
180 MP_STATE_MEM(total_bytes_allocated) += diff;
181 MP_STATE_MEM(current_bytes_allocated) += diff;
182 UPDATE_PEAK();
183 }
184 #endif
185 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
186 DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
187 #else
188 DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, new_num_bytes, new_ptr);
189 #endif
190 return new_ptr;
191}
192
193#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
194void m_free(void *ptr, size_t num_bytes)
195#else
196void m_free(void *ptr)
197#endif
198{
199 free(ptr);
200 #if MICROPY_MEM_STATS
201 MP_STATE_MEM(current_bytes_allocated) -= num_bytes;
202 #endif
203 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
204 DEBUG_printf("free %p, %d\n", ptr, num_bytes);
205 #else
206 DEBUG_printf("free %p\n", ptr);
207 #endif
208}
209
210#if MICROPY_MEM_STATS
211size_t m_get_total_bytes_allocated(void) {
212 return MP_STATE_MEM(total_bytes_allocated);
213}
214
215size_t m_get_current_bytes_allocated(void) {
216 return MP_STATE_MEM(current_bytes_allocated);
217}
218
219size_t m_get_peak_bytes_allocated(void) {
220 return MP_STATE_MEM(peak_bytes_allocated);
221}
222#endif
223