Coverage Report

Created: 2023-11-22 10:24

/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-2018 The TokTok team.
3
 * Copyright © 2013 Tox project.
4
 */
5
6
#include "mem.h"
7
8
#include <stdlib.h>
9
10
#include "ccompat.h"
11
12
nullable(1)
13
static void *sys_malloc(void *obj, uint32_t size)
14
0
{
15
0
    return malloc(size);
16
0
}
17
18
nullable(1)
19
static void *sys_calloc(void *obj, uint32_t nmemb, uint32_t size)
20
0
{
21
0
    return calloc(nmemb, size);
22
0
}
23
24
nullable(1, 2)
25
static void *sys_realloc(void *obj, void *ptr, uint32_t size)
26
0
{
27
0
    return realloc(ptr, size);
28
0
}
29
30
nullable(1, 2)
31
static void sys_free(void *obj, void *ptr)
32
0
{
33
0
    free(ptr);
34
0
}
35
36
static const Memory_Funcs system_memory_funcs = {
37
    sys_malloc,
38
    sys_calloc,
39
    sys_realloc,
40
    sys_free,
41
};
42
static const Memory system_memory_obj = {&system_memory_funcs};
43
44
const Memory *system_memory(void)
45
3.11k
{
46
3.11k
    return &system_memory_obj;
47
3.11k
}
48
49
void *mem_balloc(const Memory *mem, uint32_t size)
50
147k
{
51
147k
    void *const ptr = mem->funcs->malloc(mem->obj, size);
52
147k
    return ptr;
53
147k
}
54
55
void *mem_alloc(const Memory *mem, uint32_t size)
56
70.8k
{
57
70.8k
    void *const ptr = mem->funcs->calloc(mem->obj, 1, size);
58
70.8k
    return ptr;
59
70.8k
}
60
61
void *mem_valloc(const Memory *mem, uint32_t nmemb, uint32_t size)
62
51.0k
{
63
51.0k
    const uint32_t bytes = nmemb * size;
64
65
51.0k
    if (size != 0 && bytes / size != nmemb) {
66
0
        return nullptr;
67
0
    }
68
69
51.0k
    void *const ptr = mem->funcs->calloc(mem->obj, nmemb, size);
70
51.0k
    return ptr;
71
51.0k
}
72
73
void *mem_vrealloc(const Memory *mem, void *ptr, uint32_t nmemb, uint32_t size)
74
35.3k
{
75
35.3k
    const uint32_t bytes = nmemb * size;
76
77
35.3k
    if (size != 0 && bytes / size != nmemb) {
78
0
        return nullptr;
79
0
    }
80
81
35.3k
    void *const new_ptr = mem->funcs->realloc(mem->obj, ptr, bytes);
82
35.3k
    return new_ptr;
83
35.3k
}
84
85
void mem_delete(const Memory *mem, void *ptr)
86
292k
{
87
292k
    mem->funcs->free(mem->obj, ptr);
88
292k
}