debug_new.cc File Reference

Back to the index.

Classes | Macros | Functions | Variables
debug_new.cc File Reference
#include <iostream>
#include <new>
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "thirdparty/fast_mutex.h"
#include "thirdparty/static_assert.h"
#include "thirdparty/debug_new.h"

Go to the source code of this file.

Classes

struct  new_ptr_list_t
 Part of Wu Yongwei's new/delete debug memory leak detector. More...
 

Macros

#define _DEBUG_NEW_ALIGNMENT   16
 
#define _DEBUG_NEW_CALLER_ADDRESS   NULL
 
#define _DEBUG_NEW_ERROR_ACTION   abort()
 
#define _DEBUG_NEW_FILENAME_LEN   20
 
#define _DEBUG_NEW_HASHTABLESIZE   16384
 
#define _DEBUG_NEW_HASH(p)   (((size_t)(p) >> 8) % _DEBUG_NEW_HASHTABLESIZE)
 
#define _DEBUG_NEW_PROGNAME   NULL
 
#define _DEBUG_NEW_USE_ADDR2LINE   0
 
#define _DEBUG_NEW_REDEFINE_NEW   0
 
#define align(s)   (((s) + _DEBUG_NEW_ALIGNMENT - 1) & ~(_DEBUG_NEW_ALIGNMENT - 1))
 

Functions

int check_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 ()
 

Variables

const int aligned_list_item_size = align(sizeof(new_ptr_list_t))
 
bool new_autocheck_flag = true
 
bool new_verbose_flag = false
 
FILE * new_output_fp = stderr
 
const char * new_progname = _DEBUG_NEW_PROGNAME
 

Macro Definition Documentation

◆ _DEBUG_NEW_ALIGNMENT

#define _DEBUG_NEW_ALIGNMENT   16

@ file debug_new.cpp

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

Version
3.16, 2005/11/22
Author
Wu Yongwei

The alignment requirement of allocated memory blocks. It must be a power of two.

Definition at line 85 of file debug_new.cc.

◆ _DEBUG_NEW_CALLER_ADDRESS

#define _DEBUG_NEW_CALLER_ADDRESS   NULL

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

Definition at line 99 of file debug_new.cc.

◆ _DEBUG_NEW_ERROR_ACTION

#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).

Definition at line 114 of file debug_new.cc.

◆ _DEBUG_NEW_FILENAME_LEN

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

Definition at line 133 of file debug_new.cc.

◆ _DEBUG_NEW_HASH

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

The hash function for the pointers. This one has good performance in test for me.

Definition at line 154 of file debug_new.cc.

◆ _DEBUG_NEW_HASHTABLESIZE

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

Definition at line 144 of file debug_new.cc.

◆ _DEBUG_NEW_PROGNAME

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

Definition at line 170 of file debug_new.cc.

◆ _DEBUG_NEW_REDEFINE_NEW

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

Definition at line 201 of file debug_new.cc.

◆ _DEBUG_NEW_USE_ADDR2LINE

#define _DEBUG_NEW_USE_ADDR2LINE   0

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.

Definition at line 185 of file debug_new.cc.

◆ align

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

Gets the aligned value of memory block size.

Definition at line 207 of file debug_new.cc.

Function Documentation

◆ check_leaks()

int check_leaks ( )

Checks for memory leaks.

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

Definition at line 497 of file debug_new.cc.

References _DEBUG_NEW_HASHTABLESIZE.

Referenced by internal_w(), and __debug_new_counter::~__debug_new_counter().

◆ operator delete()

void operator delete ( void *  pointer)
throw (
)

Definition at line 624 of file debug_new.cc.

References _DEBUG_NEW_HASH.

◆ operator delete[]()

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

Definition at line 643 of file debug_new.cc.

References _DEBUG_NEW_HASH.

◆ operator new() [1/3]

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

Definition at line 602 of file debug_new.cc.

References _DEBUG_NEW_CALLER_ADDRESS.

◆ operator new() [2/3]

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

Definition at line 525 of file debug_new.cc.

References _DEBUG_NEW_ALIGNMENT, aligned_list_item_size, and STATIC_ASSERT.

◆ operator new() [3/3]

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

Definition at line 613 of file debug_new.cc.

References _DEBUG_NEW_CALLER_ADDRESS.

◆ operator new[]() [1/3]

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

Definition at line 607 of file debug_new.cc.

References _DEBUG_NEW_CALLER_ADDRESS.

◆ operator new[]() [2/3]

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

Definition at line 592 of file debug_new.cc.

References aligned_list_item_size, and new_ptr_list_t::line.

◆ operator new[]() [3/3]

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

Definition at line 618 of file debug_new.cc.

References _DEBUG_NEW_CALLER_ADDRESS.

Variable Documentation

◆ aligned_list_item_size

const int aligned_list_item_size = align(sizeof(new_ptr_list_t))

The extra memory allocated by operator new.

Definition at line 234 of file debug_new.cc.

Referenced by operator new(), and operator new[]().

◆ new_autocheck_flag

bool new_autocheck_flag = true

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

Definition at line 261 of file debug_new.cc.

Referenced by __debug_new_counter::~__debug_new_counter().

◆ new_output_fp

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).

Definition at line 273 of file debug_new.cc.

Referenced by __debug_new_counter::~__debug_new_counter().

◆ new_progname

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("_");’.

Definition at line 283 of file debug_new.cc.

◆ new_verbose_flag

bool new_verbose_flag = false

Flag to control whether verbose messages are output.

Definition at line 266 of file debug_new.cc.

Referenced by __debug_new_counter::~__debug_new_counter().


Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18