Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

debug_new.cpp File Reference

Implementation of debug versions of new and delete to check leakage. More...

#include <new>
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fast_mutex.h"
#include "static_assert.h"
#include "debug_new.h"

Include dependency graph for debug_new.cpp:

Include dependency graph

Classes

struct  new_ptr_list_t
 Structure to store the position information where new occurs. More...

Defines

#define _DEBUG_NEW_ALIGNMENT   16
 The alignment requirement of allocated memory blocks.
#define _DEBUG_NEW_CALLER_ADDRESS   __builtin_return_address(0)
 The expression to return the caller address.
#define _DEBUG_NEW_ERROR_ACTION   abort()
 The action to take when an error occurs.
#define _DEBUG_NEW_FILENAME_LEN   20
 The length of file name stored if greater than zero.
#define _DEBUG_NEW_HASHTABLESIZE   16384
 The size of the hash bucket for the table to store pointers to allocated memory.
#define _DEBUG_NEW_HASH(p)   (((size_t)(p) >> 8) % _DEBUG_NEW_HASHTABLESIZE)
 The hash function for the pointers.
#define _DEBUG_NEW_PROGNAME   NULL
 The program (executable) name to be set at compile time.
#define _DEBUG_NEW_USE_ADDR2LINE   1
 Whether to use addr2line to convert a caller address to file/line information.
#define _DEBUG_NEW_REDEFINE_NEW   0
 Macro to indicate whether redefinition of new is wanted.
#define align(s)   (((s) + _DEBUG_NEW_ALIGNMENT - 1) & ~(_DEBUG_NEW_ALIGNMENT - 1))
 Gets the aligned value of memory block size.

Functions

bool print_position_from_addr (const void *addr)
 Tries printing the position information from an instruction address.
void print_position (const void *ptr, int line)
 Prints the position information of a memory operation point.
new_ptr_list_t ** search_pointer (void *pointer, size_t hash_index)
 Searches for the raw pointer given a user pointer.
void free_pointer (new_ptr_list_t **raw_ptr, void *addr, bool array_mode)
 Frees memory and adjusts pointers relating to a raw pointer.
int check_leaks ()
 Checks for memory leaks.
void * operator new (size_t size, const char *file, int line)
void * operator new[] (size_t size, const char *file, int line)
void * operator new (size_t size) throw (std::bad_alloc)
void * operator new[] (size_t size) throw (std::bad_alloc)
void * operator new (size_t size, const std::nothrow_t &) throw ()
void * operator new[] (size_t size, const std::nothrow_t &) throw ()
void operator delete (void *pointer) throw ()
void operator delete[] (void *pointer) throw ()
void operator delete (void *pointer, const char *file, int line) throw ()
void operator delete[] (void *pointer, const char *file, int line) throw ()
void operator delete (void *pointer, const std::nothrow_t &) throw ()
void operator delete[] (void *pointer, const std::nothrow_t &) throw ()

Variables

const int aligned_list_item_size = align(sizeof(new_ptr_list_t))
 The extra memory allocated by operator new.
new_ptr_list_tnew_ptr_list [_DEBUG_NEW_HASHTABLESIZE]
 Array of pointer lists of a hash value.
fast_mutex new_ptr_lock [_DEBUG_NEW_HASHTABLESIZE]
 Array of mutex guards to protect simultaneous access to the pointer lists of a hash value.
fast_mutex new_output_lock
 The mutex guard to protect simultaneous output to new_output_fp.
size_t total_mem_alloc = 0
 Total memory allocated in bytes.
bool new_autocheck_flag = true
 Flag to control whether check_leaks will be automatically called on program exit.
bool new_verbose_flag = false
 Flag to control whether verbose messages are output.
FILE * new_output_fp = stderr
 Pointer to the output stream.
const char * new_progname = _DEBUG_NEW_PROGNAME
 Pointer to the program name.

Detailed Description

Implementation of debug versions of new and delete to check leakage.

Version:
3.17, 2007/10/05
Author:
Wu Yongwei

Define Documentation

#define _DEBUG_NEW_ALIGNMENT   16
 

The alignment requirement of allocated memory blocks.

It must be a power of two.

#define _DEBUG_NEW_CALLER_ADDRESS   __builtin_return_address(0)
 

The expression to return the caller address.

print_position will later on use this address to print the position information of memory operation points.

#define _DEBUG_NEW_ERROR_ACTION   abort()
 

The action to take when an error occurs.

The default behaviour is to call abort, unless _DEBUG_NEW_ERROR_CRASH is defined, in which case a segmentation fault will be triggered instead (which can be useful on platforms like Windows that do not generate a core dump when abort is called).

#define _DEBUG_NEW_FILENAME_LEN   20
 

The length of file name stored if greater than zero.

If it is zero, only a const char pointer will be stored. Currently the default behaviour is to copy the file name, because I found that the exit leakage check cannot access the address of the file name sometimes (in my case, a core dump will occur when trying to access the file name in a shared library after a SIGINT). If the default value is too small for you, try defining it to 52, which makes the size of new_ptr_list_t 64 (it is 32 by default) on 32-bit platforms.

#define _DEBUG_NEW_HASH  )     (((size_t)(p) >> 8) % _DEBUG_NEW_HASHTABLESIZE)
 

The hash function for the pointers.

