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 | |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 12 | struct { |
Andrew Jeffery | 23496bf | 2022-09-30 14:17:09 +0930 | [diff] [blame] | 13 | void *(*m_alloc)(size_t); |
| 14 | void (*m_free)(void *); |
| 15 | void *(*m_realloc)(void *, size_t); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 16 | } alloc_ops = { |
Jeremy Kerr | c7e764a | 2019-05-28 16:49:03 +0800 | [diff] [blame] | 17 | #ifdef MCTP_DEFAULT_ALLOC |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 18 | malloc, |
| 19 | free, |
| 20 | realloc, |
Jeremy Kerr | 3cb4eee | 2019-03-06 09:15:47 +0800 | [diff] [blame] | 21 | #endif |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | /* internal-only allocation functions */ |
| 25 | void *__mctp_alloc(size_t size) |
| 26 | { |
Jeremy Kerr | 3a666ce | 2019-03-06 09:18:50 +0800 | [diff] [blame] | 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); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 31 | assert(0); |
Andrew Jeffery | 5e56cd8 | 2020-03-10 22:23:05 +1030 | [diff] [blame] | 32 | return NULL; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void __mctp_free(void *ptr) |
| 36 | { |
Jeremy Kerr | 3a666ce | 2019-03-06 09:18:50 +0800 | [diff] [blame] | 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); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 41 | else |
| 42 | assert(0); |
| 43 | } |
| 44 | |
| 45 | void *__mctp_realloc(void *ptr, size_t size) |
| 46 | { |
Jeremy Kerr | 3a666ce | 2019-03-06 09:18:50 +0800 | [diff] [blame] | 47 | if (alloc_ops.m_realloc) |
| 48 | return alloc_ops.m_realloc(ptr, size); |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 49 | assert(0); |
Andrew Jeffery | 5e56cd8 | 2020-03-10 22:23:05 +1030 | [diff] [blame] | 50 | return NULL; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 51 | } |
| 52 | |
Andrew Jeffery | 23496bf | 2022-09-30 14:17:09 +0930 | [diff] [blame] | 53 | void mctp_set_alloc_ops(void *(*m_alloc)(size_t), void (*m_free)(void *), |
| 54 | void *(m_realloc)(void *, size_t)) |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 55 | { |
Jeremy Kerr | 3a666ce | 2019-03-06 09:18:50 +0800 | [diff] [blame] | 56 | alloc_ops.m_alloc = m_alloc; |
| 57 | alloc_ops.m_free = m_free; |
| 58 | alloc_ops.m_realloc = m_realloc; |
Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 59 | } |