blob: 25425e5755c6bd18cc39d9251041053c63ab830f [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file pseudotalloc.c
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 * All Rights Reserved
8 *
9 * SPDX-License-Identifier: GPL-2.0+
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
Harald Welte898ffef2017-05-15 21:37:34 +020024
25#include "talloc.h"
26#include <string.h>
Pau Espin Pedrol00f4ef72017-06-18 13:10:57 +020027#include <stdio.h>
Harald Welte898ffef2017-05-15 21:37:34 +020028
29void *_talloc_zero(const void *ctx, size_t size, const char *name)
30{
31 void *p = pseudotalloc_malloc(size);
32 if (!p)
33 return NULL;
34 memset(p, 0, size);
35 return p;
36}
37
38int _talloc_free(void *ptr, const char *location)
39{
40 pseudotalloc_free(ptr);
41 return 0;
42}
43
44void *talloc_named_const(const void *context, size_t size, const char *name)
45{
46 return pseudotalloc_malloc(size);
47}
48
Harald Welte513b2e32019-07-31 09:43:20 +020049void *talloc_named(const void *context, size_t size, const char *fmt, ...)
50{
51 return pseudotalloc_malloc(size);
52}
53
Harald Welte898ffef2017-05-15 21:37:34 +020054void talloc_set_name_const(const void *ptr, const char *name)
55{
56}
57
58char *talloc_strdup(const void *context, const char *p)
59{
60 char *ptr;
61 size_t len;
62
63 if (!p)
64 return NULL;
65 len = strlen(p);
66
67 ptr = talloc_size(context, len+1);
68 if (!ptr)
69 return NULL;
70 memcpy(ptr, p, len+1);
71
72 return ptr;
73}
74
75void *talloc_pool(const void *context, size_t size)
76{
77 return (void *) context;
78}
79
80void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
81{
82 return talloc_size(ctx, el_size * count);
83}
84
85void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
86{
87 return talloc_zero_size(ctx, el_size * count);
88}
Pau Espin Pedrol00f4ef72017-06-18 13:10:57 +020089
90char *talloc_asprintf(const void *ctx, const char *fmt, ...)
91{
92 char *buf;
93 size_t len = 128;
94 va_list args;
95 va_start(args, fmt);
96
97 buf = talloc_size(ctx, len);
98 if (len < vsnprintf(buf, len, fmt, args))
99 strcpy(&buf[len-6], "[...]");
100
101 va_end(args);
102 return buf;
103}
Harald Welte14c4c492018-06-28 08:28:52 +0200104
Harald Welte5f466052019-05-17 21:06:51 +0200105void *_talloc_steal_loc(const void *new_ctx, const void *obj, const char *location)
Harald Welte14c4c492018-06-28 08:28:52 +0200106{
107 /* as we don't do hierarchical allocations, this is simply a NOP */
108 return (void *)obj;
109}
110
111char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
112{
113 /* we have a hard-coded maximum string length of 128 bytes in this pseudo implementation */
114 char *buf = pseudotalloc_malloc(128);
115 if (!buf)
116 return NULL;
117 vsnprintf(buf, 128, fmt, ap);
118 return buf;
119}