Jeremy Kerr | 4cdc200 | 2019-02-07 16:49:12 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 */ |
| 2 | |
| 3 | #include <assert.h> |
| 4 | |
| 5 | #include "libmctp-alloc.h" |
| 6 | |
| 7 | struct { |
| 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 */ |
| 18 | void *__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 | |
| 27 | void __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 | |
| 37 | void *__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 | |
| 44 | void 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 | } |