diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 22 | ||||
-rw-r--r-- | src/common/cbasetypes.h | 39 | ||||
-rw-r--r-- | src/common/core.c | 4 | ||||
-rw-r--r-- | src/common/db.h | 6 | ||||
-rw-r--r-- | src/common/grfio.c | 8 | ||||
-rw-r--r-- | src/common/grfio.h | 4 | ||||
-rw-r--r-- | src/common/malloc.c | 133 | ||||
-rw-r--r-- | src/common/malloc.h | 102 | ||||
-rw-r--r-- | src/common/plugin.h | 2 | ||||
-rw-r--r-- | src/common/socket.c | 53 | ||||
-rw-r--r-- | src/common/strlib.c | 4 |
11 files changed, 202 insertions, 175 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 21feb53f9..ea8ce6b71 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -12,11 +12,11 @@ endif() set( GLOBAL_INCLUDE_DIRS ${GLOBAL_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR} CACHE INTERNAL "" ) set( SVNVERSION ${SVNVERSION} CACHE STRING "SVN version of the source code" ) -if( WITH_COMPONENT_DEVELOPMENT ) +if( INSTALL_COMPONENT_DEVELOPMENT ) install( FILES ${CMAKE_CURRENT_BINARY_DIR}/svnversion.h DESTINATION "src/common" COMPONENT Development_base ) -endif() +endif( INSTALL_COMPONENT_DEVELOPMENT ) message( STATUS "Creating svnversion.h - done" ) @@ -48,7 +48,7 @@ set( COMMON_MINI_SOURCES "${COMMON_SOURCE_DIR}/showmsg.c" "${COMMON_SOURCE_DIR}/strlib.c" CACHE INTERNAL "" ) -set( COMMON_MINI_DEFINITIONS MINICORE CACHE INTERNAL "" ) +set( COMMON_MINI_DEFINITIONS "-DMINICORE" CACHE INTERNAL "" ) # @@ -95,17 +95,17 @@ set( COMMON_BASE_SOURCES CACHE INTERNAL "common_base sources" ) set( LIBRARIES ${ZLIB_LIBRARIES} ) set( INCLUDE_DIRS ${GLOBAL_INCLUDE_DIRS} ${MT19937AR_INCLUDE_DIRS} ${ZLIB_INCLUDE_DIRS} ) -set( DEFINITIONS ${GLOBAL_DEFINITIONS} ) +set( DEFINITIONS "${GLOBAL_DEFINITIONS}" ) set( SOURCE_FILES ${MT19937AR_HEADERS} ${MT19937AR_SOURCES} ${COMMON_BASE_HEADERS} ${COMMON_BASE_SOURCES} ) source_group( mt19937ar FILES ${MT19937AR_HEADERS} ${MT19937AR_SOURCES} ) source_group( common FILES ${COMMON_BASE_HEADERS} ${COMMON_BASE_SOURCES} ) add_library( common_base ${SOURCE_FILES} ) target_link_libraries( common_base ${LIBRARIES} ) -set_target_properties( common_base PROPERTIES COMPILE_DEFINITIONS "${DEFINITIONS}" ) +set_target_properties( common_base PROPERTIES COMPILE_FLAGS "${DEFINITIONS}" ) include_directories( ${INCLUDE_DIRS} ) +set( HAVE_common_base ON CACHE INTERNAL "" ) +set( TARGET_LIST ${TARGET_LIST} common_base CACHE INTERNAL "" ) message( STATUS "Creating target common_base - done" ) -set( HAVE_common_base ON CACHE BOOL "common_base target is available" ) -mark_as_advanced( HAVE_common_base ) else() message( STATUS "Skipping target common_base (requires ZLIB)" ) unset( HAVE_common_base CACHE ) @@ -127,17 +127,17 @@ set( COMMON_SQL_SOURCES set( DEPENDENCIES common_base ) set( LIBRARIES ${MYSQL_LIBRARIES} ) set( INCLUDE_DIRS ${GLOBAL_INCLUDE_DIRS} ${MYSQL_INCLUDE_DIRS} ) -set( DEFINITIONS ${GLOBAL_DEFINITIONS} ) +set( DEFINITIONS "${GLOBAL_DEFINITIONS}" ) set( SOURCE_FILES ${COMMON_SQL_HEADERS} ${COMMON_SQL_SOURCES} ) source_group( common FILES ${COMMON_SQL_HEADERS} ${COMMON_SQL_SOURCES} ) add_library( common_sql ${SOURCE_FILES} ) add_dependencies( common_sql ${DEPENDENCIES} ) target_link_libraries( common_sql ${LIBRARIES} ${DEPENDENCIES} ) -set_target_properties( common_sql PROPERTIES COMPILE_DEFINITIONS "${DEFINITIONS}" ) +set_target_properties( common_sql PROPERTIES COMPILE_FLAGS "${DEFINITIONS}" ) include_directories( ${INCLUDE_DIRS} ) +set( HAVE_common_sql ON CACHE INTERNAL "" ) +set( TARGET_LIST ${TARGET_LIST} common_sql CACHE INTERNAL "" ) message( STATUS "Creating target common_sql - done" ) -set( HAVE_common_sql ON CACHE BOOL "common_sql target is available" ) -mark_as_advanced( HAVE_common_sql ) else() message( STATUS "Skipping target common_sql (requires common_base and MYSQL)" ) unset( HAVE_common_sql CACHE ) diff --git a/src/common/cbasetypes.h b/src/common/cbasetypes.h index e2fe17555..58acad2c7 100644 --- a/src/common/cbasetypes.h +++ b/src/common/cbasetypes.h @@ -77,10 +77,26 @@ // portable printf/scanf format macros and integer definitions // NOTE: Visual C++ uses <inttypes.h> and <stdint.h> provided in /3rdparty ////////////////////////////////////////////////////////////////////////// +#ifdef __cplusplus +#define __STDC_CONSTANT_MACROS +#define __STDC_FORMAT_MACROS +#define __STDC_LIMIT_MACROS +#endif + #include <inttypes.h> #include <stdint.h> #include <limits.h> +// temporary fix for bugreport:4961 (unintended conversion from signed to unsigned) +// (-20 >= UCHAR_MAX) returns true +// (-20 >= USHRT_MAX) returns true +#if defined(__FreeBSD__) && defined(__x86_64) +#undef UCHAR_MAX +#define UCHAR_MAX (unsigned char)0xff +#undef USHRT_MAX +#define USHRT_MAX (unsigned short)0xffff +#endif + // ILP64 isn't supported, so always 32 bits? #ifndef UINT_MAX #define UINT_MAX 0xffffffff @@ -316,4 +332,27 @@ typedef char bool; #endif #endif + +////////////////////////////////////////////////////////////////////////// +// Set a pointer variable to a pointer value. +#ifdef __cplusplus +template <typename T1, typename T2> +void SET_POINTER(T1*&var, T2* p) +{ + var = static_cast<T1*>(p); +} +template <typename T1, typename T2> +void SET_FUNCPOINTER(T1& var, T2 p) +{ + char ASSERT_POINTERSIZE[sizeof(T1) == sizeof(void*) && sizeof(T2) == sizeof(void*)?1:-1];// 1 if true, -1 if false + union{ T1 out; T2 in; } tmp;// /!\ WARNING casting a pointer to a function pointer is against the C++ standard + tmp.in = p; + var = tmp.out; +} +#else +#define SET_POINTER(var,p) (var) = (p) +#define SET_FUNCPOINTER(var,p) (var) = (p) +#endif + + #endif /* _CBASETYPES_H_ */ diff --git a/src/common/core.c b/src/common/core.c index bfa563d8c..d528861c2 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -276,7 +276,3 @@ int main (int argc, char **argv) return 0; } - -#ifdef BCHECK -unsigned int __invalid_size_argument_for_IOC; -#endif diff --git a/src/common/db.h b/src/common/db.h index d33b8ec2e..e5515803c 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -994,8 +994,8 @@ void linkdb_foreach( struct linkdb_node** head, LinkDBFunc func, ... ); do{ \ if( (__n) > VECTOR_CAPACITY(__vec) ) \ { /* increase size */ \ - if( VECTOR_CAPACITY(__vec) == 0 ) VECTOR_DATA(__vec) = aMalloc((__n)*sizeof(VECTOR_FIRST(__vec))); /* allocate new */ \ - else VECTOR_DATA(__vec) = aRealloc(VECTOR_DATA(__vec),(__n)*sizeof(VECTOR_FIRST(__vec))); /* reallocate */ \ + if( VECTOR_CAPACITY(__vec) == 0 ) SET_POINTER(VECTOR_DATA(__vec), aMalloc((__n)*sizeof(VECTOR_FIRST(__vec)))); /* allocate new */ \ + else SET_POINTER(VECTOR_DATA(__vec), aRealloc(VECTOR_DATA(__vec),(__n)*sizeof(VECTOR_FIRST(__vec)))); /* reallocate */ \ memset(VECTOR_DATA(__vec)+VECTOR_LENGTH(__vec), 0, (VECTOR_CAPACITY(__vec)-VECTOR_LENGTH(__vec))*sizeof(VECTOR_FIRST(__vec))); /* clear new data */ \ VECTOR_CAPACITY(__vec) = (__n); /* update capacity */ \ } \ @@ -1007,7 +1007,7 @@ void linkdb_foreach( struct linkdb_node** head, LinkDBFunc func, ... ); } \ else if( (__n) < VECTOR_CAPACITY(__vec) ) \ { /* reduce size */ \ - VECTOR_DATA(__vec) = aRealloc(VECTOR_DATA(__vec),(__n)*sizeof(VECTOR_FIRST(__vec))); /* reallocate */ \ + SET_POINTER(VECTOR_DATA(__vec), aRealloc(VECTOR_DATA(__vec),(__n)*sizeof(VECTOR_FIRST(__vec)))); /* reallocate */ \ VECTOR_CAPACITY(__vec) = (__n); /* update capacity */ \ if( VECTOR_LENGTH(__vec) > (__n) ) VECTOR_LENGTH(__vec) = (__n); /* update length */ \ } \ diff --git a/src/common/grfio.c b/src/common/grfio.c index cb242fe5d..1d1ce756f 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -223,17 +223,17 @@ unsigned long grfio_crc32 (const unsigned char* buf, unsigned int len) /////////////////////////////////////////////////////////////////////////////// /// Grf data sub : zip decode -int decode_zip(unsigned char* dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen) +int decode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen) { - return uncompress(dest, destLen, source, sourceLen); + return uncompress((Bytef*)dest, destLen, (const Bytef*)source, sourceLen); } /////////////////////////////////////////////////////////////////////////////// /// Grf data sub : zip encode -int encode_zip(unsigned char* dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen) +int encode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen) { - return compress(dest, destLen, source, sourceLen); + return compress((Bytef*)dest, destLen, (const Bytef*)source, sourceLen); } diff --git a/src/common/grfio.h b/src/common/grfio.h index d0baa6609..0fc9f958d 100644 --- a/src/common/grfio.h +++ b/src/common/grfio.h @@ -14,7 +14,7 @@ char *grfio_find_file(char *fname); int grfio_size(char*); // GRFIO data file size get unsigned long grfio_crc32(const unsigned char *buf, unsigned int len); -int decode_zip(unsigned char* dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen); -int encode_zip(unsigned char* dest, unsigned long* destLen, const unsigned char* source, unsigned long sourceLen); +int decode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen); +int encode_zip(void* dest, unsigned long* destLen, const void* source, unsigned long sourceLen); #endif /* _GRFIO_H_ */ diff --git a/src/common/malloc.c b/src/common/malloc.c index f80d11fb4..1769e0982 100644 --- a/src/common/malloc.c +++ b/src/common/malloc.c @@ -10,9 +10,63 @@ #include <string.h> #include <time.h> -// no logging for minicore -#if defined(MINICORE) && defined(LOG_MEMMGR) -#undef LOG_MEMMGR +////////////// Memory Libraries ////////////////// + +#if defined(MEMWATCH) + +# include <string.h> +# include "memwatch.h" +# define MALLOC(n,file,line,func) mwMalloc((n),(file),(line)) +# define CALLOC(m,n,file,line,func) mwCalloc((m),(n),(file),(line)) +# define REALLOC(p,n,file,line,func) mwRealloc((p),(n),(file),(line)) +# define STRDUP(p,file,line,func) mwStrdup((p),(file),(line)) +# define FREE(p,file,line,func) mwFree((p),(file),(line)) +# define MEMORY_USAGE() 0 +# define MEMORY_VERIFY(ptr) mwIsSafeAddr(ptr, 1) +# define MEMORY_CHECK() CHECK() + +#elif defined(DMALLOC) + +# include <string.h> +# include <stdlib.h> +# include "dmalloc.h" +# define MALLOC(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0) +# define CALLOC(m,n,file,line,func) dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0) +# define REALLOC(p,n,file,line,func) dmalloc_realloc((file),(line),(p),(n),DMALLOC_FUNC_REALLOC,0) +# define STRDUP(p,file,line,func) strdup(p) +# define FREE(p,file,line,func) free(p) +# define MEMORY_USAGE() dmalloc_memory_allocated() +# define MEMORY_VERIFY(ptr) (dmalloc_verify(ptr) == DMALLOC_VERIFY_NOERROR) +# define MEMORY_CHECK() dmalloc_log_stats(); dmalloc_log_unfreed() + +#elif defined(GCOLLECT) + +# include "gc.h" +# ifdef GC_ADD_CALLER +# define RETURN_ADDR 0, +# else +# define RETURN_ADDR +# endif +# define MALLOC(n,file,line,func) GC_debug_malloc((n), RETURN_ADDR (file),(line)) +# define CALLOC(m,n,file,line,func) GC_debug_malloc((m)*(n), RETURN_ADDR (file),(line)) +# define REALLOC(p,n,file,line,func) GC_debug_realloc((p),(n), RETURN_ADDR (file),(line)) +# define STRDUP(p,file,line,func) GC_debug_strdup((p), RETURN_ADDR (file),(line)) +# define FREE(p,file,line,func) GC_debug_free(p) +# define MEMORY_USAGE() GC_get_heap_size() +# define MEMORY_VERIFY(ptr) (GC_base(ptr) != NULL) +# define MEMORY_CHECK() GC_gcollect() + +#else + +# define MALLOC(n,file,line,func) malloc(n) +# define CALLOC(m,n,file,line,func) calloc((m),(n)) +# define REALLOC(p,n,file,line,func) realloc((p),(n)) +# define STRDUP(p,file,line,func) strdup(p) +# define FREE(p,file,line,func) free(p) +# define MEMORY_USAGE() 0 +# define MEMORY_VERIFY(ptr) true +# define MEMORY_CHECK() + #endif void* aMalloc_(size_t size, const char *file, int line, const char *func) @@ -26,17 +80,6 @@ void* aMalloc_(size_t size, const char *file, int line, const char *func) return ret; } -void* aMallocA_(size_t size, const char *file, int line, const char *func) -{ - void *ret = MALLOCA(size, file, line, func); - // ShowMessage("%s:%d: in func %s: aMallocA %d\n",file,line,func,size); - if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: aMallocA error out of memory!\n",file,line,func); - exit(EXIT_FAILURE); - } - - return ret; -} void* aCalloc_(size_t num, size_t size, const char *file, int line, const char *func) { void *ret = CALLOC(num, size, file, line, func); @@ -47,16 +90,6 @@ void* aCalloc_(size_t num, size_t size, const char *file, int line, const char * } return ret; } -void* aCallocA_(size_t num, size_t size, const char *file, int line, const char *func) -{ - void *ret = CALLOCA(num, size, file, line, func); - // ShowMessage("%s:%d: in func %s: aCallocA %d %d\n",file,line,func,num,size); - if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: aCallocA error out of memory!\n",file,line,func); - exit(EXIT_FAILURE); - } - return ret; -} void* aRealloc_(void *p, size_t size, const char *file, int line, const char *func) { void *ret = REALLOC(p, size, file, line, func); @@ -86,29 +119,6 @@ void aFree_(void *p, const char *file, int line, const char *func) p = NULL; } -#ifdef GCOLLECT - -void* _bcallocA(size_t size, size_t cnt) -{ - void *ret = MALLOCA(size * cnt); - if (ret) memset(ret, 0, size * cnt); - return ret; -} -void* _bcalloc(size_t size, size_t cnt) -{ - void *ret = MALLOC(size * cnt); - if (ret) memset(ret, 0, size * cnt); - return ret; -} -char* _bstrdup(const char *chr) -{ - int len = strlen(chr); - char *ret = (char*)MALLOC(len + 1); - if (ret) memcpy(ret, chr, len + 1); - return ret; -} - -#endif #ifdef USE_MEMMGR @@ -654,21 +664,32 @@ static void memmgr_init (void) *-------------------------------------- */ -bool malloc_verify(void* ptr) + +/// Tests the memory for errors and memory leaks. +void malloc_memory_check(void) +{ + MEMORY_CHECK(); +} + + +/// Returns true if a pointer is valid. +/// The check is best-effort, false positives are possible. +bool malloc_verify_ptr(void* ptr) { #ifdef USE_MEMMGR - return memmgr_verify(ptr); + return memmgr_verify(ptr) && MEMORY_VERIFY(ptr); #else - return true; + return MEMORY_VERIFY(ptr); #endif } + size_t malloc_usage (void) { #ifdef USE_MEMMGR return memmgr_usage (); #else - return 0; + return MEMORY_USAGE(); #endif } @@ -677,10 +698,20 @@ void malloc_final (void) #ifdef USE_MEMMGR memmgr_final (); #endif + MEMORY_CHECK(); } void malloc_init (void) { +#if defined(DMALLOC) && defined(CYGWIN) + // http://dmalloc.com/docs/latest/online/dmalloc_19.html + dmalloc_debug_setup(getenv("DMALLOC_OPTIONS")); +#endif +#ifdef GCOLLECT + // don't garbage collect, only report inaccessible memory that was not deallocated + GC_find_leak = 1; + GC_INIT(); +#endif #ifdef USE_MEMMGR memmgr_init (); #endif diff --git a/src/common/malloc.h b/src/common/malloc.h index aaf3e318d..5f8191d92 100644 --- a/src/common/malloc.h +++ b/src/common/malloc.h @@ -6,26 +6,19 @@ #include "../common/cbasetypes.h" -// Q: What are the 'a'-variant allocation functions? -// A: They allocate memory from the stack, which is automatically -// freed when the invoking function returns. -// But it's not portable (http://c-faq.com/malloc/alloca.html) -// and I have doubts our implementation works. -// -> They should NOT be used, period. - #define ALC_MARK __FILE__, __LINE__, __func__ -// disable built-in memory manager when using another manager -#if defined(MEMWATCH) || defined(DMALLOC) || defined(GCOLLECT) || defined(BCHECK) -#if !defined(NO_MEMMGR) -#define NO_MEMMGR -#endif -#endif -// Use built-in memory manager by default +// default use of the built-in memory manager #if !defined(NO_MEMMGR) && !defined(USE_MEMMGR) +#if defined(MEMWATCH) || defined(DMALLOC) || defined(GCOLLECT) +// disable built-in memory manager when using another memory library +#define NO_MEMMGR +#else +// use built-in memory manager by default #define USE_MEMMGR #endif +#endif ////////////////////////////////////////////////////////////////////// @@ -35,10 +28,13 @@ // Enable memory manager logging by default #define LOG_MEMMGR +// no logging for minicore +#if defined(MINICORE) && defined(LOG_MEMMGR) +#undef LOG_MEMMGR +#endif + # define aMalloc(n) _mmalloc(n,ALC_MARK) -# define aMallocA(n) _mmalloc(n,ALC_MARK) # define aCalloc(m,n) _mcalloc(m,n,ALC_MARK) -# define aCallocA(m,n) _mcalloc(m,n,ALC_MARK) # define aRealloc(p,n) _mrealloc(p,n,ALC_MARK) # define aStrdup(p) _mstrdup(p,ALC_MARK) # define aFree(p) _mfree(p,ALC_MARK) @@ -52,83 +48,23 @@ #else # define aMalloc(n) aMalloc_((n),ALC_MARK) -# define aMallocA(n) aMallocA_((n),ALC_MARK) # define aCalloc(m,n) aCalloc_((m),(n),ALC_MARK) -# define aCallocA(m,n) aCallocA_(m,n,ALC_MARK) # define aRealloc(p,n) aRealloc_(p,n,ALC_MARK) # define aStrdup(p) aStrdup_(p,ALC_MARK) # define aFree(p) aFree_(p,ALC_MARK) void* aMalloc_ (size_t size, const char *file, int line, const char *func); - void* aMallocA_ (size_t size, const char *file, int line, const char *func); void* aCalloc_ (size_t num, size_t size, const char *file, int line, const char *func); - void* aCallocA_ (size_t num, size_t size, const char *file, int line, const char *func); void* aRealloc_ (void *p, size_t size, const char *file, int line, const char *func); char* aStrdup_ (const char *p, const char *file, int line, const char *func); void aFree_ (void *p, const char *file, int line, const char *func); #endif -////////////// Memory Managers ////////////////// - -#if defined(MEMWATCH) - -# include "memwatch.h" -# define MALLOC(n,file,line,func) mwMalloc((n),(file),(line)) -# define MALLOCA(n,file,line,func) mwMalloc((n),(file),(line)) -# define CALLOC(m,n,file,line,func) mwCalloc((m),(n),(file),(line)) -# define CALLOCA(m,n,file,line,func) mwCalloc((m),(n),(file),(line)) -# define REALLOC(p,n,file,line,func) mwRealloc((p),(n),(file),(line)) -# define STRDUP(p,file,line,func) mwStrdup((p),(file),(line)) -# define FREE(p,file,line,func) mwFree((p),(file),(line)) - -#elif defined(DMALLOC) - -# include "dmalloc.h" -# define MALLOC(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0) -# define MALLOCA(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0) -# define CALLOC(m,n,file,line,func) dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0) -# define CALLOCA(m,n,file,line,func) dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0) -# define REALLOC(p,n,file,line,func) dmalloc_realloc((file),(line),(p),(n),DMALLOC_FUNC_REALLOC,0) -# define STRDUP(p,file,line,func) strdup(p) -# define FREE(p,file,line,func) free(p) - -#elif defined(GCOLLECT) - -# include "gc.h" -# define MALLOC(n,file,line,func) GC_MALLOC(n) -# define MALLOCA(n,file,line,func) GC_MALLOC_ATOMIC(n) -# define CALLOC(m,n,file,line,func) _bcalloc((m),(n)) -# define CALLOCA(m,n,file,line,func) _bcallocA((m),(n)) -# define REALLOC(p,n,file,line,func) GC_REALLOC((p),(n)) -# define STRDUP(p,file,line,func) _bstrdup(p) -# define FREE(p,file,line,func) GC_FREE(p) - - void * _bcalloc(size_t, size_t); - void * _bcallocA(size_t, size_t); - char * _bstrdup(const char *); - -#elif defined(BCHECK) - -# define MALLOC(n,file,line,func) malloc(n) -# define MALLOCA(n,file,line,func) malloc(n) -# define CALLOC(m,n,file,line,func) calloc((m),(n)) -# define CALLOCA(m,n,file,line,func) calloc((m),(n)) -# define REALLOC(p,n,file,line,func) realloc((p),(n)) -# define STRDUP(p,file,line,func) strdup(p) -# define FREE(p,file,line,func) free(p) - -#else - -# define MALLOC(n,file,line,func) malloc(n) -# define MALLOCA(n,file,line,func) malloc(n) -# define CALLOC(m,n,file,line,func) calloc((m),(n)) -# define CALLOCA(m,n,file,line,func) calloc((m),(n)) -# define REALLOC(p,n,file,line,func) realloc((p),(n)) -# define STRDUP(p,file,line,func) strdup(p) -# define FREE(p,file,line,func) free(p) - -#endif +// deprecated, do not use +#define aMallocA aMalloc +#define aCallocA aCalloc +#define CREATE_A CREATE /////////////// Buffer Creation ///////////////// // Full credit for this goes to Shinomori [Ajarn] @@ -148,14 +84,12 @@ ////////////// Others ////////////////////////// // should be merged with any of above later #define CREATE(result, type, number) (result) = (type *) aCalloc ((number), sizeof(type)) - -#define CREATE_A(result, type, number) (result) = (type *) aCallocA ((number), sizeof(type)) - #define RECREATE(result, type, number) (result) = (type *) aRealloc ((result), sizeof(type) * (number)) //////////////////////////////////////////////// -bool malloc_verify(void* ptr); +void malloc_memory_check(void); +bool malloc_verify_ptr(void* ptr); size_t malloc_usage (void); void malloc_init (void); void malloc_final (void); diff --git a/src/common/plugin.h b/src/common/plugin.h index a367d2537..ec6399c57 100644 --- a/src/common/plugin.h +++ b/src/common/plugin.h @@ -50,7 +50,7 @@ typedef void Plugin_Event_Func(void); #define PLUGIN_MAP 8 #define PLUGIN_CORE 16 -#define IMPORT_SYMBOL(s,n) (s) = plugin_call_table[n] +#define IMPORT_SYMBOL(s,n) SET_FUNCPOINTER((s), plugin_call_table[n]) #define SYMBOL_SERVER_TYPE 0 #define SYMBOL_SERVER_NAME 1 diff --git a/src/common/socket.c b/src/common/socket.c index 81ea19468..b522ac607 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -32,6 +32,9 @@ #ifndef SIOCGIFCONF #include <sys/sockio.h> // SIOCGIFCONF on Solaris, maybe others? [Shinomori] #endif + #ifndef FIONBIO + #include <sys/filio.h> // FIONBIO on Solaris [FlavioJS] + #endif #ifdef HAVE_SETRLIMIT #include <sys/resource.h> @@ -1245,16 +1248,23 @@ void socket_init(void) rlp.rlim_cur = FD_SETSIZE; if( 0 != setrlimit(RLIMIT_NOFILE, &rlp) ) {// failed, try setting the maximum too (permission to change system limits is required) + int err; rlp.rlim_max = FD_SETSIZE; - if( 0 != setrlimit(RLIMIT_NOFILE, &rlp) ) + err = setrlimit(RLIMIT_NOFILE, &rlp); + if( err != 0 ) {// failed + const char* errmsg = "unknown"; + int rlim_ori; // set to maximum allowed getrlimit(RLIMIT_NOFILE, &rlp); + rlim_ori = (int)rlp.rlim_cur; rlp.rlim_cur = rlp.rlim_max; setrlimit(RLIMIT_NOFILE, &rlp); // report limit getrlimit(RLIMIT_NOFILE, &rlp); - ShowWarning("socket_init: failed to set socket limit to %d (current limit %d).\n", FD_SETSIZE, (int)rlp.rlim_cur); + if( err == EPERM ) + errmsg = "permission denied"; + ShowWarning("socket_init: failed to set socket limit to %d, setting to maximum allowed (original limit=%d, current limit=%d, maximum allowed=%d, error=%s).\n", FD_SETSIZE, rlim_ori, (int)rlp.rlim_cur, (int)rlp.rlim_max, errmsg); } } } @@ -1343,6 +1353,12 @@ void send_shortlist_add_fd(int fd) if( (send_shortlist_set[i]>>bit)&1 ) return;// already in the list + if( send_shortlist_count >= ARRAYLENGTH(send_shortlist_array) ) + { + ShowDebug("send_shortlist_add_fd: shortlist is full, ignoring... (fd=%d shortlist.count=%d shortlist.length=%d)\n", fd, send_shortlist_count, ARRAYLENGTH(send_shortlist_array)); + return; + } + // set the bit send_shortlist_set[i] |= 1<<bit; // Add to the end of the shortlist array. @@ -1352,12 +1368,30 @@ void send_shortlist_add_fd(int fd) // Do pending network sends and eof handling from the shortlist. void send_shortlist_do_sends() { - int i = 0; + int i; - while( i < send_shortlist_count ) + for( i = send_shortlist_count-1; i >= 0; --i ) { int fd = send_shortlist_array[i]; + int idx = fd/32; + int bit = fd%32; + // Remove fd from shortlist, move the last fd to the current position + --send_shortlist_count; + send_shortlist_array[i] = send_shortlist_array[send_shortlist_count]; + send_shortlist_array[send_shortlist_count] = 0; + + if( fd <= 0 || fd >= FD_SETSIZE ) + { + ShowDebug("send_shortlist_do_sends: fd is out of range, corrupted memory? (fd=%d)\n", fd); + continue; + } + if( ((send_shortlist_set[idx]>>bit)&1) == 0 ) + { + ShowDebug("send_shortlist_do_sends: fd is not set, why is it in the shortlist? (fd=%d)\n", fd); + continue; + } + send_shortlist_set[idx]&=~(1<<bit);// unset fd // If this session still exists, perform send operations on it and // check for the eof state. if( session[fd] ) @@ -1372,17 +1406,10 @@ void send_shortlist_do_sends() session[fd]->func_parse(fd); // If the session still exists, is not eof and has things left to - // be sent from it we'll keep it in the shortlist. + // be sent from it we'll re-add it to the shortlist. if( session[fd] && !session[fd]->flag.eof && session[fd]->wdata_size ) - { - ++i; - continue; - } + send_shortlist_add_fd(fd); } - - // Remove fd from shortlist, move the last fd to the current position - send_shortlist_array[i] = send_shortlist_array[--send_shortlist_count]; - send_shortlist_set[fd/32]&=~(1<<(fd%32)); } } #endif diff --git a/src/common/strlib.c b/src/common/strlib.c index 097f499e6..7f79a5ef0 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -245,7 +245,7 @@ char* _strtok_r(char *s1, const char *s2, char **lasts) If no '\0' terminator is found in that many characters, return MAXLEN. */ size_t strnlen (const char* string, size_t maxlen) { - const char* end = memchr (string, '\0', maxlen); + const char* end = (const char*)memchr(string, '\0', maxlen); return end ? (size_t) (end - string) : maxlen; } #endif @@ -980,7 +980,7 @@ bool sv_readdb(const char* directory, const char* filename, char delim, int minc // allocate enough memory for the maximum requested amount of columns plus the reserved one fields_length = maxcols+1; - fields = aMalloc(fields_length*sizeof(char*)); + fields = (char**)aMalloc(fields_length*sizeof(char*)); // process rows one by one while( fgets(line, sizeof(line), fp) ) |