### F AST Memory Management and Utility Functions

AST provides a memory management layer that can be used in place of system functions such as `malloc`, `free`, `realloc`, etc. The AST replacements for these functions ( `astMalloc`, `astFree` and `astRealloc`) add extra information to each allocated memory block that allows AST to check the validity of supplied pointers. For example, this extra information allows `astFree` to detect if the supplied pointer has already been freed, and if so to issue an appropriate error message. The existence of this extra information is invisible to outside callers, and stored in a header block located just before the returned memory block.

In addition to the standard functions, AST provides other memory management functions, such as:

`astStore`
  - stores data in dynamically allocated memory, allocating the memory (or adjusting the size of previously allocated memory) to match the amount of data to be stored.
`astGrow`
  - allocates and expands memory to hold an adjustable-sized array.
`astAppendString`
  - allocates and expands memory to hold a concatenated string.

Theses are just a few of the available utilities functions in the AST memory management layer. Prototypes for all AST memory management functions are included in the header file “`ast.h`”.

An important restriction on these functions is that pointers created by other memory management functions, such as the system version of `malloc` etc., should never supplied to an AST memory management function. Only pointers created by AST should be used by these functions.

In addition to memory management functions, AST provides various other utility functions, such as a basic regular expression facility, and other string manipulation functions. These are also documented in this appendix.

The AST memory management layer is implemented on top of the usual `malloc`, tt free and `realloc` functions. By default these will be the standard functions provided by <stdlib.h>. However, the facilities of the STARMEM package (included in the Starlink Software Collection) can be used to specify alternative functions to use. This requires that AST be configured using the “–with-starmem” option when it is built.

The STARMEM package provides a wrapper for the standard malloc implementation that enables the user to switch malloc schemes at runtime by setting the STARMEM_MALLOC environment variable. Currently allowed values for this variable are:

SYSTEM
  - standard system malloc/free - the default
DL
  - Doug Lea’s malloc/free
GC
  - Hans-Boehm Garbage Collection

Copyright (C) 2021 East Asian Observatory