1 #include "private-lib-core.h"
3 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
7 /* the heap is processwide */
8 static size_t allocated;
11 #if defined(LWS_PLAT_OPTEE)
13 #define TEE_USER_MEM_HINT_NO_FILL_ZERO 0x80000000
14 #if defined (LWS_WITH_NETWORK)
18 void *__attribute__((weak))
19 TEE_Malloc(uint32_t size, uint32_t hint)
23 void *__attribute__((weak))
24 TEE_Realloc(void *buffer, uint32_t newSize)
28 void __attribute__((weak))
29 TEE_Free(void *buffer)
34 /* in-OP-TEE core apis */
37 TEE_Malloc(uint32_t size, uint32_t hint)
42 TEE_Realloc(void *buffer, uint32_t newSize)
44 return realloc(buffer, newSize);
47 TEE_Free(void *buffer)
54 void *lws_realloc(void *ptr, size_t size, const char *reason)
56 return TEE_Realloc(ptr, size);
59 void *lws_malloc(size_t size, const char *reason)
61 return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
64 void lws_free(void *p)
69 void *lws_zalloc(size_t size, const char *reason)
71 void *ptr = TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
77 void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
84 _realloc(void *ptr, size_t size, const char *reason)
89 #if defined(LWS_WITH_ESP32)
90 lwsl_notice("%s: size %lu: %s (free heap %d)\n", __func__,
91 #if defined(LWS_AMAZON_RTOS)
92 (unsigned long)size, reason, (unsigned int)xPortGetFreeHeapSize() - (int)size);
94 (unsigned long)size, reason, (unsigned int)esp_get_free_heap_size() - (int)size);
97 lwsl_debug("%s: size %lu: %s\n", __func__,
98 (unsigned long)size, reason);
101 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
103 allocated -= malloc_usable_size(ptr);
106 #if defined(LWS_PLAT_OPTEE)
107 v = (void *)TEE_Realloc(ptr, size);
109 v = (void *)realloc(ptr, size);
111 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
112 allocated += malloc_usable_size(v);
117 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
118 allocated -= malloc_usable_size(ptr);
126 void *(*_lws_realloc)(void *ptr, size_t size, const char *reason) = _realloc;
128 void *lws_realloc(void *ptr, size_t size, const char *reason)
130 return _lws_realloc(ptr, size, reason);
133 void *lws_zalloc(size_t size, const char *reason)
135 void *ptr = _lws_realloc(NULL, size, reason);
138 memset(ptr, 0, size);
143 void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
148 size_t lws_get_allocated_heap(void)
150 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)