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 |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 4 | * talloc, it is particualrly not hierarchical at all. |
| 5 | * |
| 6 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | */ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 23 | |
| 24 | #pragma once |
Harald Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <stdarg.h> |
| 28 | |
| 29 | /* those two functions have to be provided by the user/environment */ |
| 30 | extern void *pseudotalloc_malloc(size_t size); |
| 31 | extern void pseudotalloc_free(void *ptr); |
| 32 | |
| 33 | typedef void TALLOC_CTX; |
| 34 | |
| 35 | #define __TALLOC_STRING_LINE1__(s) #s |
| 36 | #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s) |
| 37 | #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__) |
| 38 | #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__ |
| 39 | |
| 40 | #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type) |
| 41 | #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__) |
| 42 | void *_talloc_zero(const void *ctx, size_t size, const char *name); |
| 43 | |
| 44 | #define talloc_free(ctx) _talloc_free(ctx, __location__) |
| 45 | int _talloc_free(void *ptr, const char *location); |
| 46 | |
| 47 | /* Unsupported! */ |
| 48 | #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type) |
| 49 | #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__) |
| 50 | void *talloc_named_const(const void *context, size_t size, const char *name); |
Harald Welte | 513b2e3 | 2019-07-31 09:43:20 +0200 | [diff] [blame] | 51 | void *talloc_named(const void *context, size_t size, const char *fmt, ...); |
Harald Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 52 | void talloc_set_name_const(const void *ptr, const char *name); |
| 53 | char *talloc_strdup(const void *t, const char *p); |
| 54 | void *talloc_pool(const void *context, size_t size); |
| 55 | #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type) |
| 56 | void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); |
| 57 | #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type) |
| 58 | void *_talloc_zero_array(const void *ctx, |
| 59 | size_t el_size, |
| 60 | unsigned count, |
| 61 | const char *name); |
Pau Espin Pedrol | 00f4ef7 | 2017-06-18 13:10:57 +0200 | [diff] [blame] | 62 | char *talloc_asprintf(const void *ctx, const char *fmt, ...); |
Harald Welte | 14c4c49 | 2018-06-28 08:28:52 +0200 | [diff] [blame] | 63 | |
Harald Welte | 5f46605 | 2019-05-17 21:06:51 +0200 | [diff] [blame] | 64 | #define talloc_steal(ctx, ptr) _talloc_steal_loc((ctx), (ptr), __location__) |
| 65 | void *_talloc_steal_loc(const void *new_ctx, const void *obj, const char *location); |
Harald Welte | 14c4c49 | 2018-06-28 08:28:52 +0200 | [diff] [blame] | 66 | char *talloc_vasprintf(const void *t, const char *fmt, va_list ap); |