Coverage Report

Created: 2026-12-01 06:31

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_PLAT_ALLOC_SIZE)
28
29
/*
30
 * The heap is processwide, and lws allocates from any thread or task that
31
 * uses it, so what is allocated is counted with atomics.  The platforms that
32
 * can size an allocation, unix and FreeRTOS, build with gcc or clang.
33
 */
34
static size_t allocated, heap_limit;
35
36
#define lws_allocated_add(n) \
37
2.67k
  (void)__atomic_add_fetch(&allocated, (n), __ATOMIC_RELAXED)
38
#define lws_allocated_sub(n) \
39
2.67k
  (void)__atomic_sub_fetch(&allocated, (n), __ATOMIC_RELAXED)
40
0
#define lws_allocated_get() __atomic_load_n(&allocated, __ATOMIC_RELAXED)
41
#endif
42
43
#if defined(LWS_WITH_ALLOC_SITES)
44
/*
45
 * Every allocation has a header in front of it saying who asked for it, and
46
 * is listed on sites_live until it's freed, so the heap no footprint
47
 * participant accounts for can be told apart by who holds it
48
 */
49
50
#define LWS_ALLOC_SITE_MAGIC  0x6c777361u
51
/* the dump ranks this many sites, and lists the biggest few of them */
52
#define LWS_ALLOC_SITE_RANKED 128
53
#define LWS_ALLOC_SITE_LISTED 24
54
55
typedef struct lws_alloc_site_hdr {
56
  lws_dll2_t    list; /* on sites_live */
57
  const char    *reason;
58
  size_t      size; /* what the caller asked for */
59
  uint32_t    magic;
60
#if defined(LWS_PLAT_CALLERS)
61
  uintptr_t   pcs[LWS_ALLOC_SITE_CALLERS];
62
#endif
63
} lws_alloc_site_hdr_t;
64
65
/* what follows is as aligned as malloc() gives, two pointers' worth */
66
#define LWS_ALLOC_SITE_ALIGN  (2 * sizeof(void *))
67
#define LWS_ALLOC_SITE_HDR_LEN  ((sizeof(lws_alloc_site_hdr_t) + \
68
          LWS_ALLOC_SITE_ALIGN - 1) & \
69
         ~(LWS_ALLOC_SITE_ALIGN - 1))
70
71
static lws_dll2_owner_t sites_live;
72
static char sites_foreign; /* the app set its own allocator */
73
74
/* lws allocates from any thread or task that uses it */
75
#if defined(LWS_PLAT_FREERTOS)
76
static portMUX_TYPE sites_mux = portMUX_INITIALIZER_UNLOCKED;
77
#define sites_lock()  portENTER_CRITICAL(&sites_mux)
78
#define sites_unlock()  portEXIT_CRITICAL(&sites_mux)
79
#elif defined(LWS_HAVE_PTHREAD_H)
80
static pthread_mutex_t sites_mtx = PTHREAD_MUTEX_INITIALIZER;
81
#define sites_lock()  pthread_mutex_lock(&sites_mtx)
82
#define sites_unlock()  pthread_mutex_unlock(&sites_mtx)
83
#else
84
#define sites_lock()
85
#define sites_unlock()
86
#endif
87
88
/* from the pointer the caller has, to the header, taking it off the list */
89
static void *
90
lws_alloc_site_take(void *ptr)
91
{
92
  lws_alloc_site_hdr_t *h = (lws_alloc_site_hdr_t *)
93
      ((uint8_t *)ptr - LWS_ALLOC_SITE_HDR_LEN);
94
95
  assert(h->magic == LWS_ALLOC_SITE_MAGIC);
96
97
  sites_lock();
98
  lws_dll2_remove(&h->list);
99
  sites_unlock();
100
101
  return h;
102
}
103
104
/* list the block as the caller's, returning the pointer the caller gets */
105
static void *
106
lws_alloc_site_give(void *v, size_t size, const char *reason,
107
        const uintptr_t *pcs)
