#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:

| Classes | |
| struct | new_ptr_list_t | 
| Structure to store the position information where newoccurs.  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 44 | 
| The length of file name stored if greater than zero. | |
| #define | _DEBUG_NEW_PROGNAME NULL | 
| The program (executable) name to be set at compile time. | |
| #define | _DEBUG_NEW_STD_OPER_NEW 1 | 
| Macro to indicate whether the standard-conformant behaviour of operator newis wanted. | |
| #define | _DEBUG_NEW_TAILCHECK 0 | 
| Macro to indicate whether a writing-past-end check will be performed. | |
| #define | _DEBUG_NEW_TAILCHECK_CHAR 0xCC | 
| Value of the padding bytes at the end of a memory block. | |
| #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 newis wanted. | |
| #define | align(s) (((s) + _DEBUG_NEW_ALIGNMENT - 1) & ~(_DEBUG_NEW_ALIGNMENT - 1)) | 
| Gets the aligned value of memory block size. | |
| Functions | |
| static bool | print_position_from_addr (const void *addr) | 
| Tries printing the position information from an instruction address. | |
| static void | print_position (const void *ptr, int line) | 
| Prints the position information of a memory operation point. | |
| static bool | check_tail (new_ptr_list_t *ptr) | 
| Checks whether the padding bytes at the end of a memory block is tampered with. | |
| static void * | alloc_mem (size_t size, const char *file, int line, bool is_array) | 
| Allocates memory and initializes control data. | |
| static void | free_pointer (void *pointer, void *addr, bool is_array) | 
| Frees memory and adjusts pointers. | |
| int | check_leaks () | 
| Checks for memory leaks. | |
| int | check_mem_corruption () | 
| Checks for heap corruption. | |
| 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 unsigned | MAGIC = 0x4442474E | 
| Magic number for error detection. | |
| const int | ALIGNED_LIST_ITEM_SIZE = align(sizeof(new_ptr_list_t)) | 
| The extra memory allocated by operator new. | |
| static new_ptr_list_t | new_ptr_list | 
| List of all new'd pointers. | |
| static fast_mutex | new_ptr_lock | 
| The mutex guard to protect simultaneous access to the pointer list. | |
| static fast_mutex | new_output_lock | 
| The mutex guard to protect simultaneous output to new_output_fp. | |
| static 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. | |
| #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 44 | 
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). The current default value makes the size of new_ptr_list_t 64 on 32-bit platforms. 
| #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_STD_OPER_NEW 1 | 
Macro to indicate whether the standard-conformant behaviour of operator new is wanted. 
It is on by default now, but the user may set it to 0 to revert to the old behaviour. 
| #define _DEBUG_NEW_TAILCHECK 0 | 
Macro to indicate whether a writing-past-end check will be performed.
Define it to a positive integer as the number of padding bytes at the end of a memory block for checking.
| #define _DEBUG_NEW_TAILCHECK_CHAR 0xCC | 
Value of the padding bytes at the end of a memory block.
| #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 | ) | (((s) + _DEBUG_NEW_ALIGNMENT - 1) & ~(_DEBUG_NEW_ALIGNMENT - 1)) | 
Gets the aligned value of memory block size.
| static void* alloc_mem | ( | size_t | size, | |
| const char * | file, | |||
| int | line, | |||
| bool | is_array | |||
| ) |  [static] | 
Allocates memory and initializes control data.
| size | size of the required memory block | |
| file | null-terminated string of the file name | |
| line | line number | |
| is_array | boolean value whether this is an array operation | 
NULL if memory allocation is not successful | int check_leaks | ( | ) | 
Checks for memory leaks.
| int check_mem_corruption | ( | ) | 
Checks for heap corruption.
| static bool check_tail | ( | new_ptr_list_t * | ptr | ) |  [static] | 
Checks whether the padding bytes at the end of a memory block is tampered with.
| ptr | pointer to a new_ptr_list_t struct | 
true if the padding bytes are untouched; false otherwise | static void free_pointer | ( | void * | pointer, | |
| void * | addr, | |||
| bool | is_array | |||
| ) |  [static] | 
Frees memory and adjusts pointers.
| pointer | pointer to delete | |
| addr | pointer to the caller | |
| is_array | 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 | |||
| ) | 
| static 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.
| 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 | 
| static 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.
| addr | the instruction address to convert and print | 
true if the address is converted successfully (and the result is printed); false if no useful information is got (and nothing is printed) 
| const int ALIGNED_LIST_ITEM_SIZE = align(sizeof(new_ptr_list_t)) | 
The extra memory allocated by operator new. 
| const unsigned MAGIC = 0x4442474E | 
Magic number for error detection.
| 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  [static] | 
| fast_mutex new_ptr_lock  [static] | 
The mutex guard to protect simultaneous access to the pointer list.
| 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.
 1.5.1
 1.5.1