next up previous contents index
Next: GRAPE Datatypes and Utility Up: GRAPE Previous: The GRAPE Function

Memory Management in GRAPE

 

To avoid problems with memory fragmentation and the different memory managements on different computer systems GRAPE has got its own memory management. The basic GRAPE function for allocating memory is

void *mem_alloc(size_t size)
which allocates a memory block of size size * sizeof(char) and returns the pointer to the block, if the allocation fails a NULL pointer is returned (the syntax is identical to the system malloc function). To free a block allocated by mem_alloc you have to use
void mem_free(void *ptr, size_t size)
where ptr is the address and size the size of the block to be freed. The difference to the system free function is that the size of the block to be freed also has to be passed to mem_free. This is necessary because it is (for historic reasons) possible to free only a part of an allocated block, though this is not recommended. To free a block without knowing its size you can use the special value G_BLOCKSIZE, but giving a size enables mem_free to check it. You can change the size of a block using
void *mem_realloc(void *ptr, size_t old_size, size_t new_size)
which basically is the same as allocating a new block, copying the data, freeing the original block and returning the new one. Though mem_realloc tries not to move the block, shrinking will never move it. In case of an error mem_realloc returns NULL and the old value of ptr will still be valid.

When working with GRAPE classes and datatypes it is necessary to use this memory management instead of the system functions -- of course it is not possible to mix both, freeing a block allocated with malloc with mem_free or vice versa will certainly result in an error (or crash). An advantage of the GRAPE functions to the system functions is that they provide a much better error checking: when an error occurs the type of the error and information about the last method called and the object it was called on is printed.

There are special memory functions based on these functions for most C and GRAPE datatypes, see section 10.2.1. When implementing a new datatype you should also provide the corresponding memory functions.


next up previous contents index
Next: GRAPE Datatypes and Utility Up: GRAPE Previous: The GRAPE Function

SFB 256 Universität Bonn and IAM Universität Freiburg

Copyright © by the Sonderforschungsbereich 256 at the Institut für Angewandte Mathematik, Universität Bonn.