ORTS

sr_mem_control.cpp File Reference

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <SR/sr_mem_control.h>
#include <SR/sr_output.h>
#include <inttypes.h>

Go to the source code of this file.

Classes

struct  srMemData

Functions

static int cmp_func (const void *pt1, const void *pt2)
static void sort ()
void * sr_mem_control_alloc (const char *type, char *file, int line, int size, void *addr)
bool sr_mem_control_free (char *file, int line, void *addr)
void * sr_mem_control_realloc (char *file, int line, int size, void *addr)
void sr_memory_report ()
unsigned sr_memory_allocated ()
void sr_memory_report_output (SrOutput &o)

Variables

static bool changed = false
static srMemDatamem_data = 0
static int mem_data_size = 0
static int mem_data_capacity = 0
static unsigned mem_bytes_used = 0
static SrOutputoutput = sr_out

Function Documentation

static int cmp_func ( const void *  pt1,
const void *  pt2 
) [static]

Definition at line 41 of file sr_mem_control.cpp.

References srMemData::file, srMemData::size, and srMemData::type.

Referenced by sort().

void* sr_mem_control_alloc ( const char *  type,
char *  file,
int  line,
int  size,
void *  addr 
)

This function saves in an internal buffer the given parameters in order to keep track of allocated memory. It may be used by replacing new/malloc calls to:

 sr_mem_control_alloc("X",__FILE__,__LINE__,sizeof(X),new X) 

Then sr_memory_report() can be called to see all allocated data that was not freed. It is better to use this function with the macros SR_NEW and SR_MALLOC.

Definition at line 60 of file sr_mem_control.cpp.

References changed, mem_bytes_used, mem_data_capacity, mem_data_size, srMemData::set(), and size.

Referenced by sr_mem_control_realloc().

bool sr_mem_control_free ( char *  file,
int  line,
void *  addr 
)

This function erases from the internal buffer the given address of memory, that was previously registered with sr_mem_control_alloc(). It is to be used replacing delete/free calls in the following way:

 sr_mem_control_free(__FILE__,__LINE__,X); delete X; 

It is better to use this function with the macros SR_DEL, SR_DELA and SR_FREE.

Definition at line 86 of file sr_mem_control.cpp.

References changed, SrOutput::fatal_error(), mem_bytes_used, mem_data_size, and srMemData::size.

Referenced by sr_mem_control_realloc().

void* sr_mem_control_realloc ( char *  file,
int  line,
int  size,
void *  addr 
)

This function performs a call to realloc(addr,size), and returns the new pointer. But it also updates the pointers saved in the internal buffer in order to keep track of allocated memory. It may be used replacing realloc calls in the following way:

 pt=sr_mem_control_realloc(__FILE__,__LINE__,newsize,pt) 

It is better to use this function with the macro SR_REALLOC.

Definition at line 103 of file sr_mem_control.cpp.

References sr_mem_control_alloc(), and sr_mem_control_free().

unsigned sr_memory_allocated ( )

Returns the total of memory allocated. Only the memory allocated using sr_mem_control_alloc/free/realloc, or the related macros are taken into account.

Definition at line 127 of file sr_mem_control.cpp.

References mem_bytes_used.

void sr_memory_report ( )

This function gives a report about all allocated data that was not freed using sr_out. But to make it work, the user needs to change all new/malloc/delete/free calls by calls to the macros SR_NEW, SR_MALLOC, SR_REALLOC, SR_DEL, SR_DELA, SR_FREE or functions sr_mem_control_alloc/free/realloc.

Definition at line 110 of file sr_mem_control.cpp.

References srMemData::addr, srMemData::file, srMemData::line, mem_bytes_used, mem_data_size, SrOutput::put(), SrOutput::putf(), srMemData::size, sort(), srnl, and srMemData::type.

void sr_memory_report_output ( SrOutput o)

Changes the output used for the reports.

Definition at line 132 of file sr_mem_control.cpp.


Variable Documentation

unsigned mem_bytes_used = 0 [static]
srMemData* mem_data = 0 [static]

Definition at line 35 of file sr_mem_control.cpp.

int mem_data_capacity = 0 [static]

Definition at line 37 of file sr_mem_control.cpp.

Referenced by sr_mem_control_alloc().

int mem_data_size = 0 [static]
SrOutput& output = sr_out [static]

Definition at line 39 of file sr_mem_control.cpp.

Referenced by TR_PFEngine::find_path().


Generated on Fri May 18 2012 03:02:55 for ORTS by Doxygen1.7.3