Xenomai API  2.6.5
Dynamic memory allocation services.
Collaboration diagram for Dynamic memory allocation services.:

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. More...
 
void xnheap_set_label (xnheap_t *heap, const char *label,...)
 Set the heap's label string. More...
 
void * xnheap_alloc (xnheap_t *heap, u_long size)
 Allocate a memory block from a memory heap. More...
 
int xnheap_test_and_free (xnheap_t *heap, void *block, int(*ckfn)(void *block))
 Test and release a memory block to a memory heap. More...
 
int xnheap_free (xnheap_t *heap, void *block)
 Release a memory block to a memory heap. More...
 
int xnheap_extend (xnheap_t *heap, void *extaddr, u_long extsize)
 Extend a memory heap. More...
 
void xnheap_schedule_free (xnheap_t *heap, void *block, xnholder_t *link)
 Schedule a memory block for release. More...
 

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]
                      }

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
heapThe descriptor address of the heap to get memory from.
sizeThe 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.

Referenced by ftruncate(), pthread_mutex_init(), rt_heap_alloc(), rt_heap_free(), rt_pipe_alloc(), rt_pipe_create(), rt_queue_alloc(), rt_task_create(), and xnshadow_map().

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
heapThe descriptor address of the heap to add an extent to.
extaddrThe address of the extent memory.
extsizeThe 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
heapThe descriptor address of the heap to release memory to.
blockThe address of the region to be returned to the heap.
Returns
0 is returned upon success, or one of the following error codes:
  • -EFAULT is returned whenever the memory address is outside the heap address space.
  • -EINVAL is returned whenever the memory address does not represent a valid block.

Environments:

This service can be called from:

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

Rescheduling: never.

References xnheap_test_and_free().

Referenced by ftruncate(), pthread_mutex_init(), rt_heap_free(), rt_mutex_delete(), and rt_pipe_free().

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
heapThe 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.
heapaddrThe 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".
heapsizeThe 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 a dynamically-sized internal header. The following formula gives the size of this header:
H = heapsize, P=pagesize, M=sizeof(struct pagemap), E=sizeof(xnextent_t)
hdrsize = ((H - E) * M) / (M + 1)
This value is then aligned on the next 16-byte boundary. The routine xnheap_overhead() computes the corrected heap size according to the previous formula.
pagesizeThe 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.

Referenced by rt_heap_create(), rt_pipe_create(), rt_queue_create(), and xnpod_init().

void xnheap_schedule_free ( xnheap_t *  heap,
void *  block,
xnholder_t *  link 
)

Schedule a memory block for release.

This routine records a block for later release by xnheap_finalize_free(). This service is useful to lazily free blocks of heap memory when immediate release is not an option, e.g. when active references are still pending on the object for a short time after the call. xnheap_finalize_free() is expected to be eventually called by the client code at some point in the future when actually freeing the idle objects is deemed safe.

Parameters
heapThe descriptor address of the heap to release memory to.
blockThe address of the region to be returned to the heap.
linkThe address of a link member, likely but not necessarily within the released object, which will be used by the heap manager to hold the block in the queue of idle objects.

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_set_label ( xnheap_t *  heap,
const char *  label,
  ... 
)

Set the heap's label string.

Set the heap label that will be used in statistic outputs.

Parameters
heapThe address of a heap descriptor.
labelLabel string displayed in statistic outputs. This parameter can be a format string, in which case succeeding parameters will be used to resolve the final label.

Environments:

This service can be called from:

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

Rescheduling: never.

Referenced by ftruncate(), rt_heap_create(), rt_pipe_create(), rt_queue_create(), and xnpod_init().

int xnheap_test_and_free ( xnheap_t *  heap,
void *  block,
int(*)(void *block)  ckfn 
)

Test and release a memory block to a memory heap.

Releases a memory region to the memory heap it was previously allocated from. Before the actual release is performed, an optional user-defined can be invoked to check for additional criteria with respect to the request consistency.

Parameters
heapThe descriptor address of the heap to release memory to.
blockThe address of the region to be returned to the heap.
ckfnThe address of a user-supplied verification routine which is to be called after the memory address specified by block has been checked for validity. The routine is expected to proceed to further consistency checks, and either return zero upon success, or non-zero upon error. In the latter case, the release process is aborted, and ckfn's return value is passed back to the caller of this service as its error return code. ckfn must not trigger the rescheduling procedure either directly or indirectly.
Returns
0 is returned upon success, or -EINVAL is returned whenever the block is not a valid region of the specified heap. Additional return codes can also be defined locally by the ckfn routine.

Environments:

This service can be called from:

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

Rescheduling: never.

Referenced by rt_queue_free(), and xnheap_free().