Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file talloc.h |
| 2 | * overly simplistic talloc replacement for deeply embedded |
Harald Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 3 | * microcontrollers. Obviously this has none of the properties of real |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 4 | * talloc, it is particualrly not hierarchical at all. */ |
| 5 | |
| 6 | #pragma once |
Harald Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 7 | |
| 8 | #include <stdlib.h> |
| 9 | #include <stdarg.h> |
| 10 | |
| 11 | /* those two functions have to be provided by the user/environment */ |
| 12 | extern void *pseudotalloc_malloc(size_t size); |
| 13 | extern void pseudotalloc_free(void *ptr); |
| 14 | |
| 15 | typedef void TALLOC_CTX; |
| 16 | |
| 17 | #define __TALLOC_STRING_LINE1__(s) #s |
| 18 | #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s) |
| 19 | #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__) |
| 20 | #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__ |
| 21 | |
| 22 | #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type) |
| 23 | #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__) |
| 24 | void *_talloc_zero(const void *ctx, size_t size, const char *name); |
| 25 | |
| 26 | #define talloc_free(ctx) _talloc_free(ctx, __location__) |
| 27 | int _talloc_free(void *ptr, const char *location); |
| 28 | |
| 29 | /* Unsupported! */ |
| 30 | #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type) |
| 31 | #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__) |
| 32 | void *talloc_named_const(const void *context, size_t size, const char *name); |
| 33 | void talloc_set_name_const(const void *ptr, const char *name); |
| 34 | char *talloc_strdup(const void *t, const char *p); |
| 35 | void *talloc_pool(const void *context, size_t size); |
| 36 | #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type) |
| 37 | void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); |
| 38 | #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type) |
| 39 | void *_talloc_zero_array(const void *ctx, |
| 40 | size_t el_size, |
| 41 | unsigned count, |
| 42 | const char *name); |
Pau Espin Pedrol | 00f4ef7 | 2017-06-18 13:10:57 +0200 | [diff] [blame] | 43 | char *talloc_asprintf(const void *ctx, const char *fmt, ...); |