alloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include "private-lib-core.h"
  25. #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
  26. #include <malloc.h>
  27. /* the heap is processwide */
  28. static size_t allocated;
  29. #endif
  30. #if defined(LWS_PLAT_OPTEE)
  31. #define TEE_USER_MEM_HINT_NO_FILL_ZERO 0x80000000
  32. #if defined (LWS_WITH_NETWORK)
  33. /* normal TA apis */
  34. void *__attribute__((weak))
  35. TEE_Malloc(uint32_t size, uint32_t hint)
  36. {
  37. return NULL;
  38. }
  39. void *__attribute__((weak))
  40. TEE_Realloc(void *buffer, uint32_t newSize)
  41. {
  42. return NULL;
  43. }
  44. void __attribute__((weak))
  45. TEE_Free(void *buffer)
  46. {
  47. }
  48. #else
  49. /* in-OP-TEE core apis */
  50. void *
  51. TEE_Malloc(uint32_t size, uint32_t hint)
  52. {
  53. return malloc(size);
  54. }
  55. void *
  56. TEE_Realloc(void *buffer, uint32_t newSize)
  57. {
  58. return realloc(buffer, newSize);
  59. }
  60. void
  61. TEE_Free(void *buffer)
  62. {
  63. free(buffer);
  64. }
  65. #endif
  66. void *lws_realloc(void *ptr, size_t size, const char *reason)
  67. {
  68. return TEE_Realloc(ptr, size);
  69. }
  70. void *lws_malloc(size_t size, const char *reason)
  71. {
  72. return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
  73. }
  74. void lws_free(void *p)
  75. {
  76. TEE_Free(p);
  77. }
  78. void *lws_zalloc(size_t size, const char *reason)
  79. {
  80. void *ptr = TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
  81. if (ptr)
  82. memset(ptr, 0, size);
  83. return ptr;
  84. }
  85. void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
  86. {
  87. (void)cb;
  88. }
  89. #else
  90. static void *
  91. _realloc(void *ptr, size_t size, const char *reason)
  92. {
  93. void *v;
  94. if (size) {
  95. #if defined(LWS_PLAT_FREERTOS)
  96. lwsl_debug("%s: size %lu: %s (free heap %d)\n", __func__,
  97. #if defined(LWS_AMAZON_RTOS)
  98. (unsigned long)size, reason, (unsigned int)xPortGetFreeHeapSize() - (int)size);
  99. #else
  100. (unsigned long)size, reason, (unsigned int)esp_get_free_heap_size() - (int)size);
  101. #endif
  102. #else
  103. lwsl_debug("%s: size %lu: %s\n", __func__,
  104. (unsigned long)size, reason);
  105. #endif
  106. #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
  107. if (ptr)
  108. allocated -= malloc_usable_size(ptr);
  109. #endif
  110. #if defined(LWS_PLAT_OPTEE)
  111. v = (void *)TEE_Realloc(ptr, size);
  112. #else
  113. v = (void *)realloc(ptr, size);
  114. #endif
  115. #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
  116. allocated += malloc_usable_size(v);
  117. #endif
  118. return v;
  119. }
  120. if (ptr) {
  121. #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
  122. allocated -= malloc_usable_size(ptr);
  123. #endif
  124. free(ptr);
  125. }
  126. return NULL;
  127. }
  128. void *(*_lws_realloc)(void *ptr, size_t size, const char *reason) = _realloc;
  129. void *lws_realloc(void *ptr, size_t size, const char *reason)
  130. {
  131. return _lws_realloc(ptr, size, reason);
  132. }
  133. void *lws_zalloc(size_t size, const char *reason)
  134. {
  135. void *ptr = _lws_realloc(NULL, size, reason);
  136. if (ptr)
  137. memset(ptr, 0, size);
  138. return ptr;
  139. }
  140. void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
  141. {
  142. _lws_realloc = cb;
  143. }
  144. size_t lws_get_allocated_heap(void)
  145. {
  146. #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
  147. return allocated;
  148. #else
  149. return 0;
  150. #endif
  151. }
  152. #endif