blob: 650ea7ec1b868d02550817183301a8664ab27ce6 [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08002
3#include <assert.h>
4
Jeremy Kerr3a666ce2019-03-06 09:18:50 +08005#include "libmctp.h"
Jeremy Kerr4cdc2002019-02-07 16:49:12 +08006#include "libmctp-alloc.h"
7
Jeremy Kerrc7e764a2019-05-28 16:49:03 +08008#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
Matt Johnston4a09e1d2024-09-13 14:55:58 +080012#include "compiler.h"
13
Matt Johnstonbbfcc6e2024-09-17 16:48:15 +080014#if defined(MCTP_DEFAULT_ALLOC) && defined(MCTP_CUSTOM_ALLOC)
15#error Default and Custom alloc are incompatible
16#endif
17
Matt Johnston4a09e1d2024-09-13 14:55:58 +080018#ifdef MCTP_DEFAULT_ALLOC
19static void *default_msg_malloc(size_t size, void *ctx __unused)
20{
21 void *ptr = __mctp_alloc(size);
22 return ptr;
23}
24
25static void default_msg_free(void *msg, void *ctx __unused)
26{
27 __mctp_free(msg);
28}
29#endif
30
Matt Johnstonbbfcc6e2024-09-17 16:48:15 +080031/* Allocators provided as functions to call */
32#ifdef MCTP_CUSTOM_ALLOC
33extern void *mctp_custom_malloc(size_t size);
34extern void mctp_custom_free(void *ptr);
35extern void *mctp_custom_msg_alloc(size_t size, void *ctx);
36extern void mctp_custom_msg_free(void *msg, void *ctx);
37#endif
38
39#ifdef MCTP_CUSTOM_ALLOC
40const
41#endif
42 struct {
Andrew Jeffery23496bf2022-09-30 14:17:09 +093043 void *(*m_alloc)(size_t);
44 void (*m_free)(void *);
Matt Johnston4a09e1d2024-09-13 14:55:58 +080045 /* Final argument is ctx */
46 void *(*m_msg_alloc)(size_t, void *);
47 void (*m_msg_free)(void *, void *);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080048} alloc_ops = {
Jeremy Kerrc7e764a2019-05-28 16:49:03 +080049#ifdef MCTP_DEFAULT_ALLOC
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080050 malloc,
51 free,
Matt Johnston4a09e1d2024-09-13 14:55:58 +080052 default_msg_malloc,
53 default_msg_free,
Jeremy Kerr3cb4eee2019-03-06 09:15:47 +080054#endif
Matt Johnstonbbfcc6e2024-09-17 16:48:15 +080055#ifdef MCTP_CUSTOM_ALLOC
56 mctp_custom_malloc,
57 mctp_custom_free,
58 mctp_custom_msg_alloc,
59 mctp_custom_msg_free,
60#endif
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080061};
62
63/* internal-only allocation functions */
64void *__mctp_alloc(size_t size)
65{
Jeremy Kerr3a666ce2019-03-06 09:18:50 +080066 if (alloc_ops.m_alloc)
67 return alloc_ops.m_alloc(size);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080068 assert(0);
Andrew Jeffery5e56cd82020-03-10 22:23:05 +103069 return NULL;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080070}
71
72void __mctp_free(void *ptr)
73{
Jeremy Kerr3a666ce2019-03-06 09:18:50 +080074 if (alloc_ops.m_free)
75 alloc_ops.m_free(ptr);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080076 else
77 assert(0);
78}
79
Matt Johnston4a09e1d2024-09-13 14:55:58 +080080void *__mctp_msg_alloc(size_t size, struct mctp *mctp)
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080081{
Matt Johnston4a09e1d2024-09-13 14:55:58 +080082 void *ctx = mctp_get_alloc_ctx(mctp);
83 if (alloc_ops.m_msg_alloc)
84 return alloc_ops.m_msg_alloc(size, ctx);
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080085 assert(0);
Andrew Jeffery5e56cd82020-03-10 22:23:05 +103086 return NULL;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +080087}
88
Matt Johnston4a09e1d2024-09-13 14:55:58 +080089void __mctp_msg_free(void *ptr, struct mctp *mctp)
90{
91 void *ctx = mctp_get_alloc_ctx(mctp);
92 if (alloc_ops.m_msg_free)
93 alloc_ops.m_msg_free(ptr, ctx);
94}
95
Matt Johnstonbbfcc6e2024-09-17 16:48:15 +080096#ifndef MCTP_CUSTOM_ALLOC
Andrew Jeffery23496bf2022-09-30 14:17:09 +093097void mctp_set_alloc_ops(void *(*m_alloc)(size_t), void (*m_free)(void *),
Matt Johnston4a09e1d2024-09-13 14:55:58 +080098 void *(*m_msg_alloc)(size_t, void *),
99 void (*m_msg_free)(void *, void *))
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800100{
Jeremy Kerr3a666ce2019-03-06 09:18:50 +0800101 alloc_ops.m_alloc = m_alloc;
102 alloc_ops.m_free = m_free;
Matt Johnston4a09e1d2024-09-13 14:55:58 +0800103 alloc_ops.m_msg_alloc = m_msg_alloc;
104 alloc_ops.m_msg_free = m_msg_free;
Jeremy Kerr4cdc2002019-02-07 16:49:12 +0800105}
Matt Johnstonbbfcc6e2024-09-17 16:48:15 +0800106#endif // MCTP_CUSTOM_ALLOC