Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file pseudotalloc.c |
| 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 | * 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 Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 24 | |
| 25 | #include "talloc.h" |
| 26 | #include <string.h> |
Pau Espin Pedrol | 00f4ef7 | 2017-06-18 13:10:57 +0200 | [diff] [blame] | 27 | #include <stdio.h> |
Harald Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 28 | |
| 29 | void *_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 | |
| 38 | int _talloc_free(void *ptr, const char *location) |
| 39 | { |
| 40 | pseudotalloc_free(ptr); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | void *talloc_named_const(const void *context, size_t size, const char *name) |
| 45 | { |
| 46 | return pseudotalloc_malloc(size); |
| 47 | } |
| 48 | |
Harald Welte | 513b2e3 | 2019-07-31 09:43:20 +0200 | [diff] [blame] | 49 | void *talloc_named(const void *context, size_t size, const char *fmt, ...) |
| 50 | { |
| 51 | return pseudotalloc_malloc(size); |
| 52 | } |
| 53 | |
Harald Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 54 | void talloc_set_name_const(const void *ptr, const char *name) |
| 55 | { |
| 56 | } |
| 57 | |
| 58 | char *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 | |
| 75 | void *talloc_pool(const void *context, size_t size) |
| 76 | { |
| 77 | return (void *) context; |
| 78 | } |
| 79 | |
| 80 | void *_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 | |
| 85 | void *_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 Pedrol | 00f4ef7 | 2017-06-18 13:10:57 +0200 | [diff] [blame] | 89 | |
| 90 | char *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 Welte | 14c4c49 | 2018-06-28 08:28:52 +0200 | [diff] [blame] | 104 | |
Harald Welte | 5f46605 | 2019-05-17 21:06:51 +0200 | [diff] [blame] | 105 | 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] | 106 | { |
| 107 | /* as we don't do hierarchical allocations, this is simply a NOP */ |
| 108 | return (void *)obj; |
| 109 | } |
| 110 | |
| 111 | char *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 | } |