[MERGED] libosmocore[master]: add libpseudotalloc as super-simplistic talloc replacement

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Wed May 17 17:48:14 UTC 2017


Harald Welte has submitted this change and it was merged.

Change subject: add libpseudotalloc as super-simplistic talloc replacement
......................................................................


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
---
M Makefile.am
M configure.ac
M src/Makefile.am
M src/codec/Makefile.am
M src/coding/Makefile.am
M src/gsm/Makefile.am
A src/pseudotalloc/Makefile.am
A src/pseudotalloc/pseudotalloc.c
A src/pseudotalloc/talloc.h
9 files changed, 145 insertions(+), 1 deletion(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/Makefile.am b/Makefile.am
index 6ef275f..3f6df9b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,7 +1,7 @@
 ACLOCAL_AMFLAGS = -I m4
 
 AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
-SUBDIRS = include src src/vty src/codec src/gsm src/coding src/gb src/ctrl src/sim utils tests
+SUBDIRS = include src src/vty src/codec src/gsm src/coding src/gb src/ctrl src/sim src/pseudotalloc utils tests
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libosmocore.pc libosmocodec.pc libosmovty.pc libosmogsm.pc \
diff --git a/configure.ac b/configure.ac
index 1dd5930..04d126e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -194,6 +194,14 @@
         [enable_ctrl=$enableval], [enable_ctrl="yes"])
 AM_CONDITIONAL(ENABLE_CTRL, test x"$enable_ctrl" = x"yes")
 
