1 #include "private-lib-core.h"
2
3 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
4
5 #include <malloc.h>
6
7 /* the heap is processwide */
8 static size_t allocated;
9 #endif
10
11 #if defined(LWS_PLAT_OPTEE)
12
13 #define TEE_USER_MEM_HINT_NO_FILL_ZERO 0x80000000
14 #if defined (LWS_WITH_NETWORK)
15
16 /* normal TA apis */
17
18 void *__attribute__((weak))
TEE_Malloc(uint32_t size,uint32_t hint)19 TEE_Malloc(uint32_t size, uint32_t hint)
20 {
21 return NULL;
22 }
23 void *__attribute__((weak))
TEE_Realloc(void * buffer,uint32_t newSize)24 TEE_Realloc(void *buffer, uint32_t newSize)
25 {
26 return NULL;
27 }
28 void __attribute__((weak))
TEE_Free(void * buffer)29 TEE_Free(void *buffer)
30 {
31 }
32 #else
33
34 /* in-OP-TEE core apis */
35
36 void *
TEE_Malloc(uint32_t size,uint32_t hint)37 TEE_Malloc(uint32_t size, uint32_t hint)
38 {
39 return malloc(size);
40 }
41 void *
TEE_Realloc(void * buffer,uint32_t newSize)42 TEE_Realloc(void *buffer, uint32_t newSize)
43 {
44 return realloc(buffer, newSize);
45 }
46 void
TEE_Free(void * buffer)47 TEE_Free(void *buffer)
48 {
49 free(buffer);
50 }
51
52 #endif
53
lws_realloc(void * ptr,size_t size,const char * reason)54 void *lws_realloc(void *ptr, size_t size, const char *reason)
55 {
56 return TEE_Realloc(ptr, size);
57 }
58
lws_malloc(size_t size,const char * reason)59 void *lws_malloc(size_t size, const char *reason)
60 {
61 return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
62 }
63
lws_free(void * p)64 void lws_free(void *p)
65 {
66 TEE_Free(p);
67 }
68
lws_zalloc(size_t size,const char * reason)69 void *lws_zalloc(size_t size, const char *reason)
70 {
71 void *ptr = TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
72 if (ptr)
73 memset(ptr, 0, size);
74 return ptr;
75 }
76
lws_set_allocator(void * (* cb)(void * ptr,size_t size,const char * reason))77 void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
78 {
79 (void)cb;
80 }
81 #else
82
83 static void *
_realloc(void * ptr,size_t size,const char * reason)84 _realloc(void *ptr, size_t size, const char *reason)
85 {
86 void *v;
87
88 if (size) {
89 #if defined(LWS_PLAT_FREERTOS)
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);
93 #else
94 (unsigned long)size, reason, (unsigned int)esp_get_free_heap_size() - (int)size);
95 #endif
96 #else
97 lwsl_debug("%s: size %lu: %s\n", __func__,
98 (unsigned long)size, reason);
99 #endif
100
101 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
102 if (ptr)
103 allocated -= malloc_usable_size(ptr);
104 #endif
105
106 #if defined(LWS_PLAT_OPTEE)
107 v = (void *)TEE_Realloc(ptr, size);
108 #else
109 v = (void *)realloc(ptr, size);
110 #endif
111 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
112 allocated += malloc_usable_size(v);
113 #endif
114 return v;
115 }
116 if (ptr) {
117 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
118 allocated -= malloc_usable_size(ptr);
119 #endif
120 free(ptr);
121 }
122
123 return NULL;
124 }
125
126 void *(*_lws_realloc)(void *ptr, size_t size, const char *reason) = _realloc;
127
lws_realloc(void * ptr,size_t size,const char * reason)128 void *lws_realloc(void *ptr, size_t size, const char *reason)
129 {
130 return _lws_realloc(ptr, size, reason);
131 }
132
lws_zalloc(size_t size,const char * reason)133 void *lws_zalloc(size_t size, const char *reason)
134 {
135 void *ptr = _lws_realloc(NULL, size, reason);
136
137 if (ptr)
138 memset(ptr, 0, size);
139
140 return ptr;
141 }
142
lws_set_allocator(void * (* cb)(void * ptr,size_t size,const char * reason))143 void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
144 {
145 _lws_realloc = cb;
146 }
147
lws_get_allocated_heap(void)148 size_t lws_get_allocated_heap(void)
149 {
150 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
151 return allocated;
152 #else
153 return 0;
154 #endif
155 }
156 #endif
157