heap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <sys/reent.h>
  9. #include <errno.h>
  10. #include <malloc.h>
  11. #include "esp_heap_caps.h"
  12. /*
  13. These contain the business logic for the malloc() and realloc() implementation. Because of heap tracing
  14. wrapping reasons, we do not want these to be a public api, however, so they're not defined publicly.
  15. */
  16. extern void *heap_caps_malloc_default( size_t size );
  17. extern void *heap_caps_realloc_default( void *ptr, size_t size );
  18. extern void *heap_caps_aligned_alloc_default( size_t alignment, size_t size );
  19. void* malloc(size_t size)
  20. {
  21. return heap_caps_malloc_default(size);
  22. }
  23. void* calloc(size_t n, size_t size)
  24. {
  25. return _calloc_r(_REENT, n, size);
  26. }
  27. void* realloc(void* ptr, size_t size)
  28. {
  29. return heap_caps_realloc_default(ptr, size);
  30. }
  31. void free(void *ptr)
  32. {
  33. heap_caps_free(ptr);
  34. }
  35. void* _malloc_r(struct _reent *r, size_t size)
  36. {
  37. return heap_caps_malloc_default(size);
  38. }
  39. void _free_r(struct _reent *r, void* ptr)
  40. {
  41. heap_caps_free(ptr);
  42. }
  43. void* _realloc_r(struct _reent *r, void* ptr, size_t size)
  44. {
  45. return heap_caps_realloc_default( ptr, size );
  46. }
  47. void* _calloc_r(struct _reent *r, size_t nmemb, size_t size)
  48. {
  49. void *result;
  50. size_t size_bytes;
  51. if (__builtin_mul_overflow(nmemb, size, &size_bytes)) {
  52. return NULL;
  53. }
  54. result = heap_caps_malloc_default(size_bytes);
  55. if (result != NULL) {
  56. bzero(result, size_bytes);
  57. }
  58. return result;
  59. }
  60. void* memalign(size_t alignment, size_t n)
  61. {
  62. return heap_caps_aligned_alloc_default(alignment, n);
  63. }
  64. int posix_memalign(void **out_ptr, size_t alignment, size_t size)
  65. {
  66. if (size == 0) {
  67. /* returning NULL for zero size is allowed, don't treat this as an error */
  68. *out_ptr = NULL;
  69. return 0;
  70. }
  71. void *result = heap_caps_aligned_alloc_default(alignment, size);
  72. if (result != NULL) {
  73. /* Modify output pointer only on success */
  74. *out_ptr = result;
  75. return 0;
  76. }
  77. /* Note: error returned, not set via errno! */
  78. return ENOMEM;
  79. }
  80. /* No-op function, used to force linking this file,
  81. instead of the heap implementation from newlib.
  82. */
  83. void newlib_include_heap_impl(void)
  84. {
  85. }
  86. /* The following functions are implemented by newlib's heap allocator,
  87. but aren't available in the heap component.
  88. Define them as non-functional stubs here, so that the application
  89. can not cause the newlib heap implementation to be linked in
  90. */
  91. int malloc_trim(size_t pad)
  92. {
  93. return 0; // indicates failure
  94. }
  95. size_t malloc_usable_size(void* p)
  96. {
  97. return 0;
  98. }
  99. void malloc_stats(void)
  100. {
  101. }
  102. int mallopt(int parameter_number, int parameter_value)
  103. {
  104. return 0; // indicates failure
  105. }
  106. struct mallinfo mallinfo(void)
  107. {
  108. struct mallinfo dummy = {0};
  109. return dummy;
  110. }
  111. void* valloc(size_t n) __attribute__((alias("malloc")));
  112. void* pvalloc(size_t n) __attribute__((alias("malloc")));
  113. void cfree(void* p) __attribute__((alias("free")));