From 2dcc86fd734050c2b2b02e526f893bebb4800e10 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Sun, 11 Dec 2016 21:23:11 +0100 Subject: [PATCH] Moved debug memory alloc functions to own file --- Makefile | 1 + src/httplib_malloc.c | 142 ++++++++++++++++++++++++++++++++++++++++++ src/libhttp-private.h | 42 +++++++++++++ src/libhttp.c | 142 ------------------------------------------ 4 files changed, 185 insertions(+), 142 deletions(-) create mode 100644 src/httplib_malloc.c diff --git a/Makefile b/Makefile index f35d5a89..b0b09a6c 100644 --- a/Makefile +++ b/Makefile @@ -131,6 +131,7 @@ LIB_SOURCES = src/libhttp.c \ src/httplib_lock_unlock_context.c \ src/httplib_log_access.c \ src/httplib_lowercase.c \ + src/httplib_malloc.c \ src/httplib_master_thread.c \ src/httplib_match_prefix.c \ src/httplib_md5.c \ diff --git a/src/httplib_malloc.c b/src/httplib_malloc.c new file mode 100644 index 00000000..ae960b82 --- /dev/null +++ b/src/httplib_malloc.c @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2016 Lammert Bies + * Copyright (c) 2013-2016 the Civetweb developers + * Copyright (c) 2004-2013 Sergey Lyubka + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + + +#define NO_HTTPLIB_MALLOC_OVERRIDE + +#include "libhttp-private.h" + + +#if defined(MEMORY_DEBUGGING) + +unsigned long mg_memory_debug_blockCount = 0; +unsigned long mg_memory_debug_totalMemUsed = 0; + + +void *XX_httplib_malloc_ex( size_t size, const char *file, unsigned line ) { + + void *data = malloc(size + sizeof(size_t)); + void *memory = 0; + char mallocStr[256]; + + if (data) { + *(size_t *)data = size; + mg_memory_debug_totalMemUsed += size; + mg_memory_debug_blockCount++; + memory = (void *)(((char *)data) + sizeof(size_t)); + } + + return memory; + +} /* XX_httplib_malloc_ex */ + + +void *XX_httplib_calloc_ex( size_t count, size_t size, const char *file, unsigned line ) { + + void *data = XX_httplib_malloc_ex(size * count, file, line); + if ( data != NULL ) memset( data, 0, size * count ); + + return data; + +} /* XX_httplib_calloc_ex */ + + +void XX_httplib_free_ex( void *memory, const char *file, unsigned line ) { + + char mallocStr[256]; + void *data = (void *)(((char *)memory) - sizeof(size_t)); + size_t size; + + if (memory) { + size = *(size_t *)data; + mg_memory_debug_totalMemUsed -= size; + mg_memory_debug_blockCount--; + + free(data); + } + +} /* XX_httplib_free_ex */ + + +void *XX_httplib_realloc_ex( void *memory, size_t newsize, const char *file, unsigned line ) { + + char mallocStr[256]; + void *data; + void *_realloc; + size_t oldsize; + + if (newsize) { + if (memory) { + data = (void *)(((char *)memory) - sizeof(size_t)); + oldsize = *(size_t *)data; + _realloc = realloc(data, newsize + sizeof(size_t)); + if (_realloc) { + data = _realloc; + mg_memory_debug_totalMemUsed -= oldsize; + mg_memory_debug_totalMemUsed += newsize; + *(size_t *)data = newsize; + data = (void *)(((char *)data) + sizeof(size_t)); + } else { + return _realloc; + } + } else { + data = XX_httplib_malloc_ex(newsize, file, line); + } + } else { + data = 0; + XX_httplib_free_ex(memory, file, line); + } + + return data; +} + + +#else /* MEMORY_DEBUGGING */ + +void *XX_httplib_malloc( size_t a ) { + + return malloc(a); + +} /* XX_httplib_malloc */ + +void *XX_httplib_calloc( size_t a, size_t b ) { + + return calloc(a, b); + +} /* XX_httplib_calloc */ + +void * XX_httplib_realloc(void *a, size_t b) { + + return realloc(a, b); + +} /* XX_httplib_realloc */ + +void XX_httplib_free( void *a ) { + + free(a); + +} /* XX_httplib_free */ + +#endif /* MEMORY_DEBUGGING */ diff --git a/src/libhttp-private.h b/src/libhttp-private.h index 5031c6c2..e36382d9 100644 --- a/src/libhttp-private.h +++ b/src/libhttp-private.h @@ -22,6 +22,48 @@ * THE SOFTWARE. */ + + +/* + * The library provides their own memory allocation functions with a debugging + * version which counts the number of blocks and bytes allocated. The normal + * allocation functions are therefore disabled here, unless a macro has been + * set because a library function needs explicit access to the OS version of + * the memory allocation functions. + */ + +#if !defined(NO_HTTPLIB_MALLOC_OVERRIDE) +#ifdef malloc +#undef malloc +#endif +#ifdef calloc +#undef calloc +#endif +#ifdef realloc +#undef realloc +#endif +#ifdef free +#undef free +#endif +#ifdef snprintf +#undef snprintf +#endif +#ifdef vsnprintf +#undef vsnprintf +#endif +#define malloc DO_NOT_USE_THIS_FUNCTION__USE_httplib_malloc +#define calloc DO_NOT_USE_THIS_FUNCTION__USE_httplib_calloc +#define realloc DO_NOT_USE_THIS_FUNCTION__USE_httplib_realloc +#define free DO_NOT_USE_THIS_FUNCTION__USE_httplib_free +#define snprintf DO_NOT_USE_THIS_FUNCTION__USE_httplib_snprintf +#ifdef _WIN32 /* vsnprintf must not be used in any system, * \ \ \ \ + * but this define only works well for Windows. */ +#define vsnprintf DO_NOT_USE_THIS_FUNCTION__USE_httplib_vsnprintf +#endif +#endif /* ! NO_HTTPLIB_MALLOC_OVERRIDE */ + + + #if defined(_WIN32) #if !defined(_CRT_SECURE_NO_WARNINGS) #define _CRT_SECURE_NO_WARNINGS /* Disable deprecation warning in VS2005 */ diff --git a/src/libhttp.c b/src/libhttp.c index ca9aef39..9791bf47 100644 --- a/src/libhttp.c +++ b/src/libhttp.c @@ -130,148 +130,6 @@ pthread_mutexattr_t XX_httplib_pthread_mutex_attr; #endif /* defined(_WIN32_WCE) */ -#if defined(MEMORY_DEBUGGING) -unsigned long mg_memory_debug_blockCount = 0; -unsigned long mg_memory_debug_totalMemUsed = 0; - - -void *XX_httplib_malloc_ex( size_t size, const char *file, unsigned line ) { - - void *data = malloc(size + sizeof(size_t)); - void *memory = 0; - char mallocStr[256]; - - if (data) { - *(size_t *)data = size; - mg_memory_debug_totalMemUsed += size; - mg_memory_debug_blockCount++; - memory = (void *)(((char *)data) + sizeof(size_t)); - } - - return memory; - -} /* XX_httplib_malloc_ex */ - - -void *XX_httplib_calloc_ex( size_t count, size_t size, const char *file, unsigned line ) { - - void *data = XX_httplib_malloc_ex(size * count, file, line); - if ( data != NULL ) memset( data, 0, size * count ); - - return data; - -} /* XX_httplib_calloc_ex */ - - -void XX_httplib_free_ex( void *memory, const char *file, unsigned line ) { - - char mallocStr[256]; - void *data = (void *)(((char *)memory) - sizeof(size_t)); - size_t size; - - if (memory) { - size = *(size_t *)data; - mg_memory_debug_totalMemUsed -= size; - mg_memory_debug_blockCount--; - - free(data); - } - -} /* XX_httplib_free_ex */ - - -void *XX_httplib_realloc_ex( void *memory, size_t newsize, const char *file, unsigned line ) { - - char mallocStr[256]; - void *data; - void *_realloc; - size_t oldsize; - - if (newsize) { - if (memory) { - data = (void *)(((char *)memory) - sizeof(size_t)); - oldsize = *(size_t *)data; - _realloc = realloc(data, newsize + sizeof(size_t)); - if (_realloc) { - data = _realloc; - mg_memory_debug_totalMemUsed -= oldsize; - mg_memory_debug_totalMemUsed += newsize; - *(size_t *)data = newsize; - data = (void *)(((char *)data) + sizeof(size_t)); - } else { - return _realloc; - } - } else { - data = XX_httplib_malloc_ex(newsize, file, line); - } - } else { - data = 0; - XX_httplib_free_ex(memory, file, line); - } - - return data; -} - - -#else /* MEMORY_DEBUGGING */ - -void *XX_httplib_malloc( size_t a ) { - - return malloc(a); - -} /* XX_httplib_malloc */ - -void *XX_httplib_calloc( size_t a, size_t b ) { - - return calloc(a, b); - -} /* XX_httplib_calloc */ - -void * XX_httplib_realloc(void *a, size_t b) { - - return realloc(a, b); - -} /* XX_httplib_realloc */ - -void XX_httplib_free( void *a ) { - - free(a); - -} /* XX_httplib_free */ - -#endif /* MEMORY_DEBUGGING */ - - - -/* This following lines are just meant as a reminder to use the mg-functions - * for memory management */ -#ifdef malloc -#undef malloc -#endif -#ifdef calloc -#undef calloc -#endif -#ifdef realloc -#undef realloc -#endif -#ifdef free -#undef free -#endif -#ifdef snprintf -#undef snprintf -#endif -#ifdef vsnprintf -#undef vsnprintf -#endif -#define malloc DO_NOT_USE_THIS_FUNCTION__USE_httplib_malloc -#define calloc DO_NOT_USE_THIS_FUNCTION__USE_httplib_calloc -#define realloc DO_NOT_USE_THIS_FUNCTION__USE_httplib_realloc -#define free DO_NOT_USE_THIS_FUNCTION__USE_httplib_free -#define snprintf DO_NOT_USE_THIS_FUNCTION__USE_httplib_snprintf -#ifdef _WIN32 /* vsnprintf must not be used in any system, * \ \ \ \ - * but this define only works well for Windows. */ -#define vsnprintf DO_NOT_USE_THIS_FUNCTION__USE_httplib_vsnprintf -#endif /* Darwin prior to 7.0 and Win32 do not have socklen_t */ #ifdef NO_SOCKLEN_T