Dynamic memory allocation services.
[Xenomai nucleus.]


Detailed Description

Dynamic memory allocation services.

The implementation of the memory allocator follows the algorithm described in a USENIX 1988 paper called "Design of a General Purpose Memory Allocator for the 4.3BSD Unix Kernel" by Marshall K. McKusick and Michael J. Karels. You can find it at various locations on the net, including http://docs.FreeBSD.org/44doc/papers/kernmalloc.pdf. A minor variation allows this implementation to have 'extendable' heaps when needed, with multiple memory extents providing autonomous page address spaces.

The data structures hierarchy is as follows:

HEAP { block_buckets[] extent_queue -------+ } | V EXTENT #1 { {static header} page_map[npages] page_array[npages][pagesize] } -+ | | V EXTENT #n { {static header} page_map[npages] page_array[npages][pagesize] }


Files

file  heap.c
 Dynamic memory allocation services.

Functions

int xnheap_init (xnheap_t *heap, void *heapaddr, u_long heapsize, u_long pagesize)
 Initialize a memory heap.
int xnheap_destroy (xnheap_t *heap, void(*flushfn)(xnheap_t *heap, void *extaddr, u_long extsize, void *cookie), void *cookie)
 Destroys a memory heap.
void * xnheap_alloc (xnheap_t *heap, u_long size)
 Allocate a memory block from a memory heap.
int xnheap_free (xnheap_t *heap, void *block)
 Release a memory block to a memory heap.
int xnheap_extend (xnheap_t *heap, void *extaddr, u_long extsize)
 Extend a memory heap.


Function Documentation

void * xnheap_alloc xnheap_t *  heap,
u_long  size
 

Allocate a memory block from a memory heap.

Allocates a contiguous region of memory from an active memory heap. Such allocation is guaranteed to be time-bounded.

Parameters:
heap The descriptor address of the heap to get memory from.
size The size in bytes of the requested block. Sizes lower or equal to the page size are rounded either to the minimum allocation size if lower than this value, or to the minimum alignment size if greater or equal to this value. In the current implementation, with MINALLOC = 8 and MINALIGN = 16, a 7 bytes request will be rounded to 8 bytes, and a 17 bytes request will be rounded to 32.
Returns:
The address of the allocated region upon success, or NULL if no memory is available from the specified heap.
Environments:

This service can be called from:

  • Kernel module initialization/cleanup code
  • Interrupt service routine
  • Kernel-based task
  • User-space task

Rescheduling: never.

void xnheap_destroy xnheap_t *  heap,
void(*)(xnheap_t *heap, void *extaddr, u_long extsize, void *cookie)  flushfn,
void *  cookie
 

Destroys a memory heap.

Destroys a memory heap.

Parameters:
heap The descriptor address of the destroyed heap.
flushfn If non-NULL, the address of a flush routine which will be called for each extent attached to the heap. This routine can be used by the calling code to further release the heap memory.
cookie If flushfn is non-NULL, cookie is an opaque pointer which will be passed unmodified to flushfn.
Returns:
0 is returned on success, or -EBUSY if external mappings are still pending on the heap memory.
Environments:

This service can be called from:

  • Kernel module initialization/cleanup code
  • Kernel-based task
  • User-space task

Rescheduling: never.

int xnheap_extend xnheap_t *  heap,
void *  extaddr,
u_long  extsize
 

Extend a memory heap.

Add a new extent to an existing memory heap.

Parameters:
heap The descriptor address of the heap to add an extent to.
extaddr The address of the extent memory.
extsize The size of the extent memory (in bytes). In the current implementation, this size must match the one of the initial extent passed to xnheap_init().
Returns:
0 is returned upon success, or -EINVAL is returned if extsize differs from the initial extent's size.
Environments:

This service can be called from:

  • Kernel module initialization/cleanup code
  • Interrupt service routine
  • Kernel-based task
  • User-space task

Rescheduling: never.

int xnheap_free xnheap_t *  heap,
void *  block
 

Release a memory block to a memory heap.

Releases a memory region to the memory heap it was previously allocated from.

Parameters:
heap The descriptor address of the heap to release memory to.
block The address of the region to be returned to the heap.
Returns:
0 is returned upon success, or -EINVAL is returned whenever the block is not a valid region of the specified heap.
Environments:

This service can be called from:

  • Kernel module initialization/cleanup code
  • Interrupt service routine
  • Kernel-based task
  • User-space task

Rescheduling: never.

int xnheap_init xnheap_t *  heap,
void *  heapaddr,
u_long  heapsize,
u_long  pagesize
 

Initialize a memory heap.

Initializes a memory heap suitable for time-bounded allocation requests of dynamic memory.

Parameters:
heap The address of a heap descriptor which will be used to store the allocation data. This descriptor must always be valid while the heap is active therefore it must be allocated in permanent memory.
heapaddr The address of the heap storage area. All allocations will be made from the given area in time-bounded mode. Since additional extents can be added to a heap, this parameter is also known as the "initial extent".
heapsize The size in bytes of the initial extent pointed at by heapaddr. heapsize must be a multiple of pagesize and lower than 16 Mbytes. heapsize must be large enough to contain an internal header. The following formula gives the size of this header: hdrsize = (sizeof(xnextent_t) + ((heapsize - sizeof(xnextent_t))) / (pagesize + 1) + 15) & ~15.
pagesize The size in bytes of the fundamental memory page which will be used to subdivide the heap internally. Choosing the right page size is important regarding performance and memory fragmentation issues, so it might be a good idea to take a look at http://docs.FreeBSD.org/44doc/papers/kernmalloc.pdf to pick the best one for your needs. In the current implementation, pagesize must be a power of two in the range [ 8 .. 32768 ] inclusive.
Returns:
0 is returned upon success, or one of the following error codes:
  • -EINVAL is returned whenever a parameter is invalid.

Environments:

This service can be called from:

  • Kernel module initialization/cleanup code
  • Kernel-based task
  • User-space task

Rescheduling: never.


Generated on Mon Dec 13 09:49:49 2004 for RTAI API by  doxygen 1.3.9.1