1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2020 Andy Green <andy@warmcat.com>
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
25 #include "private-lib-core.h"
26
27 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
28
29 #include <malloc.h>
30
31 /* the heap is processwide */
32 static size_t allocated;
33 #endif
34
35 #if defined(LWS_PLAT_OPTEE)
36
37 #define TEE_USER_MEM_HINT_NO_FILL_ZERO 0x80000000
38 #if defined (LWS_WITH_NETWORK)
39
40 /* normal TA apis */
41
42 void *__attribute__((weak))
TEE_Malloc(uint32_t size,uint32_t hint)43 TEE_Malloc(uint32_t size, uint32_t hint)
44 {
45 return NULL;
46 }
47 void *__attribute__((weak))
TEE_Realloc(void * buffer,uint32_t newSize)48 TEE_Realloc(void *buffer, uint32_t newSize)
49 {
50 return NULL;
51 }
52 void __attribute__((weak))
TEE_Free(void * buffer)53 TEE_Free(void *buffer)
54 {
55 }
56 #else
57
58 /* in-OP-TEE core apis */
59
60 void *
TEE_Malloc(uint32_t size,uint32_t hint)61 TEE_Malloc(uint32_t size, uint32_t hint)
62 {
63 return malloc(size);
64 }
65 void *
TEE_Realloc(void * buffer,uint32_t newSize)66 TEE_Realloc(void *buffer, uint32_t newSize)
67 {
68 return realloc(buffer, newSize);
69 }
70 void
TEE_Free(void * buffer)71 TEE_Free(void *buffer)
72 {
73 free(buffer);
74 }
75
76 #endif
77
lws_realloc(void * ptr,size_t size,const char * reason)78 void *lws_realloc(void *ptr, size_t size, const char *reason)
79 {
80 return TEE_Realloc(ptr, size);
81 }
82
lws_malloc(size_t size,const char * reason)83 void *lws_malloc(size_t size, const char *reason)
84 {
85 return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
86 }
87
lws_free(void * p)88 void lws_free(void *p)
89 {
90 TEE_Free(p);
91 }
92
lws_zalloc(size_t size,const char * reason)93 void *lws_zalloc(size_t size, const char *reason)
94 {
95 void *ptr = TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
96 if (ptr)
97 memset(ptr, 0, size);
98 return ptr;
99 }
100
lws_set_allocator(void * (* cb)(void * ptr,size_t size,const char * reason))101 void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
102 {
103 (void)cb;
104 }
105 #else
106
107 static void *
_realloc(void * ptr,size_t size,const char * reason)108 _realloc(void *ptr, size_t size, const char *reason)
109 {
110 void *v;
111
112 if (size) {
113 #if defined(LWS_PLAT_FREERTOS)
114 lwsl_debug("%s: size %lu: %s (free heap %d)\n", __func__,
115 #if defined(LWS_AMAZON_RTOS)
116 (unsigned long)size, reason, (unsigned int)xPortGetFreeHeapSize() - (int)size);
117 #else
118 (unsigned long)size, reason, (unsigned int)esp_get_free_heap_size() - (int)size);
119 #endif
120 #else
121 lwsl_debug("%s: size %lu: %s\n", __func__,
122 (unsigned long)size, reason);
123 #endif
124
125 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
126 if (ptr)
127 allocated -= malloc_usable_size(ptr);
128 #endif
129
130 #if defined(LWS_PLAT_OPTEE)
131 v = (void *)TEE_Realloc(ptr, size);
132 #else
133 v = (void *)realloc(ptr, size);
134 #endif
135 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
136 allocated += malloc_usable_size(v);
137 #endif
138 return v;
139 }
140 if (ptr) {
141 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
142 allocated -= malloc_usable_size(ptr);
143 #endif
144 free(ptr);
145 }
146
147 return NULL;
148 }
149
150 void *(*_lws_realloc)(void *ptr, size_t size, const char *reason) = _realloc;
151
lws_realloc(void * ptr,size_t size,const char * reason)152 void *lws_realloc(void *ptr, size_t size, const char *reason)
153 {
154 return _lws_realloc(ptr, size, reason);
155 }
156
lws_zalloc(size_t size,const char * reason)157 void *lws_zalloc(size_t size, const char *reason)
158 {
159 void *ptr = _lws_realloc(NULL, size, reason);
160
161 if (ptr)
162 memset(ptr, 0, size);
163
164 return ptr;
165 }
166
lws_set_allocator(void * (* cb)(void * ptr,size_t size,const char * reason))167 void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
168 {
169 _lws_realloc = cb;
170 }
171
lws_get_allocated_heap(void)172 size_t lws_get_allocated_heap(void)
173 {
174 #if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
175 return allocated;
176 #else
177 return 0;
178 #endif
179 }
180 #endif
181