heap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. void* memalign(size_t alignment, size_t n)
  67. {
  68. return heap_caps_aligned_alloc(alignment, n, MALLOC_CAP_DEFAULT);
  69. }
  70. /* No-op function, used to force linking this file,
  71. instead of the heap implementation from newlib.
  72. */
  73. void newlib_include_heap_impl(void)
  74. {
  75. }
  76. /* The following functions are implemented by newlib's heap allocator,
  77. but aren't available in the heap component.
  78. Define them as non-functional stubs here, so that the application
  79. can not cause the newlib heap implementation to be linked in
  80. */
  81. int malloc_trim(size_t pad)
  82. {
  83. return 0; // indicates failure
  84. }
  85. size_t malloc_usable_size(void* p)
  86. {
  87. return 0;
  88. }
  89. void malloc_stats(void)
  90. {
  91. }
  92. int mallopt(int parameter_number, int parameter_value)
  93. {
  94. return 0; // indicates failure
  95. }
  96. struct mallinfo mallinfo(void)
  97. {
  98. struct mallinfo dummy = {0};
  99. return dummy;
  100. }
  101. void* valloc(size_t n) __attribute__((alias("malloc")));
  102. void* pvalloc(size_t n) __attribute__((alias("malloc")));
  103. void cfree(void* p) __attribute__((alias("free")));