malloc.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. STATIC void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
  60. if (allow_move) {
  61. return realloc(ptr, n_bytes);
  62. } else {
  63. // We are asked to resize, but without moving the memory region pointed to
  64. // by ptr. Unless the underlying memory manager has special provision for
  65. // this behaviour there is nothing we can do except fail to resize.
  66. return NULL;
  67. }
  68. }
  69. #endif // MICROPY_ENABLE_GC
  70. void *m_malloc(size_t num_bytes) {
  71. void *ptr = malloc(num_bytes);
  72. if (ptr == NULL && num_bytes != 0) {
  73. m_malloc_fail(num_bytes);
  74. }
  75. #if MICROPY_MEM_STATS
  76. MP_STATE_MEM(total_bytes_allocated) += num_bytes;
  77. MP_STATE_MEM(current_bytes_allocated) += num_bytes;
  78. UPDATE_PEAK();
  79. #endif
  80. DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
  81. return ptr;
  82. }
  83. void *m_malloc_maybe(size_t num_bytes) {
  84. void *ptr = malloc(num_bytes);
  85. #if MICROPY_MEM_STATS
  86. MP_STATE_MEM(total_bytes_allocated) += num_bytes;
  87. MP_STATE_MEM(current_bytes_allocated) += num_bytes;
  88. UPDATE_PEAK();
  89. #endif
  90. DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
  91. return ptr;
  92. }
  93. #if MICROPY_ENABLE_FINALISER
  94. void *m_malloc_with_finaliser(size_t num_bytes) {
  95. void *ptr = malloc_with_finaliser(num_bytes);
  96. if (ptr == NULL && num_bytes != 0) {
  97. m_malloc_fail(num_bytes);
  98. }
  99. #if MICROPY_MEM_STATS
  100. MP_STATE_MEM(total_bytes_allocated) += num_bytes;
  101. MP_STATE_MEM(current_bytes_allocated) += num_bytes;
  102. UPDATE_PEAK();
  103. #endif
  104. DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
  105. return ptr;
  106. }
  107. #endif
  108. void *m_malloc0(size_t num_bytes) {
  109. void *ptr = m_malloc(num_bytes);
  110. // If this config is set then the GC clears all memory, so we don't need to.
  111. #if !MICROPY_GC_CONSERVATIVE_CLEAR
  112. memset(ptr, 0, num_bytes);
  113. #endif
  114. return ptr;
  115. }
  116. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  117. void *m_realloc(void *ptr, size_t old_num_bytes, size_t new_num_bytes) {
  118. #else
  119. void *m_realloc(void *ptr, size_t new_num_bytes) {
  120. #endif
  121. void *new_ptr = realloc(ptr, new_num_bytes);
  122. if (new_ptr == NULL && new_num_bytes != 0) {
  123. m_malloc_fail(new_num_bytes);
  124. }
  125. #if MICROPY_MEM_STATS
  126. // At first thought, "Total bytes allocated" should only grow,
  127. // after all, it's *total*. But consider for example 2K block
  128. // shrunk to 1K and then grown to 2K again. It's still 2K
  129. // allocated total. If we process only positive increments,
  130. // we'll count 3K.
  131. size_t diff = new_num_bytes - old_num_bytes;
  132. MP_STATE_MEM(total_bytes_allocated) += diff;
  133. MP_STATE_MEM(current_bytes_allocated) += diff;
  134. UPDATE_PEAK();
  135. #endif
  136. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  137. DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
  138. #else
  139. DEBUG_printf("realloc %p, %d : %p\n", ptr, new_num_bytes, new_ptr);
  140. #endif
  141. return new_ptr;
  142. }
  143. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  144. void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move) {
  145. #else
  146. void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move) {
  147. #endif
  148. void *new_ptr = realloc_ext(ptr, new_num_bytes, allow_move);
  149. #if MICROPY_MEM_STATS
  150. // At first thought, "Total bytes allocated" should only grow,
  151. // after all, it's *total*. But consider for example 2K block
  152. // shrunk to 1K and then grown to 2K again. It's still 2K
  153. // allocated total. If we process only positive increments,
  154. // we'll count 3K.
  155. // Also, don't count failed reallocs.
  156. if (!(new_ptr == NULL && new_num_bytes != 0)) {
  157. size_t diff = new_num_bytes - old_num_bytes;
  158. MP_STATE_MEM(total_bytes_allocated) += diff;
  159. MP_STATE_MEM(current_bytes_allocated) += diff;
  160. UPDATE_PEAK();
  161. }
  162. #endif
  163. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  164. DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
  165. #else
  166. DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, new_num_bytes, new_ptr);
  167. #endif
  168. return new_ptr;
  169. }
  170. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  171. void m_free(void *ptr, size_t num_bytes) {
  172. #else
  173. void m_free(void *ptr) {
  174. #endif
  175. free(ptr);
  176. #if MICROPY_MEM_STATS
  177. MP_STATE_MEM(current_bytes_allocated) -= num_bytes;
  178. #endif
  179. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  180. DEBUG_printf("free %p, %d\n", ptr, num_bytes);
  181. #else
  182. DEBUG_printf("free %p\n", ptr);
  183. #endif
  184. }
  185. #if MICROPY_MEM_STATS
  186. size_t m_get_total_bytes_allocated(void) {
  187. return MP_STATE_MEM(total_bytes_allocated);
  188. }
  189. size_t m_get_current_bytes_allocated(void) {
  190. return MP_STATE_MEM(current_bytes_allocated);
  191. }
  192. size_t m_get_peak_bytes_allocated(void) {
  193. return MP_STATE_MEM(peak_bytes_allocated);
  194. }
  195. #endif