Memory Allocation in FronTier

Most code's have their own methods and strategies for allocating memory. Ultimately memory is created through a call to the C-functions malloc() or mallopt()> However to have a system which requests big chunks of memory, and which distributes it within the code, is more efficient than frequent system calls.

FronTier's memory allocator is in "/util/vmalloc.c", while useful templates for getting memory are defined in "/util/vmalloc.h". This place is the ONLY place in FronTier, where memory for pointers is allocated. Standard C-functions, like free, have been redefined (as a macro). Therefore mixing the FronTier memory allocator, and standard use of malloc and free will be disastrous, unless one takes exceptional care in the include files.

The main high level call to memory is through the function array_T, which can return a pointer to an arbitrarily high dimensionally structured block of memory. Its use is restricted to the util library alone. Users only access it through specific macros: scalar, vector, matrix, tri_array,quad_array,quin_array,sex_array .

scalar(a,b)
produces one element to which a points, of size b.
Example: allocate space for one triangle
	{ 
	  TRI *triangle;
	  scalar(&triangle,sizeof(TRI));
	}
vector(a,b,c)
makes the pointer a point to b elements of size c.
Example: allocate space for 100 triangles
	{ 
	  TRI *triangle;
	  vector(&triangle,100,sizeof(TRI));
	}
matrix(a,b,c,d)
makes the pointer a point to a b by c matrix of elements of size c.
Example: allocate space for a 100 by 10 matrix of triangles
	{ 
	  TRI *triangle;
	  matrix(&triangle,100,10,sizeof(TRI));
	  /* there is therefore memory for triangle[99][3]*/
	}
etcetera
Memory thus requested is not freed unless specifically requested by the user. The command is free . Example:
	  free(triangle);
would release the space pointed to by triangle in all three examples above, and would in principle allow reuse by the memory allocator.

The current version of the memory allocator is however not very smart in its ability to effectively reuse space. For the next request it tries to find one single piece of free memory big enough to hold the current space request, or ask for more memory from the system. As a result, it is possible that the memory in use by FronTier. becomes fragmented, like swiss cheese, with lots of useless little holes. One method to counter balance memory fragmentation, is to make a fresh copy of a big structure, and free the original. The current time stepping algorithm does this implicitly already.



Back to Main Page


Comments?

Authors: Folkert Tangerman
Last revision: April 10, 1997

Copyright 1997 This article may be redistributed to other individuals for noncommercial use, provided that the entire text, all HTML codes, and this copyright notice remains intact and unaltered in any way. This article may not be resold, reprinted, or redistributed in whole or in part for compensation of any kind without prior written permission of the author.