blob: bc7a491078239d0a42b008041a8fc047bb5789bc [file] [log] [blame]
Harald Welte898ffef2017-05-15 21:37:34 +02001/* 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 Pedrol00f4ef72017-06-18 13:10:57 +02008#include <stdio.h>
Harald Welte898ffef2017-05-15 21:37:34 +02009
10void *_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
19int _talloc_free(void *ptr, const char *location)
20{
21 pseudotalloc_free(ptr);
22 return 0;
23}
24
25void *talloc_named_const(const void *context, size_t size, const char *name)
26{
27 return pseudotalloc_malloc(size);
28}
29
30void talloc_set_name_const(const void *ptr, const char *name)
31{
32}
33
34char *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
51void *talloc_pool(const void *context, size_t size)
52{
53 return (void *) context;
54}
55
56void *_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
61void *_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 Pedrol00f4ef72017-06-18 13:10:57 +020065
66char *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}