Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python 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 | |
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 | |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 31 | #include "mpconfig.h" |
Paul Sokolovsky | 59c675a | 2014-06-21 22:43:22 +0300 | [diff] [blame] | 32 | #include "misc.h" |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 33 | |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 34 | #if 0 // print debugging info |
Paul Sokolovsky | 44739e2 | 2014-02-16 18:11:42 +0200 | [diff] [blame] | 35 | #define DEBUG_printf DEBUG_printf |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 36 | #else // don't print debugging info |
Damien George | 1dc76af | 2014-02-26 16:57:08 +0000 | [diff] [blame] | 37 | #define DEBUG_printf(...) (void)0 |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 38 | #endif |
| 39 | |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 40 | #if MICROPY_MEM_STATS |
Paul Sokolovsky | 520e2f5 | 2014-02-12 18:31:30 +0200 | [diff] [blame] | 41 | STATIC int total_bytes_allocated = 0; |
| 42 | STATIC int current_bytes_allocated = 0; |
| 43 | STATIC int peak_bytes_allocated = 0; |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 44 | |
| 45 | #define UPDATE_PEAK() { if (current_bytes_allocated > peak_bytes_allocated) peak_bytes_allocated = 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 |
| 49 | #include "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 |
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 |
| 62 | #define realloc gc_realloc |
| 63 | #endif // MICROPY_ENABLE_GC |
| 64 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 65 | void *m_malloc(int num_bytes) { |
| 66 | if (num_bytes == 0) { |
| 67 | return NULL; |
| 68 | } |
| 69 | void *ptr = malloc(num_bytes); |
| 70 | if (ptr == NULL) { |
Damien George | 6902eed | 2014-04-04 10:52:59 +0000 | [diff] [blame] | 71 | return m_malloc_fail(num_bytes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 72 | } |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 73 | #if MICROPY_MEM_STATS |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 74 | total_bytes_allocated += num_bytes; |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame] | 75 | current_bytes_allocated += num_bytes; |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 76 | UPDATE_PEAK(); |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 77 | #endif |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 78 | DEBUG_printf("malloc %d : %p\n", num_bytes, ptr); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 79 | return ptr; |
| 80 | } |
| 81 | |
Damien George | f0954e3 | 2014-04-10 14:38:25 +0100 | [diff] [blame] | 82 | void *m_malloc_maybe(int num_bytes) { |
| 83 | void *ptr = malloc(num_bytes); |
| 84 | if (ptr == NULL) { |
| 85 | return NULL; |
| 86 | } |
| 87 | #if MICROPY_MEM_STATS |
| 88 | total_bytes_allocated += num_bytes; |
| 89 | current_bytes_allocated += num_bytes; |
| 90 | UPDATE_PEAK(); |
| 91 | #endif |
| 92 | DEBUG_printf("malloc %d : %p\n", num_bytes, ptr); |
| 93 | return ptr; |
| 94 | } |
| 95 | |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 96 | #if MICROPY_ENABLE_FINALISER |
| 97 | void *m_malloc_with_finaliser(int num_bytes) { |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 98 | if (num_bytes == 0) { |
| 99 | return NULL; |
| 100 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 101 | void *ptr = malloc_with_finaliser(num_bytes); |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 102 | if (ptr == NULL) { |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 103 | return m_malloc_fail(num_bytes); |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 104 | } |
| 105 | #if MICROPY_MEM_STATS |
| 106 | total_bytes_allocated += num_bytes; |
| 107 | current_bytes_allocated += num_bytes; |
| 108 | UPDATE_PEAK(); |
| 109 | #endif |
| 110 | DEBUG_printf("malloc %d : %p\n", num_bytes, ptr); |
| 111 | return ptr; |
| 112 | } |
Damien George | 12bab72 | 2014-04-05 20:35:48 +0100 | [diff] [blame] | 113 | #endif |
mux | 4f7e9f5 | 2014-04-03 23:55:12 +0200 | [diff] [blame] | 114 | |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 115 | void *m_malloc0(int num_bytes) { |
Paul Sokolovsky | 58ff93b | 2014-02-10 18:37:11 +0200 | [diff] [blame] | 116 | void *ptr = m_malloc(num_bytes); |
| 117 | if (ptr != NULL) { |
| 118 | memset(ptr, 0, num_bytes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 119 | } |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 120 | return ptr; |
| 121 | } |
| 122 | |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 123 | void *m_realloc(void *ptr, int old_num_bytes, int new_num_bytes) { |
| 124 | if (new_num_bytes == 0) { |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 125 | free(ptr); |
| 126 | return NULL; |
| 127 | } |
Paul Sokolovsky | cdd2c62 | 2014-01-30 03:58:17 +0200 | [diff] [blame] | 128 | void *new_ptr = realloc(ptr, new_num_bytes); |
| 129 | if (new_ptr == NULL) { |
Damien George | 6902eed | 2014-04-04 10:52:59 +0000 | [diff] [blame] | 130 | return m_malloc_fail(new_num_bytes); |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 131 | } |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 132 | #if MICROPY_MEM_STATS |
Paul Sokolovsky | 43f1c80 | 2014-01-01 23:04:25 +0200 | [diff] [blame] | 133 | // At first thought, "Total bytes allocated" should only grow, |
| 134 | // after all, it's *total*. But consider for example 2K block |
| 135 | // shrunk to 1K and then grown to 2K again. It's still 2K |
| 136 | // allocated total. If we process only positive increments, |
| 137 | // we'll count 3K. |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame] | 138 | int diff = new_num_bytes - old_num_bytes; |
| 139 | total_bytes_allocated += diff; |
| 140 | current_bytes_allocated += diff; |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 141 | UPDATE_PEAK(); |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 142 | #endif |
Paul Sokolovsky | cdd2c62 | 2014-01-30 03:58:17 +0200 | [diff] [blame] | 143 | DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr); |
| 144 | return new_ptr; |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Damien George | 58ba4c3 | 2014-04-10 14:27:31 +0000 | [diff] [blame] | 147 | void *m_realloc_maybe(void *ptr, int old_num_bytes, int new_num_bytes) { |
| 148 | void *new_ptr = realloc(ptr, new_num_bytes); |
| 149 | if (new_ptr == NULL) { |
| 150 | return NULL; |
| 151 | } |
| 152 | #if MICROPY_MEM_STATS |
| 153 | // At first thought, "Total bytes allocated" should only grow, |
| 154 | // after all, it's *total*. But consider for example 2K block |
| 155 | // shrunk to 1K and then grown to 2K again. It's still 2K |
| 156 | // allocated total. If we process only positive increments, |
| 157 | // we'll count 3K. |
| 158 | int diff = new_num_bytes - old_num_bytes; |
| 159 | total_bytes_allocated += diff; |
| 160 | current_bytes_allocated += diff; |
| 161 | UPDATE_PEAK(); |
| 162 | #endif |
| 163 | DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr); |
| 164 | return new_ptr; |
| 165 | } |
| 166 | |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 167 | void m_free(void *ptr, int num_bytes) { |
| 168 | if (ptr != NULL) { |
| 169 | free(ptr); |
| 170 | } |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 171 | #if MICROPY_MEM_STATS |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame] | 172 | current_bytes_allocated -= num_bytes; |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 173 | #endif |
Damien George | 2d15c12 | 2014-01-29 20:33:20 +0000 | [diff] [blame] | 174 | DEBUG_printf("free %p, %d\n", ptr, num_bytes); |
Damien | 732407f | 2013-12-29 19:33:23 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Damien | 8b3a7c2 | 2013-10-23 20:20:17 +0100 | [diff] [blame] | 177 | int m_get_total_bytes_allocated(void) { |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 178 | #if MICROPY_MEM_STATS |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 179 | return total_bytes_allocated; |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 180 | #else |
| 181 | return -1; |
| 182 | #endif |
Damien | 429d719 | 2013-10-04 19:53:11 +0100 | [diff] [blame] | 183 | } |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame] | 184 | |
| 185 | int m_get_current_bytes_allocated(void) { |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 186 | #if MICROPY_MEM_STATS |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame] | 187 | return current_bytes_allocated; |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 188 | #else |
| 189 | return -1; |
| 190 | #endif |
Paul Sokolovsky | 02de0c5 | 2014-01-01 23:15:47 +0200 | [diff] [blame] | 191 | } |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 192 | |
| 193 | int m_get_peak_bytes_allocated(void) { |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 194 | #if MICROPY_MEM_STATS |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 195 | return peak_bytes_allocated; |
Paul Sokolovsky | ef18102 | 2014-01-03 03:06:25 +0200 | [diff] [blame] | 196 | #else |
| 197 | return -1; |
| 198 | #endif |
Paul Sokolovsky | 780f555 | 2014-01-01 23:42:21 +0200 | [diff] [blame] | 199 | } |