Harald Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 1 | /* overly simplistic talloc replacement for deeply embedded |
| 2 | * microcontrollers. Obviously this has none of the properties of real |
| 3 | * talloc, it is particualrly not hierarchical at all */ |
| 4 | |
| 5 | |
| 6 | #include "talloc.h" |
| 7 | #include <string.h> |
Pau Espin Pedrol | 00f4ef7 | 2017-06-18 13:10:57 +0200 | [diff] [blame] | 8 | #include <stdio.h> |
Harald Welte | 898ffef | 2017-05-15 21:37:34 +0200 | [diff] [blame] | 9 | |
| 10 | void *_talloc_zero(const void *ctx, size_t size, const char *name) |
| 11 | { |
| 12 | void *p = pseudotalloc_malloc(size); |
| 13 | if (!p) |
| 14 | return NULL; |
| 15 | memset(p, 0, size); |
| 16 | return p; |
| 17 | } |
| 18 | |
| 19 | int _talloc_free(void *ptr, const char *location) |
| 20 | { |
| 21 | pseudotalloc_free(ptr); |
| 22 | return 0; |
| 23 | } |
| 24 | |
| 25 | void *talloc_named_const(const void *context, size_t size, const char *name) |
| 26 | { |
| 27 | return pseudotalloc_malloc(size); |
| 28 | } |
| 29 | |
| 30 | void talloc_set_name_const(const void *ptr, const char *name) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | char *talloc_strdup(const void *context, const char *p) |
| 35 | { |
| 36 | char *ptr; |
| 37 | size_t len; |
| 38 | |
| 39 | if (!p) |
| 40 | return NULL; |
| 41 | len = strlen(p); |
| 42 | |
| 43 | ptr = talloc_size(context, len+1); |
| 44 | if (!ptr) |
| 45 | return NULL; |
| 46 | memcpy(ptr, p, len+1); |
| 47 | |
| 48 | return ptr; |
| 49 | } |
| 50 | |
| 51 | void *talloc_pool(const void *context, size_t size) |
| 52 | { |
| 53 | return (void *) context; |
| 54 | } |
| 55 | |
| 56 | void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name) |
| 57 | { |
| 58 | return talloc_size(ctx, el_size * count); |
| 59 | } |
| 60 | |
| 61 | void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name) |
| 62 | { |
| 63 | return talloc_zero_size(ctx, el_size * count); |
| 64 | } |
Pau Espin Pedrol | 00f4ef7 | 2017-06-18 13:10:57 +0200 | [diff] [blame] | 65 | |
| 66 | char *talloc_asprintf(const void *ctx, const char *fmt, ...) |
| 67 | { |
| 68 | char *buf; |
| 69 | size_t len = 128; |
| 70 | va_list args; |
| 71 | va_start(args, fmt); |
| 72 | |
| 73 | buf = talloc_size(ctx, len); |
| 74 | if (len < vsnprintf(buf, len, fmt, args)) |
| 75 | strcpy(&buf[len-6], "[...]"); |
| 76 | |
| 77 | va_end(args); |
| 78 | return buf; |
| 79 | } |