summaryrefslogtreecommitdiffstats
path: root/src/pseudotalloc/talloc.h
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-05-15 21:37:34 +0200
committerHarald Welte <laforge@gnumonks.org>2017-05-17 15:15:52 +0100
commit898ffefde4198a23a92810a9cf39ff6846f3bb39 (patch)
treeab4f79425d35ade4b542b0b6d57b58992408321f /src/pseudotalloc/talloc.h
parent7f24c155f5acd898cd49f9f429500461fa4fd8a5 (diff)
add libpseudotalloc as super-simplistic talloc replacement
In tightly embedded builds (--enable-embedded), we want the ability to replace talloc with a very simple heap allocator to avoid the complexity of talloc without modifying all our code that assumes talloc. This will break the hierarchical notion of the allocator, but libosmo{core,gsm,coding,codec} don't rely on that anyway. Change-Id: Ie341034076f242a813f081919dd09d845775ad35
Diffstat (limited to 'src/pseudotalloc/talloc.h')
-rw-r--r--src/pseudotalloc/talloc.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/pseudotalloc/talloc.h b/src/pseudotalloc/talloc.h
new file mode 100644
index 00000000..c7127deb
--- /dev/null
+++ b/src/pseudotalloc/talloc.h
@@ -0,0 +1,42 @@
+#pragma once
+
+/* overly simplistic talloc replacement for deeply embedded
+ * microcontrollers. Obviously this has none of the properties of real
+ * talloc, it is particualrly not hierarchical at all */
+
+#include <stdlib.h>
+#include <stdarg.h>
+
+/* those two functions have to be provided by the user/environment */
+extern void *pseudotalloc_malloc(size_t size);
+extern void pseudotalloc_free(void *ptr);
+
+typedef void TALLOC_CTX;
+
+#define __TALLOC_STRING_LINE1__(s) #s
+#define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
+#define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
+#define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
+
+#define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
+#define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
+void *_talloc_zero(const void *ctx, size_t size, const char *name);
+
+#define talloc_free(ctx) _talloc_free(ctx, __location__)
+int _talloc_free(void *ptr, const char *location);
+
+/* Unsupported! */
+#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
+#define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
+void *talloc_named_const(const void *context, size_t size, const char *name);
+void talloc_set_name_const(const void *ptr, const char *name);
+char *talloc_strdup(const void *t, const char *p);
+void *talloc_pool(const void *context, size_t size);
+#define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
+void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
+#define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
+void *_talloc_zero_array(const void *ctx,
+ size_t el_size,
+ unsigned count,
+ const char *name);
+