blob: d257a981acb1755b93c9bd1396491ffd792d4350 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file talloc.h
2 * overly simplistic talloc replacement for deeply embedded
Harald Welte898ffef2017-05-15 21:37:34 +02003 * microcontrollers. Obviously this has none of the properties of real
Harald Weltee08da972017-11-13 01:00:26 +09004 * 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 Hofmeyr17518fe2017-06-20 04:35:06 +020023
24#pragma once
Harald Welte898ffef2017-05-15 21:37:34 +020025
26#include <stdlib.h>
27#include <stdarg.h>
28
29/* those two functions have to be provided by the user/environment */
30extern void *pseudotalloc_malloc(size_t size);
31extern void pseudotalloc_free(void *ptr);
32
33typedef 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__)
42void *_talloc_zero(const void *ctx, size_t size, const char *name);
43
44#define talloc_free(ctx) _talloc_free(ctx, __location__)
45int _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__)
50void *talloc_named_const(const void *context, size_t size, const char *name);
Harald Welte513b2e32019-07-31 09:43:20 +020051void *talloc_named(const void *context, size_t size, const char *fmt, ...);
Harald Welte898ffef2017-05-15 21:37:34 +020052void talloc_set_name_const(const void *ptr, const char *name);
53char *talloc_strdup(const void *t, const char *p);
54void *talloc_pool(const void *context, size_t size);
55#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
56void *_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)
58void *_talloc_zero_array(const void *ctx,
59 size_t el_size,
60 unsigned count,
61 const char *name);
Pau Espin Pedrol00f4ef72017-06-18 13:10:57 +020062char *talloc_asprintf(const void *ctx, const char *fmt, ...);
Harald Welte14c4c492018-06-28 08:28:52 +020063
Harald Welte5f466052019-05-17 21:06:51 +020064#define talloc_steal(ctx, ptr) _talloc_steal_loc((ctx), (ptr), __location__)
65void *_talloc_steal_loc(const void *new_ctx, const void *obj, const char *location);
Harald Welte14c4c492018-06-28 08:28:52 +020066char *talloc_vasprintf(const void *t, const char *fmt, va_list ap);