Coverage Report

Created: 2025-04-02 10:29

/src/c-toxcore/toxcore/mem.c
Line
Count
Source (jump to first uncovered line)
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 <stdlib.h>
9
10
#include "attributes.h"
11
#include "ccompat.h"
12
13
nullable(1)
14
static void *sys_malloc(void *obj, uint32_t size)
15
106
{
16
106
    return malloc(size);
17
106
}
18
19
nullable(1)
20
static void *sys_calloc(void *obj, uint32_t nmemb, uint32_t size)
21
1.83k
{
22
1.83k
    return calloc(nmemb, size);
23
1.83k
}
24
25
nullable(1, 2)
26
static void *sys_realloc(void *obj, void *ptr, uint32_t size)
27
0
{
28
0
    return realloc(ptr, size);
29
0
}
30
31
nullable(1, 2)
32
static void sys_free(void *obj, void *ptr)
33
1.94k
{
34
1.94k
    free(ptr);
35
1.94k
}
36
37
static const Memory_Funcs os_memory_funcs = {
38
    sys_malloc,
39
    sys_calloc,
40
    sys_realloc,
41
    sys_free,
42
};
43
static const Memory os_memory_obj = {&os_memory_funcs};
44
45
const Memory *os_memory(void)
46
2.24k
{
47
2.24k
    return &os_memory_obj;
48
2.24k
}
49
50
void *mem_balloc(const Memory *mem, uint32_t size)
51
8.70k
{
52
8.70k
    void *const ptr = mem->funcs->malloc(mem->obj, size);
53
8.70k
    return ptr;
54
8.70k
}
55
56
void *mem_brealloc(const Memory *mem, void *ptr, uint32_t size)
57
1.76k
{
58
1.76k
    void *const new_ptr = mem->funcs->realloc(mem->obj, ptr, size);
59
1.76k
    return new_ptr;
60
1.76k
}
61
62
void *mem_alloc(const Memory *mem, uint32_t size)
63
63.3k
{
64
63.3k
    void *const ptr = mem->funcs->calloc(mem->obj, 1, size);
65
63.3k
    return ptr;
66
63.3k
}
67
68
void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size)
69
19.9k
{
70
19.9k
    const uint32_t bytes = nmemb * size;
71
72
19.9k
    if (size != 0 && bytes / size != nmemb) {
73
0
        return nullptr;
74
0
    }
75
76
19.9k
    void *const ptr = mem->funcs->calloc(mem->obj, nmemb, size);
77
19.9k
    return ptr;
78
19.9k
}
79
80
void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size)
81
35.5k
{
82
35.5k
    const uint32_t bytes = nmemb * size;
83
84
35.5k
    if (size != 0 && bytes / size != nmemb) {
85
0
        return nullptr;
86
0
    }
87
88
35.5k
    void *const new_ptr = mem->funcs->realloc(mem->obj, ptr, bytes);
89
35.5k
    return new_ptr;
90
35.5k
}
91
92
void mem_delete(const Memory *mem, void *ptr)
93
1.03M
{
94
1.03M
    mem->funcs->free(mem->obj, ptr);
95
1.03M
}