Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 2 | |
| 3 | #include <assert.h> |
| 4 | |
Jeremy Kerr | 3a666ce | 2019-03-06 09:18:50 +0800 | [diff] [blame] | 5 | #include "libmctp.h" |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 6 | #include "libmctp-alloc.h" |
| 7 | |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include "config.h" |
| 10 | #endif |
| 11 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 12 | #include "compiler.h" |
| 13 | |
Matt Johnston | bbfcc6e | 2024-09-17 16:48:15 +0800 | [diff] [blame] | 14 | #if defined(MCTP_DEFAULT_ALLOC) && defined(MCTP_CUSTOM_ALLOC) |
| 15 | #error Default and Custom alloc are incompatible |
| 16 | #endif |
| 17 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 18 | #ifdef MCTP_DEFAULT_ALLOC |
| 19 | static void *default_msg_malloc(size_t size, void *ctx __unused) |
| 20 | { |
| 21 | void *ptr = __mctp_alloc(size); |
| 22 | return ptr; |
| 23 | } |
| 24 | |
| 25 | static void default_msg_free(void *msg, void *ctx __unused) |
| 26 | { |
| 27 | __mctp_free(msg); |
| 28 | } |
| 29 | #endif |
| 30 | |
Matt Johnston | bbfcc6e | 2024-09-17 16:48:15 +0800 | [diff] [blame] | 31 | /* Allocators provided as functions to call */ |
| 32 | #ifdef MCTP_CUSTOM_ALLOC |
| 33 | extern void *mctp_custom_malloc(size_t size); |
| 34 | extern void mctp_custom_free(void *ptr); |
| 35 | extern void *mctp_custom_msg_alloc(size_t size, void *ctx); |
| 36 | extern void mctp_custom_msg_free(void *msg, void *ctx); |
| 37 | #endif |
| 38 | |
| 39 | #ifdef MCTP_CUSTOM_ALLOC |
| 40 | const |
| 41 | #endif |
| 42 | struct { |
Andrew Jeffery | 23496bf | 2022-09-30 14:17:09 +0930 | [diff] [blame] | 43 | void *(*m_alloc)(size_t); |
| 44 | void (*m_free)(void *); |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 45 | /* Final argument is ctx */ |
| 46 | void *(*m_msg_alloc)(size_t, void *); |
| 47 | void (*m_msg_free)(void *, void *); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 48 | } alloc_ops = { |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 49 | #ifdef MCTP_DEFAULT_ALLOC |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 50 | malloc, |
| 51 | free, |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 52 | default_msg_malloc, |
| 53 | default_msg_free, |
Jeremy Kerr | 3cb4eee | 2019-03-06 09:15:47 +0800 | [diff] [blame] | 54 | #endif |
Matt Johnston | bbfcc6e | 2024-09-17 16:48:15 +0800 | [diff] [blame] | 55 | #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 Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | /* internal-only allocation functions */ |
| 64 | void *__mctp_alloc(size_t size) |
| 65 | { |
Jeremy Kerr | 3a666ce | 2019-03-06 09:18:50 +0800 | [diff] [blame] | 66 | if (alloc_ops.m_alloc) |
| 67 | return alloc_ops.m_alloc(size); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 68 | assert(0); |
Andrew Jeffery | 5e56cd8 | 2020-03-10 22:23:05 +1030 | [diff] [blame] | 69 | return NULL; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void __mctp_free(void *ptr) |
| 73 | { |
Jeremy Kerr | 3a666ce | 2019-03-06 09:18:50 +0800 | [diff] [blame] | 74 | if (alloc_ops.m_free) |
| 75 | alloc_ops.m_free(ptr); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 76 | else |
| 77 | assert(0); |
| 78 | } |
| 79 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 80 | void *__mctp_msg_alloc(size_t size, struct mctp *mctp) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 81 | { |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 82 | void *ctx = mctp_get_alloc_ctx(mctp); |
| 83 | if (alloc_ops.m_msg_alloc) |
| 84 | return alloc_ops.m_msg_alloc(size, ctx); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 85 | assert(0); |
Andrew Jeffery | 5e56cd8 | 2020-03-10 22:23:05 +1030 | [diff] [blame] | 86 | return NULL; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 87 | } |
| 88 | |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 89 | void __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 Johnston | bbfcc6e | 2024-09-17 16:48:15 +0800 | [diff] [blame] | 96 | #ifndef MCTP_CUSTOM_ALLOC |
Andrew Jeffery | 23496bf | 2022-09-30 14:17:09 +0930 | [diff] [blame] | 97 | void mctp_set_alloc_ops(void *(*m_alloc)(size_t), void (*m_free)(void *), |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 98 | void *(*m_msg_alloc)(size_t, void *), |
| 99 | void (*m_msg_free)(void *, void *)) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 100 | { |
Jeremy Kerr | 3a666ce | 2019-03-06 09:18:50 +0800 | [diff] [blame] | 101 | alloc_ops.m_alloc = m_alloc; |
| 102 | alloc_ops.m_free = m_free; |
Matt Johnston | 4a09e1d | 2024-09-13 14:55:58 +0800 | [diff] [blame] | 103 | alloc_ops.m_msg_alloc = m_msg_alloc; |
| 104 | alloc_ops.m_msg_free = m_msg_free; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 105 | } |
Matt Johnston | bbfcc6e | 2024-09-17 16:48:15 +0800 | [diff] [blame] | 106 | #endif // MCTP_CUSTOM_ALLOC |