From 5cf0426d93179f6ffd90e201f8123c20c1041ce5 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Thu, 11 Nov 1999 00:30:38 +0000 Subject: [PATCH] 19991111-01:18 GMT+1 Victor Szel --- harbour/ChangeLog | 30 +++++++++++ harbour/include/hbsetup.h | 11 ++++ harbour/include/hbtrace.h | 30 +++++------ harbour/source/rtl/fm.c | 104 ++++++++++++++++++++++--------------- harbour/source/vm/dynsym.c | 28 +++------- harbour/source/vm/hvm.c | 4 +- 6 files changed, 127 insertions(+), 80 deletions(-) diff --git a/harbour/ChangeLog b/harbour/ChangeLog index 28438b1d7b..7698af7fcc 100644 --- a/harbour/ChangeLog +++ b/harbour/ChangeLog @@ -1,3 +1,33 @@ +19991111-01:18 GMT+1 Victor Szel + * source/rtl/fm.c + ! hb_xrealloc() check for a passed NULL pointer. + ! hb_xrealloc() signature check moved before any MEMINFO usage. + ! Explicit \n changed to hb_consoleGetNewLine() call. + ! 256 buffer size for symbol names changed to HB_SYMBOL_NAME_LEN constant. + ! Now the hb_xrealloc() is returning a NULL pointer (instead of trash) + when the passed length is zero, and it's also freeing the pointer with + free(). + % hb_xrealloc() - pResult variable optimized out. + * source/rtl/hbsetup.h + + HB_SYMBOL_NAME_LEN constant added to set the maximum handled symbol + name length in Harbour (both compiler/runtime), this can be set to any + value by specifying C_USR=-DHB_SYMBOL_NAME_LEN=10 in the make process. + TODO: Add support for the compiler to cut the symbols to this size on + compile. This way the /10 switch may be replaced by this #define, + since /10 works perfect when the RTL also "knows" about this limit, + and is compiled using this limit. + * include/hbtrace.h + ! Every macro names prefixed with HB_ + * source/vm/hvm.c + ! Explicit buffer sizes are now determined using the HB_SYMBOL_NAME_LEN + constant. They can't overrun anymore. + * source/vm/dynsym.c + % hb_dynsymGet(), hb_dynsymFindName() - Using local stack for temp symbol + name buffer instead of hb_xgrab()/hb_xfree(). Some optimalizations. + Now they will cut the symbol name to the HB_SYMBOL_NAME_LEN size, so + by setting this to 10, we're 100% Cl*pper compatible from this respect. + These functions are now faster. + 19991110-23:20 GMT+1 Victor Szel * include/hbtrace.h % Excluded trace calls are converted to nohting instead of "do {} while 0" diff --git a/harbour/include/hbsetup.h b/harbour/include/hbsetup.h index 764fd1c770..4e1776fe63 100644 --- a/harbour/include/hbsetup.h +++ b/harbour/include/hbsetup.h @@ -157,6 +157,17 @@ #define HARBOUR_USE_GTAPI #endif +/* *********************************************************************** + * You can set here the maximum symbol name length handled by Harbour + * compiler and runtime. You can override this setting in the make process. + * + * By default this value is 128 +*/ + +#ifndef HB_SYMBOL_NAME_LEN + #define HB_SYMBOL_NAME_LEN 128 +#endif + /* *********************************************************************** * Operating system specific definitions */ diff --git a/harbour/include/hbtrace.h b/harbour/include/hbtrace.h index a830dc7fec..d8524501ea 100644 --- a/harbour/include/hbtrace.h +++ b/harbour/include/hbtrace.h @@ -63,11 +63,11 @@ /* * This is black magic... - * What we do here is to generate calls to ECHO_CREATE only for those + * What we do here is to generate calls to HB_ECHO_CREATE only for those * levels that are less or equal to the COMPILATION time HB_TR_LEVEL. */ -#define ECHO_CREATE(l, x) do \ +#define HB_ECHO_CREATE(l, x) do \ { \ if (hb_tr_level() >= l) { \ hb_tr_file_ = __FILE__; \ @@ -78,43 +78,43 @@ } while (0) #if HB_TR_LEVEL >= HB_TR_DEBUG -#define _ECHO_TRACE_HB_TR_DEBUG(x) ECHO_CREATE(HB_TR_DEBUG, x) +#define HB_ECHO_TRACE_HB_TR_DEBUG(x) HB_ECHO_CREATE(HB_TR_DEBUG, x) #else -#define _ECHO_TRACE_HB_TR_DEBUG(x) +#define HB_ECHO_TRACE_HB_TR_DEBUG(x) #endif #if HB_TR_LEVEL >= HB_TR_INFO -#define _ECHO_TRACE_HB_TR_INFO(x) ECHO_CREATE(HB_TR_INFO, x) +#define HB_ECHO_TRACE_HB_TR_INFO(x) HB_ECHO_CREATE(HB_TR_INFO, x) #else -#define _ECHO_TRACE_HB_TR_INFO(x) +#define HB_ECHO_TRACE_HB_TR_INFO(x) #endif #if HB_TR_LEVEL >= HB_TR_WARNING -#define _ECHO_TRACE_HB_TR_WARNING(x) ECHO_CREATE(HB_TR_WARNING, x) +#define HB_ECHO_TRACE_HB_TR_WARNING(x) HB_ECHO_CREATE(HB_TR_WARNING, x) #else -#define _ECHO_TRACE_HB_TR_WARNING(x) +#define HB_ECHO_TRACE_HB_TR_WARNING(x) #endif #if HB_TR_LEVEL >= HB_TR_ERROR -#define _ECHO_TRACE_HB_TR_ERROR(x) ECHO_CREATE(HB_TR_ERROR, x) +#define HB_ECHO_TRACE_HB_TR_ERROR(x) HB_ECHO_CREATE(HB_TR_ERROR, x) #else -#define _ECHO_TRACE_HB_TR_ERROR(x) +#define HB_ECHO_TRACE_HB_TR_ERROR(x) #endif #if HB_TR_LEVEL >= HB_TR_FATAL -#define _ECHO_TRACE_HB_TR_FATAL(x) ECHO_CREATE(HB_TR_FATAL, x) +#define HB_ECHO_TRACE_HB_TR_FATAL(x) HB_ECHO_CREATE(HB_TR_FATAL, x) #else -#define _ECHO_TRACE_HB_TR_FATAL(x) +#define HB_ECHO_TRACE_HB_TR_FATAL(x) #endif #if 1 /* always! */ -#define _ECHO_TRACE_HB_TR_ALWAYS(x) ECHO_CREATE(HB_TR_ALWAYS, x) +#define HB_ECHO_TRACE_HB_TR_ALWAYS(x) HB_ECHO_CREATE(HB_TR_ALWAYS, x) #else -#define _ECHO_TRACE_HB_TR_ALWAYS(x) +#define HB_ECHO_TRACE_HB_TR_ALWAYS(x) #endif -#define HB_TRACE(l, x) _ECHO_TRACE_##l(x) +#define HB_TRACE(l, x) HB_ECHO_TRACE_##l(x) diff --git a/harbour/source/rtl/fm.c b/harbour/source/rtl/fm.c index 7bf24cbc97..56db91de47 100644 --- a/harbour/source/rtl/fm.c +++ b/harbour/source/rtl/fm.c @@ -49,6 +49,10 @@ * */ +/* TODO: hb_xrealloc() is not completely consistent now, since it will + destroy the pointer when called with a zero parameter, and return NULL. + But will not accept NULL pointer as a parameter. [vszel] */ + /* NOTE: If you turn this on, the memory subsystem will collect information about several statistical data about memory management, it will show these on exit if memory seem to have leaked. @@ -76,7 +80,7 @@ typedef struct _HB_MEMINFO ULONG ulSignature; ULONG ulSize; USHORT uiProcLine; - char szProcName[ 256 ]; + char szProcName[ HB_SYMBOL_NAME_LEN + 1 ]; struct _HB_MEMINFO * pPrevBlock; struct _HB_MEMINFO * pNextBlock; } HB_MEMINFO, * PHB_MEMINFO; @@ -234,61 +238,75 @@ void * hb_xrealloc( void * pMem, ULONG ulSize ) /* reallocates memory */ PHB_MEMINFO pMemBlock; ULONG ulMemSize; - void * pResult; HB_TRACE(HB_TR_DEBUG, ("hb_xrealloc(%p, %lu)", pMem, ulSize)); + if( ! pMem ) + hb_errInternal( 9999, "hb_xrealloc called with a NULL pointer", NULL, NULL ); + pMemBlock = ( PHB_MEMINFO ) ( ( char * ) pMem - sizeof( HB_MEMINFO ) ); + if( pMemBlock->ulSignature != HB_MEMINFO_SIGNATURE ) + hb_errInternal( 9999, "hb_xrealloc called with a bad pointer", NULL, NULL ); + ulMemSize = pMemBlock->ulSize; if( ulSize == 0 ) + { DeleteNode( pMemBlock ); + free( ( void * ) pMem ); + pMem = NULL; + + s_lMemoryBlocks--; + } else { - if( pMemBlock->ulSignature != HB_MEMINFO_SIGNATURE ) - hb_errInternal( 9999, "hb_xrealloc called with a bad pointer", NULL, NULL ); + pMem = realloc( pMemBlock, ulSize + sizeof( HB_MEMINFO ) ); - pResult = realloc( pMemBlock, ulSize + sizeof( HB_MEMINFO ) ); - - if( pResult ) + if( pMem ) { - ( ( PHB_MEMINFO ) pResult )->pPrevBlock = pMemBlock->pPrevBlock; - ( ( PHB_MEMINFO ) pResult )->pNextBlock = pMemBlock->pNextBlock; + ( ( PHB_MEMINFO ) pMem )->pPrevBlock = pMemBlock->pPrevBlock; + ( ( PHB_MEMINFO ) pMem )->pNextBlock = pMemBlock->pNextBlock; if( pMemBlock->pPrevBlock ) - ( ( PHB_MEMINFO ) pResult )->pPrevBlock->pNextBlock = ( PHB_MEMINFO ) pResult; - if( ( ( PHB_MEMINFO ) pResult )->pNextBlock ) - ( ( PHB_MEMINFO ) pResult )->pNextBlock->pPrevBlock = ( PHB_MEMINFO ) pResult; + ( ( PHB_MEMINFO ) pMem )->pPrevBlock->pNextBlock = ( PHB_MEMINFO ) pMem; + if( ( ( PHB_MEMINFO ) pMem )->pNextBlock ) + ( ( PHB_MEMINFO ) pMem )->pNextBlock->pPrevBlock = ( PHB_MEMINFO ) pMem; } } - if( ! pResult ) - hb_errInternal( 9999, "hb_xrealloc can't reallocate memory", NULL, NULL ); - - ( ( PHB_MEMINFO ) pResult )->ulSignature = HB_MEMINFO_SIGNATURE; - ( ( PHB_MEMINFO ) pResult )->ulSize = ulSize; /* size of the memory block */ - - if( ! ulSize ) - s_lMemoryBlocks--; - s_lMemoryConsumed += ( ulSize - ulMemSize ); if( s_lMemoryMaxConsumed < s_lMemoryConsumed ) s_lMemoryMaxConsumed = s_lMemoryConsumed; - return ( char * ) pResult + sizeof( HB_MEMINFO ); + if( ! pMem ) + hb_errInternal( 9999, "hb_xrealloc can't reallocate memory", NULL, NULL ); + + ( ( PHB_MEMINFO ) pMem )->ulSignature = HB_MEMINFO_SIGNATURE; + ( ( PHB_MEMINFO ) pMem )->ulSize = ulSize; /* size of the memory block */ + + return ( char * ) pMem + sizeof( HB_MEMINFO ); #else - void * pResult; - HB_TRACE(HB_TR_DEBUG, ("hb_xrealloc(%p, %lu)", pMem, ulSize)); - pResult = realloc( ( char * ) pMem, ulSize ); + if( ! pMem ) + hb_errInternal( 9999, "hb_xrealloc called with a NULL pointer", NULL, NULL ); - if( ! pResult ) - hb_errInternal( 9999, "hb_xrealloc can't reallocate memory", NULL, NULL ); + if( ulSize == 0 ) + { + free( ( void * ) pMem ); + pMem = NULL; + } + else + { + pMem = realloc( ( char * ) pMem, ulSize ); - return ( char * ) pResult; + if( ! pMem ) + hb_errInternal( 9999, "hb_xrealloc can't reallocate memory", NULL, NULL ); + } + + return ( char * ) pMem; #endif } @@ -316,7 +334,7 @@ void hb_xfree( void * pMem ) /* frees fixed memory */ free( ( void * ) pMemBlock ); } else - hb_errInternal( 9999, "hb_xfree called with a null pointer", NULL, NULL ); + hb_errInternal( 9999, "hb_xfree called with a NULL pointer", NULL, NULL ); #else @@ -327,7 +345,7 @@ void hb_xfree( void * pMem ) /* frees fixed memory */ free( ( char * ) pMem - sizeof( ULONG ) - sizeof( ULONG ) ); } else - hb_errInternal( 9999, "hb_xfree called with a null pointer", NULL, NULL ); + hb_errInternal( 9999, "hb_xfree called with a NULL pointer", NULL, NULL ); #endif } @@ -358,29 +376,33 @@ void hb_xexit( void ) /* Deinitialize fixed memory subsystem */ #ifdef HB_FM_STATISTICS if( s_lMemoryBlocks || hb_cmdargCheck( "INFO" ) ) { - char buffer[ 100 ]; - - hb_outerr( hb_consoleGetNewLine(), 0 ); - hb_outerr( "----------------------------------------", 0 ); - hb_outerr( hb_consoleGetNewLine(), 0 ); - sprintf( buffer, "Total memory allocated: %li bytes (%li blocks)", s_lMemoryMaxConsumed, s_lMemoryMaxBlocks ); - hb_outerr( buffer, 0 ); - - if( s_lMemoryBlocks ) + /* NOTE: Put this in a separate block to optimize stack space usage [vszel] */ { + char buffer[ 100 ]; + hb_outerr( hb_consoleGetNewLine(), 0 ); - sprintf( buffer, "WARNING! Memory allocated but not released: %li bytes (%li blocks)", s_lMemoryConsumed, s_lMemoryBlocks ); + hb_outerr( "----------------------------------------", 0 ); + hb_outerr( hb_consoleGetNewLine(), 0 ); + sprintf( buffer, "Total memory allocated: %li bytes (%li blocks)", s_lMemoryMaxConsumed, s_lMemoryMaxBlocks ); hb_outerr( buffer, 0 ); + + if( s_lMemoryBlocks ) + { + hb_outerr( hb_consoleGetNewLine(), 0 ); + sprintf( buffer, "WARNING! Memory allocated but not released: %li bytes (%li blocks)", s_lMemoryConsumed, s_lMemoryBlocks ); + hb_outerr( buffer, 0 ); + } } if( s_pFirstBlock ) { + char buffer[ HB_SYMBOL_NAME_LEN + 40 ]; PHB_MEMINFO pMemBlock = s_pFirstBlock; USHORT ui = 1; do { - sprintf( buffer, "\nblock %i: %s line %i", ui++, pMemBlock->szProcName, + sprintf( buffer, "%sblock %i: %s line %i", hb_consoleGetNewLine(), ui++, pMemBlock->szProcName, pMemBlock->uiProcLine ); HB_TRACE( HB_TR_ERROR, (buffer)); pMemBlock = pMemBlock->pNextBlock; diff --git a/harbour/source/vm/dynsym.c b/harbour/source/vm/dynsym.c index e951300fc9..825bc0c05a 100644 --- a/harbour/source/vm/dynsym.c +++ b/harbour/source/vm/dynsym.c @@ -131,43 +131,27 @@ PHB_DYNS hb_dynsymNew( PHB_SYMB pSymbol ) /* creates a new dynamic symbol */ PHB_DYNS hb_dynsymGet( char * szName ) /* finds and creates a symbol if not found */ { PHB_DYNS pDynSym; - char * szUprName; + char szUprName[ HB_SYMBOL_NAME_LEN + 1 ]; HB_TRACE(HB_TR_DEBUG, ("hb_dynsymGet(%s)", szName)); - szUprName = ( char * ) hb_xgrab( strlen( szName ) + 1 ); - strcpy( szUprName, szName ); /* make a copy as we may get a const string */ - hb_strupr( szUprName ); /* turn it uppercase */ - - /* if( strlen( szUprName ) > 10 ) - szUprName[ 10 ] = '\0'; keeps this here for 10 chars /c compatibility mode */ - - pDynSym = hb_dynsymFind( szUprName ); + /* make a copy as we may get a const string, the turn it to uppercase */ + pDynSym = hb_dynsymFind( hb_strncpyUpper( szUprName, szName, HB_SYMBOL_NAME_LEN ) ); if( ! pDynSym ) /* Does it exists ? */ pDynSym = hb_dynsymNew( hb_symbolNew( szUprName ) ); /* Make new symbol */ - hb_xfree( szUprName ); /* release memory */ - return pDynSym; } PHB_DYNS hb_dynsymFindName( char * szName ) /* finds a symbol */ { PHB_DYNS pDynSym; - ULONG ulLen; - char * szUprName; + char szUprName[ HB_SYMBOL_NAME_LEN + 1 ]; HB_TRACE(HB_TR_DEBUG, ("hb_dynsymFindName(%s)", szName)); - ulLen = strlen( szName ); - szUprName = ( char * ) hb_xgrab( ulLen + 1 ); - hb_strncpyUpper( szUprName, szName, ulLen ); /* make a copy as we may get a const string */ - - /* if( strlen( szUprName ) > 10 ) - szUprName[ 10 ] = '\0'; keeps this here for 10 chars /c compatibility mode */ - - pDynSym = hb_dynsymFind( szUprName ); - hb_xfree( szUprName ); /* release memory */ + /* make a copy as we may get a const string */ + pDynSym = hb_dynsymFind( hb_strncpyUpper( szUprName, szName, HB_SYMBOL_NAME_LEN ) ); return pDynSym; } diff --git a/harbour/source/vm/hvm.c b/harbour/source/vm/hvm.c index 0d7e9381e6..4b1cd6649f 100644 --- a/harbour/source/vm/hvm.c +++ b/harbour/source/vm/hvm.c @@ -3129,7 +3129,7 @@ void hb_stackDispCall( void ) while( pBase != hb_stack.pItems ) { - char buffer[ 128 ]; + char buffer[ HB_SYMBOL_NAME_LEN + HB_SYMBOL_NAME_LEN + 32 ]; pBase = hb_stack.pItems + pBase->item.asSymbol.stackbase; @@ -3661,7 +3661,7 @@ void hb_vmRequestCancel( void ) if( hb_set.HB_SET_CANCEL ) { - char buffer[ 128 ]; + char buffer[ HB_SYMBOL_NAME_LEN + 32 ]; hb_outerr( hb_consoleGetNewLine(), 0 ); sprintf( buffer, "Cancelled at: %s (%i)", hb_stack.pBase->item.asSymbol.value->szName, hb_stack.pBase->item.asSymbol.lineno );