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.orgHarald Welte has uploaded this change for review. ( https://gerrit.osmocom.org/13561 Change subject: Allow application to override heap allocations ...................................................................... Allow application to override heap allocations 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 src/Makefile.am A src/smpp34_heap.c A src/smpp34_heap.h M src/smpp34_params.c M src/smpp34_unpack.c 5 files changed, 85 insertions(+), 12 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libsmpp34 refs/changes/61/13561/1 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..921beff --- /dev/null +++ b/src/smpp34_heap.c @@ -0,0 +1,54 @@ +#include <stdlib.h> +#include <stdbool.h> +#include <stdio.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(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..c17334d --- /dev/null +++ b/src/smpp34_heap.h @@ -0,0 +1,15 @@ +#pragma once +#include <stdlib.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(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: newchange Gerrit-Change-Id: I3656117115e89638c093bfbcbc4369ce302f7a94 Gerrit-Change-Number: 13561 Gerrit-PatchSet: 1 Gerrit-Owner: Harald Welte <laforge at gnumonks.org> -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190409/08809a95/attachment.htm>