+AC_ARG_ENABLE(pseudotalloc,
+        [AS_HELP_STRING(
+                [--enable-pseudotalloc],
+                [Enable building pseudotalloc library],
+        )],
+        [enable_pseudotalloc=$enableval], [enable_pseudotalloc="no"])
+AM_CONDITIONAL(ENABLE_PSEUDOTALLOC, test x"$enable_pseudotalloc" = x"yes")
+
 AC_ARG_ENABLE(embedded,
 	[AS_HELP_STRING(
 		[--enable-embedded],
@@ -211,6 +219,7 @@
 	AM_CONDITIONAL(ENABLE_UTILITIES, false)
 	AM_CONDITIONAL(ENABLE_GB, false)
 	AM_CONDITIONAL(ENABLE_PCSC, false)
+	AM_CONDITIONAL(ENABLE_PSEUDOTALLOC, true)
 	AC_DEFINE([PANIC_INFLOOP],[1],[Use infinite loop on panic rather than fprintf/abort])
 fi
 
@@ -287,6 +296,7 @@
 	src/gsm/Makefile
 	src/gb/Makefile
 	src/ctrl/Makefile
+	src/pseudotalloc/Makefile
 	tests/Makefile
 	tests/atlocal
 	utils/Makefile
diff --git a/src/Makefile.am b/src/Makefile.am
index c083936..5724055 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -6,6 +6,10 @@
 AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
 AM_CFLAGS = -Wall $(TALLOC_CFLAGS)
 
+if ENABLE_PSEUDOTALLOC
+AM_CPPFLAGS += -I$(top_srcdir)/src/pseudotalloc
+endif
+
 lib_LTLIBRARIES = libosmocore.la
 
 libosmocore_la_LIBADD = $(BACKTRACE_LIB) $(TALLOC_LIBS)
diff --git a/src/codec/Makefile.am b/src/codec/Makefile.am
index f61d2fe..b4df5da 100644
--- a/src/codec/Makefile.am
+++ b/src/codec/Makefile.am
@@ -6,6 +6,11 @@
 AM_CPPFLAGS = -I$(top_srcdir)/include $(TALLOC_CFLAGS)
 AM_CFLAGS = -Wall
 
+if ENABLE_PSEUDOTALLOC
+AM_CPPFLAGS += -I$(top_srcdir)/src/pseudotalloc
+endif
+
+
 lib_LTLIBRARIES = libosmocodec.la
 
 libosmocodec_la_SOURCES = gsm610.c gsm620.c gsm660.c gsm690.c
diff --git a/src/coding/Makefile.am b/src/coding/Makefile.am
index cbdcf47..06e55e5 100644
--- a/src/coding/Makefile.am
+++ b/src/coding/Makefile.am
@@ -9,6 +9,10 @@
 	$(TALLOC_CFLAGS)
 AM_CFLAGS = -Wall
 
+if ENABLE_PSEUDOTALLOC
+AM_CPPFLAGS += -I$(top_srcdir)/src/pseudotalloc
+endif
+
 lib_LTLIBRARIES = libosmocoding.la
 
 libosmocoding_la_SOURCES = \
diff --git a/src/gsm/Makefile.am b/src/gsm/Makefile.am
index e64c9e7..d15174a 100644
--- a/src/gsm/Makefile.am
+++ b/src/gsm/Makefile.am
@@ -6,6 +6,10 @@
 AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include $(TALLOC_CFLAGS)
 AM_CFLAGS = -Wall ${GCC_FVISIBILITY_HIDDEN}
 
+if ENABLE_PSEUDOTALLOC
+AM_CPPFLAGS += -I$(top_srcdir)/src/pseudotalloc
+endif
+
 # FIXME: this should eventually go into a milenage/Makefile.am
 noinst_HEADERS = milenage/aes.h milenage/aes_i.h milenage/aes_wrap.h \
 		 milenage/common.h milenage/crypto.h milenage/includes.h \
diff --git a/src/pseudotalloc/Makefile.am b/src/pseudotalloc/Makefile.am
new file mode 100644
index 0000000..3c78bba
--- /dev/null
+++ b/src/pseudotalloc/Makefile.am
@@ -0,0 +1,12 @@
+AM_CFLAGS = -Wall -I. $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/include
+
+if ENABLE_PSEUDOTALLOC
+
+lib_LTLIBRARIES = libpseudotalloc.la
+
+libpseudotalloc_la_SOURCES = pseudotalloc.c
+libpseudotalloc_la_LDFLAGS = -no-undefined
+
+nobase_include_HEADERS = talloc.h
+
+endif
diff --git a/src/pseudotalloc/pseudotalloc.c b/src/pseudotalloc/pseudotalloc.c
new file mode 100644
index 0000000..fe7f1ed
--- /dev/null
+++ b/src/pseudotalloc/pseudotalloc.c
@@ -0,0 +1,63 @@
+/* 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 "talloc.h"
+#include <string.h>
+
+void *_talloc_zero(const void *ctx, size_t size, const char *name)
+{
+	void *p = pseudotalloc_malloc(size);
+	if (!p)
+		return NULL;
+	memset(p, 0, size);
+	return p;
+}
+
+int _talloc_free(void *ptr, const char *location)
+{
+	pseudotalloc_free(ptr);
+	return 0;
+}
+
+void *talloc_named_const(const void *context, size_t size, const char *name)
+{
+	return pseudotalloc_malloc(size);
+}
+
+void talloc_set_name_const(const void *ptr, const char *name)
+{
+}
+
+char *talloc_strdup(const void *context, const char *p)
+{
+	char *ptr;
+	size_t len;
+
+	if (!p)
+		return NULL;
+	len = strlen(p);
+
+	ptr = talloc_size(context, len+1);
+	if (!ptr)
+		return NULL;
+	memcpy(ptr, p, len+1);
+
+	return ptr;
+}
+
+void *talloc_pool(const void *context, size_t size)
+{
+	return (void *) context;
+}
+
+void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
+{
+	return talloc_size(ctx, el_size * count);
+}
+
+void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
+{
+	return talloc_zero_size(ctx, el_size * count);
+}
diff --git a/src/pseudotalloc/talloc.h b/src/pseudotalloc/talloc.h
new file mode 100644
index 0000000..c7127de
--- /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);
+

-- 
To view, visit https://gerrit.osmocom.org/2651
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie341034076f242a813f081919dd09d845775ad35
Gerrit-PatchSet: 2
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Vadim Yanitskiy <axilirator at gmail.com>



More information about the gerrit-log mailing list