Numworks Epsilon  1.4.1
Graphing Calculator Operating System
malloc.c
Go to the documentation of this file.
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 #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); }
43 #endif
44 
45 #if MICROPY_ENABLE_GC
46 #include "py/gc.h"
47 
48 // We redirect standard alloc functions to GC heap - just for the rest of
49 // this module. In the rest of MicroPython source, system malloc can be
50 // freely accessed - for interfacing with system and 3rd-party libs for
51 // example. On the other hand, some (e.g. bare-metal) ports may use GC
52 // heap as system heap, so, to avoid warnings, we do undef's first.
53 #undef malloc
54 #undef free
55 #undef realloc
56 #define malloc(b) gc_alloc((b), false)
57 #define malloc_with_finaliser(b) gc_alloc((b), true)
58 #define free gc_free
59 #define realloc(ptr, n) gc_realloc(ptr, n, true)
60 #define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv)
61 #else
62 STATIC void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
63  if (allow_move) {
64  return realloc(ptr, n_bytes);
65  } else {
66  // We are asked to resize, but without moving the memory region pointed to
67  // by ptr. Unless the underlying memory manager has special provision for
68  // this behaviour there is nothing we can do except fail to resize.
69  return NULL;
70  }
71 }
72 #endif // MICROPY_ENABLE_GC
73 
74 void *m_malloc(size_t num_bytes) {
75  void *ptr = malloc(num_bytes);
76  if (ptr == NULL && num_bytes != 0) {
77  m_malloc_fail(num_bytes);
78  }
79 #if MICROPY_MEM_STATS
80  MP_STATE_MEM(total_bytes_allocated) += num_bytes;
81  MP_STATE_MEM(current_bytes_allocated) += num_bytes;
82  UPDATE_PEAK();
83 #endif
84  DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
85  return ptr;
86 }
87 
88 void *m_malloc_maybe(size_t num_bytes) {
89  void *ptr = malloc(num_bytes);
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 
99 #if MICROPY_ENABLE_FINALISER
100 void *m_malloc_with_finaliser(size_t num_bytes) {
101  void *ptr = malloc_with_finaliser(num_bytes);
102  if (ptr == NULL && num_bytes != 0) {
103  m_malloc_fail(num_bytes);
104  }
105 #if MICROPY_MEM_STATS
106  MP_STATE_MEM(total_bytes_allocated) += num_bytes;
107  MP_STATE_MEM(current_bytes_allocated) += num_bytes;
108  UPDATE_PEAK();
109 #endif
110  DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
111  return ptr;
112 }
113 #endif
114 
115 void *m_malloc0(size_t num_bytes) {
116  void *ptr = m_malloc(num_bytes);
117  if (ptr == NULL && num_bytes != 0) {
118  m_malloc_fail(num_bytes);
119  }
120  // If this config is set then the GC clears all memory, so we don't need to.
121  #if !MICROPY_GC_CONSERVATIVE_CLEAR
122  memset(ptr, 0, num_bytes);
123  #endif
124  return ptr;
125 }
126 
127 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
128 void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
129 #else
130 void *m_realloc(void *ptr, size_t new_num_bytes) {
131 #endif
132  void *new_ptr = realloc(ptr, new_num_bytes);
133  if (new_ptr == NULL && new_num_bytes != 0) {
134  m_malloc_fail(new_num_bytes);
135  }
136 #if MICROPY_MEM_STATS
137  // At first thought, "Total bytes allocated" should only grow,
138  // after all, it's *total*. But consider for example 2K block
139  // shrunk to 1K and then grown to 2K again. It's still 2K
140  // allocated total. If we process only positive increments,
141  // we'll count 3K.
142  size_t diff = new_num_bytes - old_num_bytes;
143  MP_STATE_MEM(total_bytes_allocated) += diff;
144  MP_STATE_MEM(current_bytes_allocated) += diff;
145  UPDATE_PEAK();
146 #endif
147  DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
148  return new_ptr;
149 }
150 
151 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
152 void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move) {
153 #else
154 void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move) {
155 #endif
156  void *new_ptr = realloc_ext(ptr, new_num_bytes, allow_move);
157 #if MICROPY_MEM_STATS
158  // At first thought, "Total bytes allocated" should only grow,
159  // after all, it's *total*. But consider for example 2K block
160  // shrunk to 1K and then grown to 2K again. It's still 2K
161  // allocated total. If we process only positive increments,
162  // we'll count 3K.
163  // Also, don't count failed reallocs.
164  if (!(new_ptr == NULL && new_num_bytes != 0)) {
165  size_t diff = new_num_bytes - old_num_bytes;
166  MP_STATE_MEM(total_bytes_allocated) += diff;
167  MP_STATE_MEM(current_bytes_allocated) += diff;
168  UPDATE_PEAK();
169  }
170 #endif
171  DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
172  return new_ptr;
173 }
174 
175 #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
176 void m_free(void *ptr, size_t num_bytes) {
177 #else
178 void m_free(void *ptr) {
179 #endif
180  free(ptr);
181 #if MICROPY_MEM_STATS
182  MP_STATE_MEM(current_bytes_allocated) -= num_bytes;
183 #endif
184  DEBUG_printf("free %p, %d\n", ptr, num_bytes);
185 }
186 
187 #if MICROPY_MEM_STATS
188 size_t m_get_total_bytes_allocated(void) {
189  return MP_STATE_MEM(total_bytes_allocated);
190 }
191 
192 size_t m_get_current_bytes_allocated(void) {
193  return MP_STATE_MEM(current_bytes_allocated);
194 }
195 
196 size_t m_get_peak_bytes_allocated(void) {
197  return MP_STATE_MEM(peak_bytes_allocated);
198 }
199 #endif
void * memset(void *b, int c, size_t len)
Definition: memset.c:3
void * malloc(size_t size)
Definition: malloc.c:44
void free(void *ptr)
Definition: malloc.c:33
STATIC void * realloc_ext(void *ptr, size_t n_bytes, bool allow_move)
Definition: malloc.c:62
#define STATIC
Definition: mpconfig.h:1178
void * m_realloc(void *ptr, size_t new_num_bytes)
Definition: malloc.c:130
#define MP_STATE_MEM(x)
Definition: mpstate.h:242
void * m_malloc_with_finaliser(size_t num_bytes)
#define NULL
Definition: stddef.h:4
#define DEBUG_printf(...)
Definition: malloc.c:38
void * m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move)
Definition: malloc.c:154
void * realloc(void *ptr, size_t size)
Definition: malloc.c:67
void * m_malloc0(size_t num_bytes)
Definition: malloc.c:115
void m_free(void *ptr)
Definition: malloc.c:178
NORETURN void m_malloc_fail(size_t num_bytes)
Definition: runtime.c:1437
void * m_malloc(size_t num_bytes)
Definition: malloc.c:74
void * m_malloc_maybe(size_t num_bytes)
Definition: malloc.c:88