malloc.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include "py/mpconfig.h"
  30. #include "py/misc.h"
  31. #include "py/mpstate.h"
  32. #if MICROPY_DEBUG_VERBOSE // print debugging info
  33. #define DEBUG_printf DEBUG_printf
  34. #else // don't print debugging info
  35. #define DEBUG_printf(...) (void)0
  36. #endif
  37. #if MICROPY_MEM_STATS
  38. #if !MICROPY_MALLOC_USES_ALLOCATED_SIZE
  39. #error MICROPY_MEM_STATS requires MICROPY_MALLOC_USES_ALLOCATED_SIZE
  40. #endif
  41. #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); }
  42. #endif
  43. #if MICROPY_ENABLE_GC
  44. #include "py/gc.h"
  45. // We redirect standard alloc functions to GC heap - just for the rest of
  46. // this module. In the rest of MicroPython source, system malloc can be
  47. // freely accessed - for interfacing with system and 3rd-party libs for
  48. // example. On the other hand, some (e.g. bare-metal) ports may use GC
  49. // heap as system heap, so, to avoid warnings, we do undef's first.
  50. #undef malloc
  51. #undef free
  52. #undef realloc
  53. #define malloc(b) gc_alloc((b), false)
  54. #define malloc_with_finaliser(b) gc_alloc((b), true)
  55. #define free gc_free
  56. #define realloc(ptr, n) gc_realloc(ptr, n, true)
  57. #define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv)
  58. #else
  59. // GC is disabled. Use system malloc/realloc/free.
  60. #if MICROPY_ENABLE_FINALISER
  61. #error MICROPY_ENABLE_FINALISER requires MICROPY_ENABLE_GC
  62. #endif
  63. static void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
  64. if (allow_move) {
  65. return realloc(ptr, n_bytes);
  66. } else {
  67. // We are asked to resize, but without moving the memory region pointed to
  68. // by ptr. Unless the underlying memory manager has special provision for
  69. // this behaviour there is nothing we can do except fail to resize.
  70. return NULL;
  71. }
  72. }
  73. #endif // MICROPY_ENABLE_GC
  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. void *m_malloc_maybe(size_t num_bytes) {
  88. void *ptr = malloc(num_bytes);
  89. #if MICROPY_MEM_STATS
  90. MP_STATE_MEM(total_bytes_allocated) += num_bytes;
  91. MP_STATE_MEM(current_bytes_allocated) += num_bytes;
  92. UPDATE_PEAK();
  93. #endif
  94. DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
  95. return ptr;
  96. }
  97. #if MICROPY_ENABLE_FINALISER
  98. void *m_malloc_with_finaliser(size_t num_bytes) {
  99. void *ptr = malloc_with_finaliser(num_bytes);
  100. if (ptr == NULL && num_bytes != 0) {
  101. m_malloc_fail(num_bytes);
  102. }
  103. #if MICROPY_MEM_STATS
  104. MP_STATE_MEM(total_bytes_allocated) += num_bytes;
  105. MP_STATE_MEM(current_bytes_allocated) += num_bytes;
  106. UPDATE_PEAK();
  107. #endif
  108. DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
  109. return ptr;
  110. }
  111. #endif
  112. void *m_malloc0(size_t num_bytes) {
  113. void *ptr = m_malloc(num_bytes);
  114. // If this config is set then the GC clears all memory, so we don't need to.
  115. #if !MICROPY_GC_CONSERVATIVE_CLEAR
  116. memset(ptr, 0, num_bytes);
  117. #endif
  118. return ptr;
  119. }
  120. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  121. void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes)
  122. #else
  123. void *m_realloc(void *ptr, size_t new_num_bytes)
  124. #endif
  125. {
  126. void *new_ptr = realloc(ptr, new_num_bytes);
  127. if (new_ptr == NULL && new_num_bytes != 0) {
  128. m_malloc_fail(new_num_bytes);
  129. }
  130. #if MICROPY_MEM_STATS
  131. // At first thought, "Total bytes allocated" should only grow,
  132. // after all, it's *total*. But consider for example 2K block
  133. // shrunk to 1K and then grown to 2K again. It's still 2K
  134. // allocated total. If we process only positive increments,
  135. // we'll count 3K.
  136. size_t diff = new_num_bytes - old_num_bytes;
  137. MP_STATE_MEM(total_bytes_allocated) += diff;
  138. MP_STATE_MEM(current_bytes_allocated) += diff;
  139. UPDATE_PEAK();
  140. #endif
  141. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  142. DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
  143. #else
  144. DEBUG_printf("realloc %p, %d : %p\n", ptr, new_num_bytes, new_ptr);
  145. #endif
  146. return new_ptr;
  147. }
  148. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  149. void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move)
  150. #else
  151. void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move)
  152. #endif
  153. {
  154. void *new_ptr = realloc_ext(ptr, new_num_bytes, allow_move);
  155. #if MICROPY_MEM_STATS
  156. // At first thought, "Total bytes allocated" should only grow,
  157. // after all, it's *total*. But consider for example 2K block
  158. // shrunk to 1K and then grown to 2K again. It's still 2K
  159. // allocated total. If we process only positive increments,
  160. // we'll count 3K.
  161. // Also, don't count failed reallocs.
  162. if (!(new_ptr == NULL && new_num_bytes != 0)) {
  163. size_t diff = new_num_bytes - old_num_bytes;
  164. MP_STATE_MEM(total_bytes_allocated) += diff;
  165. MP_STATE_MEM(current_bytes_allocated) += diff;
  166. UPDATE_PEAK();
  167. }
  168. #endif
  169. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  170. DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
  171. #else
  172. DEBUG_printf("realloc %p, %d : %p\n", ptr, new_num_bytes, new_ptr);
  173. #endif
  174. return new_ptr;
  175. }
  176. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  177. void m_free(void *ptr, size_t num_bytes)
  178. #else
  179. void m_free(void *ptr)
  180. #endif
  181. {
  182. free(ptr);
  183. #if MICROPY_MEM_STATS
  184. MP_STATE_MEM(current_bytes_allocated) -= num_bytes;
  185. #endif
  186. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  187. DEBUG_printf("free %p, %d\n", ptr, num_bytes);
  188. #else
  189. DEBUG_printf("free %p\n", ptr);
  190. #endif
  191. }
  192. #if MICROPY_TRACKED_ALLOC
  193. #define MICROPY_TRACKED_ALLOC_STORE_SIZE (!MICROPY_ENABLE_GC)
  194. typedef struct _m_tracked_node_t {
  195. struct _m_tracked_node_t *prev;
  196. struct _m_tracked_node_t *next;
  197. #if MICROPY_TRACKED_ALLOC_STORE_SIZE
  198. uintptr_t size;
  199. #endif
  200. uint8_t data[];
  201. } m_tracked_node_t;
  202. #if MICROPY_DEBUG_VERBOSE
  203. static size_t m_tracked_count_links(size_t *nb) {
  204. m_tracked_node_t *node = MP_STATE_VM(m_tracked_head);
  205. size_t n = 0;
  206. *nb = 0;
  207. while (node != NULL) {
  208. ++n;
  209. #if MICROPY_TRACKED_ALLOC_STORE_SIZE
  210. *nb += node->size;
  211. #else
  212. *nb += gc_nbytes(node);
  213. #endif
  214. node = node->next;
  215. }
  216. return n;
  217. }
  218. #endif
  219. void *m_tracked_calloc(size_t nmemb, size_t size) {
  220. m_tracked_node_t *node = m_malloc_maybe(sizeof(m_tracked_node_t) + nmemb * size);
  221. if (node == NULL) {
  222. return NULL;
  223. }
  224. #if MICROPY_DEBUG_VERBOSE
  225. size_t nb;
  226. size_t n = m_tracked_count_links(&nb);
  227. DEBUG_printf("m_tracked_calloc(%u, %u) -> (%u;%u) %p\n", (int)nmemb, (int)size, (int)n, (int)nb, node);
  228. #endif
  229. if (MP_STATE_VM(m_tracked_head) != NULL) {
  230. MP_STATE_VM(m_tracked_head)->prev = node;
  231. }
  232. node->prev = NULL;
  233. node->next = MP_STATE_VM(m_tracked_head);
  234. MP_STATE_VM(m_tracked_head) = node;
  235. #if MICROPY_TRACKED_ALLOC_STORE_SIZE
  236. node->size = nmemb * size;
  237. #endif
  238. #if !MICROPY_GC_CONSERVATIVE_CLEAR
  239. memset(&node->data[0], 0, nmemb * size);
  240. #endif
  241. return &node->data[0];
  242. }
  243. void m_tracked_free(void *ptr_in) {
  244. if (ptr_in == NULL) {
  245. return;
  246. }
  247. m_tracked_node_t *node = (m_tracked_node_t *)((uint8_t *)ptr_in - sizeof(m_tracked_node_t));
  248. #if MICROPY_DEBUG_VERBOSE
  249. size_t data_bytes;
  250. #if MICROPY_TRACKED_ALLOC_STORE_SIZE
  251. data_bytes = node->size;
  252. #else
  253. data_bytes = gc_nbytes(node);
  254. #endif
  255. size_t nb;
  256. size_t n = m_tracked_count_links(&nb);
  257. DEBUG_printf("m_tracked_free(%p, [%p, %p], nbytes=%u, links=%u;%u)\n", node, node->prev, node->next, (int)data_bytes, (int)n, (int)nb);
  258. #endif
  259. if (node->next != NULL) {
  260. node->next->prev = node->prev;
  261. }
  262. if (node->prev != NULL) {
  263. node->prev->next = node->next;
  264. } else {
  265. MP_STATE_VM(m_tracked_head) = node->next;
  266. }
  267. m_free(node
  268. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  269. #if MICROPY_TRACKED_ALLOC_STORE_SIZE
  270. , node->size
  271. #else
  272. , gc_nbytes(node)
  273. #endif
  274. #endif
  275. );
  276. }
  277. #endif // MICROPY_TRACKED_ALLOC
  278. #if MICROPY_MEM_STATS
  279. size_t m_get_total_bytes_allocated(void) {
  280. return MP_STATE_MEM(total_bytes_allocated);
  281. }
  282. size_t m_get_current_bytes_allocated(void) {
  283. return MP_STATE_MEM(current_bytes_allocated);
  284. }
  285. size_t m_get_peak_bytes_allocated(void) {
  286. return MP_STATE_MEM(peak_bytes_allocated);
  287. }
  288. #endif