malloc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, %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_MEM_STATS
  193. size_t m_get_total_bytes_allocated(void) {
  194. return MP_STATE_MEM(total_bytes_allocated);
  195. }
  196. size_t m_get_current_bytes_allocated(void) {
  197. return MP_STATE_MEM(current_bytes_allocated);
  198. }
  199. size_t m_get_peak_bytes_allocated(void) {
  200. return MP_STATE_MEM(peak_bytes_allocated);
  201. }
  202. #endif