This one has good performance in test for me.

#define _DEBUG_NEW_HASHTABLESIZE   16384
 

The size of the hash bucket for the table to store pointers to allocated memory.

To ensure good performance, always make it a power of two.

#define _DEBUG_NEW_PROGNAME   NULL
 

The program (executable) name to be set at compile time.

It is better to assign the full program path to new_progname in main (at run time) than to use this (compile-time) macro, but this macro serves well as a quick hack. Note also that double quotation marks need to be used around the program name, i.e., one should specify a command-line option like -D_DEBUG_NEW_PROGNAME=\"a.out\" in bash, or -D_DEBUG_NEW_PROGNAME=\"a.exe\" in the Windows command prompt.

#define _DEBUG_NEW_REDEFINE_NEW   0
 

Macro to indicate whether redefinition of new is wanted.

Here it is defined to 0 to disable the redefinition of new.

#define _DEBUG_NEW_USE_ADDR2LINE   1
 

Whether to use addr2line to convert a caller address to file/line information.

Defining it to a non-zero value will enable the conversion (automatically done if GCC is detected). Defining it to zero will disable the conversion.

#define align  )     (((s) + _DEBUG_NEW_ALIGNMENT - 1) & ~(_DEBUG_NEW_ALIGNMENT - 1))
 

Gets the aligned value of memory block size.


Function Documentation

int check_leaks  ) 
 

Checks for memory leaks.

Returns:
zero if no leakage is found; the number of leaks otherwise

void free_pointer new_ptr_list_t **  raw_ptr,
void *  addr,
bool  array_mode
[static]
 

Frees memory and adjusts pointers relating to a raw pointer.

If the highest bit of line (set from a previous new[] call) does not agree with array_mode, program will abort with an error message.

Parameters:
raw_ptr raw pointer to free
addr pointer to the caller
array_mode flag indicating whether it is invoked by a delete[] call

void operator delete void *  pointer,
const std::nothrow_t & 
throw ()
 

void operator delete void *  pointer,
const char *  file,
int  line
throw ()
 

void operator delete void *  pointer  )  throw ()
 

void operator delete[] void *  pointer,
const std::nothrow_t & 
throw ()
 

void operator delete[] void *  pointer,
const char *  file,
int  line
throw ()
 

void operator delete[] void *  pointer  )  throw ()
 

void* operator new size_t  size,
const std::nothrow_t & 
throw ()
 

void* operator new size_t  size  )  throw (std::bad_alloc)
 

void* operator new size_t  size,
const char *  file,
int  line
 

void* operator new[] size_t  size,
const std::nothrow_t & 
throw ()
 

void* operator new[] size_t  size  )  throw (std::bad_alloc)
 

void* operator new[] size_t  size,
const char *  file,
int  line
 

void print_position const void *  ptr,
int  line
[static]
 

Prints the position information of a memory operation point.

When _DEBUG_NEW_USE_ADDR2LINE is defined to a non-zero value, this function will try to convert a given caller address to file/line information with addr2line.

Parameters:
ptr source file name if line is non-zero; caller address otherwise
line source line number if non-zero; indication that ptr is the caller address otherwise

bool print_position_from_addr const void *  addr  )  [static]
 

Tries printing the position information from an instruction address.

This is the version that uses addr2line.

Parameters:
addr the instruction address to convert and print
Returns:
true if the address is converted successfully (and the result is printed); false if no useful information is got (and nothing is printed)

new_ptr_list_t** search_pointer void *  pointer,
size_t  hash_index
[static]
 

Searches for the raw pointer given a user pointer.

The term `raw pointer' here refers to the pointer to the pointer to originally malloc'd memory.

Parameters:
pointer user pointer to search for
hash_index hash index of the user pointer
Returns:
the raw pointer if searching is successful; or NULL otherwise


Variable Documentation

const int aligned_list_item_size = align(sizeof(new_ptr_list_t))
 

The extra memory allocated by operator new.

bool new_autocheck_flag = true
 

Flag to control whether check_leaks will be automatically called on program exit.

FILE* new_output_fp = stderr
 

Pointer to the output stream.

The default output is stderr, and one may change it to a user stream if needed (say, new_verbose_flag is true and there are a lot of (de)allocations).

fast_mutex new_output_lock [static]
 

The mutex guard to protect simultaneous output to new_output_fp.

const char* new_progname = _DEBUG_NEW_PROGNAME
 

Pointer to the program name.

Its initial value is the macro _DEBUG_NEW_PROGNAME. You should try to assign the program path to it early in your application. Assigning argv[0] to it in main is one way. If you use bash or ksh (or similar), the following statement is probably what you want: `new_progname = getenv("_");'.

new_ptr_list_t* new_ptr_list[_DEBUG_NEW_HASHTABLESIZE] [static]
 

Array of pointer lists of a hash value.

fast_mutex new_ptr_lock[_DEBUG_NEW_HASHTABLESIZE] [static]
 

Array of mutex guards to protect simultaneous access to the pointer lists of a hash value.

bool new_verbose_flag = false
 

Flag to control whether verbose messages are output.

size_t total_mem_alloc = 0 [static]
 

Total memory allocated in bytes.


Generated on Sat Oct 6 08:27:24 2007 for Nvwa by  doxygen 1.3.9.1