13d36ee2eSJeremy Kerr /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
24cdc200fSJeremy Kerr
34cdc200fSJeremy Kerr #include <assert.h>
44cdc200fSJeremy Kerr
53a666ce0SJeremy Kerr #include "libmctp.h"
64cdc200fSJeremy Kerr #include "libmctp-alloc.h"
74cdc200fSJeremy Kerr
8c7e764a2SJeremy Kerr #ifdef HAVE_CONFIG_H
9c7e764a2SJeremy Kerr #include "config.h"
10c7e764a2SJeremy Kerr #endif
11c7e764a2SJeremy Kerr
124a09e1dcSMatt Johnston #include "compiler.h"
134a09e1dcSMatt Johnston
14*bbfcc6e1SMatt Johnston #if defined(MCTP_DEFAULT_ALLOC) && defined(MCTP_CUSTOM_ALLOC)
15*bbfcc6e1SMatt Johnston #error Default and Custom alloc are incompatible
16*bbfcc6e1SMatt Johnston #endif
17*bbfcc6e1SMatt Johnston
184a09e1dcSMatt Johnston #ifdef MCTP_DEFAULT_ALLOC
default_msg_malloc(size_t size,void * ctx __unused)194a09e1dcSMatt Johnston static void *default_msg_malloc(size_t size, void *ctx __unused)
204a09e1dcSMatt Johnston {
214a09e1dcSMatt Johnston void *ptr = __mctp_alloc(size);
224a09e1dcSMatt Johnston return ptr;
234a09e1dcSMatt Johnston }
244a09e1dcSMatt Johnston
default_msg_free(void * msg,void * ctx __unused)254a09e1dcSMatt Johnston static void default_msg_free(void *msg, void *ctx __unused)
264a09e1dcSMatt Johnston {
274a09e1dcSMatt Johnston __mctp_free(msg);
284a09e1dcSMatt Johnston }
294a09e1dcSMatt Johnston #endif
304a09e1dcSMatt Johnston
31*bbfcc6e1SMatt Johnston /* Allocators provided as functions to call */
32*bbfcc6e1SMatt Johnston #ifdef MCTP_CUSTOM_ALLOC
33*bbfcc6e1SMatt Johnston extern void *mctp_custom_malloc(size_t size);
34*bbfcc6e1SMatt Johnston extern void mctp_custom_free(void *ptr);
35*bbfcc6e1SMatt Johnston extern void *mctp_custom_msg_alloc(size_t size, void *ctx);
36*bbfcc6e1SMatt Johnston extern void mctp_custom_msg_free(void *msg, void *ctx);
37*bbfcc6e1SMatt Johnston #endif
38*bbfcc6e1SMatt Johnston
39*bbfcc6e1SMatt Johnston #ifdef MCTP_CUSTOM_ALLOC
40*bbfcc6e1SMatt Johnston const
41*bbfcc6e1SMatt Johnston #endif
424cdc200fSJeremy Kerr struct {
433a666ce0SJeremy Kerr void *(*m_alloc)(size_t);
443a666ce0SJeremy Kerr void (*m_free)(void *);
454a09e1dcSMatt Johnston /* Final argument is ctx */
464a09e1dcSMatt Johnston void *(*m_msg_alloc)(size_t, void *);
474a09e1dcSMatt Johnston void (*m_msg_free)(void *, void *);
484cdc200fSJeremy Kerr } alloc_ops = {
49c7e764a2SJeremy Kerr #ifdef MCTP_DEFAULT_ALLOC
504cdc200fSJeremy Kerr malloc,
514cdc200fSJeremy Kerr free,
524a09e1dcSMatt Johnston default_msg_malloc,
534a09e1dcSMatt Johnston default_msg_free,
543cb4eee4SJeremy Kerr #endif
55*bbfcc6e1SMatt Johnston #ifdef MCTP_CUSTOM_ALLOC
56*bbfcc6e1SMatt Johnston mctp_custom_malloc,
57*bbfcc6e1SMatt Johnston mctp_custom_free,
58*bbfcc6e1SMatt Johnston mctp_custom_msg_alloc,
59*bbfcc6e1SMatt Johnston mctp_custom_msg_free,
60*bbfcc6e1SMatt Johnston #endif
614cdc200fSJeremy Kerr };
624cdc200fSJeremy Kerr
634cdc200fSJeremy Kerr /* internal-only allocation functions */
__mctp_alloc(size_t size)644cdc200fSJeremy Kerr void *__mctp_alloc(size_t size)
654cdc200fSJeremy Kerr {
663a666ce0SJeremy Kerr if (alloc_ops.m_alloc)
673a666ce0SJeremy Kerr return alloc_ops.m_alloc(size);
684cdc200fSJeremy Kerr assert(0);
695e56cd8aSAndrew Jeffery return NULL;
704cdc200fSJeremy Kerr }
714cdc200fSJeremy Kerr
__mctp_free(void * ptr)724cdc200fSJeremy Kerr void __mctp_free(void *ptr)
734cdc200fSJeremy Kerr {
743a666ce0SJeremy Kerr if (alloc_ops.m_free)
753a666ce0SJeremy Kerr alloc_ops.m_free(ptr);
764cdc200fSJeremy Kerr else
774cdc200fSJeremy Kerr assert(0);
784cdc200fSJeremy Kerr }
794cdc200fSJeremy Kerr
__mctp_msg_alloc(size_t size,struct mctp * mctp)804a09e1dcSMatt Johnston void *__mctp_msg_alloc(size_t size, struct mctp *mctp)
814cdc200fSJeremy Kerr {
824a09e1dcSMatt Johnston void *ctx = mctp_get_alloc_ctx(mctp);
834a09e1dcSMatt Johnston if (alloc_ops.m_msg_alloc)
844a09e1dcSMatt Johnston return alloc_ops.m_msg_alloc(size, ctx);
854cdc200fSJeremy Kerr assert(0);
865e56cd8aSAndrew Jeffery return NULL;
874cdc200fSJeremy Kerr }
884cdc200fSJeremy Kerr
__mctp_msg_free(void * ptr,struct mctp * mctp)894a09e1dcSMatt Johnston void __mctp_msg_free(void *ptr, struct mctp *mctp)
904a09e1dcSMatt Johnston {
914a09e1dcSMatt Johnston void *ctx = mctp_get_alloc_ctx(mctp);
924a09e1dcSMatt Johnston if (alloc_ops.m_msg_free)
934a09e1dcSMatt Johnston alloc_ops.m_msg_free(ptr, ctx);
944a09e1dcSMatt Johnston }
954a09e1dcSMatt Johnston
96*bbfcc6e1SMatt Johnston #ifndef MCTP_CUSTOM_ALLOC
mctp_set_alloc_ops(void * (* m_alloc)(size_t),void (* m_free)(void *),void * (* m_msg_alloc)(size_t,void *),void (* m_msg_free)(void *,void *))9723496bf2SAndrew Jeffery void mctp_set_alloc_ops(void *(*m_alloc)(size_t), void (*m_free)(void *),
984a09e1dcSMatt Johnston void *(*m_msg_alloc)(size_t, void *),
994a09e1dcSMatt Johnston void (*m_msg_free)(void *, void *))
1004cdc200fSJeremy Kerr {
1013a666ce0SJeremy Kerr alloc_ops.m_alloc = m_alloc;
1023a666ce0SJeremy Kerr alloc_ops.m_free = m_free;
1034a09e1dcSMatt Johnston alloc_ops.m_msg_alloc = m_msg_alloc;
1044a09e1dcSMatt Johnston alloc_ops.m_msg_free = m_msg_free;
1054cdc200fSJeremy Kerr }
106*bbfcc6e1SMatt Johnston #endif // MCTP_CUSTOM_ALLOC
107