File memory.c

RCS Header: /home/amb/CVS/cxref/src/memory.c,v 1.13 2004-06-19 19:03:13 amb Exp

C Cross Referencing & Documentation tool. Version 1.6.
Memory management functions


Included Files


Preprocessor definitions

The amount of debugging, non-zero for totals, 2 for logging, 4 for printing each call.

#define DEBUG 0

The size of each of the heap allocations

#define HEAP_INC 8192

The size of a string that is large enough to have it's own mallocation.

#define SMALL_STRING 256


Typedef Heap

A private memory heap is used to reduce the number of malloc calls that are made, the Heap type is a pointer to this.

typedef struct _Heap* Heap

See: Type struct _Heap

Type struct _Heap

A structure containing all of the information about the private heap in a linked list.
struct _Heap

struct _Heap  
   {  
      char* mem; The memory that is private to the heap.
      Heap next; The next Heap structure.
   }  

Local Variables

first

Local variable to control the usage of the private heap; the first segment of memory on the private heap.

static Heap first

Used in: TidyMemory()  
  add_to_heap()  

heap_left

the amount of space left in the current heap segment.

static int heap_left

Used in: TidyMemory()  
  get_space()  

Global Function ConcatStrings()

A function to concatenate a number of strings.

char* ConcatStrings ( int n, char* s, ... )

char* ConcatStrings
Returns the a pointer to the new string.
int n
The number of strings
char* s
The first string.
...
The other strings, 'n' including 's'.

Any of the strings that are inputs can be NULL, in this case they are quietly ignored.

Prototyped in: memory.h
Calls: get_space() memory.c
  __builtin_va_arg(), __builtin_va_end(), __builtin_va_start(), strcat(), strlen()
Called by: CreateAppendix() xref.c
  CrossReference() xref.c
  CrossReferenceDelete() xref.c
  ParseOptions() cxref.c
  SeenFileChange() preproc.c
  SeenFuncIntComment() func.c
  SeenFunctionDefinition() func.c
  SeenInclude() preproc.c
  SeenStructUnionComp() type.c
  SplitComment() comment.c
  WriteHTMLAppendix() html.c
  WriteHTMLDocument() html.c
  WriteHTMLFile() html.c
  WriteHTMLFileDelete() html.c
  WriteHTMLSource() html.c
  WriteLatexAppendix() latex.c
  WriteLatexDocument() latex.c
  WriteLatexFile() latex.c
  WriteLatexFileDelete() latex.c
  WriteLatexTemplate() latex.c
  WriteRTFAppendix() rtf.c
  WriteRTFFile() rtf.c
  WriteRTFFileDelete() rtf.c
  WriteSGMLAppendix() sgml.c
  WriteSGMLFile() sgml.c
  WriteSGMLFileDelete() sgml.c
  main() cxref.c
  yyparse() parse-yacc.c

Global Function CopyString()

A function to copy a string on the local private memory heap.

char* CopyString ( char* x )

char* CopyString
Returns the copy of the string.
char* x
The string to be copied.

Prototyped in: memory.h
Calls: get_space() memory.c
  strcpy(), strlen()
Called by: ParseConfigFile() cxref.c
  SeenFileChange() preproc.c
  SplitComment() comment.c
  yylex() parse-lex.c
  yyparse() parse-yacc.c

Global Function PrintMemoryStatistics()

Prints out the number of mallocs / reallocs and frees.

void PrintMemoryStatistics ( void )

Prototyped in: memory.h
Called by: main() cxref.c

Global Function SafeCalloc()

A replacement calloc() function.

void* SafeCalloc ( unsigned int n, unsigned int size, char* file, int line )

void* SafeCalloc
Returns the address.
unsigned int n
The number of items to allocate.
unsigned int size
The size of the memory to allocate.
char* file
The file that the function is called from.
int line
The line number that the function is called from.

Prototyped in: memory.h
Calls: calloc(), printf()
Called by: NewDefineType() preproc.c
  NewFile() file.c
  NewFunctionType() func.c
  NewIncludeType() preproc.c
  NewStringList() slist.c
  NewStringList2() slist.c
  NewStructUnionType() type.c
  NewTypedefType() type.c
  NewVariableType() var.c

Global Function SafeFree()

A replacement free() function.

void SafeFree ( void* ptr, char* file, int line )

void* ptr
The pointer that is to be freed up.
char* file
The file that the function is called from.
int line
The line number that the function is called from.

