Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 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 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Paul Sokolovsky | 58ff93b | 2014-02-10 18:37:11 +0200 | [diff] [blame] | 29 | #include <string.h> |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 30 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 31 | #include "py/mpconfig.h" |
| 32 | #include "py/misc.h" |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 33 | #include "py/mpstate.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 34 | |
Stefan Naumann | ace9fb5 | 2017-07-24 18:55:14 +0200 | [diff] [blame] | 35 | #if MICROPY_DEBUG_VERBOSE // print debugging info |
Paul Sokolovsky | 44739e2 | 2014-02-16 18:11:42 +0200 | [diff] [blame] | 36 | #define DEBUG_printf DEBUG_printf |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 37 | #else // don't print debugging info |
Damien George | 1dc76af | 2014-02-26 16:57:08 +0000 | [diff] [blame] | 38 | #define DEBUG_printf(...) (void)0 |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 39 | #endif |
| 40 | |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 41 | #if MICROPY_MEM_STATS |
Paul Sokolovsky | 88a8043 | 2017-12-07 10:52:40 +0200 | [diff] [blame] | 42 | #if !MICROPY_MALLOC_USES_ALLOCATED_SIZE |
| 43 | #error MICROPY_MEM_STATS requires MICROPY_MALLOC_USES_ALLOCATED_SIZE |
| 44 | #endif |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 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); } |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 46 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 47 | |
Paul Sokolovsky | b62c30b | 2014-02-10 22:37:09 +0200 | [diff] [blame] | 48 | #if MICROPY_ENABLE_GC |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 49 | #include "py/gc.h" |
Paul Sokolovsky | b62c30b | 2014-02-10 22:37:09 +0200 | [diff] [blame] | 50 | |
| 51 | // We redirect standard alloc functions to GC heap - just for the rest of |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 52 | // this module. In the rest of MicroPython source, system malloc can be |
Paul Sokolovsky | b62c30b | 2014-02-10 22:37:09 +0200 | [diff] [blame] | 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 |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 59 | #define malloc(b) gc_alloc((b), false) |
| 60 | #define malloc_with_finaliser(b) gc_alloc((b), true) |
Paul Sokolovsky | b62c30b | 2014-02-10 22:37:09 +0200 | [diff] [blame] | 61 | #define free gc_free |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 62 | #define realloc(ptr, n) gc_realloc(ptr, n, true) |
| 63 | #define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv) |
| 64 | #else |
Damien George | fcf621b | 2018-07-09 14:40:02 +1000 | [diff] [blame] | 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 | |
Damien George | e9d1a94 | 2016-02-23 13:53:38 +0000 | [diff] [blame] | 72 | STATIC 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 | } |
Damien George | fcf621b | 2018-07-09 14:40:02 +1000 | [diff] [blame] | 82 | |
Paul Sokolovsky | b62c30b | 2014-02-10 22:37:09 +0200 | [diff] [blame] | 83 | #endif // MICROPY_ENABLE_GC |
| 84 | |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 85 | void *m_malloc(size_t num_bytes) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 86 | void *ptr = malloc(num_bytes); |
Damien George | 37378f8 | 2014-10-23 12:02:00 +0100 | [diff] [blame] | 87 | if (ptr == NULL && num_bytes != 0) { |
Damien George | ca21aed | 2017-08-31 17:00:14 +1000 | [diff] [blame] | 88 | m_malloc_fail(num_bytes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 89 | } |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 90 | #if MICROPY_MEM_STATS |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 91 | MP_STATE_MEM(total_bytes_allocated) += num_bytes; |
| 92 | MP_STATE_MEM(current_bytes_allocated) += num_bytes; |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 93 | UPDATE_PEAK(); |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 94 | #endif |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 95 | DEBUG_printf("malloc %d : %p\n", num_bytes, ptr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 96 | return ptr; |
| 97 | } |
| 98 | |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 99 | void *m_malloc_maybe(size_t num_bytes) { |
Damien George | f0954e3 | 2014-04-10 14:38:25 +0100 | [diff] [blame] | 100 | void *ptr = malloc(num_bytes); |
Damien George | f0954e3 | 2014-04-10 14:38:25 +0100 | [diff] [blame] | 101 | #if MICROPY_MEM_STATS |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 102 | MP_STATE_MEM(total_bytes_allocated) += num_bytes; |
| 103 | MP_STATE_MEM(current_bytes_allocated) += num_bytes; |
Damien George | f0954e3 | 2014-04-10 14:38:25 +0100 | [diff] [blame] | 104 | UPDATE_PEAK(); |
| 105 | #endif |
| 106 | DEBUG_printf("malloc %d : %p\n", num_bytes, ptr); |
| 107 | return ptr; |
| 108 | } |
| 109 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 110 | #if MICROPY_ENABLE_FINALISER |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 111 | void *m_malloc_with_finaliser(size_t num_bytes) { |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 112 | void *ptr = malloc_with_finaliser(num_bytes); |
Damien George | 37378f8 | 2014-10-23 12:02:00 +0100 | [diff] [blame] | 113 | if (ptr == NULL && num_bytes != 0) { |
Damien George | ca21aed | 2017-08-31 17:00:14 +1000 | [diff] [blame] | 114 | m_malloc_fail(num_bytes); |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 115 | } |
| 116 | #if MICROPY_MEM_STATS |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 117 | MP_STATE_MEM(total_bytes_allocated) += num_bytes; |
| 118 | MP_STATE_MEM(current_bytes_allocated) += num_bytes; |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 119 | UPDATE_PEAK(); |
| 120 | #endif |
| 121 | DEBUG_printf("malloc %d : %p\n", num_bytes, ptr); |
| 122 | return ptr; |
| 123 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 124 | #endif |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 125 | |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 126 | void *m_malloc0(size_t num_bytes) { |
Paul Sokolovsky | 58ff93b | 2014-02-10 18:37:11 +0200 | [diff] [blame] | 127 | void *ptr = m_malloc(num_bytes); |
Damien George | 5ffe1d8 | 2016-08-26 15:35:26 +1000 | [diff] [blame] | 128 | // If this config is set then the GC clears all memory, so we don't need to. |
| 129 | #if !MICROPY_GC_CONSERVATIVE_CLEAR |
Damien George | 37378f8 | 2014-10-23 12:02:00 +0100 | [diff] [blame] | 130 | memset(ptr, 0, num_bytes); |
Damien George | 5ffe1d8 | 2016-08-26 15:35:26 +1000 | [diff] [blame] | 131 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 132 | return ptr; |
| 133 | } |
| 134 | |
Damien George | d891452 | 2015-02-27 09:54:12 +0000 | [diff] [blame] | 135 | #if MICROPY_MALLOC_USES_ALLOCATED_SIZE |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 136 | void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) { |
Damien George | d891452 | 2015-02-27 09:54:12 +0000 | [diff] [blame] | 137 | #else |
| 138 | void *m_realloc(void *ptr, size_t new_num_bytes) { |
| 139 | #endif |
Paul Sokolovsky | cdd2c62 | 2014-01-30 03:58:17 +0200 | [diff] [blame] | 140 | void *new_ptr = realloc(ptr, new_num_bytes); |
Damien George | 37378f8 | 2014-10-23 12:02:00 +0100 | [diff] [blame] | 141 | if (new_ptr == NULL && new_num_bytes != 0) { |
Damien George | ca21aed | 2017-08-31 17:00:14 +1000 | [diff] [blame] | 142 | m_malloc_fail(new_num_bytes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 143 | } |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 144 | #if MICROPY_MEM_STATS |
Paul Sokolovsky | 43f1c80 | 2014-01-01 23:04:25 +0200 | [diff] [blame] | 145 | // At first thought, "Total bytes allocated" should only grow, |
| 146 | // after all, it's *total*. But consider for example 2K block |
| 147 | // shrunk to 1K and then grown to 2K again. It's still 2K |
| 148 | // allocated total. If we process only positive increments, |
| 149 | // we'll count 3K. |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 150 | size_t diff = new_num_bytes - old_num_bytes; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 151 | MP_STATE_MEM(total_bytes_allocated) += diff; |
| 152 | MP_STATE_MEM(current_bytes_allocated) += diff; |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 153 | UPDATE_PEAK(); |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 154 | #endif |
Paul Sokolovsky | 9ebc037 | 2017-12-07 17:57:33 +0200 | [diff] [blame] | 155 | #if MICROPY_MALLOC_USES_ALLOCATED_SIZE |
Paul Sokolovsky | cdd2c62 | 2014-01-30 03:58:17 +0200 | [diff] [blame] | 156 | DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr); |
Paul Sokolovsky | 9ebc037 | 2017-12-07 17:57:33 +0200 | [diff] [blame] | 157 | #else |
| 158 | DEBUG_printf("realloc %p, %d : %p\n", ptr, new_num_bytes, new_ptr); |
| 159 | #endif |
Paul Sokolovsky | cdd2c62 | 2014-01-30 03:58:17 +0200 | [diff] [blame] | 160 | return new_ptr; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Damien George | d891452 | 2015-02-27 09:54:12 +0000 | [diff] [blame] | 163 | #if MICROPY_MALLOC_USES_ALLOCATED_SIZE |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 164 | void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move) { |
Damien George | d891452 | 2015-02-27 09:54:12 +0000 | [diff] [blame] | 165 | #else |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 166 | void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move) { |
Damien George | d891452 | 2015-02-27 09:54:12 +0000 | [diff] [blame] | 167 | #endif |
Damien George | ade9a05 | 2015-06-13 21:53:22 +0100 | [diff] [blame] | 168 | void *new_ptr = realloc_ext(ptr, new_num_bytes, allow_move); |
Damien George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 169 | #if MICROPY_MEM_STATS |
| 170 | // At first thought, "Total bytes allocated" should only grow, |
| 171 | // after all, it's *total*. But consider for example 2K block |
| 172 | // shrunk to 1K and then grown to 2K again. It's still 2K |
| 173 | // allocated total. If we process only positive increments, |
| 174 | // we'll count 3K. |
Damien George | 37378f8 | 2014-10-23 12:02:00 +0100 | [diff] [blame] | 175 | // Also, don't count failed reallocs. |
| 176 | if (!(new_ptr == NULL && new_num_bytes != 0)) { |
| 177 | size_t diff = new_num_bytes - old_num_bytes; |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 178 | MP_STATE_MEM(total_bytes_allocated) += diff; |
| 179 | MP_STATE_MEM(current_bytes_allocated) += diff; |
Damien George | 37378f8 | 2014-10-23 12:02:00 +0100 | [diff] [blame] | 180 | UPDATE_PEAK(); |
| 181 | } |
Damien George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 182 | #endif |
Paul Sokolovsky | 9ebc037 | 2017-12-07 17:57:33 +0200 | [diff] [blame] | 183 | #if MICROPY_MALLOC_USES_ALLOCATED_SIZE |
Damien George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 184 | DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr); |
Paul Sokolovsky | 9ebc037 | 2017-12-07 17:57:33 +0200 | [diff] [blame] | 185 | #else |
| 186 | DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, new_num_bytes, new_ptr); |
| 187 | #endif |
Damien George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 188 | return new_ptr; |
| 189 | } |
| 190 | |
Damien George | d891452 | 2015-02-27 09:54:12 +0000 | [diff] [blame] | 191 | #if MICROPY_MALLOC_USES_ALLOCATED_SIZE |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 192 | void m_free(void *ptr, size_t num_bytes) { |
Damien George | d891452 | 2015-02-27 09:54:12 +0000 | [diff] [blame] | 193 | #else |
| 194 | void m_free(void *ptr) { |
| 195 | #endif |
Damien George | 37378f8 | 2014-10-23 12:02:00 +0100 | [diff] [blame] | 196 | free(ptr); |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 197 | #if MICROPY_MEM_STATS |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 198 | MP_STATE_MEM(current_bytes_allocated) -= num_bytes; |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 199 | #endif |
Paul Sokolovsky | 9ebc037 | 2017-12-07 17:57:33 +0200 | [diff] [blame] | 200 | #if MICROPY_MALLOC_USES_ALLOCATED_SIZE |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 201 | DEBUG_printf("free %p, %d\n", ptr, num_bytes); |
Paul Sokolovsky | 9ebc037 | 2017-12-07 17:57:33 +0200 | [diff] [blame] | 202 | #else |
| 203 | DEBUG_printf("free %p\n", ptr); |
| 204 | #endif |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 207 | #if MICROPY_MEM_STATS |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 208 | size_t m_get_total_bytes_allocated(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 209 | return MP_STATE_MEM(total_bytes_allocated); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 210 | } |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame] | 211 | |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 212 | size_t m_get_current_bytes_allocated(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 213 | return MP_STATE_MEM(current_bytes_allocated); |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame] | 214 | } |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 215 | |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 216 | size_t m_get_peak_bytes_allocated(void) { |
Damien George | b4b10fd | 2015-01-01 23:30:53 +0000 | [diff] [blame] | 217 | return MP_STATE_MEM(peak_bytes_allocated); |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 218 | } |
Damien George | b026134 | 2014-09-23 18:10:17 +0100 | [diff] [blame] | 219 | #endif |