Change in libsmpp34[master]: Allow application to override default heap allocator

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
Thu Apr 11 07:24:21 UTC 2019


Harald Welte has submitted this change and it was merged. ( https://gerrit.osmocom.org/13561 )

Change subject: Allow application to override default heap allocator
......................................................................

Allow application to override default heap allocator

Let's introduce a mechanism by which libsmpp34-using applications can
override the memory allocator functions.  This allows us e.g. in the
Osmocom context to use talloc which aids us in debugging memory leaks.

Closes: OS#3913
Change-Id: I3656117115e89638c093bfbcbc4369ce302f7a94
---
M TODO-RELEASE
M src/Makefile.am
A src/smpp34_heap.c
A src/smpp34_heap.h
M src/smpp34_params.c
M src/smpp34_unpack.c
6 files changed, 109 insertions(+), 12 deletions(-)

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



diff --git a/TODO-RELEASE b/TODO-RELEASE
index d0852fc..bbc1495 100644
--- a/TODO-RELEASE
+++ b/TODO-RELEASE
@@ -7,3 +7,4 @@
 # If any interfaces have been added since the last public release: c:r:a + 1.
 # If any interfaces have been removed or changed since the last public release: c:r:0.
 #library	what		description / commit summary line
+libsmpp34	smpp34_heap	Adding new API/ABI to override memory allocator
diff --git a/src/Makefile.am b/src/Makefile.am
index 67550f3..55097c4 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -10,6 +10,7 @@
     $(LIBRARY_SOURCE_DIR)/smpp34.h \
     $(LIBRARY_SOURCE_DIR)/smpp34_dumpBuf.c \
     $(LIBRARY_SOURCE_DIR)/smpp34_dumpPdu.c \
+    $(LIBRARY_SOURCE_DIR)/smpp34_heap.c \
     $(LIBRARY_SOURCE_DIR)/smpp34_pack.c \
     $(LIBRARY_SOURCE_DIR)/smpp34_unpack.c \
     $(LIBRARY_SOURCE_DIR)/smpp34_structs.c \
@@ -19,6 +20,7 @@
 
 include_HEADERS = \
     $(LIBRARY_SOURCE_DIR)/smpp34.h \
+    $(LIBRARY_SOURCE_DIR)/smpp34_heap.h \
     $(LIBRARY_SOURCE_DIR)/smpp34_structs.h \
     $(LIBRARY_SOURCE_DIR)/smpp34_params.h
 
diff --git a/src/smpp34_heap.c b/src/smpp34_heap.c
new file mode 100644
index 0000000..f93d457
--- /dev/null
+++ b/src/smpp34_heap.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2019 Harald Welte
+ * File  : smpp34_heap.c
+ * Author: Harald Welte <laforge at gnumonks.org>
+ *
+ * This file is part of libsmpp34 (c-open-smpp3.4 library).
+ *
+ * The libsmpp34 library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of the
+ * License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+
+#include "smpp34_heap.h"
+
+static void *smpp34_libc_malloc(size_t sz)
+{
+	return malloc(sz);
+}
+
+static void *smpp34_libc_realloc(void *ptr, size_t sz)
+{
+	return realloc(ptr, sz);
+}
+
+static void smpp34_libc_free(void *ptr)
+{
+	return free(ptr);
+}
+
+static bool allocator_used = false;
+
+static struct smpp34_memory_functions smpp34_allocator = {
+	.malloc_fun = smpp34_libc_malloc,
+	.realloc_fun = smpp34_libc_realloc,
+	.free_fun = smpp34_libc_free,
+};
+
+void smpp34_set_memory_functions(const struct smpp34_memory_functions *mf)
+{
+	if (allocator_used) {
+		fprintf(stderr, "%s must be called before first use of smpp34_malloc\n", __func__);
+		return;
+	}
+	smpp34_allocator = *mf;
+}
+
+void *smpp34_malloc(size_t sz)
+{
+	allocator_used = true;
+	return smpp34_allocator.malloc_fun(sz);
+}
+
+void *smpp34_realloc(void *ptr, size_t sz)
+{
+	allocator_used = true;
+	return smpp34_allocator.realloc_fun(ptr, sz);
+}
+
+void smpp34_free(void *ptr)
+{
+	smpp34_allocator.free_fun(ptr);
+}
diff --git a/src/smpp34_heap.h b/src/smpp34_heap.h
new file mode 100644
index 0000000..6b9ac42
--- /dev/null
+++ b/src/smpp34_heap.h
@@ -0,0 +1,15 @@
+#pragma once
+#include <stddef.h>
+
+/* override the allocator with these methods; to be called BEFORE allocating anything */
+struct smpp34_memory_functions {
+	void * (*malloc_fun)(size_t sz);
+	void * (*realloc_fun)(void *ptr, size_t sz);
+	void (*free_fun)(void *ptr);
+};
+
+void smpp34_set_memory_functions(const struct smpp34_memory_functions *mf);
+
+void *smpp34_malloc(size_t sz);
+void *smpp34_realloc(void *ptr, size_t sz);
+void smpp34_free(void *ptr);
diff --git a/src/smpp34_params.c b/src/smpp34_params.c
index a91f9eb..e7eb680 100644
--- a/src/smpp34_params.c
+++ b/src/smpp34_params.c
@@ -27,15 +27,16 @@
 
 #include "smpp34.h"
 #include "smpp34_structs.h"
+#include "smpp34_heap.h"
 
 int 
 build_udad( udad_t **dest, udad_t *source )
 {
 
     /* Build new DAD-Chain ************************************************/
-    udad_t *dummy = (udad_t*)malloc(sizeof( udad_t ));
+    udad_t *dummy = (udad_t*)smpp34_malloc(sizeof( udad_t ));
     if( dummy == NULL ){
-        printf("Error in malloc()\n" );
+        printf("Error in smpp34_malloc()\n" );
         return( -1 );
     };
     memcpy(dummy, source, sizeof( udad_t ));
@@ -54,7 +55,7 @@
     /* Destroy DAD-Chain **************************************************/
     while( sourceList != NULL ){
         i = sourceList->next;
-        free((void*)sourceList);
+        smpp34_free((void*)sourceList);
         sourceList = i;
     };
 
@@ -68,9 +69,9 @@
 {
 
     /* Build new DAD-Chain ************************************************/
-    dad_t *dummy = (dad_t*)malloc(sizeof( dad_t ));
+    dad_t *dummy = (dad_t*)smpp34_malloc(sizeof( dad_t ));
     if( dummy == NULL ){
-        printf("Error in malloc()\n" );
+        printf("Error in smpp34_malloc()\n" );
         return( -1 );
     };
     memcpy(dummy, source, sizeof( dad_t ));
@@ -89,7 +90,7 @@
     /* Destroy DAD-Chain **************************************************/
     while( sourceList != NULL ){
         i = sourceList->next;
-        free((void*)sourceList);
+        smpp34_free((void*)sourceList);
         sourceList = i;
     };
 
@@ -102,9 +103,9 @@
 {
 
     /* Build new TLV-Chain ************************************************/
-    tlv_t *dummy = (tlv_t*)malloc(sizeof( tlv_t ));
+    tlv_t *dummy = (tlv_t*)smpp34_malloc(sizeof( tlv_t ));
     if( dummy == NULL ){
-        printf("Error in malloc()\n" );
+        printf("Error in smpp34_malloc()\n" );
         return( -1 );
     };
     memcpy(dummy, source, sizeof( tlv_t ));
@@ -123,7 +124,7 @@
     /* Destroy TLV-Chain **************************************************/
     while( sourceList != NULL ){
         i = sourceList->next;
-        free((void*)sourceList);
+        smpp34_free((void*)sourceList);
         sourceList = i;
     };
 
diff --git a/src/smpp34_unpack.c b/src/smpp34_unpack.c
index 749a037..ec73d35 100644
--- a/src/smpp34_unpack.c
+++ b/src/smpp34_unpack.c
@@ -31,6 +31,7 @@
 #include "smpp34.h"
 #include "smpp34_structs.h"
 #include "smpp34_params.h"
+#include "smpp34_heap.h"
 
 /* GLOBALS ********************************************************************/
 /* EXTERN *********************************************************************/
@@ -179,7 +180,7 @@
 #define TLV( inst, tlv3, do_tlv ){\
     tlv_t *aux_tlv = NULL;\
     while( (aux - ini) < t1->command_length ){\
-        aux_tlv = (tlv_t *) malloc(sizeof( tlv_t ));\
+        aux_tlv = (tlv_t *) smpp34_malloc(sizeof( tlv_t ));\
         memset(aux_tlv, 0, sizeof(tlv_t));\
         do_tlv( aux_tlv );\
         aux_tlv->next = inst tlv3;\
@@ -191,7 +192,7 @@
     udad_t *aux_udad = NULL;\
     int c = 0;\
     while( c < t1->no_unsuccess ){\
-        aux_udad = (udad_t *) malloc(sizeof( udad_t ));\
+        aux_udad = (udad_t *) smpp34_malloc(sizeof( udad_t ));\
         memset(aux_udad, 0, sizeof(udad_t));\
         do_udad( aux_udad );\
         aux_udad->next = inst udad3;\
@@ -204,7 +205,7 @@
     dad_t *aux_dad = NULL;\
     int c = 0;\
     while( c < t1->number_of_dests ){\
-        aux_dad = (dad_t *) malloc(sizeof( dad_t ));\
+        aux_dad = (dad_t *) smpp34_malloc(sizeof( dad_t ));\
         memset(aux_dad, 0, sizeof(dad_t));\
         do_dad( aux_dad );\
         aux_dad->next = inst dad3;\

-- 
To view, visit https://gerrit.osmocom.org/13561
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libsmpp34
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I3656117115e89638c093bfbcbc4369ce302f7a94
Gerrit-Change-Number: 13561
Gerrit-PatchSet: 7
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Assignee: fixeria <axilirator at gmail.com>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder (1000002)
Gerrit-Reviewer: Pau Espin Pedrol <pespin at sysmocom.de>
Gerrit-CC: Vadim Yanitskiy <axilirator at gmail.com>
Gerrit-CC: fixeria <axilirator at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190411/afecb3c4/attachment.htm>


More information about the gerrit-log mailing list