Prototyped in: memory.h
Calls: free(), printf()
Called by: DeleteComment() comment.c
  DeleteDefineType() preproc.c
  DeleteFile() file.c
  DeleteFunctionType() func.c
  DeleteIncludeType() preproc.c
  DeleteStringList() slist.c
  DeleteStringList2() slist.c
  DeleteStructUnionType() type.c
  DeleteTypedefType() type.c
  DeleteVariableType() var.c
  ParseConfigFile() cxref.c
  ParseOptions() cxref.c
  ResetParser() parse-yacc.c
  ResetPreProcAnalyser() preproc.c
  ResetVariableAnalyser() var.c
  SeenFileChange() preproc.c
  SeenFuncIntComment() func.c
  SeenFunctionArg() func.c
  SeenFunctionDefinition() func.c
  SeenStructUnionComp() type.c
  SeenVariableDefinition() var.c
  SplitComment() comment.c
  TidyMemory() memory.c
  check_for_var_func() xref.c
  fixup_extern_var() xref.c
  html() html.c
  latex() latex.c
  main() cxref.c
  rtf() rtf.c
  sgml() sgml.c

Global Function SafeMalloc()

A replacement malloc() function.

void* SafeMalloc ( unsigned int size, char* file, int line )

void* SafeMalloc
Returns the address.
unsigned int size
The size of the memory to allocate.
char* file
The file that the function is called from.
int line
The line number that the function is called from.

Prototyped in: memory.h
Calls: malloc(), printf()
Called by: AddToStringList() slist.c
  AddToStringList2() slist.c
  CopyStructUnion() type.c
  ParseConfigFile() cxref.c
  ParseOptions() cxref.c
  SafeMallocString() memory.c
  SeenComment() comment.c
  SeenFileChange() preproc.c
  SeenStructUnionComp() type.c
  UpScope() var.c
  add_to_heap() memory.c
  html() html.c
  latex() latex.c
  main() cxref.c
  push() parse-yacc.c
  rtf() rtf.c
  sgml() sgml.c

Global Function SafeMallocString()

A function to copy a string on the public global heap.

char* SafeMallocString ( char* x, char* file, int line )

char* SafeMallocString
Returns the copy of the string.
char* x
The string to be copied.
char* file
The file that the function is called from.
int line
The line number that the function is called from.

Prototyped in: memory.h
Calls: SafeMalloc() memory.c
  strcpy(), strlen()
Called by: AddToStringList() slist.c
  AddToStringList2() slist.c
  CopyStructUnion() type.c
  NewDefineType() preproc.c
  NewFile() file.c
  NewFunctionType() func.c
  NewIncludeType() preproc.c
  NewStructUnionType() type.c
  NewTypedefType() type.c
  NewVariableType() var.c
  ParseOptions() cxref.c
  SeenDefine() preproc.c
  SeenDefineComment() preproc.c
  SeenDefineFuncArgComment() preproc.c
  SeenDefineValue() preproc.c
  SeenFileChange() preproc.c
  SeenFileComment() file.c
  SeenFuncIntComment() func.c
  SeenFunctionArg() func.c
  SeenFunctionDeclaration() func.c
  SeenFunctionDefinition() func.c
  SeenInclude() preproc.c
  SeenIncludeComment() preproc.c
  SeenStructUnionComp() type.c
  SeenStructUnionStart() type.c
  SeenTypedef() type.c
  SeenVariableDefinition() var.c
  SplitComment() comment.c
  check_for_caller() xref.c
  check_for_var() xref.c
  check_for_var_func() xref.c
  fixup_extern_var() xref.c
  main() cxref.c

Global Function SafeRealloc()

A replacement realloc() function.

void* SafeRealloc ( void* ptr, unsigned int size, char* file, int line )

void* SafeRealloc
Returns the address.
void* ptr
The old pointer.
unsigned int size
The size of the new memory to allocate.
char* file
The file that the function is called from.
int line
The line number that the function is called from.

Prototyped in: memory.h
Calls: printf(), realloc()
Called by: AddToStringList() slist.c
  AddToStringList2() slist.c
  ParseConfigFile() cxref.c
  ParseOptions() cxref.c
  SeenComment() comment.c
  SeenFileChange() preproc.c
  SeenStructUnionComp() type.c
  SetCurrentComment() comment.c
  UpScope() var.c
  html() html.c
  latex() latex.c
  main() cxref.c
  push() parse-yacc.c
  rtf() rtf.c
  sgml() sgml.c

Global Function TidyMemory()

Tidies up the local heap of memory.

void TidyMemory ( void )

Prototyped in: memory.h
Calls: SafeFree() memory.c
Called by: main() cxref.c
References Variables: first memory.c
  heap_left memory.c

Local Function add_to_heap()

Add some bytes to the privately maintained memory heap.

static Heap add_to_heap ( unsigned int l )

Heap add_to_heap
Returns a pointer to the required memory.
unsigned int l
The size of the memory that is required.

Prototyped in: memory.c
Calls: SafeMalloc() memory.c
Called by: get_space() memory.c
References Variables: first memory.c

Local Function get_space()

A function to get some memory for a string, allocate a new heap structure if needed.

static char* get_space ( unsigned int l )

char* get_space
Returns a pointer to enough space.
unsigned int l
The amount of space that is needed.

Prototyped in: memory.c
Calls: add_to_heap() memory.c
Called by: ConcatStrings() memory.c
  CopyString() memory.c
References Variables: heap_left memory.c