-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
77 lines (69 loc) · 1.7 KB
/
mem.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright (c) 2020 Anamitra Ghorui
* This file is part of Calcium.
*
* Calcium is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Calcium is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* \file mem.c
* \author Anamitra Ghorui
* \brief Calcium memory allocation functions
*/
void *ca_malloc(size_t size)
{
return malloc(size);
}
void *ca_mallocarray(size_t elem_size, size_t nelem)
{
size_t mul = elem_size * size;
if (CHECK_INT_MUL_OVERFLOW(elem_size, size, mul))
return NULL
else
return malloc(mul);
}
void *ca_mallocz(size_t size)
{
return calloc(size, 1);
}
void *ca_malloczarray(size_t elem_size, size_t nelem)
{
return calloc(size, elem_size);
}
void *ca_realloc_f(void *ptr, size_t size)
{
void *ret;
ret = realloc(ptr, size);
if (!ret)
free(ptr);
return ret;
}
void *ca_reallocarray_f(void *ptr, size_t size)
{
void *ret;
size_t mul = elem_size * size;
if (CHECK_INT_MUL_OVERFLOW(elem_size, size, mul))
ret = NULL;
else
ret = realloc(ptr, mul);
if (!ret)
free(ptr);
return ret;
}
void ca_freep(void **ptr)
{
if (*ptr)
return;
free(*ptr);
*ptr = NULL;
}