next up previous contents index
Next: Fast Functions for Large Up: Memory Functions Previous: Memory Functions

Basic Memory Functions

All memory functions are based on the functions mem_alloc, mem_free and mem_realloc which are described in section 5.2.2. There are special memory functions for all normal datatypes and the GRAPE datatypes. These functions will be described in the next sections and should be used instead of the general functions.

void *mem_alloc (size_t size)
Allocate a memory block of the size size*sizeof(char) and will return the pointer to the block. If the allocation fails the function will return a NULL-pointer.

void mem_free (void *ptr, size_t size)
This function will free a block at the adress ptr with the size size*sizeof(char). You can see that the function mem_free needs the size of the block, so you need to store the size of a block you allocate to free it correctly. To free a block without knowing its size you can use the special value G_BLOCKSIZE instead of the real size, but giving a size enables mem_free to check if your program assumed a wrong size.

void *mem_realloc (void *ptr, size_t old_size, size_t new_size)
This function resizes a memory block. It is basically 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_alloc returns NULL and the old value of ptr will still be valid. G_BLOCKSIZE is allowed as old_size, but not recommended, as in mem_free. If ptr is NULL, mem_realloc does the same as mem_alloc, if new_size is zero the block is simply freed.



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.