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
This commit is contained in:
Harald Welte 2019-04-10 00:16:25 +02:00
parent 3cf5229fa0
commit aa53bba608
5 changed files with 85 additions and 12 deletions

View File

@ -10,6 +10,7 @@ libsmpp34_la_SOURCES = \
$(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 @@ libsmpp34_la_SOURCES = \
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

54
src/smpp34_heap.c Normal file
View File

@ -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(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);
}

15
src/smpp34_heap.h Normal file
View File

@ -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(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);

View File

@ -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_udad( udad_t *sourceList )
/* Destroy DAD-Chain **************************************************/
while( sourceList != NULL ){
i = sourceList->next;
free((void*)sourceList);
smpp34_free((void*)sourceList);
sourceList = i;
};
@ -68,9 +69,9 @@ build_dad( dad_t **dest, dad_t *source )
{
/* 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( dad_t *sourceList )
/* Destroy DAD-Chain **************************************************/
while( sourceList != NULL ){
i = sourceList->next;
free((void*)sourceList);
smpp34_free((void*)sourceList);
sourceList = i;
};
@ -102,9 +103,9 @@ build_tlv( tlv_t **dest, tlv_t *source )
{
/* 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( tlv_t *sourceList )
/* Destroy TLV-Chain **************************************************/
while( sourceList != NULL ){
i = sourceList->next;
free((void*)sourceList);
smpp34_free((void*)sourceList);
sourceList = i;
};

View File

@ -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 @@ smpp34_unpack(uint32_t type, void* tt, const uint8_t *ptrBuf, int ptrLen)
#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 @@ smpp34_unpack(uint32_t type, void* tt, const uint8_t *ptrBuf, int ptrLen)
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 @@ smpp34_unpack(uint32_t type, void* tt, const uint8_t *ptrBuf, int ptrLen)
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;\