1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2
3 #include <assert.h>
4
5 #include "libmctp.h"
6 #include "libmctp-alloc.h"
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 struct {
13 void *(*m_alloc)(size_t);
14 void (*m_free)(void *);
15 void *(*m_realloc)(void *, size_t);
16 } alloc_ops = {
17 #ifdef MCTP_DEFAULT_ALLOC
18 malloc,
19 free,
20 realloc,
21 #endif
22 };
23
24 /* internal-only allocation functions */
__mctp_alloc(size_t size)25 void *__mctp_alloc(size_t size)
26 {
27 if (alloc_ops.m_alloc)
28 return alloc_ops.m_alloc(size);
29 if (alloc_ops.m_realloc)
30 return alloc_ops.m_realloc(NULL, size);
31 assert(0);
32 return NULL;
33 }
34
__mctp_free(void * ptr)35 void __mctp_free(void *ptr)
36 {
37 if (alloc_ops.m_free)
38 alloc_ops.m_free(ptr);
39 else if (alloc_ops.m_realloc)
40 alloc_ops.m_realloc(ptr, 0);
41 else
42 assert(0);
43 }
44
__mctp_realloc(void * ptr,size_t size)45 void *__mctp_realloc(void *ptr, size_t size)
46 {
47 if (alloc_ops.m_realloc)
48 return alloc_ops.m_realloc(ptr, size);
49 assert(0);
50 return NULL;
51 }
52
mctp_set_alloc_ops(void * (* m_alloc)(size_t),void (* m_free)(void *),void * (m_realloc)(void *,size_t))53 void mctp_set_alloc_ops(void *(*m_alloc)(size_t), void (*m_free)(void *),
54 void *(m_realloc)(void *, size_t))
55 {
56 alloc_ops.m_alloc = m_alloc;
57 alloc_ops.m_free = m_free;
58 alloc_ops.m_realloc = m_realloc;
59 }
60