heap.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <sys/reent.h>
  17. #include <malloc.h>
  18. #include "esp_heap_caps.h"
  19. /*
  20. These contain the business logic for the malloc() and realloc() implementation. Because of heap tracing
  21. wrapping reasons, we do not want these to be a public api, however, so they're not defined publicly.
  22. */
  23. extern void *heap_caps_malloc_default( size_t size );
  24. extern void *heap_caps_realloc_default( void *ptr, size_t size );
  25. void* malloc(size_t size)
  26. {
  27. return heap_caps_malloc_default(size);
  28. }
  29. void* calloc(size_t n, size_t size)
  30. {
  31. return _calloc_r(_REENT, n, size);
  32. }
  33. void* realloc(void* ptr, size_t size)
  34. {
  35. return heap_caps_realloc_default(ptr, size);
  36. }
  37. void free(void *ptr)
  38. {
  39. heap_caps_free(ptr);
  40. }
  41. void* _malloc_r(struct _reent *r, size_t size)
  42. {
  43. return heap_caps_malloc_default(size);
  44. }
  45. void _free_r(struct _reent *r, void* ptr)
  46. {
  47. heap_caps_free(ptr);
  48. }
  49. void* _realloc_r(struct _reent *r, void* ptr, size_t size)
  50. {
  51. return heap_caps_realloc_default( ptr, size );
  52. }
  53. void* _calloc_r(struct _reent *r, size_t nmemb, size_t size)
  54. {
  55. void *result;
  56. size_t size_bytes;
  57. if (__builtin_mul_overflow(nmemb, size, &size_bytes)) {
  58. return NULL;
  59. }
  60. result = heap_caps_malloc_default(size_bytes);
  61. if (result != NULL) {
  62. bzero(result, size_bytes);
  63. }
  64. return result;
  65. }
  66. /* No-op function, used to force linking this file,
  67. instead of the heap implementation from newlib.
  68. */
  69. void newlib_include_heap_impl(void)
  70. {
  71. }
  72. /* The following functions are implemented by newlib's heap allocator,
  73. but aren't available in the heap component.
  74. Define them as non-functional stubs here, so that the application
  75. can not cause the newlib heap implementation to be linked in
  76. */
  77. void* memalign(size_t alignment, size_t n)
  78. {
  79. extern void memalign_function_was_linked_but_unsupported_in_esp_idf(void);
  80. memalign_function_was_linked_but_unsupported_in_esp_idf();
  81. return NULL;
  82. }
  83. int malloc_trim(size_t pad)
  84. {
  85. return 0; // indicates failure
  86. }
  87. size_t malloc_usable_size(void* p)
  88. {
  89. return 0;
  90. }
  91. void malloc_stats(void)
  92. {
  93. }
  94. int mallopt(int parameter_number, int parameter_value)
  95. {
  96. return 0; // indicates failure
  97. }
  98. struct mallinfo mallinfo(void)
  99. {
  100. struct mallinfo dummy = {0};
  101. return dummy;
  102. }
  103. void* valloc(size_t n) __attribute__((alias("malloc")));
  104. void* pvalloc(size_t n) __attribute__((alias("malloc")));
  105. void cfree(void* p) __attribute__((alias("free")));