forked from openbmc/libmctp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.c
59 lines (51 loc) · 1.08 KB
/
alloc.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
/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
#include <assert.h>
#include "libmctp.h"
#include "libmctp-alloc.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
struct {
void *(*m_alloc)(size_t);
void (*m_free)(void *);
void *(*m_realloc)(void *, size_t);
} alloc_ops = {
#ifdef MCTP_DEFAULT_ALLOC
malloc,
free,
realloc,
#endif
};
/* internal-only allocation functions */
void *__mctp_alloc(size_t size)
{
if (alloc_ops.m_alloc)
return alloc_ops.m_alloc(size);
if (alloc_ops.m_realloc)
return alloc_ops.m_realloc(NULL, size);
assert(0);
return NULL;
}
void __mctp_free(void *ptr)
{
if (alloc_ops.m_free)
alloc_ops.m_free(ptr);
else if (alloc_ops.m_realloc)
alloc_ops.m_realloc(ptr, 0);
else
assert(0);
}
void *__mctp_realloc(void *ptr, size_t size)
{
if (alloc_ops.m_realloc)
return alloc_ops.m_realloc(ptr, size);
assert(0);
return NULL;
}
void mctp_set_alloc_ops(void *(*m_alloc)(size_t), void (*m_free)(void *),
void *(m_realloc)(void *, size_t))
{
alloc_ops.m_alloc = m_alloc;
alloc_ops.m_free = m_free;
alloc_ops.m_realloc = m_realloc;
}