static void memory_free( void *block ) { if ( allocations != NULL ) { char block_addr[32]; UChar u_block_addr[32]; snprintf( block_addr, 31, "%ld", (long) block ); str2ustr( block_addr,u_block_addr,32); if ( !hashmap_remove(allocations,u_block_addr) ) debug_report("double free!\n"); else free( block ); } else debug_report("allocations table is empty\n"); }
int main(int argc, char *argv[]) { // Read cmd line arguments if (!read_cmd_args(argc, argv)) { printf(XPP_HELP); return 1; } if (m_option.verbose) { printf(XPP_BANNER); } // Create output file if (m_option.output) { if (!create_output_file()) { return 2; } } // Report results. if (m_option.output) { if (m_option.verbose) { printf("done.\n"); } else { printf("XPP created %s\n", m_output_file); } } if (m_option.debug) { debug_report(); } return 0; // success }
void vp( void *p ) { if ( allocations != NULL ) { char block_addr[32]; if ( p == NULL ) debug_report("NULL pointer!\n"); else { UChar u_block_addr[32]; snprintf( block_addr, 31, "%ld", (long) p ); str2ustr( block_addr,u_block_addr,32); if ( !hashmap_contains(allocations,u_block_addr) ) { debug_report("invalid pointer %s!\n",block_addr); } } } }
static UChar *string_store( UChar *str ) { int len = u_strlen( str ); if ( strings==NULL ) { strings = malloc(STRING_BLOCK_SIZE); if ( strings != NULL && len*sizeof(UChar)<STRING_BLOCK_SIZE ) { allocated = STRING_BLOCK_SIZE; } else { debug_report("out of memory %s %d",__FILE__,__LINE__); exit( 0 ); } } else if ( len+highwater >= allocated ) { UChar *copy = malloc( allocated+len*sizeof(UChar)+STRING_BLOCK_SIZE ); if ( copy != NULL ) { memcpy( copy, strings, highwater ); free( strings ); strings = copy; allocated += len+STRING_BLOCK_SIZE ; } else { debug_report("out of memory %s %d",__FILE__,__LINE__); exit( 0 ); } } if ( strings != NULL ) { UChar *orig = &strings[highwater]; strcpy( &strings[highwater], str ); highwater += len+1; return orig; } else return NULL; }
/** * Print out details of any unfreed blocks or report that all blocks were freed */ void memory_print() { if ( allocations != NULL ) { int unfreed = 0; hashmap_iterator *iter = hashmap_iterator_create( allocations ); while ( hashmap_iterator_has_next(iter) ) { UChar *key = hashmap_iterator_next( iter ); UChar *value = hashmap_get( allocations, key ); debug_report("unfreed block %s at %s \n", key, value ); unfreed++; } if ( unfreed == 0 ) debug_report("all blocks were freed\n"); hashmap_dispose( allocations ); free( strings ); strings = NULL; } }
static void memory_store( void *block, const char *file, int line ) { if ( allocations == NULL ) allocations = hashmap_create(); if ( allocations != NULL ) { char block_addr[32]; char block_location[32]; snprintf( block_addr, 31, "%ld", (long) block ); snprintf( block_location, 31, "%s: %d", file, line ); char *value = string_store(block_location); hashmap_put( allocations, block_addr, value ); } else debug_report("failed to allocate memory allocations table\n"); }