108
{
109
  lws_alloc_site_hdr_t *h = (lws_alloc_site_hdr_t *)v;
110
111
  /* realloc() moved our old list node along with the block */
112
  memset(&h->list, 0, sizeof(h->list));
113
  h->reason = reason ? reason : "-";
114
  h->size   = size;
115
  h->magic  = LWS_ALLOC_SITE_MAGIC;
116
#if defined(LWS_PLAT_CALLERS)
117
  memcpy(h->pcs, pcs, sizeof(h->pcs));
118
#else
119
  (void)pcs;
120
#endif
121
122
  sites_lock();
123
  lws_dll2_add_tail(&h->list, &sites_live);
124
  sites_unlock();
125
126
  return (uint8_t *)v + LWS_ALLOC_SITE_HDR_LEN;
127
}
128
129
#if defined(LWS_PLAT_ALLOC_SIZE)
130
size_t
131
lws_alloc_site_block_size(void *p)
132
{
133
  lws_alloc_site_hdr_t *h = (lws_alloc_site_hdr_t *)
134
      ((uint8_t *)p - LWS_ALLOC_SITE_HDR_LEN);
135
136
  /* the app's allocator made it, without our header */
137
  if (sites_foreign)
138
    return lws_plat_alloc_size(p);
139
140
  assert(h->magic == LWS_ALLOC_SITE_MAGIC);
141
142
  return lws_plat_alloc_size(h);
143
}
144
#endif
145
146
static int
147
lws_alloc_site_same(const lws_alloc_site_t *s, const lws_alloc_site_hdr_t *h)
148
{
149
#if defined(LWS_PLAT_CALLERS)
150
  if (memcmp(s->pcs, h->pcs, sizeof(h->pcs)))
151
    return 0;
152
#endif
153
  return s->reason == h->reason || !strcmp(s->reason, h->reason);
154
}
155
156
int
157
lws_alloc_sites(lws_alloc_site_t *sites, int max, lws_alloc_site_t *rest)
158
{
159
  int ns = 0, n, m;
160
161
  if (rest)
162
    memset(rest, 0, sizeof(*rest));
163
164
  sites_lock();
165
  lws_start_foreach_dll(struct lws_dll2 *, d,
166
            lws_dll2_get_head(&sites_live)) {
167
    lws_alloc_site_hdr_t *h = lws_container_of(d,
168
          lws_alloc_site_hdr_t, list);
169
170
    for (n = 0; n < ns; n++)
171
      if (lws_alloc_site_same(&sites[n], h))
172
        break;
173
174
    if (n == ns) {
175
      if (ns == max) {
176
        if (rest) {
177
          rest->bytes += h->size;
178
          rest->count++;
179
        }
180
        continue;
181
      }
182
      memset(&sites[ns], 0, sizeof(sites[ns]));
183
      sites[ns].reason = h->reason;
184
#if defined(LWS_PLAT_CALLERS)
185
      memcpy(sites[ns].pcs, h->pcs, sizeof(h->pcs));
186
#endif
187
      ns++;
188
    }
189
    sites[n].bytes += h->size;
190
    sites[n].count++;
191
  } lws_end_foreach_dll(d);
192
  sites_unlock();
193
194
  /* biggest first */
195
  for (n = 0; n + 1 < ns; n++)
196
    for (m = n + 1; m < ns; m++)
197
      if (sites[m].bytes > sites[n].bytes) {
198
        lws_alloc_site_t t = sites[n];
199
200
        sites[n] = sites[m];
201
        sites[m] = t;
202
      }
203
204
  return ns;
205
}
206
207
void
208
lws_alloc_sites_dump(struct lws_context *cx)
209
{
210
  /*
211
   * Too big for the stack of whatever ran out of heap, and only in
212
   * builds that are for looking at the heap
213
   */
214
  static lws_alloc_site_t s[LWS_ALLOC_SITE_RANKED];
215
  lws_alloc_site_t rest;
216
  unsigned int count;
217
  int ns, n;
218
219
  /*
220
   * Gathered under the lock first, so nothing is logged holding it.  The
221
   * sites must all be ranked before the biggest can be told, so the
222
   * ones past what we list are only added to the rest after
223
   */
224
  ns = lws_alloc_sites(s, (int)LWS_ARRAY_SIZE(s), &rest);
225
  for (n = LWS_ALLOC_SITE_LISTED; n < ns; n++) {
226
    rest.bytes += s[n].bytes;
227
    rest.count += s[n].count;
228
  }
229
  if (ns > LWS_ALLOC_SITE_LISTED)
230
    ns = LWS_ALLOC_SITE_LISTED;
231
232
  sites_lock();
233
  count = sites_live.count;
234
  sites_unlock();
235
236
  for (n = 0; n < ns; n++)
237
#if defined(LWS_PLAT_CALLERS)
238
    lwsl_cx_notice(cx, "fp:   %7llu B %4u x %-28.28s 0x%08llx "
239
           "0x%08llx 0x%08llx",
240
           (unsigned long long)s[n].bytes,
241
           s[n].count, s[n].reason,
242
           (unsigned long long)s[n].pcs[0],
243
           (unsigned long long)s[n].pcs[1],
244
           (unsigned long long)s[n].pcs[2]);
245
#else
246
    lwsl_cx_notice(cx, "fp:   %7llu B %4u x %s",
247
             (unsigned long long)s[n].bytes, s[n].count,
248
             s[n].reason);
249
#endif
250
  if (rest.count)
251
    lwsl_cx_notice(cx, "fp:   %7llu B %4u x from elsewhere",
252
             (unsigned long long)rest.bytes, rest.count);
253
254
  lwsl_cx_notice(cx, "fp:   %7llu B for the %u headers listing them",
255
           (unsigned long long)(count * LWS_ALLOC_SITE_HDR_LEN),
256
           count);
257
}
258
#endif
259
260
/*
261
 * Reclaimable heap occupants, processwide like the heap.  LRU order: the
262
 * head is the least recently used.
263
 */
