Changeset 1407 for trunk/psLib/src/sysUtils
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- Location:
- trunk/psLib/src/sysUtils
- Files:
-
- 15 edited
-
psAbort.c (modified) (10 diffs)
-
psAbort.h (modified) (4 diffs)
-
psError.c (modified) (10 diffs)
-
psError.h (modified) (4 diffs)
-
psHash.c (modified) (20 diffs)
-
psHash.h (modified) (2 diffs)
-
psLogMsg.c (modified) (18 diffs)
-
psLogMsg.h (modified) (8 diffs)
-
psMemory.c (modified) (26 diffs)
-
psMemory.h (modified) (20 diffs)
-
psString.c (modified) (11 diffs)
-
psString.h (modified) (6 diffs)
-
psTrace.c (modified) (25 diffs)
-
psTrace.h (modified) (3 diffs)
-
psType.h (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psAbort.c
r1406 r1407 1 1 2 /** @file psAbort.c 2 3 * … … 9 10 * @author Eric Van Alst, MHPCC 10 11 * 11 * @version $Revision: 1. 7$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-08-0 6 22:34:05$12 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-08-07 00:06:06 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 16 17 17 18 /******************************************************************************/ 19 18 20 /* INCLUDE FILES */ 21 19 22 /******************************************************************************/ 20 23 #include <stdarg.h> … … 24 27 25 28 /******************************************************************************/ 29 26 30 /* DEFINE STATEMENTS */ 31 27 32 /******************************************************************************/ 28 33 … … 30 35 31 36 /******************************************************************************/ 37 32 38 /* TYPE DEFINITIONS */ 39 33 40 /******************************************************************************/ 34 41 … … 36 43 37 44 /*****************************************************************************/ 45 38 46 /* GLOBAL VARIABLES */ 47 39 48 /*****************************************************************************/ 40 49 … … 42 51 43 52 /*****************************************************************************/ 53 44 54 /* FILE STATIC VARIABLES */ 55 45 56 /*****************************************************************************/ 46 57 … … 48 59 49 60 /*****************************************************************************/ 61 50 62 /* FUNCTION IMPLEMENTATION - LOCAL */ 63 51 64 /*****************************************************************************/ 52 65 … … 54 67 55 68 /*****************************************************************************/ 69 56 70 /* FUNCTION IMPLEMENTATION - PUBLIC */ 71 57 72 /*****************************************************************************/ 58 73 59 void psAbort(const char *name, const char *fmt, ...)74 void psAbort(const char *name, const char *fmt, ...) 60 75 { 61 va_list argPtr;// variable list arguement pointer76 va_list argPtr; // variable list arguement pointer 62 77 63 78 // Get the variable list parameters to pass to logging function … … 73 88 abort(); 74 89 } 75 -
trunk/psLib/src/sysUtils/psAbort.h
r974 r1407 1 1 2 /** @file psAbort.h 2 3 * … … 11 12 * @author Eric Van Alst, MHPCC 12 13 * 13 * @version $Revision: 1. 4$ $Name: not supported by cvs2svn $14 * @date $Date: 2004-0 6-10 01:58:06 $14 * @version $Revision: 1.5 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-08-07 00:06:06 $ 15 16 * 16 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 18 19 19 20 #ifndef PS_ABORT_H 20 #define PS_ABORT_H 21 21 # define PS_ABORT_H 22 22 23 23 // Doxygen grouping tags 24 24 25 /** @addtogroup ErrorHandling 25 26 * @{ … … 36 37 * 37 38 */ 38 void psAbort( 39 const char* name, ///< Source of abort such as file or function detected 40 const char* fmt, ///< A printf style formatting statement defining msg 41 ... 42 ); 39 void psAbort(const char *name, // /< Source of abort such as file or function detected 40 const char *fmt, // /< A printf style formatting statement defining msg 41 ... 42 ); 43 43 44 /* @} */ // Doxygen - End of SystemGroup Functions44 /* @} */// Doxygen - End of SystemGroup Functions 45 45 46 46 #endif 47 -
trunk/psLib/src/sysUtils/psError.c
r1406 r1407 1 1 2 /** @file psError.c 2 3 * … … 10 11 * @author Eric Van Alst, MHPCC 11 12 * 12 * @version $Revision: 1. 9$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-08-0 6 22:34:05$13 * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-08-07 00:06:06 $ 14 15 * 15 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 17 18 18 19 /******************************************************************************/ 20 19 21 /* INCLUDE FILES */ 22 20 23 /******************************************************************************/ 21 24 #include <stdarg.h> … … 24 27 25 28 /******************************************************************************/ 29 26 30 /* DEFINE STATEMENTS */ 31 27 32 /******************************************************************************/ 28 33 … … 30 35 31 36 /******************************************************************************/ 37 32 38 /* TYPE DEFINITIONS */ 39 33 40 /******************************************************************************/ 34 41 … … 36 43 37 44 /*****************************************************************************/ 45 38 46 /* GLOBAL VARIABLES */ 47 39 48 /*****************************************************************************/ 40 49 … … 42 51 43 52 /*****************************************************************************/ 53 44 54 /* FILE STATIC VARIABLES */ 55 45 56 /*****************************************************************************/ 46 57 … … 48 59 49 60 /*****************************************************************************/ 61 50 62 /* FUNCTION IMPLEMENTATIONS - LOCAL */ 63 51 64 /*****************************************************************************/ 52 65 … … 54 67 55 68 /*****************************************************************************/ 69 56 70 /* FUNCTION IMPLEMENTATIONS - PUBLIC */ 71 57 72 /*****************************************************************************/ 58 73 59 void psError(const char *name, const char *fmt, ...)74 void psError(const char *name, const char *fmt, ...) 60 75 { 61 va_list argPtr;// variable list arguement pointer76 va_list argPtr; // variable list arguement pointer 62 77 63 // Get the variable list parameters to pass to logging function78 // Get the variable list parameters to pass to logging function 64 79 va_start(argPtr, fmt); 65 80 … … 70 85 va_end(argPtr); 71 86 } 72 -
trunk/psLib/src/sysUtils/psError.h
r974 r1407 1 1 2 /** @file psError.h 2 3 * … … 12 13 * @author Eric Van Alst, MHPCC 13 14 * 14 * @version $Revision: 1. 4$ $Name: not supported by cvs2svn $15 * @date $Date: 2004-0 6-10 01:58:06 $15 * @version $Revision: 1.5 $ $Name: not supported by cvs2svn $ 16 * @date $Date: 2004-08-07 00:06:06 $ 16 17 * 17 18 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 19 20 20 21 #ifndef PS_ERROR_H 21 # define PS_ERROR_H22 # define PS_ERROR_H 22 23 23 24 /** @addtogroup ErrorHandling … … 34 35 * 35 36 */ 36 void psError( 37 const char *name, ///< Source of error such as file or function detected 38 const char *fmt, ///< A printf style formatting statement defining msg 39 ... 40 ); 37 void psError(const char *name, // /< Source of error such as file or function detected 38 const char *fmt, // /< A printf style formatting statement defining msg 39 ... 40 ); 41 41 42 /* @} */ // End of SysUtils Functions42 /* @} */// End of SysUtils Functions 43 43 44 44 #endif 45 -
trunk/psLib/src/sysUtils/psHash.c
r1406 r1407 1 1 2 /** @file psHash.c 2 3 * … … 10 11 * @author George Gusciora, MHPCC 11 12 * 12 * @version $Revision: 1.1 5$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-08-0 6 22:34:05$13 * @version $Revision: 1.16 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-08-07 00:06:06 $ 14 15 * 15 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 25 26 #include "psAbort.h" 26 27 27 static psHashBucket *hashBucketAlloc( const char *key, void *data, psHashBucket *next ); 28 static void hashBucketFree( psHashBucket *bucket ); 29 static void *doHashWork( psHash* table, const char* key, void* data, bool remove 30 ) 31 ; 32 static void hashFree( psHash *table ); 28 static psHashBucket *hashBucketAlloc(const char *key, void *data, psHashBucket * next); 29 static void hashBucketFree(psHashBucket * bucket); 30 static void *doHashWork(psHash * table, const char *key, void *data, bool remove 31 ); 32 static void hashFree(psHash * table); 33 33 34 34 /****************************************************************************** … … 40 40 The linked list 41 41 *****************************************************************************/ 42 psList *psHashKeyList( psHash *table)43 { 44 int i = 0; // Loop index variable42 psList *psHashKeyList(psHash * table) 43 { 44 int i = 0; // Loop index variable 45 45 psList *myLinkList = NULL; // The output data structure 46 psHashBucket *ptr = NULL; // Used to step thru linked list.47 48 if ( table == NULL) {46 psHashBucket *ptr = NULL; // Used to step thru linked list. 47 48 if (table == NULL) { 49 49 return NULL; 50 50 } 51 52 51 // Create the linked list 53 myLinkList = psListAlloc( NULL);52 myLinkList = psListAlloc(NULL); 54 53 55 54 // Loop through every bucket in the hash table. If that bucket is not 56 55 // NULL, then add the bucket's key to the linked list. 57 for ( i = 0;i < table->nbucket;i++) {58 if ( table->buckets[ i ] != NULL) {56 for (i = 0; i < table->nbucket; i++) { 57 if (table->buckets[i] != NULL) { 59 58 // Since a bucket contains a linked list of keys/data, we must 60 59 // step trough each key in that linked list: 61 60 62 ptr = table->buckets[ i];63 while ( ptr != NULL) {64 psListAdd( myLinkList, ptr->key, PS_LIST_HEAD);61 ptr = table->buckets[i]; 62 while (ptr != NULL) { 63 psListAdd(myLinkList, ptr->key, PS_LIST_HEAD); 65 64 ptr = ptr->next; 66 65 } … … 69 68 70 69 // Return the linked list 71 return ( myLinkList ); 72 } 73 74 70 return (myLinkList); 71 } 75 72 76 73 /****************************************************************************** … … 84 81 the new hash bucket. 85 82 *****************************************************************************/ 86 static psHashBucket *hashBucketAlloc( const char *key, 87 void *data, 88 psHashBucket *next ) 89 { 90 if ( key == NULL ) { 91 psAbort( __func__, "psHashBucket() called with NULL key." ); 92 } 93 83 static psHashBucket *hashBucketAlloc(const char *key, void *data, psHashBucket * next) 84 { 85 if (key == NULL) { 86 psAbort(__func__, "psHashBucket() called with NULL key."); 87 } 94 88 // Allocate memory for the new hash bucket. 95 psHashBucket *bucket = psAlloc( sizeof( psHashBucket ) ); 96 p_psMemSetDeallocator( bucket, ( psFreeFcn ) hashBucketFree ); 89 psHashBucket *bucket = psAlloc(sizeof(psHashBucket)); 90 91 p_psMemSetDeallocator(bucket, (psFreeFcn) hashBucketFree); 97 92 98 93 // Initialize the bucket. 99 bucket->key = psStringCopy( key);100 101 if ( data == NULL) {94 bucket->key = psStringCopy(key); 95 96 if (data == NULL) { 102 97 // NOTE: Should we flag a warning message? 103 98 bucket->data = NULL; 104 99 } else { 105 bucket->data = psMemIncrRefCounter( data);100 bucket->data = psMemIncrRefCounter(data); 106 101 } 107 102 … … 110 105 return bucket; 111 106 } 112 113 107 114 108 /****************************************************************************** … … 120 114 NONE 121 115 *****************************************************************************/ 122 static void hashBucketFree( psHashBucket *bucket ) 123 { 124 if ( bucket == NULL ) { 125 return ; 126 } 127 116 static void hashBucketFree(psHashBucket * bucket) 117 { 118 if (bucket == NULL) { 119 return; 120 } 128 121 // A bucket is actually a linked list of buckets. We recursively step 129 122 // through that linked list, free each bucket. 130 psFree( bucket->next ); 131 132 psFree( bucket->key ); 133 134 psFree( bucket->data ); 135 } 136 123 psFree(bucket->next); 124 125 psFree(bucket->key); 126 127 psFree(bucket->data); 128 } 137 129 138 130 /****************************************************************************** … … 144 136 The new hash table. 145 137 *****************************************************************************/ 146 psHash *psHashAlloc( int nbucket ) // initial number of buckets 147 { 148 int i = 0; // loop index variable 138 psHash *psHashAlloc(int nbucket) // initial number of buckets 139 { 140 int i = 0; // loop index variable 141 149 142 // Create the new hash table. 150 psHash *table = psAlloc( sizeof( psHash ) ); 151 p_psMemSetDeallocator( table, ( psFreeFcn ) hashFree ); 143 psHash *table = psAlloc(sizeof(psHash)); 144 145 p_psMemSetDeallocator(table, (psFreeFcn) hashFree); 152 146 153 147 // Allocate memory for the buckets. 154 table->buckets = psAlloc( nbucket * sizeof( psHashBucket * ));148 table->buckets = psAlloc(nbucket * sizeof(psHashBucket *)); 155 149 table->nbucket = nbucket; 156 150 157 psTrace( "utils.hash", 1, "Creating %d-element hash table\n", nbucket);151 psTrace("utils.hash", 1, "Creating %d-element hash table\n", nbucket); 158 152 159 153 // Initialize all buckets to NULL. 160 for ( i = 0; i < nbucket; i++)154 for (i = 0; i < nbucket; i++) 161 155 { 162 table->buckets[ i] = NULL;156 table->buckets[i] = NULL; 163 157 } 164 158 … … 166 160 return table; 167 161 } 168 169 170 162 171 163 /****************************************************************************** … … 179 171 NONE 180 172 *****************************************************************************/ 181 static void hashFree( psHash *table ) 182 { 183 int i = 0; // Loop index variable. 184 185 if ( table == NULL ) { 186 return ; 187 } 188 173 static void hashFree(psHash * table) 174 { 175 int i = 0; // Loop index variable. 176 177 if (table == NULL) { 178 return; 179 } 189 180 // Loop through each bucket in the hash table. If that bucket is not 190 181 // NULL, then free the bucket via a function call to hashBucketFree(); 191 for ( i = 0; i < table->nbucket; i++) {182 for (i = 0; i < table->nbucket; i++) { 192 183 193 184 // A bucket is composed of a linked list of buckets. 194 if ( table->buckets[ i ] != NULL) {195 psFree( table->buckets[ i ]);185 if (table->buckets[i] != NULL) { 186 psFree(table->buckets[i]); 196 187 } 197 188 } 198 189 199 190 // Free the bucket structure, then the hash table. 200 psFree( table->buckets);191 psFree(table->buckets); 201 192 } 202 193 … … 220 211 there is little common code between those functions. 221 212 *****************************************************************************/ 222 static void *doHashWork( psHash *table, const char *key, void *data, bool remove213 static void *doHashWork(psHash * table, const char *key, void *data, bool remove 223 214 ) 224 215 { 225 long int hash = 1; // This will contain an integer value 216 long int hash = 1; // This will contain an integer value 217 226 218 // "hashed" from the key. 227 char *tmpchar = NULL; // Used in computing the hash function. 228 psHashBucket *ptr = NULL; // Used to retrieve the hash bucket. 229 psHashBucket *optr = NULL; // "original pointer": used to step 219 char *tmpchar = NULL; // Used in computing the hash function. 220 psHashBucket *ptr = NULL; // Used to retrieve the hash bucket. 221 psHashBucket *optr = NULL; // "original pointer": used to step 222 230 223 // thru the linked list for a bucket. 231 224 … … 233 226 // function, but I'm checking it anyway since future coders might change 234 227 // the way this procedure is called. 235 if ( ( table == NULL ) || ( key == NULL ) ) { 236 237 psAbort( __func__, "psHashRemove() called with NULL key or table." ); 238 } 239 228 if ((table == NULL) || (key == NULL)) { 229 230 psAbort(__func__, "psHashRemove() called with NULL key or table."); 231 } 240 232 // NOTE: This is the originally supplied hash function. 241 // for (int i = 0, len = strlen(key); i < len; i++) {242 // hash = (hash << 1) ^ key[i];243 // }244 // hash &= (table->nbucket - 1);233 // for (int i = 0, len = strlen(key); i < len; i++) { 234 // hash = (hash << 1) ^ key[i]; 235 // } 236 // hash &= (table->nbucket - 1); 245 237 246 238 // This hash algorithm is from Sedgewick. NOTE: must reread to ensure that 247 239 // the size of the hash table is not required to be a prime number. 248 tmpchar = ( char * )key;249 for ( hash = 0; *tmpchar != '\0'; tmpchar++) {250 hash = ( 64 * hash + *tmpchar ) % ( table->nbucket);240 tmpchar = (char *)key; 241 for (hash = 0; *tmpchar != '\0'; tmpchar++) { 242 hash = (64 * hash + *tmpchar) % (table->nbucket); 251 243 } 252 244 253 245 // NOTE: This should not be necessary, but for now, I'm checking bounds 254 246 // anyway. 255 if ( ( hash < 0 ) || ( hash >= table->nbucket ) ) { 256 psAbort( __func__, "Internal hash function out of range (%d)", hash ); 257 } 258 247 if ((hash < 0) || (hash >= table->nbucket)) { 248 psAbort(__func__, "Internal hash function out of range (%d)", hash); 249 } 259 250 // ptr will have the correct hash bucket. 260 ptr = table->buckets[ hash];251 ptr = table->buckets[hash]; 261 252 262 253 // We know the correct hash bucket, now we need to know what to do. … … 264 255 // or a remove operation on the hash table. 265 256 266 if ( data == NULL ) { 267 if ( remove 268 ) { 269 // We search through the linked list for this bucket in 270 // the hash table and look for an entry for this key. 271 257 if (data == NULL) { 258 if (remove 259 ) { 260 // We search through the linked list for this bucket in 261 // the hash table and look for an entry for this key. 262 263 optr = ptr; 264 while (ptr != NULL) { 265 // Dtermine if this entry holds the correct key. 266 if (strcmp(key, ptr->key) == 0) { 267 // The following lines of code are fairly standard ways 268 // of removing an item from a single-linked list. 269 270 void *data = ptr->data; 271 272 optr->next = ptr->next; 273 if (ptr == table->buckets[hash]) { 274 table->buckets[hash] = ptr->next; 275 } 276 277 psFree(ptr); 278 279 // By definition, the data associated with that key 280 // must be returned, not freed. 281 return data; 282 } 272 283 optr = ptr; 273 while ( ptr != NULL ) { 274 // Dtermine if this entry holds the correct key. 275 if ( strcmp( key, ptr->key ) == 0 ) { 276 // The following lines of code are fairly standard ways 277 // of removing an item from a single-linked list. 278 279 void * data = ptr->data; 280 optr->next = ptr->next; 281 if ( ptr == table->buckets[ hash ] ) { 282 table->buckets[ hash ] = ptr->next; 283 } 284 285 psFree( ptr ); 286 287 // By definition, the data associated with that key 288 // must be returned, not freed. 289 return data; 290 } 291 optr = ptr; 292 ptr = ptr->next; 293 } 294 return NULL; // not in hash 284 ptr = ptr->next; 295 285 } 286 return NULL; // not in hash 287 } 296 288 else { 297 289 // If we get here, then a retrieve operation is requested. So, 298 290 // we step trough the linked list at this bucket, and return the 299 291 // data once we find it, or return NULL if we don't. 300 while ( ptr != NULL) {301 if ( strcmp( key, ptr->key ) == 0) {292 while (ptr != NULL) { 293 if (strcmp(key, ptr->key) == 0) { 302 294 return ptr->data; 303 295 } 304 296 ptr = ptr->next; 305 297 } 306 return NULL; // not in hash298 return NULL; // not in hash 307 299 } 308 300 } else { … … 312 304 // the hash table and look for a duplicate entry for this key. 313 305 314 while ( ptr != NULL) {315 if ( strcmp( key, ptr->key ) == 0) {306 while (ptr != NULL) { 307 if (strcmp(key, ptr->key) == 0) { 316 308 // We have found this key in the hash table. 317 309 318 psTrace( "utils.hash.insert", 3, "Replacing data for %s\n", 319 key ); 310 psTrace("utils.hash.insert", 3, "Replacing data for %s\n", key); 320 311 321 312 // NOTE: I have changed this behavior from the originally … … 323 314 // the new data was not inserted into the hash table. 324 315 325 psFree( ptr->data);326 327 ptr->data = psMemIncrRefCounter( data);316 psFree(ptr->data); 317 318 ptr->data = psMemIncrRefCounter(data); 328 319 return data; 329 320 } … … 333 324 // table. So, we insert this data at the head of that linked list. 334 325 335 table->buckets[ hash ] = hashBucketAlloc( key, 336 data, 337 table->buckets[ hash ] ); 326 table->buckets[hash] = hashBucketAlloc(key, data, table->buckets[hash]); 338 327 return data; 339 328 } … … 351 340 boolean value defining success or failure 352 341 *****************************************************************************/ 353 bool psHashAdd( psHash *table, const char *key, void *data)354 { 355 if ( table == NULL) {356 psAbort( __func__, "psHashInsert() called with NULL hash table.");357 } 358 if ( key == NULL) {359 psAbort( __func__, "psHashInsert() called with NULL key.");360 } 361 if ( data == NULL) {362 psAbort( __func__, "psHashLookup() called with NULL data.");363 } 364 365 return ( doHashWork( table, key, data, 0 ) != NULL);342 bool psHashAdd(psHash * table, const char *key, void *data) 343 { 344 if (table == NULL) { 345 psAbort(__func__, "psHashInsert() called with NULL hash table."); 346 } 347 if (key == NULL) { 348 psAbort(__func__, "psHashInsert() called with NULL key."); 349 } 350 if (data == NULL) { 351 psAbort(__func__, "psHashLookup() called with NULL data."); 352 } 353 354 return (doHashWork(table, key, data, 0) != NULL); 366 355 } 367 356 … … 377 366 The data associated with that key. 378 367 *****************************************************************************/ 379 void *psHashLookup( psHash *table, // table to lookup key in 380 const char *key ) // key to lookup 381 { 382 if ( table == NULL ) { 383 psAbort( __func__, "psHashLookup() called with NULL hash table." ); 384 } 385 if ( key == NULL ) { 386 psAbort( __func__, "psHashLookup() called with NULL key." ); 387 } 388 389 390 return doHashWork( table, key, NULL, 0 ); 368 void *psHashLookup(psHash * table, // table to lookup key in 369 const char *key) // key to lookup 370 { 371 if (table == NULL) { 372 psAbort(__func__, "psHashLookup() called with NULL hash table."); 373 } 374 if (key == NULL) { 375 psAbort(__func__, "psHashLookup() called with NULL key."); 376 } 377 378 return doHashWork(table, key, NULL, 0); 391 379 } 392 380 … … 401 389 boolean value defining success or failure 402 390 *****************************************************************************/ 403 bool psHashRemove( psHash *table, const char *key)404 { 405 void * data = NULL;391 bool psHashRemove(psHash * table, const char *key) 392 { 393 void *data = NULL; 406 394 bool retVal = false; 407 395 408 if ( table == NULL) {409 psAbort( __func__, "psHashRemove() called with NULL hash table.");410 } 411 if ( key == NULL) {412 psAbort( __func__, "psHashRemove() called with NULL key.");413 } 414 415 data = doHashWork( table, key, NULL, 1);416 if ( data != NULL) {417 psFree( data);396 if (table == NULL) { 397 psAbort(__func__, "psHashRemove() called with NULL hash table."); 398 } 399 if (key == NULL) { 400 psAbort(__func__, "psHashRemove() called with NULL key."); 401 } 402 403 data = doHashWork(table, key, NULL, 1); 404 if (data != NULL) { 405 psFree(data); 418 406 retVal = true; 419 407 } else { -
trunk/psLib/src/sysUtils/psHash.h
r1406 r1407 1 1 2 /** @file psHash.h 2 3 * @brief Contains support for basic hashing functions. … … 10 11 * @author George Gusciora, MHPCC 11 12 * 12 * @version $Revision: 1.1 8$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-08-0 6 22:34:05$13 * @version $Revision: 1.19 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-08-07 00:06:06 $ 14 15 * 15 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 16 17 */ 17 18 #if !defined(PS_HASH_H) 18 # define PS_HASH_H19 # define PS_HASH_H 19 20 20 21 /** \addtogroup HashTable 21 22 * \{ 22 23 */ 23 # include<stdbool.h>24 # include<stdbool.h> 24 25 25 # include "psList.h"26 # include "psList.h" 26 27 27 28 /** A bucket that holds an item of data. */ 28 29 typedef struct psHashBucket 29 30 { 30 char *key; ///< key for this item of data31 void *data; ///< the data itself32 struct psHashBucket *next; ///< list of other possible keys31 char *key; // /< key for this item of data 32 void *data; // /< the data itself 33 struct psHashBucket *next; // /< list of other possible keys 33 34 } 34 35 psHashBucket; 35 36 36 37 //typedef struct HashTable psHash; ///< Opaque type for a hash table 38 37 39 /** The hash-table itself. */ 38 40 typedef struct psHash 39 41 { 40 int nbucket; ///< Number of buckets in hash table.41 psHashBucket **buckets; ///< The bucket data.42 int nbucket; // /< Number of buckets in hash table. 43 psHashBucket **buckets; // /< The bucket data. 42 44 } 43 45 psHash; 44 46 45 47 /// Allocate hash buckets in table. 46 psHash *psHashAlloc(int nbucket ///< The number of buckets to allocate.48 psHash *psHashAlloc(int nbucket // /< The number of buckets to allocate. 47 49 ); 48 50 49 51 /// Insert entry into table. 50 bool psHashAdd(psHash * table, ///< table to insert in51 const char *key, ///< key to use52 void *data ///< data to insert52 bool psHashAdd(psHash * table, // /< table to insert in 53 const char *key, // /< key to use 54 void *data // /< data to insert 53 55 ); 54 56 55 57 /// Lookup key in table. 56 void *psHashLookup(psHash * table, ///< table to lookup key in57 const char *key ///< key to lookup58 void *psHashLookup(psHash * table, // /< table to lookup key in 59 const char *key // /< key to lookup 58 60 ); 59 61 60 62 /// Remove key from table. 61 bool psHashRemove(psHash * table, ///< table to lookup key in62 const char *key ///< key to lookup63 bool psHashRemove(psHash * table, // /< table to lookup key in 64 const char *key // /< key to lookup 63 65 ); 64 66 65 67 /// List all keys in table. 66 psList *psHashKeyList(psHash * table ///< table to list keys from.68 psList *psHashKeyList(psHash * table // /< table to list keys from. 67 69 ); 68 70 69 /* \} */ // End of DataGroup Functions71 /* \} */// End of DataGroup Functions 70 72 71 73 #endif -
trunk/psLib/src/sysUtils/psLogMsg.c
r1406 r1407 1 1 2 /** @file psLogMsg.c 2 3 * @brief Procedures for logging messages. … … 11 12 * @author George Gusciora, MHPCC 12 13 * 13 * @version $Revision: 1.2 3$ $Name: not supported by cvs2svn $14 * @date $Date: 2004-08-0 6 22:34:05$14 * @version $Revision: 1.24 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-08-07 00:06:06 $ 15 16 * 16 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 44 45 45 46 #define MAX_LOG_LINE_LENGTH 128 46 static FILE * logDest = (FILE*)1;// flag to initialize to stderr before using.47 static int globalLogLevel = PS_LOG_INFO; // log all messages at this or above48 static bool logTime = true; // Flag to include time info49 static bool logHost = true; // Flag to include host info50 static bool logLevel = true; // Flag to include level info51 static bool logName = true; // Flag to include name info52 static bool logMsg = true; // Flag to include message info47 static FILE *logDest = (FILE *) 1; // flag to initialize to stderr before using. 48 static int globalLogLevel = PS_LOG_INFO; // log all messages at this or above 49 static bool logTime = true; // Flag to include time info 50 static bool logHost = true; // Flag to include host info 51 static bool logLevel = true; // Flag to include level info 52 static bool logName = true; // Flag to include name info 53 static bool logMsg = true; // Flag to include message info 53 54 54 55 /***************************************************************************** … … 67 68 68 69 if ((level < MIN_LOG_LEVEL) || (level > MAX_LOG_LEVEL)) { 69 psLogMsg("logmsg", PS_LOG_WARN, 70 "Attempt to set invalid logMsg level: %d", level); 70 psLogMsg("logmsg", PS_LOG_WARN, "Attempt to set invalid logMsg level: %d", level); 71 71 level = (level < MIN_LOG_LEVEL) ? MIN_LOG_LEVEL : MAX_LOG_LEVEL; 72 72 } 73 74 73 // Set new global log level 75 74 globalLogLevel = level; … … 78 77 return oldLevel; 79 78 } 80 81 79 82 80 /***************************************************************************** … … 91 89 An integer specifying the old log destination. 92 90 *****************************************************************************/ 93 int psLogSetDestination(const char *dest)91 int psLogSetDestination(const char *dest) 94 92 { 95 93 char protocol[5]; … … 97 95 98 96 // if logDest has not been initialized, do so before using it 99 if (logDest ==(FILE*)1) {97 if (logDest == (FILE *) 1) { 100 98 logDest = stderr; 101 99 } 102 100 103 if (dest == NULL || strcmp(dest, "none")==0) {101 if (dest == NULL || strcmp(dest, "none") == 0) { 104 102 if (logDest != NULL && logDest != stderr && logDest != stdout) { 105 103 fclose(logDest); … … 109 107 } 110 108 111 if (sscanf(dest, "%4s:%256s",protocol,location) < 2) {112 psError(__func__, "The specified destination, %s, is malformed.", dest);109 if (sscanf(dest, "%4s:%256s", protocol, location) < 2) { 110 psError(__func__, "The specified destination, %s, is malformed.", dest); 113 111 return 1; 114 112 } 115 113 116 if (strcmp(protocol, "dest") == 0) {117 if (strcmp(location, "stderr") == 0) {114 if (strcmp(protocol, "dest") == 0) { 115 if (strcmp(location, "stderr") == 0) { 118 116 if (logDest != NULL && logDest != stderr && logDest != stdout) { 119 117 fclose(logDest); … … 122 120 return 0; 123 121 } 124 if (strcmp(location, "stdout") == 0) {122 if (strcmp(location, "stdout") == 0) { 125 123 if (logDest != NULL && logDest != stderr && logDest != stdout) { 126 124 fclose(logDest); … … 129 127 return 0; 130 128 } 131 psError(__func__, "The location, %s, for protocol 'dest' is invalid.",location);129 psError(__func__, "The location, %s, for protocol 'dest' is invalid.", location); 132 130 return 1; 133 } else 134 if (strcmp(protocol,"file") == 0) {135 FILE* file = fopen(location,"w"); 136 if (file == NULL) {137 psError(__func__,"Could not open file '%s' for output.",location);138 return 1;139 }140 if (logDest != NULL && logDest != stderr && logDest != stdout) {141 fclose(logDest);142 }143 logDest = file;144 return 0;145 }146 147 psError(__func__, "Do not know how to handle the protocol '%s'.",protocol);131 } else if (strcmp(protocol, "file") == 0) { 132 FILE *file = fopen(location, "w"); 133 134 if (file == NULL) { 135 psError(__func__, "Could not open file '%s' for output.", location); 136 return 1; 137 } 138 if (logDest != NULL && logDest != stderr && logDest != stdout) { 139 fclose(logDest); 140 } 141 logDest = file; 142 return 0; 143 } 144 145 psError(__func__, "Do not know how to handle the protocol '%s'.", protocol); 148 146 return 1; 149 147 } … … 181 179 fmt = "THLNM"; 182 180 } 183 184 181 // Step through each character in the format string. For each letter 185 182 // in that string, set/unset the appropriate logging. … … 214 211 215 212 if (!logMsg) { 216 psTrace("utils.logMsg", 1, 217 "You must at least log error messages (You chose \"%s\")", 218 fmt); 219 } 220 } 221 222 223 224 #if !defined(HOST_NAME_MAX) // should be in limits.h 225 # define HOST_NAME_MAX 256 213 psTrace("utils.logMsg", 1, "You must at least log error messages (You chose \"%s\")", fmt); 214 } 215 } 216 217 #if !defined(HOST_NAME_MAX) // should be in limits.h 218 # define HOST_NAME_MAX 256 226 219 #endif 220 227 221 /***************************************************************************** 228 222 psVLogMsg(): This routine sends the message, which is a printf style … … 239 233 NULL. 240 234 *****************************************************************************/ 241 void psLogMsgV(const char *name, 242 int level, 243 const char *fmt, 244 va_list ap) 245 { 246 static int first = 1; // Flag for calling gethostname() 235 void psLogMsgV(const char *name, int level, const char *fmt, va_list ap) 236 { 237 static int first = 1; // Flag for calling gethostname() 247 238 static char hostname[HOST_NAME_MAX + 1]; 239 248 240 // Buffer for hostname. 249 char clevel =0;// letter-name for level250 char head[MAX_LOG_LINE_LENGTH +2];// the added two are for the ending | and \0251 char *head_ptr = head; // where we've got to in head241 char clevel = 0; // letter-name for level 242 char head[MAX_LOG_LINE_LENGTH + 2]; // the added two are for the ending | and \0 243 char *head_ptr = head; // where we've got to in head 252 244 int maxLength = MAX_LOG_LINE_LENGTH; 253 time_t clock = time(NULL); // The current time.245 time_t clock = time(NULL); // The current time. 254 246 struct tm *utc = gmtime(&clock); // The current gm time. 255 247 256 248 // if logDest has not been initialized, do so before using it 257 if (logDest ==(FILE*)1) {249 if (logDest == (FILE *) 1) { 258 250 logDest = stderr; 259 251 } 260 261 252 // If logging is off, or if the level is too high, return immediately. 262 253 if ((level > globalLogLevel) || (logDest == NULL)) { 263 254 return; 264 255 } 265 266 256 // If I have not been here yet, determine my hostname and save it. 267 257 if (first) { … … 297 287 298 288 default: 299 psTrace("utils.logMsg", 2, "Invalid logMsg level: %d (%s)\n", 300 level, fmt); 289 psTrace("utils.logMsg", 2, "Invalid logMsg level: %d (%s)\n", level, fmt); 301 290 level = (level < 0) ? 0 : 9; 302 291 break; … … 307 296 maxLength -= snprintf(head_ptr, maxLength, "%4d:%02d:%02d %02d:%02d:%02dZ", 308 297 utc->tm_year + 1900, utc->tm_mon + 1, utc->tm_mday, 309 utc->tm_hour, utc->tm_min, utc->tm_sec) -1;298 utc->tm_hour, utc->tm_min, utc->tm_sec) - 1; 310 299 head_ptr += strlen(head_ptr); 311 300 } … … 336 325 if (head_ptr > head) { 337 326 *head_ptr++ = '|'; 338 } else 339 if (!logMsg) { // no output desired 340 return; 341 } 327 } else if (!logMsg) { // no output desired 328 return; 329 } 342 330 *head_ptr = '\0'; 343 331 … … 352 340 } 353 341 } 354 355 342 356 343 /***************************************************************************** … … 369 356 NULL 370 357 *****************************************************************************/ 371 void psLogMsg(const char *name, 372 int level, 373 const char *fmt, 374 ...) 358 void psLogMsg(const char *name, int level, const char *fmt, ...) 375 359 { 376 360 va_list ap; -
trunk/psLib/src/sysUtils/psLogMsg.h
r1406 r1407 1 1 2 /** @file psLogMsg.h 2 3 * @brief Procedures for logging messages. … … 11 12 * @author George Gusciora, MHPCC 12 13 * 13 * @version $Revision: 1.1 2$ $Name: not supported by cvs2svn $14 * @date $Date: 2004-08-0 6 22:34:05$14 * @version $Revision: 1.13 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2004-08-07 00:06:06 $ 15 16 * 16 17 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 17 18 */ 18 19 #if !defined(PS_LOG_MSG_H) 19 # define PS_LOG_MSG_H20 # include <stdarg.h>20 # define PS_LOG_MSG_H 21 # include <stdarg.h> 21 22 22 23 /** @addtogroup LogTrace … … 29 30 /// In future versions, this procedure will take a character string as an 30 31 /// argument which can specify more general log destinations. 31 int psLogSetDestination(const char * dest ///< Specifies where to send messages.32 int psLogSetDestination(const char *dest // /< Specifies where to send messages. 32 33 ); 33 34 34 35 35 /// This procedure sets the message level for future log messages. Subsequent … … 38 38 /// Ie. higher values set by this procedure will cause more log messages to 39 39 /// be displayed. 40 int psLogSetLevel(int level ///< Specifies the system log level40 int psLogSetLevel(int level // /< Specifies the system log level 41 41 ); 42 43 42 44 43 /// This procedure sets the log format for future log messages. The argument … … 47 46 /// Deleting a letter from the string will cause the associated information 48 47 /// to not be logged. 49 void psLogSetFormat(const char *fmt ///< Specifies the system log format48 void psLogSetFormat(const char *fmt // /< Specifies the system log format 50 49 ); 51 52 50 53 51 /// This procedure logs a message to the destination set by a prior … … 55 53 /// specified by a prior call to psLogSetLevel(). The message is specified 56 54 /// with a printf-stype string an arguments. 57 void psLogMsg(const char *name, ///< name of the log source58 int myLevel, ///< severity level of this log message59 const char *fmt, ... ///< printf-style format command55 void psLogMsg(const char *name, // /< name of the log source 56 int myLevel, // /< severity level of this log message 57 const char *fmt, ... // /< printf-style format command 60 58 ); 61 62 59 63 60 /// This procedure is functionally equivalent to psLogMsg(), except that 64 61 /// it takes a va_list as the message parameter, not a printf-style string. 65 void psLogMsgV(const char *name, ///< name of the log source66 int myLevel, ///< severity level of this log message67 const char *fmt, ///< printf-style format command68 va_list ap ///< varargs argument list62 void psLogMsgV(const char *name, // /< name of the log source 63 int myLevel, // /< severity level of this log message 64 const char *fmt, // /< printf-style format command 65 va_list ap // /< varargs argument list 69 66 ); 70 67 … … 74 71 PS_LOG_ERROR, 75 72 PS_LOG_WARN, 76 PS_LOG_INFO }; 73 PS_LOG_INFO 74 }; 77 75 78 76 ///< Destinations for log messages … … 80 78 PS_LOG_NONE, 81 79 PS_LOG_TO_STDERR, 82 PS_LOG_TO_STDOUT }; 80 PS_LOG_TO_STDOUT 81 }; 83 82 84 83 /// @} -
trunk/psLib/src/sysUtils/psMemory.c
r1406 r1407 1 1 2 /** @file psMemory.c 2 3 * … … 8 9 * @author Robert Lupton, Princeton University 9 10 * 10 * @version $Revision: 1.3 2$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-08-0 6 22:34:05$11 * @version $Revision: 1.33 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-08-07 00:06:06 $ 12 13 * 13 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 14 15 */ 15 16 16 #define PS_ALLOW_MALLOC // we're allowed to call malloc()17 #define PS_ALLOW_MALLOC // we're allowed to call malloc() 17 18 18 19 #include <stdlib.h> … … 26 27 #include "psLogMsg.h" 27 28 28 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header29 30 #define P_PS_LARGE_BLOCK_SIZE 65536 // size where under, we try to recycle31 32 static int checkMemBlock( const psMemBlock *m, const char* funcName);29 #define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header 30 31 #define P_PS_LARGE_BLOCK_SIZE 65536 // size where under, we try to recycle 32 33 static int checkMemBlock(const psMemBlock * m, const char *funcName); 33 34 static psMemBlock *lastMemBlockAllocated = NULL; 34 35 static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER; … … 38 39 39 40 static int recycleBins = 13; 40 static int recycleBinSize[ 14 ] =41 {42 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, P_PS_LARGE_BLOCK_SIZE43 }; 41 static int recycleBinSize[14] = { 42 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, P_PS_LARGE_BLOCK_SIZE 43 }; 44 44 45 // N.B. recycleBinSize should be terminated by P_PS_LARGE_BLOCK_SIZE (simplifies search loops) 45 static psMemBlock* recycleMemBlockList[ 13 ] = 46 { 47 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 48 }; 46 static psMemBlock *recycleMemBlockList[13] = { 47 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 48 }; 49 49 50 50 #ifdef PS_MEM_DEBUG 51 static psMemBlock * deadBlockList;// a place to put dead memBlocks in debug mode.51 static psMemBlock *deadBlockList; // a place to put dead memBlocks in debug mode. 52 52 #endif 53 53 54 /** 54 55 * Unique ID for allocated blocks … … 59 60 * Default memExhausted callback. 60 61 */ 61 static void *memExhaustedCallbackDefault( size_t size)62 { 63 void * ptr = NULL;64 65 pthread_mutex_lock( &recycleMemBlockListMutex);62 static void *memExhaustedCallbackDefault(size_t size) 63 { 64 void *ptr = NULL; 65 66 pthread_mutex_lock(&recycleMemBlockListMutex); 66 67 int level = recycleBins - 1; 67 while ( level >= 0 && ptr == NULL ) { 68 while ( recycleMemBlockList[ level ] != NULL && ptr == NULL ) { 69 psMemBlock * old = recycleMemBlockList[ level ]; 70 recycleMemBlockList[ level ] = recycleMemBlockList[ level ] ->nextBlock; 71 free( old ); 72 ptr = malloc( size ); 68 69 while (level >= 0 && ptr == NULL) { 70 while (recycleMemBlockList[level] != NULL && ptr == NULL) { 71 psMemBlock *old = recycleMemBlockList[level]; 72 73 recycleMemBlockList[level] = recycleMemBlockList[level]->nextBlock; 74 free(old); 75 ptr = malloc(size); 73 76 } 74 77 level--; 75 78 } 76 pthread_mutex_unlock( &recycleMemBlockListMutex);79 pthread_mutex_unlock(&recycleMemBlockListMutex); 77 80 78 81 return ptr; … … 81 84 static psMemExhaustedCallback memExhaustedCallback = memExhaustedCallbackDefault; 82 85 83 psMemExhaustedCallback psMemExhaustedCallbackSet( psMemExhaustedCallback func)86 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func) 84 87 { 85 88 psMemExhaustedCallback old = memExhaustedCallback; 86 89 87 if ( func != NULL) {90 if (func != NULL) { 88 91 memExhaustedCallback = func; 89 92 } else { … … 94 97 } 95 98 96 static void memProblemCallbackDefault( const psMemBlock *ptr, 97 const char *file, int lineno ) 98 { 99 if ( ptr->refCounter < 1 ) { 100 psError( __func__, 101 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 102 ptr->id, ptr->file, ptr->lineno, file, lineno ); 103 } 104 105 if ( lineno > 0 ) { 106 psAbort( __func__, "Detected a problem in the memory system at %s:%d", file, lineno ); 99 static void memProblemCallbackDefault(const psMemBlock * ptr, const char *file, int lineno) 100 { 101 if (ptr->refCounter < 1) { 102 psError(__func__, 103 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 104 ptr->id, ptr->file, ptr->lineno, file, lineno); 105 } 106 107 if (lineno > 0) { 108 psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno); 107 109 } 108 110 } 109 111 static psMemProblemCallback memProblemCallback = memProblemCallbackDefault; 110 112 111 psMemProblemCallback psMemProblemCallbackSet( psMemProblemCallback func)113 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func) 112 114 { 113 115 psMemProblemCallback old = memProblemCallback; 114 116 115 if ( func != NULL) {117 if (func != NULL) { 116 118 memProblemCallback = func; 117 119 } else { … … 121 123 return old; 122 124 } 125 123 126 /* 124 127 * And now the I-want-to-be-informed callbacks … … 126 129 * Call the callbacks when these IDs are allocated/freed 127 130 */ 128 psMemoryId p_psMemAllocateID = 0; // notify user this block is allocated131 psMemoryId p_psMemAllocateID = 0; // notify user this block is allocated 129 132 psMemoryId p_psMemFreeID = 0; // notify user this block is freed 130 133 131 psMemoryId psMemAllocateCallbackSetID( psMemoryId id)134 psMemoryId psMemAllocateCallbackSetID(psMemoryId id) 132 135 { 133 136 psMemoryId old = p_psMemAllocateID; 137 134 138 p_psMemAllocateID = id; 135 139 … … 137 141 } 138 142 139 psMemoryId psMemFreeCallbackSetID( psMemoryId id)143 psMemoryId psMemFreeCallbackSetID(psMemoryId id) 140 144 { 141 145 psMemoryId old = p_psMemFreeID; 146 142 147 p_psMemFreeID = id; 143 148 … … 151 156 * isn't resignalled) 152 157 */ 153 static psMemoryId memAllocateCallbackDefault( const psMemBlock *ptr)154 { 155 static psMemoryId incr = 0; // "p_psMemAllocateID += incr"158 static psMemoryId memAllocateCallbackDefault(const psMemBlock * ptr) 159 { 160 static psMemoryId incr = 0; // "p_psMemAllocateID += incr" 156 161 157 162 return incr; 158 163 } 159 164 160 static psMemoryId memFreeCallbackDefault( const psMemBlock *ptr)161 { 162 static psMemoryId incr = 0; // "p_psMemFreeID += incr"165 static psMemoryId memFreeCallbackDefault(const psMemBlock * ptr) 166 { 167 static psMemoryId incr = 0; // "p_psMemFreeID += incr" 163 168 164 169 return incr; … … 171 176 static psMemFreeCallback memFreeCallback = memFreeCallbackDefault; 172 177 173 psMemAllocateCallback psMemAllocateCallbackSet( psMemAllocateCallback func)178 psMemAllocateCallback psMemAllocateCallbackSet(psMemAllocateCallback func) 174 179 { 175 180 psMemFreeCallback old = memAllocateCallback; 176 181 177 if ( func != NULL) {182 if (func != NULL) { 178 183 memAllocateCallback = func; 179 184 } else { … … 184 189 } 185 190 186 psMemFreeCallback psMemFreeCallbackSet( psMemFreeCallback func)191 psMemFreeCallback psMemFreeCallbackSet(psMemFreeCallback func) 187 192 { 188 193 psMemFreeCallback old = memFreeCallback; 189 194 190 if ( func != NULL) {195 if (func != NULL) { 191 196 memFreeCallback = func; 192 197 } else { … … 200 205 * Return memory ID counter for next block to be allocated 201 206 */ 202 psMemoryId psMemGetId( void)207 psMemoryId psMemGetId(void) 203 208 { 204 209 psMemoryId id; 205 pthread_mutex_lock( &memIdMutex ); 210 211 pthread_mutex_lock(&memIdMutex); 206 212 id = memid + 1; 207 pthread_mutex_unlock( &memIdMutex);213 pthread_mutex_unlock(&memIdMutex); 208 214 209 215 return id; … … 216 222 */ 217 223 218 static int checkMemBlock( const psMemBlock *m, const char* funcName)224 static int checkMemBlock(const psMemBlock * m, const char *funcName) 219 225 { 220 226 // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked, 221 227 // we shouldn't call such things as p_psAlloc/p_psFree here. 222 228 223 if ( m == NULL) {224 psError( funcName, "Memory Corruption: NULL memory block found.");229 if (m == NULL) { 230 psError(funcName, "Memory Corruption: NULL memory block found."); 225 231 return 1; 226 232 } 227 233 228 if ( m->refCounter == 0) {234 if (m->refCounter == 0) { 229 235 // using an unreferenced block of memory, are you? 230 psError( __func__, "Memory Corruption: memory block %ld was freed but still used.", 231 m->id ); 236 psError(__func__, "Memory Corruption: memory block %ld was freed but still used.", m->id); 232 237 return 1; 233 238 } 234 239 235 if ( m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC ) { 236 psError( funcName, "Memory Corruption: memory block %ld is corrupted (buffer underflow)", 237 m->id ); 240 if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) { 241 psError(funcName, "Memory Corruption: memory block %ld is corrupted (buffer underflow)", m->id); 238 242 return 1; 239 243 } 240 if ( *( void** ) ( ( int8_t* ) ( m + 1 ) + m->userMemorySize ) != P_PS_MEMMAGIC ) { 241 psError( funcName, "Memory Corruption: memory block %ld is corrupted (buffer overflow)", 242 m->id ); 244 if (*(void **)((int8_t *) (m + 1) + m->userMemorySize) != P_PS_MEMMAGIC) { 245 psError(funcName, "Memory Corruption: memory block %ld is corrupted (buffer overflow)", m->id); 243 246 return 1; 244 247 } … … 247 250 } 248 251 249 int psMemCheckCorruption( bool abort_on_error)250 { 251 int nbad = 0; // number of bad blocks252 int psMemCheckCorruption(bool abort_on_error) 253 { 254 int nbad = 0; // number of bad blocks 252 255 253 256 // get exclusive access to the memBlock list to avoid it changing on us while we use it. 254 pthread_mutex_lock( &memBlockListMutex);255 256 for ( psMemBlock * iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) {257 if ( checkMemBlock( iter, __func__ )) {257 pthread_mutex_lock(&memBlockListMutex); 258 259 for (psMemBlock * iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) { 260 if (checkMemBlock(iter, __func__)) { 258 261 nbad++; 259 262 260 memProblemCallback( iter, __func__, __LINE__);261 262 if ( abort_on_error) {263 memProblemCallback(iter, __func__, __LINE__); 264 265 if (abort_on_error) { 263 266 // release the lock on the memblock list 264 pthread_mutex_unlock( &memBlockListMutex);265 psAbort( __func__, "Detected memory corruption");267 pthread_mutex_unlock(&memBlockListMutex); 268 psAbort(__func__, "Detected memory corruption"); 266 269 return nbad; 267 270 } … … 270 273 271 274 // release the lock on the memblock list 272 pthread_mutex_unlock( &memBlockListMutex);275 pthread_mutex_unlock(&memBlockListMutex); 273 276 return nbad; 274 277 } 275 278 276 void *p_psAlloc( size_t size, const char *file, int lineno)277 { 278 279 psMemBlock * ptr = NULL;279 void *p_psAlloc(size_t size, const char *file, int lineno) 280 { 281 282 psMemBlock *ptr = NULL; 280 283 281 284 // memory is of the size I want to bother recycling? 282 if ( size < P_PS_LARGE_BLOCK_SIZE) {285 if (size < P_PS_LARGE_BLOCK_SIZE) { 283 286 // find the bin we need. 284 287 int level = 0; 285 while ( size > recycleBinSize[ level ] ) { 288 289 while (size > recycleBinSize[level]) { 286 290 level++; 287 291 } 288 292 // Are we in one of the bins 289 if ( level < recycleBins) {290 291 size = recycleBinSize[ level]; // round-up size to next sized bin.292 293 pthread_mutex_lock( &recycleMemBlockListMutex);294 295 if ( recycleMemBlockList[ level ] != NULL) {296 ptr = recycleMemBlockList[ level];297 recycleMemBlockList[ level] = ptr->nextBlock;298 if ( recycleMemBlockList[ level ] != NULL) {299 recycleMemBlockList[ level ]->previousBlock = NULL;293 if (level < recycleBins) { 294 295 size = recycleBinSize[level]; // round-up size to next sized bin. 296 297 pthread_mutex_lock(&recycleMemBlockListMutex); 298 299 if (recycleMemBlockList[level] != NULL) { 300 ptr = recycleMemBlockList[level]; 301 recycleMemBlockList[level] = ptr->nextBlock; 302 if (recycleMemBlockList[level] != NULL) { 303 recycleMemBlockList[level]->previousBlock = NULL; 300 304 } 301 305 size = ptr->userMemorySize; 302 306 } 303 307 304 pthread_mutex_unlock( &recycleMemBlockListMutex ); 305 } 306 } 307 308 if ( ptr == NULL ) { 309 ptr = malloc( sizeof( psMemBlock ) + size + sizeof( void* ) ); 310 311 if ( ptr == NULL ) { 312 ptr = memExhaustedCallback( size ); 313 if ( ptr == NULL ) { 314 psAbort( __func__, "Failed to allocate %u bytes at %s:%d", 315 size, file, lineno ); 308 pthread_mutex_unlock(&recycleMemBlockListMutex); 309 } 310 } 311 312 if (ptr == NULL) { 313 ptr = malloc(sizeof(psMemBlock) + size + sizeof(void *)); 314 315 if (ptr == NULL) { 316 ptr = memExhaustedCallback(size); 317 if (ptr == NULL) { 318 psAbort(__func__, "Failed to allocate %u bytes at %s:%d", size, file, lineno); 316 319 } 317 320 } … … 320 323 ptr->endblock = P_PS_MEMMAGIC; 321 324 ptr->userMemorySize = size; 322 pthread_mutex_init( &ptr->refCounterMutex, NULL ); 323 } 324 325 pthread_mutex_init(&ptr->refCounterMutex, NULL); 326 } 325 327 // increment the memory id safely. 326 pthread_mutex_lock( &memBlockListMutex);327 *( psMemoryId*) & ptr->id = ++memid;328 pthread_mutex_unlock( &memBlockListMutex);328 pthread_mutex_lock(&memBlockListMutex); 329 *(psMemoryId *) & ptr->id = ++memid; 330 pthread_mutex_unlock(&memBlockListMutex); 329 331 330 332 ptr->file = file; 331 333 ptr->freeFcn = NULL; 332 *( unsigned int* ) &ptr->lineno = lineno;333 *( void** ) ( ( int8_t* ) ( ptr + 1 ) + size) = P_PS_MEMMAGIC;334 *(unsigned int *)&ptr->lineno = lineno; 335 *(void **)((int8_t *) (ptr + 1) + size) = P_PS_MEMMAGIC; 334 336 ptr->previousBlock = NULL; 335 337 336 ptr->refCounter = 1; // one user so far338 ptr->refCounter = 1; // one user so far 337 339 338 340 // need exclusive access of the memory block list now... 339 pthread_mutex_lock( &memBlockListMutex);341 pthread_mutex_lock(&memBlockListMutex); 340 342 341 343 // insert the new block to the front of the memBlock linked-list 342 344 ptr->nextBlock = lastMemBlockAllocated; 343 if ( ptr->nextBlock != NULL) {345 if (ptr->nextBlock != NULL) { 344 346 ptr->nextBlock->previousBlock = ptr; 345 347 } 346 348 lastMemBlockAllocated = ptr; 347 349 348 pthread_mutex_unlock( &memBlockListMutex ); 349 350 // Did the user ask to be informed about this allocation? 351 if ( ptr->id == p_psMemAllocateID ) { 352 p_psMemAllocateID += memAllocateCallback( ptr ); 353 } 354 350 pthread_mutex_unlock(&memBlockListMutex); 351 352 // Did the user ask to be informed about this allocation? 353 if (ptr->id == p_psMemAllocateID) { 354 p_psMemAllocateID += memAllocateCallback(ptr); 355 } 355 356 // And return the user the memory that they allocated 356 return ptr + 1; // user memory357 } 358 359 void *p_psRealloc( void *vptr, size_t size, const char *file, int lineno)360 { 361 if ( vptr == NULL) {362 return p_psAlloc( size, file, lineno);357 return ptr + 1; // user memory 358 } 359 360 void *p_psRealloc(void *vptr, size_t size, const char *file, int lineno) 361 { 362 if (vptr == NULL) { 363 return p_psAlloc(size, file, lineno); 363 364 } else { 364 psMemBlock *ptr = ( ( psMemBlock * ) vptr) - 1;365 psMemBlock *ptr = ((psMemBlock *) vptr) - 1; 365 366 bool isBlockLast = false; 366 367 367 if ( checkMemBlock( ptr, __func__ ) != 0 ) { 368 memProblemCallback( ptr, file, lineno ); 369 psAbort( file, "Realloc detected a memory corruption (id %ld @ %s:%d).", 370 ptr->id, ptr->file, ptr->lineno ); 371 } 372 373 pthread_mutex_lock( &memBlockListMutex ); 374 375 isBlockLast = ( ptr == lastMemBlockAllocated ); 376 377 ptr = ( psMemBlock* ) realloc( ptr, sizeof( psMemBlock ) + size + sizeof( void* ) ); 378 379 if ( ptr == NULL ) { 380 psAbort( __func__, "Failed to reallocate %ld bytes at %s:%d", 381 size, file, lineno ); 368 if (checkMemBlock(ptr, __func__) != 0) { 369 memProblemCallback(ptr, file, lineno); 370 psAbort(file, "Realloc detected a memory corruption (id %ld @ %s:%d).", 371 ptr->id, ptr->file, ptr->lineno); 372 } 373 374 pthread_mutex_lock(&memBlockListMutex); 375 376 isBlockLast = (ptr == lastMemBlockAllocated); 377 378 ptr = (psMemBlock *) realloc(ptr, sizeof(psMemBlock) + size + sizeof(void *)); 379 380 if (ptr == NULL) { 381 psAbort(__func__, "Failed to reallocate %ld bytes at %s:%d", size, file, lineno); 382 382 } 383 383 384 384 ptr->userMemorySize = size; 385 *( void** ) ( ( int8_t* ) ( ptr + 1 ) + size) = P_PS_MEMMAGIC;386 387 if ( isBlockLast) {385 *(void **)((int8_t *) (ptr + 1) + size) = P_PS_MEMMAGIC; 386 387 if (isBlockLast) { 388 388 lastMemBlockAllocated = ptr; 389 389 } 390 391 390 // the block location may have changed, so fix the linked list addresses. 392 if ( ptr->nextBlock != NULL) {391 if (ptr->nextBlock != NULL) { 393 392 ptr->nextBlock->previousBlock = ptr; 394 393 } 395 if ( ptr->previousBlock != NULL) {394 if (ptr->previousBlock != NULL) { 396 395 ptr->previousBlock->nextBlock = ptr; 397 396 } 398 397 399 pthread_mutex_unlock( &memBlockListMutex);400 401 // Did the user ask to be informed about this allocation?402 if ( ptr->id == p_psMemAllocateID) {403 p_psMemAllocateID += memAllocateCallback( ptr);404 } 405 406 return ptr + 1; // usr memory407 } 408 } 409 410 void p_psFree( void *vptr, const char *file, int lineno)411 { 412 ( void ) p_psMemDecrRefCounter( vptr, file, lineno );// this handles the free, if required.398 pthread_mutex_unlock(&memBlockListMutex); 399 400 // Did the user ask to be informed about this allocation? 401 if (ptr->id == p_psMemAllocateID) { 402 p_psMemAllocateID += memAllocateCallback(ptr); 403 } 404 405 return ptr + 1; // usr memory 406 } 407 } 408 409 void p_psFree(void *vptr, const char *file, int lineno) 410 { 411 (void)p_psMemDecrRefCounter(vptr, file, lineno); // this handles the free, if required. 413 412 } 414 413 … … 416 415 * Check for memory leaks. 417 416 */ 418 int psMemCheckLeaks( psMemoryId id0, psMemBlock ***arr, FILE *fd)417 int psMemCheckLeaks(psMemoryId id0, psMemBlock *** arr, FILE * fd) 419 418 { 420 419 int nleak = 0; 421 420 int j = 0; 422 psMemBlock *topBlock = lastMemBlockAllocated;423 424 pthread_mutex_lock( &memBlockListMutex);425 426 for ( psMemBlock * iter = topBlock; iter != NULL; iter = iter->nextBlock) {427 if ( ( psMemGetRefCounter( iter + 1 ) > 0 ) && ( iter->id >= id0 )) {421 psMemBlock *topBlock = lastMemBlockAllocated; 422 423 pthread_mutex_lock(&memBlockListMutex); 424 425 for (psMemBlock * iter = topBlock; iter != NULL; iter = iter->nextBlock) { 426 if ((psMemGetRefCounter(iter + 1) > 0) && (iter->id >= id0)) { 428 427 nleak++; 429 428 430 if ( fd != NULL) {431 if ( nleak == 1) {432 fprintf( fd, " %20s:line ID\n", "file");429 if (fd != NULL) { 430 if (nleak == 1) { 431 fprintf(fd, " %20s:line ID\n", "file"); 433 432 } 434 433 435 fprintf( fd, " %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id);436 } 437 } 438 } 439 440 pthread_mutex_unlock( &memBlockListMutex);441 442 if ( nleak == 0 || arr == NULL) {434 fprintf(fd, " %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id); 435 } 436 } 437 } 438 439 pthread_mutex_unlock(&memBlockListMutex); 440 441 if (nleak == 0 || arr == NULL) { 443 442 return nleak; 444 443 } 445 444 446 *arr = p_psAlloc( nleak * sizeof( psMemBlock ), __FILE__, __LINE__);447 pthread_mutex_lock( &memBlockListMutex);448 449 for ( psMemBlock * iter = topBlock; iter != NULL; iter = iter->nextBlock) {450 if ( ( psMemGetRefCounter( iter + 1 ) > 0 ) && ( iter->id >= id0 )) {451 ( *arr ) [ j++] = iter;452 if ( j == nleak ) {// found them all445 *arr = p_psAlloc(nleak * sizeof(psMemBlock), __FILE__, __LINE__); 446 pthread_mutex_lock(&memBlockListMutex); 447 448 for (psMemBlock * iter = topBlock; iter != NULL; iter = iter->nextBlock) { 449 if ((psMemGetRefCounter(iter + 1) > 0) && (iter->id >= id0)) { 450 (*arr)[j++] = iter; 451 if (j == nleak) { // found them all 453 452 break; 454 453 } … … 456 455 } 457 456 458 pthread_mutex_unlock( &memBlockListMutex);457 pthread_mutex_unlock(&memBlockListMutex); 459 458 460 459 return nleak; … … 465 464 */ 466 465 // return refCounter 467 psReferenceCount psMemGetRefCounter( void *vptr)468 { 469 psMemBlock * ptr;466 psReferenceCount psMemGetRefCounter(void *vptr) 467 { 468 psMemBlock *ptr; 470 469 unsigned int refCount; 471 470 472 if ( vptr == NULL) {471 if (vptr == NULL) { 473 472 return 0; 474 473 } 475 474 476 ptr = ( ( psMemBlock * ) vptr) - 1;477 478 if ( checkMemBlock( ptr, __func__ ) != 0) {479 memProblemCallback( ptr, __func__, __LINE__);480 } 481 482 pthread_mutex_lock( &ptr->refCounterMutex);475 ptr = ((psMemBlock *) vptr) - 1; 476 477 if (checkMemBlock(ptr, __func__) != 0) { 478 memProblemCallback(ptr, __func__, __LINE__); 479 } 480 481 pthread_mutex_lock(&ptr->refCounterMutex); 483 482 refCount = ptr->refCounter; 484 pthread_mutex_unlock( &ptr->refCounterMutex);483 pthread_mutex_unlock(&ptr->refCounterMutex); 485 484 486 485 return refCount; 487 486 } 487 488 488 // increment and return refCounter 489 void * p_psMemIncrRefCounter( void *vptr, const char *file, int lineno)490 { 491 psMemBlock * ptr;492 493 if ( vptr == NULL) {489 void *p_psMemIncrRefCounter(void *vptr, const char *file, int lineno) 490 { 491 psMemBlock *ptr; 492 493 if (vptr == NULL) { 494 494 return vptr; 495 495 } 496 496 497 ptr = ( ( psMemBlock * ) vptr) - 1;498 499 if ( checkMemBlock( ptr, __func__ )) {500 memProblemCallback( ptr, file, lineno);501 } 502 503 pthread_mutex_lock( &ptr->refCounterMutex);497 ptr = ((psMemBlock *) vptr) - 1; 498 499 if (checkMemBlock(ptr, __func__)) { 500 memProblemCallback(ptr, file, lineno); 501 } 502 503 pthread_mutex_lock(&ptr->refCounterMutex); 504 504 ptr->refCounter++; 505 pthread_mutex_unlock( &ptr->refCounterMutex);505 pthread_mutex_unlock(&ptr->refCounterMutex); 506 506 507 507 return vptr; … … 509 509 510 510 // decrement and return refCounter 511 void * p_psMemDecrRefCounter( void *vptr, const char *file, int lineno)512 { 513 if ( vptr == NULL) {511 void *p_psMemDecrRefCounter(void *vptr, const char *file, int lineno) 512 { 513 if (vptr == NULL) { 514 514 return NULL; 515 515 } 516 516 517 psMemBlock *ptr = ( ( psMemBlock * ) vptr) - 1;518 519 if ( checkMemBlock( ptr, __func__ ) != 0) {520 memProblemCallback( ptr, file, lineno);517 psMemBlock *ptr = ((psMemBlock *) vptr) - 1; 518 519 if (checkMemBlock(ptr, __func__) != 0) { 520 memProblemCallback(ptr, file, lineno); 521 521 return NULL; 522 522 } 523 523 524 pthread_mutex_lock( &ptr->refCounterMutex);525 526 if ( ptr->refCounter > 1) {527 // / XXX - Probably should have another mutex here.528 ptr->refCounter--; // multiple references, just decrement the count.529 pthread_mutex_unlock( &ptr->refCounterMutex);524 pthread_mutex_lock(&ptr->refCounterMutex); 525 526 if (ptr->refCounter > 1) { 527 // / XXX - Probably should have another mutex here. 528 ptr->refCounter--; // multiple references, just decrement the count. 529 pthread_mutex_unlock(&ptr->refCounterMutex); 530 530 531 531 } else { 532 pthread_mutex_unlock( &ptr->refCounterMutex);532 pthread_mutex_unlock(&ptr->refCounterMutex); 533 533 534 534 // Did the user ask to be informed about this deallocation? 535 if ( ptr->id == p_psMemFreeID) {536 p_psMemFreeID += memFreeCallback( ptr);537 } 538 539 if ( ptr->freeFcn != NULL) {540 ptr->freeFcn( vptr);541 } 542 543 pthread_mutex_lock( &memBlockListMutex);535 if (ptr->id == p_psMemFreeID) { 536 p_psMemFreeID += memFreeCallback(ptr); 537 } 538 539 if (ptr->freeFcn != NULL) { 540 ptr->freeFcn(vptr); 541 } 542 543 pthread_mutex_lock(&memBlockListMutex); 544 544 545 545 // cut the memBlock out of the memBlock list 546 if ( ptr->nextBlock != NULL) {546 if (ptr->nextBlock != NULL) { 547 547 ptr->nextBlock->previousBlock = ptr->previousBlock; 548 548 } 549 if ( ptr->previousBlock != NULL) {549 if (ptr->previousBlock != NULL) { 550 550 ptr->previousBlock->nextBlock = ptr->nextBlock; 551 551 } 552 if ( lastMemBlockAllocated == ptr) {552 if (lastMemBlockAllocated == ptr) { 553 553 lastMemBlockAllocated = ptr->nextBlock; 554 554 } 555 555 556 pthread_mutex_unlock( &memBlockListMutex ); 557 556 pthread_mutex_unlock(&memBlockListMutex); 558 557 559 558 // do we need to recycle? 560 if ( ptr->userMemorySize < P_PS_LARGE_BLOCK_SIZE) {559 if (ptr->userMemorySize < P_PS_LARGE_BLOCK_SIZE) { 561 560 562 561 int level = 1; 563 while ( ptr->userMemorySize >= recycleBinSize[ level ] ) { 562 563 while (ptr->userMemorySize >= recycleBinSize[level]) { 564 564 level++; 565 565 } … … 569 569 ptr->previousBlock = NULL; 570 570 571 pthread_mutex_lock( &recycleMemBlockListMutex);572 ptr->nextBlock = recycleMemBlockList[ level];573 if ( recycleMemBlockList[ level ] != NULL) {574 recycleMemBlockList[ level ]->previousBlock = ptr;575 } 576 recycleMemBlockList[ level] = ptr;577 pthread_mutex_unlock( &recycleMemBlockListMutex);571 pthread_mutex_lock(&recycleMemBlockListMutex); 572 ptr->nextBlock = recycleMemBlockList[level]; 573 if (recycleMemBlockList[level] != NULL) { 574 recycleMemBlockList[level]->previousBlock = ptr; 575 } 576 recycleMemBlockList[level] = ptr; 577 pthread_mutex_unlock(&recycleMemBlockListMutex); 578 578 579 579 } else { 580 580 // memory is larger than I want to recycle. 581 581 #ifdef PS_MEM_DEBUG 582 ( void ) p_psRealloc( vptr, 0, file, lineno);582 (void)p_psRealloc(vptr, 0, file, lineno); 583 583 ptr->previousBlock = NULL; 584 584 ptr->nextBlock = deadBlockList; 585 if ( deadBlockList != NULL) {585 if (deadBlockList != NULL) { 586 586 deadBlockList->previous = ptr; 587 587 } … … 589 589 #else 590 590 591 pthread_mutex_destroy( &ptr->refCounterMutex);592 free( ptr);591 pthread_mutex_destroy(&ptr->refCounterMutex); 592 free(ptr); 593 593 #endif 594 594 595 595 } 596 596 597 vptr = NULL; // since we freed it, make sure we return NULL.597 vptr = NULL; // since we freed it, make sure we return NULL. 598 598 } 599 599 … … 601 601 } 602 602 603 void p_psMemSetDeallocator( void* vptr, psFreeFcn freeFcn)604 { 605 if ( vptr == NULL) {606 return ;607 } 608 609 psMemBlock *ptr = ( ( psMemBlock * ) vptr) - 1;610 611 if ( checkMemBlock( ptr, __func__ ) != 0) {612 memProblemCallback( ptr, __func__, __LINE__);603 void p_psMemSetDeallocator(void *vptr, psFreeFcn freeFcn) 604 { 605 if (vptr == NULL) { 606 return; 607 } 608 609 psMemBlock *ptr = ((psMemBlock *) vptr) - 1; 610 611 if (checkMemBlock(ptr, __func__) != 0) { 612 memProblemCallback(ptr, __func__, __LINE__); 613 613 } 614 614 … … 616 616 617 617 } 618 psFreeFcn p_psMemGetDeallocator( void* vptr)619 { 620 if ( vptr == NULL) {618 psFreeFcn p_psMemGetDeallocator(void *vptr) 619 { 620 if (vptr == NULL) { 621 621 return NULL; 622 622 } 623 623 624 psMemBlock *ptr = ( ( psMemBlock * ) vptr) - 1;625 626 if ( checkMemBlock( ptr, __func__ ) != 0) {627 memProblemCallback( ptr, __func__, __LINE__);624 psMemBlock *ptr = ((psMemBlock *) vptr) - 1; 625 626 if (checkMemBlock(ptr, __func__) != 0) { 627 memProblemCallback(ptr, __func__, __LINE__); 628 628 } 629 629 -
trunk/psLib/src/sysUtils/psMemory.h
r1406 r1407 1 1 #if !defined(PS_MEMORY_H) 2 #define PS_MEMORY_H 2 # define PS_MEMORY_H 3 3 4 /** @file psMemory.h 4 5 * … … 14 15 * @ingroup MemoryManagement 15 16 * 16 * @version $Revision: 1.2 3$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-08-0 6 22:34:05$17 * @version $Revision: 1.24 $ $Name: not supported by cvs2svn $ 18 * @date $Date: 2004-08-07 00:06:06 $ 18 19 * 19 20 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 20 21 */ 21 22 22 # include <stdio.h>// needed for FILE23 # include <stdbool.h>24 # include <pthread.h>// we need a mutex to make this stuff thread safe.23 # include <stdio.h> // needed for FILE 24 # include <stdbool.h> 25 # include <pthread.h> // we need a mutex to make this stuff thread safe. 25 26 26 27 /** @addtogroup MemoryManagement … … 53 54 54 55 /// typedef for deallocator. 55 typedef void (*psFreeFcn) (void*ptr);56 typedef void (*psFreeFcn) (void *ptr); 56 57 57 58 /** Book-keeping data for storage allocator. … … 62 63 typedef struct psMemBlock 63 64 { 64 const void * startblock; ///< initialised to p_psMEMMAGIC65 struct psMemBlock * previousBlock; ///< previous block in allocation list66 struct psMemBlock * nextBlock; ///< next block allocation list67 psFreeFcn freeFcn; ///< deallocator. If NULL, use generic deallocation.68 size_t userMemorySize; ///< the size of the user-portion of the memory block69 const psMemoryId id; ///< a unique ID for this allocation70 const char * file; ///< set from __FILE__ in e.g. p_psAlloc71 const int lineno; ///< set from __LINE__ in e.g. p_psAlloc72 pthread_mutex_t refCounterMutex; ///< mutex to ensure exclusive access to reference counter73 psReferenceCount refCounter; ///< how many times pointer is referenced74 const void * endblock; ///< initialised to p_psMEMMAGIC65 const void *startblock; // /< initialised to p_psMEMMAGIC 66 struct psMemBlock *previousBlock; // /< previous block in allocation list 67 struct psMemBlock *nextBlock; // /< next block allocation list 68 psFreeFcn freeFcn; // /< deallocator. If NULL, use generic deallocation. 69 size_t userMemorySize; // /< the size of the user-portion of the memory block 70 const psMemoryId id; // /< a unique ID for this allocation 71 const char *file; // /< set from __FILE__ in e.g. p_psAlloc 72 const int lineno; // /< set from __LINE__ in e.g. p_psAlloc 73 pthread_mutex_t refCounterMutex; // /< mutex to ensure exclusive access to reference counter 74 psReferenceCount refCounter; // /< how many times pointer is referenced 75 const void *endblock; // /< initialised to p_psMEMMAGIC 75 76 } 76 77 psMemBlock; … … 81 82 * @ingroup memCallback 82 83 */ 83 typedef psMemoryId (*psMemAllocateCallback)( 84 const psMemBlock *ptr ///< the psMemBlock just allocated 85 ); 84 typedef psMemoryId(*psMemAllocateCallback) (const psMemBlock * ptr // /< the psMemBlock just allocated 85 ); 86 86 87 87 /** prototype of memory free callback used by memory functions … … 90 90 * @ingroup memCallback 91 91 */ 92 typedef psMemoryId (*psMemFreeCallback)( 93 const psMemBlock *ptr ///< the psMemBlock being freed 94 ); 92 typedef psMemoryId(*psMemFreeCallback) (const psMemBlock * ptr // /< the psMemBlock being freed 93 ); 95 94 96 95 /** prototype of a callback used in error conditions … … 101 100 * @ingroup memCallback 102 101 */ 103 typedef void (*psMemProblemCallback) (104 const psMemBlock *ptr, ///< the pointer to the problematic memoryblock.105 const char *file, ///< the file in which the problem originated106 int lineno ///< the line number in which the problem originated107 );102 typedef void (*psMemProblemCallback) (const psMemBlock * ptr, // /< the pointer to the problematic memory 103 // block. 104 const char *file, // /< the file in which the problem originated 105 int lineno // /< the line number in which the problem originated 106 ); 108 107 109 108 /** prototype of a callback function used when memory runs out … … 115 114 * @ingroup memCallback 116 115 */ 117 typedef void *(*psMemExhaustedCallback)( 118 size_t size //< the size of buffer required 119 ); 116 typedef void *(*psMemExhaustedCallback) (size_t size // < the size of buffer required 117 ); 120 118 121 119 /** Memory allocation. This operates much like malloc(), but is guaranteed to return a non-NULL value. … … 124 122 * @see psFree 125 123 */ 126 #ifdef DOXYGEN 127 void* psAlloc( 128 size_t size ///< Size required 129 ); 130 #else 131 void* p_psAlloc( 132 size_t size, ///< Size required 133 const char *file, ///< File of call 134 int lineno ///< Line number of call 135 ); 136 137 void p_psMemSetDeallocator(void* ptr, psFreeFcn freeFcn); 138 psFreeFcn p_psMemGetDeallocator(void* ptr); 124 # ifdef DOXYGEN 125 void *psAlloc(size_t size // /< Size required 126 ); 127 # else 128 void *p_psAlloc(size_t size, // /< Size required 129 const char *file, // /< File of call 130 int lineno // /< Line number of call 131 ); 132 133 void p_psMemSetDeallocator(void *ptr, psFreeFcn freeFcn); 134 psFreeFcn p_psMemGetDeallocator(void *ptr); 139 135 140 136 /// Memory allocation. psAlloc sends file and line number to p_psAlloc. 141 #define psAlloc(size) p_psAlloc(size, __FILE__, __LINE__) 142 #endif 143 137 # define psAlloc(size) p_psAlloc(size, __FILE__, __LINE__) 138 # endif 144 139 145 140 /** Memory re-allocation. This operates much like realloc(), but is guaranteed to return a non-NULL value. … … 148 143 * @see psAlloc, psFree 149 144 */ 150 # ifdef DOXYGEN151 void * psRealloc(void *ptr ///< Pointer to re-allocate152 size_t size, ///< Size required145 # ifdef DOXYGEN 146 void *psRealloc(void *ptr // /< Pointer to re-allocate 147 size_t size, // /< Size required 153 148 ); 154 #else 155 void* p_psRealloc(void *ptr, ///< Pointer to re-allocate 156 size_t size, ///< Size required 157 const char *file, ///< File of call 158 int lineno ///< Line number of call 149 # else 150 void *p_psRealloc(void *ptr, // /< Pointer to re-allocate 151 size_t size, // /< Size required 152 const char *file, // /< File of call 153 int lineno // /< Line number of call 154 ); 155 156 /// Memory re-allocation. psRealloc sends file and line number to p_psRealloc. 157 # define psRealloc(ptr, size) p_psRealloc(ptr, size, __FILE__, __LINE__) 158 159 # endif 160 161 /** Free memory. This operates much like free(). 162 * 163 * @see psAlloc, psRealloc 164 */ 165 # ifdef DOXYGEN 166 void psFree(void *ptr, // /< Pointer to free, if NULL, function returns immediately. 167 ); 168 # else 169 void p_psFree(void *ptr, // /< Pointer to free 170 const char *file, // /< File of call 171 int lineno // /< Line number of call 159 172 ); 160 173 161 /// Memory re-allocation. psRealloc sends file and line number to p_psRealloc.162 #define psRealloc(ptr, size) p_psRealloc(ptr, size, __FILE__, __LINE__)163 164 #endif165 166 167 /** Free memory. This operates much like free().168 *169 * @see psAlloc, psRealloc170 */171 #ifdef DOXYGEN172 void psFree(void *ptr, ///< Pointer to free, if NULL, function returns immediately.173 );174 #else175 void p_psFree(void *ptr, ///< Pointer to free176 const char *file, ///< File of call177 int lineno ///< Line number of call178 );179 180 174 /// Free memory. psFree sends file and line number to p_psFree. 181 # define psFree(size) p_psFree(size, __FILE__, __LINE__)182 183 # endif175 # define psFree(size) p_psFree(size, __FILE__, __LINE__) 176 177 # endif 184 178 185 179 /** Check for memory leaks. This scans for allocated memory buffers not freed with an ID not less than id0. … … 197 191 * @ingroup memTracing 198 192 */ 199 int psMemCheckLeaks( 200 psMemoryId id0, ///< don't list blocks with id < id0 201 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL 202 FILE *fd ///< print list of leaks to fd (or NULL) 203 ); 193 int psMemCheckLeaks(psMemoryId id0, // /< don't list blocks with id < id0 194 psMemBlock *** arr, // /< pointer to array of pointers to leaked blocks, or NULL 195 FILE * fd // /< print list of leaks to fd (or NULL) 196 ); 204 197 205 198 /** Check for memory corruption. Scans all currently allocated memory buffers and checks for corruptions, … … 208 201 * @ingroup memTracing 209 202 */ 210 int psMemCheckCorruption( 211 bool abort_on_error ///< Abort on detecting corruption? 212 ); 203 int psMemCheckCorruption(bool abort_on_error // /< Abort on detecting corruption? 204 ); 213 205 214 206 /** Return reference counter … … 216 208 * @ingroup memRefCount 217 209 */ 218 psReferenceCount psMemGetRefCounter( 219 void *vptr ///< Pointer to get refCounter for 220 ); 210 psReferenceCount psMemGetRefCounter(void *vptr // /< Pointer to get refCounter for 211 ); 221 212 222 213 /** Increment reference counter and return the pointer … … 224 215 * @ingroup memRefCount 225 216 */ 226 #ifdef DOXYGEN 227 void* psMemIncrRefCounter( 228 void *vptr ///< Pointer to increment refCounter, and return 229 ); 230 #else 231 void* p_psMemIncrRefCounter( 232 void *vptr, ///< Pointer to increment refCounter, and return 233 const char *file, ///< File of call 234 int lineno ///< Line number of call 235 ); 236 #define psMemIncrRefCounter(vptr) p_psMemIncrRefCounter(vptr, __FILE__, __LINE__) 237 #endif 217 # ifdef DOXYGEN 218 void *psMemIncrRefCounter(void *vptr // /< Pointer to increment refCounter, and return 219 ); 220 # else 221 void *p_psMemIncrRefCounter(void *vptr, // /< Pointer to increment refCounter, and return 222 const char *file, // /< File of call 223 int lineno // /< Line number of call 224 ); 225 226 # define psMemIncrRefCounter(vptr) p_psMemIncrRefCounter(vptr, __FILE__, __LINE__) 227 # endif 238 228 239 229 /** Decrement reference counter and return the pointer … … 241 231 * @ingroup memRefCount 242 232 */ 243 #ifdef DOXYGEN 244 void* psMemDecrRefCounter( 245 void *vptr ///< Pointer to decrement refCounter, and return 246 ); 247 #else 248 void* p_psMemDecrRefCounter( 249 void *vptr, ///< Pointer to decrement refCounter, and return 250 const char *file, ///< File of call 251 int lineno ///< Line number of call 252 ); 253 #define psMemDecrRefCounter(vptr) p_psMemDecrRefCounter(vptr, __FILE__, __LINE__) 254 #endif 233 # ifdef DOXYGEN 234 void *psMemDecrRefCounter(void *vptr // /< Pointer to decrement refCounter, and return 235 ); 236 # else 237 void *p_psMemDecrRefCounter(void *vptr, // /< Pointer to decrement refCounter, and return 238 const char *file, // /< File of call 239 int lineno // /< Line number of call 240 ); 241 242 # define psMemDecrRefCounter(vptr) p_psMemDecrRefCounter(vptr, __FILE__, __LINE__) 243 # endif 255 244 256 245 /** Set callback for problems 257 246 * @ingroup memCallback 258 247 */ 259 psMemProblemCallback psMemProblemCallbackSet( 260 psMemProblemCallback func ///< Function to run 261 ); 248 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func // /< Function to run 249 ); 262 250 263 251 /** Set callback for out-of-memory … … 265 253 * @ingroup memCallback 266 254 */ 267 psMemExhaustedCallback psMemExhaustedCallbackSet( 268 psMemExhaustedCallback func ///< Function to run 269 ); 255 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func // /< Function to run 256 ); 270 257 271 258 /** Set call back for when a particular memory block is allocated … … 273 260 * @ingroup memCallback 274 261 */ 275 psMemAllocateCallback psMemAllocateCallbackSet( 276 psMemAllocateCallback func ///< Function to run 277 ); 262 psMemAllocateCallback psMemAllocateCallbackSet(psMemAllocateCallback func // /< Function to run 263 ); 278 264 279 265 /** Set call back for when a particular memory block is freed … … 281 267 * @ingroup memCallback 282 268 */ 283 psMemFreeCallback psMemFreeCallbackSet( 284 psMemFreeCallback func ///< Function to run 285 ); 269 psMemFreeCallback psMemFreeCallbackSet(psMemFreeCallback func // /< Function to run 270 ); 286 271 287 272 /** get next memory ID … … 295 280 * @ingroup memCallback 296 281 */ 297 psMemoryId psMemAllocateCallbackSetID( 298 psMemoryId id ///< ID to set 299 ); 282 psMemoryId psMemAllocateCallbackSetID(psMemoryId id // /< ID to set 283 ); 300 284 301 285 /** set p_psMemFreeID to id … … 303 287 * @ingroup memCallback 304 288 */ 305 psMemoryId psMemFreeCallbackSetID( 306 psMemoryId id ///< ID to set 307 ); 289 psMemoryId psMemFreeCallbackSetID(psMemoryId id // /< ID to set 290 ); 308 291 309 292 //@} End of Memory Management Functions 310 293 311 # ifndef DOXYGEN294 # ifndef DOXYGEN 312 295 313 296 /* 314 297 * Ensure that any program using malloc/realloc/free will fail to compile 315 298 */ 316 #ifndef PS_ALLOW_MALLOC 317 #ifdef __GNUC__ 318 #pragma GCC poison malloc realloc calloc free 319 #else 320 #define malloc(S) _Pragma("error Use of malloc is not allowed. Use psAlloc instead.") 321 #define realloc(P,S) _Pragma("error Use of realloc is not allowed. Use psRealloc instead.") 322 #define calloc(S) _Pragma("error Use of calloc is not allowed. Use psAlloc instead.") 323 #define free(P) _Pragma("error Use of free is not allowed. Use psFree instead.") 324 #endif 325 #endif 326 327 #endif // doxygen skip 328 329 #endif // end of header file 299 # ifndef PS_ALLOW_MALLOC 300 # ifdef __GNUC__ 301 # pragma GCC poison malloc realloc calloc free 302 # else 303 # define malloc(S) _Pragma("error Use of malloc is not allowed. Use psAlloc instead.") 304 # define realloc(P,S) _Pragma("error Use of realloc is not allowed. Use psRealloc instead.") 305 # define calloc(S) _Pragma("error Use of calloc is not allowed. Use psAlloc instead.") 306 # define free(P) _Pragma("error Use of free is not allowed. Use psFree instead.") 307 # endif 308 # endif 309 310 # endif 311 // doxygen skip 312 313 #endif // end of header file -
trunk/psLib/src/sysUtils/psString.c
r1406 r1407 1 1 2 /** @file psString.c 2 3 * … … 8 9 * @author Eric Van Alst, MHPCC 9 10 * 10 * @version $Revision: 1. 7$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-08-0 6 22:34:05$11 * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-08-07 00:06:06 $ 12 13 * 13 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 15 16 16 17 /******************************************************************************/ 18 17 19 /* INCLUDE FILES */ 20 18 21 /******************************************************************************/ 19 22 #include <stdlib.h> … … 24 27 25 28 /******************************************************************************/ 29 26 30 /* DEFINE STATEMENTS */ 31 27 32 /******************************************************************************/ 28 33 … … 30 35 31 36 /******************************************************************************/ 37 32 38 /* TYPE DEFINITIONS */ 39 33 40 /******************************************************************************/ 34 41 … … 36 43 37 44 /*****************************************************************************/ 45 38 46 /* GLOBAL VARIABLES */ 47 39 48 /*****************************************************************************/ 40 49 … … 42 51 43 52 /*****************************************************************************/ 53 44 54 /* FILE STATIC VARIABLES */ 55 45 56 /*****************************************************************************/ 46 57 … … 48 59 49 60 /*****************************************************************************/ 61 50 62 /* FUNCTION IMPLEMENTATION - LOCAL */ 63 51 64 /*****************************************************************************/ 52 65 … … 54 67 55 68 /*****************************************************************************/ 69 56 70 /* FUNCTION IMPLEMENTATION - PUBLIC */ 71 57 72 /*****************************************************************************/ 58 73 59 char *psStringCopy( 60 const char* str 61 ) 74 char *psStringCopy(const char *str) 62 75 { 63 76 // Allocate memory using psAlloc function … … 67 80 } 68 81 69 char *psStringNCopy( 70 const char* str, 71 int nChar 72 ) 82 char *psStringNCopy(const char *str, int nChar) 73 83 { 74 84 char *returnValue = NULL; 75 85 76 86 // Check the number of characters to copy is non-negative 77 if ( nChar < 0) {87 if (nChar < 0) { 78 88 // Log error message and return NULL 79 psError(__FILE__,"psStringNCopy with negative count specified %d", 80 nChar); 89 psError(__FILE__, "psStringNCopy with negative count specified %d", nChar); 81 90 return NULL; 82 91 } … … 84 93 // Copy input string to memory allocated up to nChar characters 85 94 // Return the copy 86 returnValue = strncpy(psAlloc((size_t) nChar + 1), str, (size_t)nChar);95 returnValue = strncpy(psAlloc((size_t) nChar + 1), str, (size_t) nChar); 87 96 88 97 // Ensure the last byte is NULL character 89 if ( nChar > 0) {90 returnValue[nChar -1] = '\0';98 if (nChar > 0) { 99 returnValue[nChar - 1] = '\0'; 91 100 } 92 93 101 // Return the string pointer 94 102 return returnValue; 95 103 } 96 97 -
trunk/psLib/src/sysUtils/psString.h
r974 r1407 1 1 2 /** @file psString.h 2 3 * … … 10 11 * @author Eric Van Alst, MHPCC 11 12 * 12 * @version $Revision: 1. 5$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-0 6-10 01:58:06 $13 * @version $Revision: 1.6 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-08-07 00:06:06 $ 14 15 * 15 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 17 18 18 19 #ifndef PS_STRING_H 19 # define PS_STRING_H20 # define PS_STRING_H 20 21 21 22 /******************************************************************************/ 23 22 24 /* DEFINE STATEMENTS */ 25 23 26 /******************************************************************************/ 24 27 25 28 /** This macro will convert the arguement to a quoted string */ 26 # define PS_STRING(S) #S29 # define PS_STRING(S) #S 27 30 28 31 /******************************************************************************/ 32 29 33 /* TYPE DEFINITIONS */ 34 30 35 /******************************************************************************/ 31 36 … … 33 38 34 39 /*****************************************************************************/ 40 35 41 /* FUNCTION PROTOTYPES */ 42 36 43 /*****************************************************************************/ 37 44 38 45 // Doxygen group tags 46 39 47 /** @addtogroup SysUtils 40 48 * @{ … … 49 57 * 50 58 */ 51 char *psStringCopy(52 const char *str /**< Input string of characters to copy */53 );54 59 60 char *psStringCopy(const char *str 61 /**< Input string of characters to copy */ 62 ); 55 63 56 64 /** Copies the input string up to the specified number of characters … … 67 75 * 68 76 */ 77 69 78 /*@null@*/ 70 char *psStringNCopy(71 const char *str, /**< Input string of characters to copy */72 int nChar /**< Number of bytes to allocate for string copy */73 );74 79 75 /* @} */ // Doxygen - End of SystemGroup Functions 80 char *psStringNCopy(const char *str, 81 /**< Input string of characters to copy */ 82 83 int nChar 84 /**< Number of bytes to allocate for string copy */ 85 ); 86 87 /* @} */// Doxygen - End of SystemGroup Functions 76 88 77 89 #endif 78 -
trunk/psLib/src/sysUtils/psTrace.c
r1406 r1407 1 1 2 /** @file psTrace.c 2 3 * \brief basic run-time trace facilities … … 9 10 * @author George Gusciora, MHPCC 10 11 * 11 * @version $Revision: 1.1 4$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-08-0 6 22:34:05$12 * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-08-07 00:06:06 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 16 */ 17 16 18 /***************************************************************************** 17 19 NOTES: … … 34 36 #ifndef PS_NO_TRACE 35 37 36 # include <stdlib.h>37 # include <stdio.h>38 # include <string.h>39 # include <stdarg.h>40 # include "psMemory.h"41 # include "psTrace.h"42 # include "psString.h"43 # include "psError.h"44 45 static p_psComponent *p_psCroot = NULL; // The root of the trace component46 static FILE *p_psTraceFP = NULL; // File destination for messages.47 48 static void componentFree(p_psComponent * comp);49 static p_psComponent *componentAlloc(const char *name, int level);38 # include <stdlib.h> 39 # include <stdio.h> 40 # include <string.h> 41 # include <stdarg.h> 42 # include "psMemory.h" 43 # include "psTrace.h" 44 # include "psString.h" 45 # include "psError.h" 46 47 static p_psComponent *p_psCroot = NULL; // The root of the trace component 48 static FILE *p_psTraceFP = NULL; // File destination for messages. 49 50 static void componentFree(p_psComponent * comp); 51 static p_psComponent *componentAlloc(const char *name, int level); 50 52 51 53 /***************************************************************************** 52 54 componentAlloc(): allocate memory for a new node, and initialize members. 53 55 *****************************************************************************/ 54 static p_psComponent *componentAlloc(const char *name, 55 int level) 56 static p_psComponent *componentAlloc(const char *name, int level) 56 57 { 57 58 p_psComponent *comp = psAlloc(sizeof(p_psComponent)); 58 p_psMemSetDeallocator(comp,(psFreeFcn)componentFree); 59 60 p_psMemSetDeallocator(comp, (psFreeFcn) componentFree); 59 61 comp->name = psStringCopy(name); 60 62 comp->level = level; … … 64 66 } 65 67 66 67 68 /***************************************************************************** 68 69 componentFree(): free the current node in the root tree, and all children 69 70 nodes as well. 70 71 *****************************************************************************/ 71 static void componentFree(p_psComponent * comp)72 static void componentFree(p_psComponent * comp) 72 73 { 73 74 if (comp == NULL) { … … 85 86 } 86 87 87 88 88 /***************************************************************************** 89 89 initTrace(): simply initialize the component root tree. … … 96 96 } 97 97 98 99 98 /***************************************************************************** 100 99 Set all trace levels to zero. 101 100 *****************************************************************************/ 102 void p_psTraceReset(p_psComponent * currentNode)101 void p_psTraceReset(p_psComponent * currentNode) 103 102 { 104 103 int i = 0; … … 109 108 110 109 currentNode->level = 0; 111 for (i =0;i<currentNode->n;i++) {110 for (i = 0; i < currentNode->n; i++) { 112 111 if (NULL == currentNode->subcomp[i]) { 113 112 psError(__func__, 114 "Sub-component %d of node %s in the trace tree is NULL.\n", 115 i, currentNode->name); 113 "Sub-component %d of node %s in the trace tree is NULL.\n", i, currentNode->name); 116 114 } else { 117 115 p_psTraceReset(currentNode->subcomp[i]); … … 129 127 } 130 128 131 132 129 /***************************************************************************** 133 130 Free all nodes in the component tree. … … 137 134 psFree(p_psCroot); 138 135 } 139 140 136 141 137 /***************************************************************************** … … 145 141 to ANSI-C. 146 142 *****************************************************************************/ 147 static void componentAdd(const char *addNodeName, 148 int level) 149 { 150 int i = 0; // Loop index variable. 151 char name[strlen(addNodeName) + 1]; // buffer for writeable copy. 152 char *pname=name; 153 char *firstComponent = NULL; // first component of name 143 static void componentAdd(const char *addNodeName, int level) 144 { 145 int i = 0; // Loop index variable. 146 char name[strlen(addNodeName) + 1]; // buffer for writeable copy. 147 char *pname = name; 148 char *firstComponent = NULL; // first component of name 154 149 p_psComponent *currentNode = p_psCroot; 155 int nodeExists = 0;156 157 // Is this the root node? If so, simply set level and return.150 int nodeExists = 0; 151 152 // Is this the root node? If so, simply set level and return. 158 153 if (strcmp(".", addNodeName) == 0) { 159 154 p_psCroot->level = level; … … 162 157 163 158 if (addNodeName[0] != '.') { 164 printf("ERROR: failed to add %s to the root component tree.\n", 165 addNodeName); 159 printf("ERROR: failed to add %s to the root component tree.\n", addNodeName); 166 160 exit(1); 167 161 } … … 188 182 if (nodeExists == 0) { 189 183 currentNode->subcomp = psRealloc(currentNode->subcomp, 190 (currentNode->n + 1) * sizeof(p_psComponent*)); 191 currentNode->n = (currentNode->n)+1; 192 193 currentNode->subcomp[(currentNode->n)-1] = 194 componentAlloc(firstComponent, level); 195 } 196 } 197 } 198 184 (currentNode->n + 1) * sizeof(p_psComponent *)); 185 currentNode->n = (currentNode->n) + 1; 186 187 currentNode->subcomp[(currentNode->n) - 1] = componentAlloc(firstComponent, level); 188 } 189 } 190 } 199 191 200 192 /***************************************************************************** … … 210 202 *****************************************************************************/ 211 203 int psTraceSetLevel(const char *comp, // component of interest 212 int level) // desired trace level204 int level) // desired trace level 213 205 { 214 206 // If the root component tree does not exist, then initialize it. … … 216 208 initTrace(); 217 209 } 218 219 210 // Add the new component to the component tree. 220 211 componentAdd(comp, level); … … 223 214 return 0; 224 215 } 225 226 216 227 217 /***************************************************************************** … … 241 231 static int doGetTraceLevel(const char *aname) 242 232 { 243 char name[strlen(aname) + 1];// need a writeable copy: for strsep()244 char *pname=name;245 char *firstComponent = NULL;// first component of name233 char name[strlen(aname) + 1]; // need a writeable copy: for strsep() 234 char *pname = name; 235 char *firstComponent = NULL; // first component of name 246 236 p_psComponent *currentNode = p_psCroot; 247 int i = 0;237 int i = 0; 248 238 249 239 if (NULL == currentNode) { 250 return (PS_UNKNOWN_TRACE_LEVEL);240 return (PS_UNKNOWN_TRACE_LEVEL); 251 241 } 252 242 253 243 if (strcmp(".", aname) == 0) { 254 return (p_psCroot->level);244 return (p_psCroot->level); 255 245 } 256 246 257 247 if (aname[0] != '.') { 258 return (PS_UNKNOWN_TRACE_LEVEL);248 return (PS_UNKNOWN_TRACE_LEVEL); 259 249 } 260 250 … … 266 256 if (NULL == currentNode->subcomp[i]) { 267 257 psError(__func__, 268 "Sub-component %d of node %s in trace tree is NULL.\n", 269 i, currentNode->name); 258 "Sub-component %d of node %s in trace tree is NULL.\n", i, currentNode->name); 270 259 } 271 260 … … 273 262 currentNode = currentNode->subcomp[i]; 274 263 if (pname == NULL) { 275 return (currentNode->level);264 return (currentNode->level); 276 265 } 277 266 } 278 267 } 279 268 } 280 return(PS_UNKNOWN_TRACE_LEVEL); 281 } 282 269 return (PS_UNKNOWN_TRACE_LEVEL); 270 } 283 271 284 272 /***************************************************************************** … … 297 285 { 298 286 if (p_psCroot == NULL) { 299 return(PS_UNKNOWN_TRACE_LEVEL); 300 } 301 287 return (PS_UNKNOWN_TRACE_LEVEL); 288 } 302 289 // Search the component root tree, determine the trace level. 303 return(doGetTraceLevel(name)); 304 } 305 290 return (doGetTraceLevel(name)); 291 } 306 292 307 293 /***************************************************************************** … … 317 303 null 318 304 *****************************************************************************/ 319 static void doPrintTraceLevels(const p_psComponent *comp, 320 int depth) 305 static void doPrintTraceLevels(const p_psComponent * comp, int depth) 321 306 { 322 307 int i = 0; … … 327 312 } else { 328 313 if (comp->level == PS_UNKNOWN_TRACE_LEVEL) { 329 printf("%*s%-*s %s\n", depth, "", 20 - depth, 330 comp->name, "."); 314 printf("%*s%-*s %s\n", depth, "", 20 - depth, comp->name, "."); 331 315 } else { 332 printf("%*s%-*s %d\n", depth, "", 20 - depth, 333 comp->name, comp->level); 316 printf("%*s%-*s %d\n", depth, "", 20 - depth, comp->name, comp->level); 334 317 } 335 318 } 336 319 337 320 for (i = 0; i < comp->n; i++) { 338 doPrintTraceLevels(comp->subcomp[i], depth+1); 339 } 340 } 341 321 doPrintTraceLevels(comp->subcomp[i], depth + 1); 322 } 323 } 342 324 343 325 /***************************************************************************** … … 359 341 doPrintTraceLevels(p_psCroot, 0); 360 342 } 361 362 343 363 344 /***************************************************************************** … … 376 357 *****************************************************************************/ 377 358 void p_psTrace(const char *comp, // component being traced 378 int level, // desired trace level379 ...) // arguments359 int level, // desired trace level 360 ...) // arguments 380 361 { 381 362 char *fmt = NULL; … … 384 365 385 366 if (NULL == comp) { 386 psError(__func__, 387 "p_psTrace() called on a NULL trace level tree\n"); 388 } 389 367 psError(__func__, "p_psTrace() called on a NULL trace level tree\n"); 368 } 390 369 // Only display this message if it's trace level is less than the level 391 370 // of it's associatedcomponent. … … 413 392 va_end(ap); 414 393 } 415 416 // NOTE: should we free *fmt as well? Read the man page. 417 } 418 419 void psTraceSetDestination(FILE *fp) 394 // NOTE: should we free *fmt as well? Read the man page. 395 } 396 397 void psTraceSetDestination(FILE * fp) 420 398 { 421 399 p_psTraceFP = fp; -
trunk/psLib/src/sysUtils/psTrace.h
r1393 r1407 1 1 2 /** @file psTrace.h 2 3 * \brief basic run-time trace facilities … … 9 10 * @author George Gusciora, MHPCC 10 11 * 11 * @version $Revision: 1.1 1$ $Name: not supported by cvs2svn $12 * @date $Date: 2004-08-0 5 19:38:52$12 * @version $Revision: 1.12 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-08-07 00:06:06 $ 13 14 * 14 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 15 16 */ 16 17 #if !defined(PS_TRACE_H) 17 # define PS_TRACE_H 118 # define PS_TRACE_H 1 18 19 19 #define PS_UNKNOWN_TRACE_LEVEL -9999 // we don't know this name's level 20 #define PS_DEFAULT_TRACE_LEVEL 0 21 20 # define PS_UNKNOWN_TRACE_LEVEL -9999 // we don't know this name's level 21 # define PS_DEFAULT_TRACE_LEVEL 0 22 22 23 23 /** \addtogroup LogTrace … … 29 29 //#define PS_NO_TRACE 1 ///< to turn off all tracing 30 30 31 #if defined(PS_NO_TRACE) 32 #define psTrace(facil, level, ...) (void)0 /* do nothing */ 33 #define p_psTrace(facil, level, ...) (void)0 /* do nothing */ 34 #define psTraceSetLevel(facil,level) 0 35 #define psTraceGetLevel(facil) 0 36 #define psTraceReset() (void)0 /* do nothing */ 37 #define psTraceFree() (void)0 /* do nothing */ 38 #define psTracePrintLevels() (void)0 /* do nothing */ 39 #define psTraceSetDestination(fp) (void)0 /* do nothing */ 40 #else 31 # if defined(PS_NO_TRACE) 32 # define psTrace(facil, level, ...) (void)0 33 /* do nothing */ 34 # define p_psTrace(facil, level, ...) (void)0 35 /* do nothing */ 36 # define psTraceSetLevel(facil,level) 0 37 # define psTraceGetLevel(facil) 0 38 # define psTraceReset() (void)0 /* do nothing */ 39 # define psTraceFree() (void)0 /* do nothing */ 40 # define psTracePrintLevels() (void)0 41 /* do nothing */ 42 # define psTraceSetDestination(fp) (void)0 43 /* do nothing */ 44 # else 41 45 42 /** Basic structure for the component tree. A component is a string of the43 form aaa.bbb.ccc, and may itself contain further subcomponents. The44 Component structure doesn't in fact contain it's full name, but only the45 last part. */46 typedef struct p_psComponent47 {48 const char *name;// last part of name of component49 int level;// trace level for this component50 int n;// number of subcomponents51 struct p_psComponent **subcomp;// next level of subcomponents52 }46 /** Basic structure for the component tree. A component is a string of the 47 form aaa.bbb.ccc, and may itself contain further subcomponents. The 48 Component structure doesn't in fact contain it's full name, but only the 49 last part. */ 50 typedef struct p_psComponent 51 { 52 const char *name; // last part of name of component 53 int level; // trace level for this component 54 int n; // number of subcomponents 55 struct p_psComponent **subcomp; // next level of subcomponents 56 } 53 57 p_psComponent; 54 58 55 # ifndef DOXYGEN59 # ifndef DOXYGEN 56 60 /// Send a trace message 57 void p_psTrace(const char *facil, ///< facilty of interest58 int myLevel, ///< desired trace level59 ...) ///< trace message arguments61 void p_psTrace(const char *facil, // /< facilty of interest 62 int myLevel, // /< desired trace level 63 ...) // /< trace message arguments 60 64 ; 61 # endif65 # endif 62 66 63 67 /// Set trace level 64 int psTraceSetLevel(const char *facil, ///< facilty of interest65 int level) ///< desired trace level68 int psTraceSetLevel(const char *facil, // /< facilty of interest 69 int level) // /< desired trace level 66 70 ; 67 71 68 72 /// Get the trace level 69 int psTraceGetLevel(const char *facil) ///< facilty of interest73 int psTraceGetLevel(const char *facil) // /< facilty of interest 70 74 ; 71 75 72 76 /// Set all trace levels to zero (do not free nodes in the component tree). 73 void psTraceReset() 74 ; 77 void psTraceReset(); 75 78 76 79 /// Free all nodes in the component tree. 77 void psTraceFree() 78 ; 80 void psTraceFree(); 79 81 80 82 /// print trace levels 81 void psTracePrintLevels(void) 82 ; 83 void psTracePrintLevels(void); 83 84 84 85 /// Set the destination of future trace messages. 85 void psTraceSetDestination(FILE * fp);86 void psTraceSetDestination(FILE * fp); 86 87 88 /* \} */// End of SystemGroup Functions 87 89 88 /* \} */ // End of SystemGroup Functions 89 90 #define psTrace(facil, level, ...) p_psTrace(facil, level, __VA_ARGS__) 91 #endif 90 # define psTrace(facil, level, ...) p_psTrace(facil, level, __VA_ARGS__) 91 # endif 92 92 93 93 #endif 94 -
trunk/psLib/src/sysUtils/psType.h
r1385 r1407 1 1 2 /** @file psType.h 2 3 * … … 10 11 * @author Ross Harman, MHPCC 11 12 * 12 * @version $Revision: 1.1 6$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-08-0 4 23:37:39$13 * @version $Revision: 1.17 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-08-07 00:06:06 $ 14 15 * 15 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 17 18 18 19 #ifndef PS_TYPE_H 19 # define PS_TYPE_H20 # define PS_TYPE_H 20 21 21 # include <complex.h>22 # include <stdint.h>23 # include <float.h>22 # include <complex.h> 23 # include <stdint.h> 24 # include <float.h> 24 25 25 26 /// @addtogroup DataContainer … … 27 28 28 29 /******************************************************************************/ 30 29 31 /* TYPE DEFINITIONS */ 32 30 33 /******************************************************************************/ 31 34 … … 37 40 */ 38 41 39 typedef uint8_t psU8; ///< 8-bit unsigned int 40 typedef uint16_t psU16; ///< 16-bit unsigned int 41 typedef uint32_t psU32; ///< 32-bit unsigned int 42 typedef uint64_t psU64; ///< 64-bit unsigned int 43 typedef int8_t psS8; ///< 8-bit signed int 44 typedef int16_t psS16; ///< 16-bit signed int 45 typedef int32_t psS32; ///< 32-bit signed int 46 typedef int64_t psS64; ///< 64-bit signed int 47 typedef float psF32; ///< 32-bit floating point 48 typedef double psF64; ///< 64-bit floating point 49 typedef complex float psC32; ///< complex with 32-bit floating point Real and Imagary numbers 50 typedef complex double psC64; ///< complex with 64-bit floating point Real and Imagary numbers 51 typedef void* psPTR; ///< void pointer 52 42 typedef uint8_t psU8; // /< 8-bit unsigned int 43 typedef uint16_t psU16; // /< 16-bit unsigned int 44 typedef uint32_t psU32; // /< 32-bit unsigned int 45 typedef uint64_t psU64; // /< 64-bit unsigned int 46 typedef int8_t psS8; // /< 8-bit signed int 47 typedef int16_t psS16; // /< 16-bit signed int 48 typedef int32_t psS32; // /< 32-bit signed int 49 typedef int64_t psS64; // /< 64-bit signed int 50 typedef float psF32; // /< 32-bit floating point 51 typedef double psF64; // /< 64-bit floating point 52 typedef complex float psC32; // /< complex with 32-bit floating point Real and Imagary numbers 53 typedef complex double psC64; // /< complex with 64-bit floating point Real and Imagary numbers 54 typedef void *psPTR; // /< void pointer 53 55 54 56 typedef enum { 55 PS_TYPE_S8 = 0x0101, ///< Character. 56 PS_TYPE_S16 = 0x0102, ///< Short integer. 57 PS_TYPE_S32 = 0x0104, ///< Integer. 58 PS_TYPE_S64 = 0x0108, ///< Long integer. 59 PS_TYPE_U8 = 0x0301, ///< Unsigned character. 60 PS_TYPE_U16 = 0x0302, ///< Unsigned short integer. 61 PS_TYPE_U32 = 0x0304, ///< Unsigned integer. 62 PS_TYPE_U64 = 0x0308, ///< Unsigned long integer. 63 PS_TYPE_F32 = 0x0404, ///< Single-precision Floating point. 64 PS_TYPE_F64 = 0x0408, ///< Double-precision floating point. 65 PS_TYPE_C32 = 0x0808, ///< Complex numbers consisting of single-precision floating point. 66 PS_TYPE_C64 = 0x0810, ///< Complex numbers consisting of double-precision floating point. 67 PS_TYPE_PTR = 0x0000 ///< Something else that's not supported for arithmetic. 57 PS_TYPE_S8 = 0x0101, // /< Character. 58 PS_TYPE_S16 = 0x0102, // /< Short integer. 59 PS_TYPE_S32 = 0x0104, // /< Integer. 60 PS_TYPE_S64 = 0x0108, // /< Long integer. 61 PS_TYPE_U8 = 0x0301, // /< Unsigned character. 62 PS_TYPE_U16 = 0x0302, // /< Unsigned short integer. 63 PS_TYPE_U32 = 0x0304, // /< Unsigned integer. 64 PS_TYPE_U64 = 0x0308, // /< Unsigned long integer. 65 PS_TYPE_F32 = 0x0404, // /< Single-precision Floating point. 66 PS_TYPE_F64 = 0x0408, // /< Double-precision floating point. 67 PS_TYPE_C32 = 0x0808, // /< Complex numbers consisting of single-precision floating 68 // point. 69 PS_TYPE_C64 = 0x0810, // /< Complex numbers consisting of double-precision floating 70 // point. 71 PS_TYPE_PTR = 0x0000 // /< Something else that's not supported for arithmetic. 68 72 } psElemType; 69 73 70 # define PS_TYPE_MASK PS_TYPE_U8 ///< the psElemType to use for mask image71 # define PS_TYPE_MASK_DATA U8 ///< the data member to use for mask image72 # define PS_TYPE_MASK_NAME "psU8"73 typedef psU8 psMaskType; ///< the C datatype for a mask image74 # define PS_TYPE_MASK PS_TYPE_U8 // /< the psElemType to use for mask image 75 # define PS_TYPE_MASK_DATA U8 // /< the data member to use for mask image 76 # define PS_TYPE_MASK_NAME "psU8" 77 typedef psU8 psMaskType; // /< the C datatype for a mask image 74 78 75 # define PS_MIN_S8 INT8_MIN76 # define PS_MIN_S16 INT16_MIN77 # define PS_MIN_S32 INT32_MIN78 # define PS_MIN_S64 INT64_MIN79 # define PS_MIN_U8 080 # define PS_MIN_U16 081 # define PS_MIN_U32 082 # define PS_MIN_U64 083 # define PS_MIN_F32 -FLT_MAX84 # define PS_MIN_F64 -DBL_MAX85 # define PS_MIN_C32 -FLT_MAX86 # define PS_MIN_C64 -DBL_MAX79 # define PS_MIN_S8 INT8_MIN 80 # define PS_MIN_S16 INT16_MIN 81 # define PS_MIN_S32 INT32_MIN 82 # define PS_MIN_S64 INT64_MIN 83 # define PS_MIN_U8 0 84 # define PS_MIN_U16 0 85 # define PS_MIN_U32 0 86 # define PS_MIN_U64 0 87 # define PS_MIN_F32 -FLT_MAX 88 # define PS_MIN_F64 -DBL_MAX 89 # define PS_MIN_C32 -FLT_MAX 90 # define PS_MIN_C64 -DBL_MAX 87 91 88 # define PS_MAX_S8 INT8_MAX89 # define PS_MAX_S16 INT16_MAX90 # define PS_MAX_S32 INT32_MAX91 # define PS_MAX_S64 INT64_MAX92 # define PS_MAX_U8 UINT8_MAX93 # define PS_MAX_U16 UINT16_MAX94 # define PS_MAX_U32 UINT32_MAX95 # define PS_MAX_U64 UINT64_MAX96 # define PS_MAX_F32 FLT_MAX97 # define PS_MAX_F64 DBL_MAX98 # define PS_MAX_C32 FLT_MAX99 # define PS_MAX_C64 DBL_MAX92 # define PS_MAX_S8 INT8_MAX 93 # define PS_MAX_S16 INT16_MAX 94 # define PS_MAX_S32 INT32_MAX 95 # define PS_MAX_S64 INT64_MAX 96 # define PS_MAX_U8 UINT8_MAX 97 # define PS_MAX_U16 UINT16_MAX 98 # define PS_MAX_U32 UINT32_MAX 99 # define PS_MAX_U64 UINT64_MAX 100 # define PS_MAX_F32 FLT_MAX 101 # define PS_MAX_F64 DBL_MAX 102 # define PS_MAX_C32 FLT_MAX 103 # define PS_MAX_C64 DBL_MAX 100 104 101 105 /// Macro to get the bad pixel reason code (stored as part of mask value) 102 # define PS_BADPIXEL_BITMASK 0x0f103 # define PS_GET_BADPIXEL(maskValue) (maskValue & PS_BADPIXEL_BITMASK)106 # define PS_BADPIXEL_BITMASK 0x0f 107 # define PS_GET_BADPIXEL(maskValue) (maskValue & PS_BADPIXEL_BITMASK) 104 108 105 # define PS_IS_BADPIXEL(maskValue) (PS_GET_BADPIXEL(maskValue) != 0)109 # define PS_IS_BADPIXEL(maskValue) (PS_GET_BADPIXEL(maskValue) != 0) 106 110 107 111 /// Macro to apply a bad pixel reason code to mask image 108 # define PS_SET_BADPIXEL(maskValue, reasonCode) \112 # define PS_SET_BADPIXEL(maskValue, reasonCode) \ 109 113 { \ 110 114 maskValue = (psMaskType)((reasonCode & PS_BADPIXEL_BITMASK) | (maskValue & ~PS_BADPIXEL_BITMASK)); \ … … 112 116 113 117 /// Macro to determine if the psElemType is an integer. 114 # define PS_IS_PSELEMTYPE_INT(x) ((x & 0x100) == 0x100)118 # define PS_IS_PSELEMTYPE_INT(x) ((x & 0x100) == 0x100) 115 119 /// Macro to determine if the psElemType is unsigned. 116 # define PS_IS_PSELEMTYPE_UNSIGNED(x) ((x & 0x200) == 0x200)120 # define PS_IS_PSELEMTYPE_UNSIGNED(x) ((x & 0x200) == 0x200) 117 121 /// Macro to determine if the psElemType is a real (non-complex) floating-point type. 118 # define PS_IS_PSELEMTYPE_REAL(x) ((x & 0x400) == 0x400)122 # define PS_IS_PSELEMTYPE_REAL(x) ((x & 0x400) == 0x400) 119 123 /// Macro to determine if the psElemType is complex number type. 120 # define PS_IS_PSELEMTYPE_COMPLEX(x) ((x & 0x800) == 0x800)124 # define PS_IS_PSELEMTYPE_COMPLEX(x) ((x & 0x800) == 0x800) 121 125 /// Macro to determine the storage size, in bytes, of the psElemType. 122 # define PSELEMTYPE_SIZEOF(x) ( (x==PS_TYPE_PTR) ? sizeof(void*) :(x & 0xFF) )126 # define PSELEMTYPE_SIZEOF(x) ( (x==PS_TYPE_PTR) ? sizeof(void*) :(x & 0xFF) ) 123 127 124 128 /** Dimensions of a data type. … … 128 132 */ 129 133 typedef enum { 130 PS_DIMEN_SCALAR, ///< Scalar.131 PS_DIMEN_VECTOR, ///< Vector.132 PS_DIMEN_TRANSV, ///< Transposed vector.133 PS_DIMEN_IMAGE, ///< Image.134 PS_DIMEN_OTHER ///< Something else that's not supported for arithmetic.134 PS_DIMEN_SCALAR, // /< Scalar. 135 PS_DIMEN_VECTOR, // /< Vector. 136 PS_DIMEN_TRANSV, // /< Transposed vector. 137 PS_DIMEN_IMAGE, // /< Image. 138 PS_DIMEN_OTHER // /< Something else that's not supported for arithmetic. 135 139 } psDimen; 136 140 … … 143 147 typedef struct 144 148 { 145 psElemType type; ///< Primitive type.146 psDimen dimen; ///< Dimensionality.149 psElemType type; // /< Primitive type. 150 psDimen dimen; // /< Dimensionality. 147 151 } 148 152 psType;
Note:
See TracChangeset
for help on using the changeset viewer.
