The GNU C Library - Aligned Memory Blocks

Node: Aligned Memory Blocks Next: Heap Consistency Checking Prev: Efficiency and Malloc Up: Unconstrained Allocation

Allocating Aligned Memory Blocks

The address of a block returned by malloc or realloc in the GNU system is always a multiple of eight. If you need a block whose address is a multiple of a higher power of two than that, use memalign or valloc . These functions are declared in `stdlib.h'.

With the GNU library, you can use free to free the blocks that memalign and valloc return. That does not work in BSD, however---BSD does not provide any way to free such blocks.

Function void * memalign (size_t size, size_t boundary)
The memalign function allocates a block of size bytes whose address is a multiple of boundary. The boundary must be a power of two! The function memalign works by calling malloc to allocate a somewhat larger block, and then returning an address within the block that is on the specified boundary.

Function void * valloc (size_t size)
Using valloc is like using memalign and passing the page size as the value of the second argument. It is implemented like this:

	void *
	valloc (size_t size)
	{
	  return memalign (size, getpagesize ());
	}


Next: Heap Consistency Checking Up: Unconstrained Allocation