Harald Welte | 8e7fca3 | 2017-05-07 16:14:33 +0200 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | |
| 3 | #include "talloc.h" |
| 4 | #include "trace.h" |
| 5 | #include "osmocom/core/utils.h" |
| 6 | |
| 7 | #define NUM_RCTX_SMALL 10 |
| 8 | #define RCTX_SIZE_SMALL 348 |
| 9 | |
| 10 | static uint8_t msgb_data[NUM_RCTX_SMALL][RCTX_SIZE_SMALL] __attribute__((aligned(sizeof(long)))); |
| 11 | static uint8_t msgb_inuse[NUM_RCTX_SMALL]; |
| 12 | |
| 13 | void *_talloc_zero(const void *ctx, size_t size, const char *name) |
| 14 | { |
| 15 | unsigned int i; |
| 16 | |
| 17 | if (size > RCTX_SIZE_SMALL) { |
| 18 | TRACE_ERROR("%s() request too large(%d > %d)\r\n", __func__, size, RCTX_SIZE_SMALL); |
| 19 | return NULL; |
| 20 | } |
| 21 | |
| 22 | for (i = 0; i < ARRAY_SIZE(msgb_inuse); i++) { |
| 23 | if (!msgb_inuse[i]) { |
| 24 | uint8_t *out = msgb_data[i]; |
| 25 | msgb_inuse[i] = 1; |
| 26 | memset(out, 0, size); |
| 27 | return out; |
| 28 | } |
| 29 | } |
| 30 | TRACE_ERROR("%s() out of memory!\r\n", __func__); |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | int _talloc_free(void *ptr, const char *location) |
| 35 | { |
| 36 | unsigned int i; |
| 37 | for (i = 0; i < ARRAY_SIZE(msgb_inuse); i++) { |
| 38 | if (ptr == msgb_data[i]) { |
| 39 | if (!msgb_inuse[i]) { |
| 40 | TRACE_ERROR("%s: double_free by \r\n", __func__, location); |
| 41 | } else { |
| 42 | msgb_inuse[i] = 0; |
| 43 | } |
| 44 | return 0; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | TRACE_ERROR("%s: invalid pointer %p from %s\r\n", __func__, ptr, location); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | void talloc_set_name_const(const void *ptr, const char *name) |
| 53 | { |
| 54 | /* do nothing */ |
| 55 | } |
| 56 | |
| 57 | #if 0 |
| 58 | void *talloc_named_const(const void *context, size_t size, const char *name) |
| 59 | { |
| 60 | if (size) |
| 61 | TRACE_ERROR("%s: called with size!=0 from %s\r\n", __func__, name); |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | void *talloc_pool(const void *context, size_t size) |
| 66 | { |
| 67 | } |
| 68 | #endif |
| 69 | |