blob: 3ce9f37ab1203927c5467a24077401bc824580a7 [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
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02004 * talloc, it is particualrly not hierarchical at all. */
5
6#pragma once
Harald Welte898ffef2017-05-15 21:37:34 +02007
8#include <stdlib.h>
9#include <stdarg.h>
10
11/* those two functions have to be provided by the user/environment */
12extern void *pseudotalloc_malloc(size_t size);
13extern void pseudotalloc_free(void *ptr);
14
15typedef 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__)
24void *_talloc_zero(const void *ctx, size_t size, const char *name);
25
26#define talloc_free(ctx) _talloc_free(ctx, __location__)
27int _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__)
32void *talloc_named_const(const void *context, size_t size, const char *name);
33void talloc_set_name_const(const void *ptr, const char *name);
34char *talloc_strdup(const void *t, const char *p);
35void *talloc_pool(const void *context, size_t size);
36#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
37void *_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)
39void *_talloc_zero_array(const void *ctx,
40 size_t el_size,
41 unsigned count,
42 const char *name);
Pau Espin Pedrol00f4ef72017-06-18 13:10:57 +020043char *talloc_asprintf(const void *ctx, const char *fmt, ...);