264
265
static lws_dll2_owner_t reclaimables;
266
static char in_reclaim;
267
/*
268
 * lws_zalloc_try() is asking: an allocation with a fallback.  Processwide
269
 * like in_reclaim, since it only lasts for the one allocation.
270
 */
271
static char alloc_trying;
272
/* lws_alloc_arena_set(): processwide too, see there */
273
static lws_alloc_arena_t alloc_arena;
274
275
lws_alloc_arena_t
276
lws_alloc_arena_set(lws_alloc_arena_t arena)
277
0
{
278
0
  lws_alloc_arena_t was = alloc_arena;
279
280
0
  alloc_arena = arena;
281
282
0
  return was;
283
0
}
284
285
void
286
lws_reclaimable_add(lws_reclaimable_t *r)
287
0
{
288
0
  lws_dll2_add_tail(&r->list, &reclaimables);
289
0
}
290
291
void
292
lws_reclaimable_remove(lws_reclaimable_t *r)
293
0
{
294
0
  lws_dll2_remove(&r->list);
295
0
}
296
297
void
298
lws_reclaimable_touch(lws_reclaimable_t *r)
299
0
{
300
0
  if (!lws_dll2_is_detached(&r->list)) {
301
0
    lws_dll2_remove(&r->list);
302
0
    lws_dll2_add_tail(&r->list, &reclaimables);
303
0
  }
304
0
}
305
306
void
307
lws_reclaimable_pin(lws_reclaimable_t *r)
308
0
{
309
0
  r->pins++;
310
0
}
311
312
void
313
lws_reclaimable_unpin(lws_reclaimable_t *r)
314
0
{
315
0
  if (r->pins)
316
0
    r->pins--;
317
0
}
318
319
size_t
320
lws_reclaim(size_t want)
321
0
{
322
0
  size_t freed = 0;
323
324
  /*
325
   * A tenant's evict() must not allocate; if one does and that fails,
326
   * it gets a plain NULL rather than a nested reclaim
327
   */
328
0
  if (in_reclaim)
329
0
    return 0;
330
0
  in_reclaim = 1;
331
332
0
  lws_start_foreach_dll_safe(struct lws_dll2 *, d, d1,
333
0
           lws_dll2_get_head(&reclaimables)) {
334
0
    lws_reclaimable_t *r = lws_container_of(d, lws_reclaimable_t,
335
0
              list);
336
337
0
    if (freed >= want)
338
0
      break;
339
340
0
    if (r->pins || !r->resident || !r->evict)
341
0
      continue;
342
343
0
    freed += r->evict(r);
344
0
  } lws_end_foreach_dll_safe(d, d1);
345
346
0
  in_reclaim = 0;
347
348
0
  return freed;
349
0
}
350
351
void
352
lws_heap_limit_set(size_t bytes)
353
0
{
354
0
#if defined(LWS_PLAT_ALLOC_SIZE)
355
0
  heap_limit = bytes;
356
#else
357
  (void)bytes;
358
#endif
359
0
}
360
361
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
362
static lws_dll2_owner_t active;
363
#endif
364
365
#if defined(LWS_PLAT_OPTEE)
366
367
#define TEE_USER_MEM_HINT_NO_FILL_ZERO       0x80000000
368
#if defined (LWS_WITH_NETWORK)
369
370
/* normal TA apis */
371
372
void *__attribute__((weak))
373
  TEE_Malloc(uint32_t size, uint32_t hint)
