Coverage Report

Created: 2025-11-11 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libwebsockets/lib/core/alloc.c
Line
Count
Source
1
/*
2
 * libwebsockets - small server side websockets and web server implementation
3
 *
4
 * Copyright (C) 2010 - 2022 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_WITH_ALLOC_METADATA_LWS)
36
static lws_dll2_owner_t active;
37
#endif
38
39
#if defined(LWS_PLAT_OPTEE)
40
41
#define TEE_USER_MEM_HINT_NO_FILL_ZERO       0x80000000
42
#if defined (LWS_WITH_NETWORK)
43
44
/* normal TA apis */
45
46
void *__attribute__((weak))
47
  TEE_Malloc(uint32_t size, uint32_t hint)
48
{
49
  return NULL;
50
}
51
void *__attribute__((weak))
52
  TEE_Realloc(void *buffer, uint32_t newSize)
53
{
54
  return NULL;
55
}
56
void __attribute__((weak))
57
  TEE_Free(void *buffer)
58
{
59
}
60
#else
61
62
/* in-OP-TEE core apis */
63
64
void *
65
  TEE_Malloc(uint32_t size, uint32_t hint)
66
{
67
  return malloc(size);
68
}
69
void *
70
  TEE_Realloc(void *buffer, uint32_t newSize)
71
{
72
  return realloc(buffer, newSize);
73
}
74
void
75
  TEE_Free(void *buffer)
76
{
77
  free(buffer);
78
}
79
80
#endif
81
82
void *lws_realloc(void *ptr, size_t size, const char *reason)
83
{
84
  return TEE_Realloc(ptr, size);
85
}
86
87
void *lws_malloc(size_t size, const char *reason)
88
{
89
  return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
90
}
91
92
void lws_free(void *p)
93
{
94
  TEE_Free(p);
95
}
96
97
void *lws_zalloc(size_t size, const char *reason)
98
{
99
  void *ptr = TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
100
  if (ptr)
101
    memset(ptr, 0, size);
102
  return ptr;
103
}
104
105
void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
106
{
107
  (void)cb;
108
}
109
#else
110
111
static void *
112
_realloc(void *ptr, size_t size, const char *reason)
113
4.76k
{
114
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
115
  uint8_t comp[16 * LWS_ARRAY_SIZE(((lws_backtrace_info_t *)NULL)->st)];
116
  size_t complen;
117
  size_t adj = 0;
118
#endif
119
4.76k
  void *v;
120
121
4.76k
  if (size) {
122
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
123
    lws_alloc_metadata_gen(size, comp, sizeof(comp), &adj, &complen);
124
    size += adj;
125
#endif
126
127
#if defined(LWS_PLAT_FREERTOS)
128
    lwsl_debug("%s: size %lu: %s (free heap %d)\n", __func__,
129
#if defined(LWS_AMAZON_RTOS)
130
          (unsigned long)size, reason, (unsigned int)xPortGetFreeHeapSize() - (int)size);
131
#else
132
          (unsigned long)size, reason, (unsigned int)esp_get_free_heap_size() - (int)size);
133
#endif
134
#else
135
2.38k
    lwsl_debug("%s: size %lu: %s\n", __func__,
136
2.38k
         (unsigned long)size, reason);
137
2.38k
#endif
138
139
2.38k
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
140
2.38k
    if (ptr)
141
0
      allocated -= malloc_usable_size(ptr);
142
2.38k
#endif
143
144
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
145
    size += adj;
146
#endif
147
148
#if defined(LWS_PLAT_OPTEE)
149
    v = (void *)TEE_Realloc(ptr, size);
150
#else
151
2.38k
    v = (void *)realloc(ptr, size);
152
2.38k
#endif
153
154
2.38k
    if (!v)
155
0
      return v;
156
157
2.38k
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
158
2.38k
    allocated += malloc_usable_size(v);
159
2.38k
#endif
160
161
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
162
    _lws_alloc_metadata_adjust(&active, &v, adj, comp, (unsigned int)complen);
163
#endif
164
165
2.38k
    return v;
166
2.38k
  }
167
168
  /*
169
   * We are freeing it then...
170
   */
171
172
2.38k
  if (ptr) {
173
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
174
    _lws_alloc_metadata_trim(&ptr, NULL, NULL);
175
#endif
176
177
2.38k
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
178
2.38k
    allocated -= malloc_usable_size(ptr);
179
2.38k
#endif
180
2.38k
    free(ptr);
181
#if defined(LWS_PLAT_FREERTOS)
182
    lwsl_debug("%s: free heap %d\n", __func__,
183
#if defined(LWS_AMAZON_RTOS)
184
          (unsigned int)xPortGetFreeHeapSize() - (int)size);
185
#else
186
          (unsigned int)esp_get_free_heap_size() - (int)size);
187
#endif
188
#endif
189
2.38k
  }
190
191
2.38k
  return NULL;
192
4.76k
}
193
194
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
195
void
196
_lws_alloc_metadata_dump_lws(lws_dll2_foreach_cb_t cb, void *arg)
197
{
198
  lwsl_err("%s\n", __func__);
199
  _lws_alloc_metadata_dump(&active, cb, arg);
200
}
201
#endif
202
203
void *(*_lws_realloc)(void *ptr, size_t size, const char *reason) = _realloc;
204
205
void *lws_realloc(void *ptr, size_t size, const char *reason)
206
3.57k
{
207
3.57k
  return _lws_realloc(ptr, size, reason);
208
3.57k
}
209
210
void *lws_zalloc(size_t size, const char *reason)
211
1.19k
{
212
1.19k
  void *ptr = _lws_realloc(NULL, size, reason);
213
214
1.19k
  if (ptr)
215
1.19k
    memset(ptr, 0, size);
216
217
1.19k
  return ptr;
218
1.19k
}
219
220
void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
221
0
{
222
0
  _lws_realloc = cb;
223
0
}
224
225
size_t lws_get_allocated_heap(void)
226
0
{
227
0
#if defined(LWS_HAVE_MALLOC_USABLE_SIZE)
228
0
  return allocated;
229
#else
230
  return 0;
231
#endif
232
0
}
233
#endif