blob: 7e1157c9e486ba747b6ac8d79df198aad42c63b3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
/* libmisc/heap.c - Heap memory allocator
*
* Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <stdint.h> /* for uintptr_t */
#include <string.h> /* for memset(), memcpy(), strlen() */
#include <libmisc/_intercept.h>
#include <libmisc/assert.h>
#include <libmisc/heap.h>
void *_heap_aligned_realloc(void *ptr, size_t align, size_t objcnt, size_t objsize) {
if (align < sizeof(void*))
align = sizeof(void*);
size_t size;
if (__builtin_mul_overflow(objcnt, objsize, &size))
assert_notreached("heap multiplication overflow");
void *base = __lm_heap_aligned_realloc(ptr, align, size);
assert(base);
assert((uintptr_t)base % align == 0);
return base;
}
void heap_free(void *ptr) {
if (ptr)
__lm_heap_free(ptr);
}
void heap_take(void *ptr) {
if (ptr)
__lm_heap_take(ptr);
}
char *heap_strdup(const char *str) {
assert(str);
size_t len = strlen(str)+1;
char *ret = heap_alloc(len, char);
memcpy(ret, str, len);
return ret;
}
|