The following commit has been merged in the master branch: commit e565a771aa7241d91dab2a6b103970614629e007 Author: Marek Lindner lindner_marek@yahoo.de Date: Sun Feb 11 21:48:38 2007 +0100
memory usage overview added
diff --git a/allocate.c b/allocate.c index 707313b..46e4164 100644 --- a/allocate.c +++ b/allocate.c @@ -26,6 +26,7 @@ #include "allocate.h"
#define DEBUG_MALLOC +/*#define MEMORY_USAGE*/
#define MAGIC_NUMBER 0x12345678
@@ -36,22 +37,123 @@ struct chunkHeader *chunkList = NULL; struct chunkHeader { struct chunkHeader *next; - unsigned int length; - int tag; - unsigned int magicNumber; + uint32_t length; + int32_t tag; + uint32_t magicNumber; };
struct chunkTrailer { - unsigned int magicNumber; + uint32_t magicNumber; };
+ + +#if defined MEMORY_USAGE + +struct memoryUsage *memoryList = NULL; + + +struct memoryUsage +{ + struct memoryUsage *next; + uint32_t length; + uint32_t counter; + int32_t tag; +}; + + +void addMemory( uint32_t length, int32_t tag ) { + + struct memoryUsage *walker; + + + for ( walker = memoryList; walker != NULL; walker = walker->next ) { + + if ( walker->tag == tag ) { + + walker->counter++; + break; + + } + + } + + if ( walker == NULL ) { + + walker = malloc( sizeof(struct memoryUsage) ); + + walker->length = length; + walker->tag = tag; + walker->counter = 1; + + walker->next = memoryList; + memoryList = walker; + + } + +} + + +void removeMemory( int32_t tag, int32_t freetag ) { + + struct memoryUsage *walker; + + + for ( walker = memoryList; walker != NULL; walker = walker->next ) { + + if ( walker->tag == tag ) { + + if ( walker->counter == 0 ) { + + fprintf( stderr, "Freeing more memory than was allocated: malloc tag = %d, free tag = %d\n", tag, freetag ); + exit(1); + + } + + walker->counter--; + break; + + } + + } + + if ( walker == NULL ) { + + fprintf( stderr, "Freeing memory that was never allocated: malloc tag = %d, free tag = %d\n", tag, freetag ); + exit(1); + + } + +} + +#endif + + + void checkIntegrity(void) { struct chunkHeader *walker; struct chunkTrailer *chunkTrailer; unsigned char *memory;
+ +#if defined MEMORY_USAGE + + struct memoryUsage *memoryWalker; + + fprintf( stderr, "Memory usage information:\n" ); + + for ( memoryWalker = memoryList; memoryWalker != NULL; memoryWalker = memoryWalker->next ) { + + if ( memoryWalker->counter != 0 ) + fprintf( stderr, " tag: %i, num malloc: %i, bytes per malloc: %i, total: %i\n", memoryWalker->tag, memoryWalker->counter, memoryWalker->length, memoryWalker->counter * memoryWalker->length ); + + } + +#endif + + for (walker = chunkList; walker != NULL; walker = walker->next) { if (walker->magicNumber != MAGIC_NUMBER) @@ -80,7 +182,7 @@ void checkLeak(void) fprintf(stderr, "Memory leak detected, malloc tag = %d\n", walker->tag); }
-void *debugMalloc(unsigned int length, int tag) +void *debugMalloc(uint32_t length, int32_t tag) { unsigned char *memory; struct chunkHeader *chunkHeader; @@ -110,16 +212,22 @@ void *debugMalloc(unsigned int length, int tag) chunkHeader->next = chunkList; chunkList = chunkHeader;
+#if defined MEMORY_USAGE + + addMemory( length, tag ); + +#endif + return chunk; }
-void *debugRealloc(void *memoryParameter, unsigned int length, int tag) +void *debugRealloc(void *memoryParameter, uint32_t length, int32_t tag) { unsigned char *memory; struct chunkHeader *chunkHeader; struct chunkTrailer *chunkTrailer; unsigned char *result; - unsigned int copyLength; + uint32_t copyLength;
if (memoryParameter) { /* if memoryParameter==NULL, realloc() should work like malloc() !! */ memory = memoryParameter; @@ -203,7 +311,14 @@ void debugFree(void *memoryParameter, int tag) exit(1); }
+#if defined MEMORY_USAGE + + removeMemory( chunkHeader->tag, tag ); + +#endif + free(chunkHeader); + }
#else @@ -216,7 +331,7 @@ void checkLeak(void) { }
-void *debugMalloc(unsigned int length, int tag) +void *debugMalloc(uint32_t length, int32_t tag) { void *result;
@@ -231,7 +346,7 @@ void *debugMalloc(unsigned int length, int tag) return result; }
-void *debugRealloc(void *memory, unsigned int length, int tag) +void *debugRealloc(void *memory, uint32_t length, int32_t tag) { void *result;
@@ -246,7 +361,7 @@ void *debugRealloc(void *memory, unsigned int length, int tag) return result; }
-void debugFree(void *memory, int tag) +void debugFree(void *memory, int32_t tag) { free(memory); } diff --git a/allocate.h b/allocate.h index 4c15bfc..6f610ec 100644 --- a/allocate.h +++ b/allocate.h @@ -19,8 +19,12 @@
+#include <stdint.h> + + + void checkIntegrity(void); void checkLeak(void); -void *debugMalloc(unsigned int length, int tag); -void *debugRealloc(void *memory, unsigned int length, int tag); -void debugFree(void *memoryParameter, int tag); +void *debugMalloc(uint32_t length, int32_t tag); +void *debugRealloc(void *memory, uint32_t length, int32_t tag); +void debugFree(void *memoryParameter, int32_t tag); diff --git a/posix-specific.c b/posix-specific.c index 149bcfc..2e3ec56 100644 --- a/posix-specific.c +++ b/posix-specific.c @@ -336,7 +336,7 @@ void apply_init_args( int argc, char *argv[] ) { uint32_t vis_server = 0; // fd_set wait_sockets, tmp_wait_sockets;
- memset(&tmp_ip_holder, 0, sizeof (struct in_addr)); + memset( &tmp_ip_holder, 0, sizeof (struct in_addr) );
printf( "WARNING: You are using the unstable batman branch. If you are interested in *using* batman get the latest stable release !\n" ); @@ -1003,8 +1003,8 @@ void *client_to_gw_tun( void *arg ) { struct curr_gw_data *curr_gw_data = (struct curr_gw_data *)arg; struct sockaddr_in gw_addr, my_addr, sender_addr; struct timeval tv; - int res, max_sock, status, buff_len, curr_gateway_tcp_sock, curr_gateway_tun_sock, curr_gateway_tun_fd, server_keep_alive_timeout; - unsigned int addr_len; + int32_t res, max_sock, status, buff_len, curr_gateway_tcp_sock, curr_gateway_tun_sock, curr_gateway_tun_fd, server_keep_alive_timeout; + uint32_t addr_len; char curr_gateway_tun_if[IFNAMSIZ], keep_alive_string[] = "ping\0"; unsigned char buff[1500]; fd_set wait_sockets, tmp_wait_sockets; @@ -1239,6 +1239,7 @@ int8_t add_default_route() { if ( pthread_create( &curr_gateway_thread_id, NULL, &client_to_gw_tun, curr_gw_data ) != 0 ) {
debug_output( 0, "Error - couldn't spawn thread: %s\n", strerror(errno) ); + debugFree( curr_gw_data, 247 ); curr_gateway = NULL;
} @@ -1396,8 +1397,8 @@ void *gw_listen( void *arg ) { struct in_addr tmp_ip_holder; socklen_t sin_size = sizeof(struct sockaddr_in); char gw_addr[16], str2[16], tun_dev[IFNAMSIZ], tun_ip[] = "104.255.255.254\0"; - int res, status, max_sock_min, max_sock, buff_len, tun_fd; - unsigned int addr_len, client_timeout; + int32_t res, status, max_sock_min, max_sock, buff_len, tun_fd; + uint32_t addr_len, client_timeout; unsigned char buff[1500]; fd_set wait_sockets, tmp_wait_sockets;
diff --git a/posix.c b/posix.c index f14090e..22772e9 100644 --- a/posix.c +++ b/posix.c @@ -51,26 +51,26 @@ extern struct vis_if vis_if;
static struct timeval start_time; -static short stop; +static int8_t stop;
-static void get_time_internal(struct timeval *tv) -{ - int sec; - int usec; - gettimeofday(tv, NULL); +static void get_time_internal( struct timeval *tv ) { + + int32_t sec, usec; + + gettimeofday( tv, NULL );
sec = tv->tv_sec - start_time.tv_sec; usec = tv->tv_usec - start_time.tv_usec;
- if (usec < 0) - { + if ( usec < 0 ) { sec--; usec += 1000000; }
tv->tv_sec = sec; tv->tv_usec = usec; + }
uint32_t get_time( void ) { @@ -124,14 +124,14 @@ void sym_print( char x, char y, char *z ) {
-void bat_wait( int T, int t ) { +void bat_wait( int32_t T, int32_t t ) {
struct timeval time;
time.tv_sec = T; time.tv_usec = ( t * 10000 );
- select(0, NULL, NULL, NULL, &time); + select( 0, NULL, NULL, NULL, &time );
return;
@@ -219,12 +219,16 @@ int8_t is_aborted() {
}
+ + void addr_to_string( uint32_t addr, char *str, int32_t len ) {
- inet_ntop(AF_INET, &addr, str, len); + inet_ntop( AF_INET, &addr, str, len );
}
+ + int32_t rand_num( int32_t limit ) {
return ( limit == 0 ? 0 : rand() % limit ); @@ -232,6 +236,7 @@ int32_t rand_num( int32_t limit ) { }
+ static void handler( int32_t sig ) {
stop = 1;