/src/c-toxcore/toxcore/mem.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2025 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | #include "mem.h" |
7 | | |
8 | | #include <string.h> |
9 | | |
10 | | #include "ccompat.h" |
11 | | #include "tox_memory.h" |
12 | | |
13 | | void *mem_balloc(const Memory *mem, uint32_t size) |
14 | 11.1k | { |
15 | 11.1k | void *const ptr = tox_memory_malloc(mem, size); |
16 | 11.1k | return ptr; |
17 | 11.1k | } |
18 | | |
19 | | void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size) |
20 | 1.36k | { |
21 | 1.36k | void *const new_ptr = tox_memory_realloc(mem, ptr, size); |
22 | 1.36k | return new_ptr; |
23 | 1.36k | } |
24 | | |
25 | | void *mem_alloc(const Memory *mem, uint32_t size) |
26 | 49.9k | { |
27 | 49.9k | void *const ptr = tox_memory_malloc(mem, size); |
28 | 49.9k | if (ptr != nullptr) { |
29 | 49.8k | memset(ptr, 0, size); |
30 | 49.8k | } |
31 | 49.9k | return ptr; |
32 | 49.9k | } |
33 | | |
34 | | void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size) |
35 | 15.6k | { |
36 | 15.6k | const uint32_t bytes = nmemb * size; |
37 | | |
38 | 15.6k | if (size != 0 && bytes / size != nmemb) { |
39 | 0 | return nullptr; |
40 | 0 | } |
41 | | |
42 | 15.6k | void *const ptr = tox_memory_malloc(mem, bytes); |
43 | 15.6k | if (ptr != nullptr) { |
44 | 15.6k | memset(ptr, 0, bytes); |
45 | 15.6k | } |
46 | 15.6k | return ptr; |
47 | 15.6k | } |
48 | | |
49 | | void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size) |
50 | 35.5k | { |
51 | 35.5k | const uint32_t bytes = nmemb * size; |
52 | | |
53 | 35.5k | if (size != 0 && bytes / size != nmemb) { |
54 | 0 | return nullptr; |
55 | 0 | } |
56 | | |
57 | 35.5k | void *const new_ptr = tox_memory_realloc(mem, ptr, bytes); |
58 | 35.5k | return new_ptr; |
59 | 35.5k | } |
60 | | |
61 | | void mem_delete(const Memory *mem, void *ptr) |
62 | 981k | { |
63 | 981k | tox_memory_dealloc(mem, ptr); |
64 | 981k | } |