374
{
375
  return NULL;
376
}
377
void *__attribute__((weak))
378
  TEE_Realloc(void *buffer, uint32_t newSize)
379
{
380
  return NULL;
381
}
382
void __attribute__((weak))
383
  TEE_Free(void *buffer)
384
{
385
}
386
#else
387
388
/* in-OP-TEE core apis */
389
390
void *
391
  TEE_Malloc(uint32_t size, uint32_t hint)
392
{
393
  return malloc(size);
394
}
395
void *
396
  TEE_Realloc(void *buffer, uint32_t newSize)
397
{
398
  return realloc(buffer, newSize);
399
}
400
void
401
  TEE_Free(void *buffer)
402
{
403
  free(buffer);
404
}
405
406
#endif
407
408
void *lws_realloc(void *ptr, size_t size, const char *reason)
409
{
410
  return TEE_Realloc(ptr, size);
411
}
412
413
void *lws_malloc(size_t size, const char *reason)
414
{
415
  return TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
416
}
417
418
void lws_free(void *p)
419
{
420
  TEE_Free(p);
421
}
422
423
void *lws_zalloc(size_t size, const char *reason)
424
{
425
  void *ptr = TEE_Malloc(size, TEE_USER_MEM_HINT_NO_FILL_ZERO);
426
  if (ptr)
427
    memset(ptr, 0, size);
428
  return ptr;
429
}
430
431
void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
432
{
433
  (void)cb;
434
}
435
#else
436
437
static void *
438
_realloc(void *ptr, size_t size, const char *reason)
439
5.34k
{
440
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
441
  uint8_t comp[16 * LWS_ARRAY_SIZE(((lws_backtrace_info_t *)NULL)->st)];
442
  size_t complen, user_size = size;
443
  size_t adj = 0, old_adj = 0;
444
#endif
445
#if defined(LWS_WITH_ALLOC_SITES)
446
  uintptr_t pcs[LWS_ALLOC_SITE_CALLERS];
447
  size_t site_size = size;
448
#endif
449
5.34k
  void *v;
450
451
5.34k
  if (size) {
452
#if defined(LWS_WITH_ALLOC_SITES)
453
    if (size > (size_t)-1 - LWS_ALLOC_SITE_HDR_LEN)
454
      return NULL;
455
456
    memset(pcs, 0, sizeof(pcs));
457
#if defined(LWS_PLAT_CALLERS)
458
    /*
459
     * How many frames stand between us and the code that wanted
460
     * the memory depends on the wrapper it called, the platform's
461
     * walk and what the compiler inlined or tail called, so keep
462
     * the nearest few: the first is usually lws_realloc() or
463
     * lws_zalloc(), and the reason says the rest of the time
464
     */
465
    lws_plat_callers(pcs, LWS_ALLOC_SITE_CALLERS, 0);
466
#endif
467
468
    /* realloc() must see the true base, and the room for our header */
469
    if (ptr)
470
      ptr = lws_alloc_site_take(ptr);
471
    size += LWS_ALLOC_SITE_HDR_LEN;
472
#endif
473
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
474
    /*
475
     * Any metadata, old or new, is at most a dll2 and a comp
476
     * buffer's worth: a size so close to the top of size_t that
477
     * adding that wraps must fail like the plain allocation would,
478
     * not wrap into a tiny block the caller then overruns
479
     */
480
    if (size > (size_t)-1 - sizeof(lws_dll2_t) - sizeof(comp))
481
      return NULL;
482
483
    lws_alloc_metadata_gen(size, comp, sizeof(comp), &adj, &complen);
484
485
    /*
486
     * The pointer we handed the caller last time points into the
487
     * middle of the real allocation, behind our metadata... we
488
     * have to recover the true base (which also unlinks the old
489
     * metadata node from "active") before realloc() may see it
490
     */
491
492
    if (ptr) {
493
      uint8_t *u = (uint8_t *)ptr;
494
495
      _lws_alloc_metadata_trim(&ptr, NULL, NULL);
496
      old_adj = lws_ptr_diff_size_t(u, ptr);
497
    }
498
499
    /*
500
     * The metadata size depends on this call's backtrace and size,
501
     * so it generally differs from the old one: realloc() keeps the
502
     * caller's data at old_adj, but it must end up at adj.  Get
503
     * room for it at whichever offset is further in, and move it
504
     * across once we have the new block.
505
     */
506
507
    size += adj > old_adj ? adj : old_adj;
508
#endif
509
510
#if defined(LWS_PLAT_FREERTOS)
511
    lwsl_debug("%s: size %lu: %s (free heap %ld)\n", __func__,
512
          (unsigned long)size, reason,
513
          (long)lws_plat_free_heap() - (long)size);
514
#else
515
2.67k
    lwsl_debug("%s: size %lu: %s\n", __func__,
516
2.67k
         (unsigned long)size, reason);
517
2.67k
#endif
518
519
2.67k
#if defined(LWS_PLAT_ALLOC_SIZE)
520
2.67k
    if (ptr)
521
0
      lws_allocated_sub(lws_plat_alloc_size(ptr));
522
2.67k
#endif
523
524
    /*
525
     * If the platform can't give us the memory, ask the
526
     * reclaimable heap occupants to give some back and try
527
     * again, until it works or nothing more can be reclaimed.
528
     * The caller then sees only a successful allocation.
529
     */
530
531
2.67k
    for (;;) {
532
2.67k
#if defined(LWS_PLAT_ALLOC_SIZE)
533
2.67k
      if (heap_limit && lws_allocated_get() + size > heap_limit)
534
0
        v = NULL;
535
2.67k
      else
536
2.67k
#endif
537
#if defined(LWS_PLAT_OPTEE)
538
        v = (void *)TEE_Realloc(ptr, size);
539
#elif defined(LWS_PLAT_REALLOC_ARENA)
540
        v = lws_plat_realloc_arena(ptr, size,
541
                 (int)alloc_arena);
542
#else
543
2.67k
        v = (void *)realloc(ptr, size);
544
2.67k
#endif
545
2.67k
      if (v)
546
2.67k
        break;
547
548
      /*
549
       * An allocation with a fallback just fails: nobody is
550
       * asked to give anything back for it, and the heap
551
       * didn't run out for anybody
552
       */
553
0
      if (alloc_trying || !lws_reclaim(size)) {
554
#if defined(LWS_WITH_FOOTPRINT)
555
        if (!alloc_trying)
556
          lws_footprint_alloc_failed(size, reason);
557
#endif
558
0
#if defined(LWS_PLAT_ALLOC_SIZE)
559
        /* the old block is still ours */
560
0
        if (ptr)
561
0
          lws_allocated_add(lws_plat_alloc_size(ptr));
562
0
#endif
563
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
564
        /* ... and still live: list it again */
565
        if (ptr)
566
          lws_dll2_add_tail((lws_dll2_t *)
567
            ((uint8_t *)ptr + old_adj -
568
             sizeof(lws_dll2_t)), &active);
569
#endif
570
#if defined(LWS_WITH_ALLOC_SITES)
571
        /* ... and still live: list it again */
572
        if (ptr) {
573
          sites_lock();
574
          lws_dll2_add_tail(&((lws_alloc_site_hdr_t *)
575
            ptr)->list, &sites_live);
576
          sites_unlock();
577
        }
578
#endif
579
0
        return NULL;
580
0
      }
581
0
    }
582
583
2.67k
#if defined(LWS_PLAT_ALLOC_SIZE)
584
2.67k
    lws_allocated_add(lws_plat_alloc_size(v));
585
2.67k
#endif
586
587
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
588
    /*
589
     * Before the new metadata goes in, since with adj > old_adj it
590
     * overlaps where the data was.  user_size is in bounds at both
591
     * offsets; on a grow the part past the old data is garbage
592
     * either way.
593
     */
594
    if (old_adj && old_adj != adj)
595
      memmove((uint8_t *)v + adj, (uint8_t *)v + old_adj,
596
        user_size);
597
598
    _lws_alloc_metadata_adjust(&active, &v, adj, comp, (unsigned int)complen);
599
#endif
600
#if defined(LWS_WITH_ALLOC_SITES)
601
    v = lws_alloc_site_give(v, site_size, reason, pcs);
602
#endif
603
604
2.67k
    return v;
605
2.67k
  }
606
607
  /*
608
   * We are freeing it then...
609
   */
610
611
2.67k
  if (ptr) {
612
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
613
    _lws_alloc_metadata_trim(&ptr, NULL, NULL);
614
#endif
615
#if defined(LWS_WITH_ALLOC_SITES)
616
    ptr = lws_alloc_site_take(ptr);
617
    ((lws_alloc_site_hdr_t *)ptr)->magic = 0;
618
#endif
619
620
2.67k
#if defined(LWS_PLAT_ALLOC_SIZE)
621
2.67k
    lws_allocated_sub(lws_plat_alloc_size(ptr));
622
2.67k
#endif
623
2.67k
    free(ptr);
624
#if defined(LWS_PLAT_FREERTOS)
625
    lwsl_debug("%s: free heap %ld\n", __func__,
626
          (long)lws_plat_free_heap() - (long)size);
627
#endif
628
2.67k
  }
629
630
2.67k
  return NULL;
631
5.34k
}
632
633
#if defined(LWS_WITH_ALLOC_METADATA_LWS)
634
void
635
_lws_alloc_metadata_dump_lws(lws_dll2_foreach_cb_t cb, void *arg)
636
{
637
  lwsl_err("%s\n", __func__);
638
  _lws_alloc_metadata_dump(&active, cb, arg);
639
}
640
#endif
641
642
void *(*_lws_realloc)(void *ptr, size_t size, const char *reason) = _realloc;
643
644
void *lws_realloc(void *ptr, size_t size, const char *reason)
645
4.00k
{
646
4.00k
  return _lws_realloc(ptr, size, reason);
647
4.00k
}
648
649
void *lws_zalloc(size_t size, const char *reason)
650
1.33k
{
651
1.33k
  void *ptr = _lws_realloc(NULL, size, reason);
652
653
1.33k
  if (ptr)
654
1.33k
    memset(ptr, 0, size);
655
656
1.33k
  return ptr;
657
1.33k
}
658
659
void lws_set_allocator(void *(*cb)(void *ptr, size_t size, const char *reason))
660
0
{
661
#if defined(LWS_WITH_ALLOC_SITES)
662
  sites_foreign = cb != _realloc;
663
#endif
664
0
  _lws_realloc = cb;
665
0
}
666
667
size_t lws_get_allocated_heap(void)
668
0
{
669
0
#if defined(LWS_PLAT_ALLOC_SIZE)
670
0
  return lws_allocated_get();
671
#else
672
  return 0;
673
#endif
674
0
}
675
#endif
676
677
void *
678
lws_zalloc_try(size_t size, const char *reason)
679
0
{
680
0
  void *v;
681
682
0
  alloc_trying = 1;
683
0
  v = lws_zalloc(size, reason);
684
0
  alloc_trying = 0;
685
686
0
  return v;
687
0
}