# File memory.c

RCS Header: /home/amb/cxref/RCS/memory.c 1.12 1999/01/16 11:09:59 amb Exp

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

## Included Files

- `#include "autoconfig.h"`

- `#include <stdio.h>`

- `#include <stdlib.h>`

- `#include <string.h>`

- `#include <stdarg.h>`

- `#include <memory.h>`

- `#include "memory.h"`

## 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
| strcat(), strlen()
Called by: | CreateAppendix() | xref.c
| CrossReference() | xref.c
| CrossReferenceDelete() | xref.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
| 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