malloc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #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); }
  39. #endif
  40. #if MICROPY_ENABLE_GC
  41. #include "py/gc.h"
  42. // We redirect standard alloc functions to GC heap - just for the rest of
  43. // this module. In the rest of MicroPython source, system malloc can be
  44. // freely accessed - for interfacing with system and 3rd-party libs for
  45. // example. On the other hand, some (e.g. bare-metal) ports may use GC
  46. // heap as system heap, so, to avoid warnings, we do undef's first.
  47. #undef malloc
  48. #undef free
  49. #undef realloc
  50. #define malloc(b) gc_alloc((b), false)
  51. #define malloc_with_finaliser(b) gc_alloc((b), true)
  52. #define free gc_free
  53. #define realloc(ptr, n) gc_realloc(ptr, n, true)
  54. #define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv)
  55. #else
  56. STATIC void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
  57. if (allow_move) {
  58. return realloc(ptr, n_bytes);
  59. } else {
  60. // We are asked to resize, but without moving the memory region pointed to
  61. // by ptr. Unless the underlying memory manager has special provision for
  62. // this behaviour there is nothing we can do except fail to resize.
  63. return NULL;
  64. }
  65. }
  66. #endif // MICROPY_ENABLE_GC
  67. void *m_malloc(size_t num_bytes) {
  68. void *ptr = malloc(num_bytes);
  69. if (ptr == NULL && num_bytes != 0) {
  70. m_malloc_fail(num_bytes);
  71. }
  72. #if MICROPY_MEM_STATS
  73. MP_STATE_MEM(total_bytes_allocated) += num_bytes;
  74. MP_STATE_MEM(current_bytes_allocated) += num_bytes;
  75. UPDATE_PEAK();
  76. #endif
  77. DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
  78. return ptr;
  79. }
  80. void *m_malloc_maybe(size_t num_bytes) {
  81. void *ptr = malloc(num_bytes);
  82. #if MICROPY_MEM_STATS
  83. MP_STATE_MEM(total_bytes_allocated) += num_bytes;
  84. MP_STATE_MEM(current_bytes_allocated) += num_bytes;
  85. UPDATE_PEAK();
  86. #endif
  87. DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
  88. return ptr;
  89. }
  90. #if MICROPY_ENABLE_FINALISER
  91. void *m_malloc_with_finaliser(size_t num_bytes) {
  92. void *ptr = malloc_with_finaliser(num_bytes);
  93. if (ptr == NULL && num_bytes != 0) {
  94. m_malloc_fail(num_bytes);
  95. }
  96. #if MICROPY_MEM_STATS
  97. MP_STATE_MEM(total_bytes_allocated) += num_bytes;
  98. MP_STATE_MEM(current_bytes_allocated) += num_bytes;
  99. UPDATE_PEAK();
  100. #endif
  101. DEBUG_printf("malloc %d : %p\n", num_bytes, ptr);
  102. return ptr;
  103. }
  104. #endif
  105. void *m_malloc0(size_t num_bytes) {
  106. void *ptr = m_malloc(num_bytes);
  107. if (ptr == NULL && num_bytes != 0) {
  108. m_malloc_fail(num_bytes);
  109. }
  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. DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
  137. return new_ptr;
  138. }
  139. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  140. void *m_realloc_maybe(void *ptr, size_t old_num_bytes, size_t new_num_bytes, bool allow_move) {
  141. #else
  142. void *m_realloc_maybe(void *ptr, size_t new_num_bytes, bool allow_move) {
  143. #endif
  144. void *new_ptr = realloc_ext(ptr, new_num_bytes, allow_move);
  145. #if MICROPY_MEM_STATS
  146. // At first thought, "Total bytes allocated" should only grow,
  147. // after all, it's *total*. But consider for example 2K block
  148. // shrunk to 1K and then grown to 2K again. It's still 2K
  149. // allocated total. If we process only positive increments,
  150. // we'll count 3K.
  151. // Also, don't count failed reallocs.
  152. if (!(new_ptr == NULL && new_num_bytes != 0)) {
  153. size_t diff = new_num_bytes - old_num_bytes;
  154. MP_STATE_MEM(total_bytes_allocated) += diff;
  155. MP_STATE_MEM(current_bytes_allocated) += diff;
  156. UPDATE_PEAK();
  157. }
  158. #endif
  159. DEBUG_printf("realloc %p, %d, %d : %p\n", ptr, old_num_bytes, new_num_bytes, new_ptr);
  160. return new_ptr;
  161. }
  162. #if MICROPY_MALLOC_USES_ALLOCATED_SIZE
  163. void m_free(void *ptr, size_t num_bytes) {
  164. #else
  165. void m_free(void *ptr) {
  166. #endif
  167. free(ptr);
  168. #if MICROPY_MEM_STATS
  169. MP_STATE_MEM(current_bytes_allocated) -= num_bytes;
  170. #endif
  171. DEBUG_printf("free %p, %d\n", ptr, num_bytes);
  172. }
  173. #if MICROPY_MEM_STATS
  174. size_t m_get_total_bytes_allocated(void) {
  175. return MP_STATE_MEM(total_bytes_allocated);
  176. }
  177. size_t m_get_current_bytes_allocated(void) {
  178. return MP_STATE_MEM(current_bytes_allocated);
  179. }
  180. size_t m_get_peak_bytes_allocated(void) {
  181. return MP_STATE_MEM(peak_bytes_allocated);
  182. }
  183. #endif