Main Page   Modules   Compound List   File List   Compound Members   File Members  

memory_stack.h File Reference

#include <gandalf/common/misc_defs.h>
#include <stddef.h>

Go to the source code of this file.

Compounds

struct  Gan_MemoryStack
 Structure to hold state of stack memory blocks. More...


Typedefs

typedef double Gan_BigType
 Big C type for byte alignment.

typedef Gan_MemoryStack Gan_MemoryStack
 Structure to hold state of stack memory blocks.


Functions

Gan_MemoryStackgan_memory_stack_form (Gan_MemoryStack *ms, int nblocks, size_t bsize)
 Initialise temporary memory allocation structure.

void * gan_ms_malloc (Gan_MemoryStack *ms, size_t size)
 Temporary memory allocation routine, faster than malloc().

void gan_ms_free (Gan_MemoryStack *ms, void *ptr)
 Temporary memory free routine.

void gan_ms_free_va (Gan_MemoryStack *ms, void *ptr,...)
 Frees a list of temporaray blocks terminated by NULL.

void gan_memory_stack_free (Gan_MemoryStack *ms)
 Frees all temporary memory.

void gan_memory_stack_clean (Gan_MemoryStack *ms)
 Frees unused temporary memory.

size_t gan_memory_stack_total (Gan_MemoryStack *ms)
 Returns the total temporary memory currently allocated.

Gan_MemoryStackgan_memory_stack_alloc (int nblocks, size_t bsize)
 Macro: Allocate and initialise temporary memory allocation structure.

void * gan_ms_malloc_object (Gan_MemoryStack *ms, TYPE obj_type)
 Macro: Dynamically allocate one item of a specific type with "stack" memory.

void * gan_ms_malloc_array (Gan_MemoryStack *ms, TYPE obj_type, size_t size)
 Macro: Dynamically allocate n items of a specific type with "stack" memory.


Detailed Description

Module: Stack-style first-in first-out memory allocation module

Part of: Gandalf Library

Revision: Last edited: Author:

Copyright: (c) 2002 Imagineer Software Limited


Function Documentation

void gan_memory_stack_clean Gan_MemoryStack   ms
 

Frees unused temporary memory.

Parameters:
ms Pointer to memory stack structure
Frees memory stack down to the last gan_ms_free() call.

Gan_MemoryStack* gan_memory_stack_form Gan_MemoryStack   ms,
int    nblocks,
size_t    bsize
 

Initialise temporary memory allocation structure.

Parameters:
ms Memoru stack structure pointer
nblocks Maximum number of blocks of memory to allow
bsize Size of each block in bytes
Returns:
Pointer to initialised structure, or NULL on failure.

void gan_memory_stack_free Gan_MemoryStack   ms
 

Frees all temporary memory.

Parameters:
ms Pointer to memory stack structure
Frees all memory allocated using calls to gan_ms_malloc().

size_t gan_memory_stack_total Gan_MemoryStack   ms
 

Returns the total temporary memory currently allocated.

Parameters:
ms Pointer to memory stack structure
Returns the total temporary memory currently allocated by calls to gan_ms_malloc().

void gan_ms_free Gan_MemoryStack   ms,
void *    ptr
 

Temporary memory free routine.

Parameters:
ms Pointer to memory stack structure
ptr Pointer to memory area to free, as returned by gan_ms_malloc()
Temporary memory free routine, Memory must be freed in reverse of the order it was allocated using gan_ms_malloc(). gan_ms_free() does not actually free any memory, but allows the marked memory to be used in subsequent gan_ms_malloc() calls. After using the temporary memory, call gan_memory_stack_free() to actually free the memory blocks.

void gan_ms_free_va Gan_MemoryStack   ms,
void *    ptr,
...   
 

Frees a list of temporaray blocks terminated by NULL.

Parameters:
ms Pointer to memory stack structure
ptr The first memory block to free
... List of other blocks to free, terminated by NULL
Frees a list of temporaray blocks allocated by gan_ms_malloc(), teminated by NULL. gan_ms_free() is called for each block, preserving the order of the arguments in the calls.

void* gan_ms_malloc Gan_MemoryStack   ms,
size_t    size
 

Temporary memory allocation routine, faster than malloc().

Parameters:
ms Pointer to memory stack structure
size Amount of memory to allocate in bytes
Returns:
Non NULL successfully allocated block, NULL on failure
For allocating chunks of memory which can be freed in reverse order, The module allocates large blocks at a time using malloc(), the blocks being "a lot" bigger than the chunks to be gan_ms_malloc()'d, to avoid wasting memory. Calls to gan_ms_malloc() then fill up the current block, and when the end is reached another block is malloc()'d. There's a dirty bit concerning memory alignment where I assume that everything should be aligned according to a "big" type (double). malloc() guarantees that it returns pointers that may be safely cast to any pointer type, but I (PFM) don't know how to do that properly in C.


Generated on Mon Oct 13 16:14:45 2003 by doxygen1.3-rc1