blob: 2389dd35813cb83b0441590bb39d979a7d2dc529 [file] [log] [blame]
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08001/* SPDX-License-Identifier: Apache-2.0 */
2
3#include <assert.h>
4
5#include "libmctp-alloc.h"
6
7struct {
8 void *(*alloc)(size_t);
9 void (*free)(void *);
10 void *(*realloc)(void *, size_t);
11} alloc_ops = {
12 malloc,
13 free,
14 realloc,
15};
16
17/* internal-only allocation functions */
18void *__mctp_alloc(size_t size)
19{
20 if (alloc_ops.alloc)
21 return alloc_ops.alloc(size);
22 if (alloc_ops.realloc)
23 return alloc_ops.realloc(NULL, size);
24 assert(0);
25}
26
27void __mctp_free(void *ptr)
28{
29 if (alloc_ops.free)
30 alloc_ops.free(ptr);
31 else if (alloc_ops.realloc)
32 alloc_ops.realloc(ptr, 0);
33 else
34 assert(0);
35}
36
37void *__mctp_realloc(void *ptr, size_t size)
38{
39 if (alloc_ops.realloc)
40 return alloc_ops.realloc(ptr, size);
41 assert(0);
42}
43
44void mctp_set_alloc_ops(void *(*alloc)(size_t),
45 void (*free)(void *),
46 void *(realloc)(void *, size_t))
47{
48 alloc_ops.alloc = alloc;
49 alloc_ops.free = free;
50 alloc_ops.realloc = realloc;
51}