#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 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_t * | new_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. | |
|
|
The alignment requirement of allocated memory blocks. It must be a power of two. |
|
|
The expression to return the caller address. print_position will later on use this address to print the position information of memory operation points. |
|
|
The action to take when an error occurs.
The default behaviour is to call abort, unless |
|
|
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 |
|
|
The hash function for the pointers. This one has good performance in test for me. |
|
|
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. |
|
|
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 |
|
|
Macro to indicate whether redefinition of
Here it is defined to |
|
|
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. |
|
|
Gets the aligned value of memory block size.
|
|
|
Checks for memory leaks.
|
|
||||||||||||||||
|
Frees memory and adjusts pointers relating to a raw pointer.
If the highest bit of
|
|
||||||||||||
|
|
|
||||||||||||||||
|
|
|
|
|
|
||||||||||||
|
|
|
||||||||||||||||
|
|
|
|
|
|
||||||||||||
|
|
|
|
|
|
||||||||||||||||
|
|
|
||||||||||||
|
|
|
|
|
|
||||||||||||||||
|
|
|
||||||||||||
|
Prints the position information of a memory operation point.
When
|
|
|
Tries printing the position information from an instruction address. This is the version that uses addr2line.
|
|
||||||||||||
|
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.
|
|
|
The extra memory allocated by
|
|
|
Flag to control whether check_leaks will be automatically called on program exit.
|
|
|
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 |
|
|
The mutex guard to protect simultaneous output to new_output_fp.
|
|
|
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 |
|
|
Array of pointer lists of a hash value.
|
|
|
Array of mutex guards to protect simultaneous access to the pointer lists of a hash value.
|
|
|
Flag to control whether verbose messages are output.
|
|
|
Total memory allocated in bytes.
|
1.3.9.1