Xenomai API  2.6.5
Memory heap services.
Collaboration diagram for Memory heap services.:

Files

file  heap.c
 This file is part of the Xenomai project.
 

Functions

int rt_heap_create (RT_HEAP *heap, const char *name, size_t heapsize, int mode)
 Create a memory heap or a shared memory segment. More...
 
int rt_heap_delete (RT_HEAP *heap)
 Delete a real-time heap. More...
 
int rt_heap_alloc (RT_HEAP *heap, size_t size, RTIME timeout, void **blockp)
 Allocate a block or return the single segment base. More...
 
int rt_heap_free (RT_HEAP *heap, void *block)
 Free a block. More...
 
int rt_heap_inquire (RT_HEAP *heap, RT_HEAP_INFO *info)
 Inquire about a heap. More...
 
int rt_heap_bind (RT_HEAP *heap, const char *name, RTIME timeout)
 Bind to a mappable heap. More...
 
int rt_heap_unbind (RT_HEAP *heap)
 Unbind from a mappable heap. More...
 

Detailed Description

Memory heaps are regions of memory used for dynamic memory allocation in a time-bounded fashion. Blocks of memory are allocated and freed in an arbitrary order and the pattern of allocation and size of blocks is not known until run time.

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.

Xenomai memory heaps are built over the nucleus's heap objects, which in turn provide the needed support for sharing a memory area between kernel and user-space using direct memory mapping.

Function Documentation

int rt_heap_alloc ( RT_HEAP *  heap,
size_t  size,
RTIME  timeout,
void **  blockp 
)

Allocate a block or return the single segment base.

This service allocates a block from the heap's internal pool, or returns the address of the single memory segment in the caller's address space. Tasks may wait for some requested amount of memory to become available from local heaps.

Parameters
heapThe descriptor address of the heap to allocate a block from.
sizeThe requested size in bytes of the block. If the heap is managed as a single-block area (H_SINGLE), this value can be either zero, or the same value given to rt_heap_create(). In that case, the same block covering the entire heap space will always be returned to all callers of this service.
timeoutThe number of clock ticks to wait for a block of sufficient size to be available from a local heap (see note). Passing TM_INFINITE causes the caller to block indefinitely until some block is eventually available. Passing TM_NONBLOCK causes the service to return immediately without waiting if no block is available on entry. This parameter has no influence if the heap is managed as a single-block area since the entire heap space is always available.
blockpA pointer to a memory location which will be written upon success with the address of the allocated block, or the start address of the single memory segment. In the former case, the block should be freed using rt_heap_free().
Returns
0 is returned upon success. Otherwise:
  • -EINVAL is returned if heap is not a heap descriptor, or heap is managed as a single-block area (i.e. H_SINGLE mode) and size is non-zero but does not match the original heap size passed to rt_heap_create().
  • -EIDRM is returned if heap is a deleted heap descriptor.
  • -ETIMEDOUT is returned if timeout is different from TM_NONBLOCK and no block is available within the specified amount of time.
  • -EWOULDBLOCK is returned if timeout is equal to TM_NONBLOCK and no block is immediately available on entry.
  • -EINTR is returned if rt_task_unblock() has been called for the waiting task before any block was available.
  • -EPERM is returned if this service should block but was called from a context which cannot sleep (e.g. interrupt, non-realtime context).

Environments:

This service can be called from:

  • Kernel module initialization/cleanup code
  • Interrupt service routine only if timeout is equal to TM_NONBLOCK, or the heap is managed as a single-block area.
  • Kernel-based task
  • User-space task (switches to primary mode)

Rescheduling: always unless the request is immediately satisfied or timeout specifies a non-blocking operation. Operations on single-block heaps never start the rescheduling procedure.

Note
The timeout value will be interpreted as jiffies if the native skin is bound to a periodic time base (see CONFIG_XENO_OPT_NATIVE_PERIOD), or nanoseconds otherwise.

References XNBREAK, xnheap_alloc(), XNRMID, xnsynch_sleep_on(), and XNTIMEO.

int rt_heap_bind ( RT_HEAP *  heap,
const char *  name,
RTIME  timeout 
)

Bind to a mappable heap.

This user-space only service retrieves the uniform descriptor of a given mappable Xenomai heap identified by its symbolic name. If the heap does not exist on entry, this service blocks the caller until a heap of the given name is created.

Parameters
nameA valid NULL-terminated name which identifies the heap to bind to.
heapThe address of a heap descriptor retrieved by the operation. Contents of this memory is undefined upon failure.
timeoutThe number of clock ticks to wait for the registration to occur (see note). Passing TM_INFINITE causes the caller to block indefinitely until the object is registered. Passing TM_NONBLOCK causes the service to return immediately without waiting if the object is not registered on entry.
Returns
0 is returned upon success. Otherwise:
  • -EFAULT is returned if heap or name is referencing invalid memory.
  • -EINTR is returned if rt_task_unblock() has been called for the waiting task before the retrieval has completed.
  • -EWOULDBLOCK is returned if timeout is equal to TM_NONBLOCK and the searched object is not registered on entry.
  • -ETIMEDOUT is returned if the object cannot be retrieved within the specified amount of time.
  • -EPERM is returned if this service should block, but was called from a context which cannot sleep (e.g. interrupt, non-realtime context).
  • -ENOENT is returned if the special file /dev/rtheap (character-mode, major 10, minor 254) is not available from the filesystem. This device is needed to map the shared heap memory into the caller's address space. udev-based systems should not need manual creation of such device entry. Environments:

This service can be called from:

  • User-space task (switches to primary mode)

Rescheduling: always unless the request is immediately satisfied or timeout specifies a non-blocking operation.

Note
The timeout value will be interpreted as jiffies if the native skin is bound to a periodic time base (see CONFIG_XENO_OPT_NATIVE_PERIOD), or nanoseconds otherwise.
Examples:
shared_mem.c.
int rt_heap_create ( RT_HEAP *  heap,
const char *  name,
size_t  heapsize,
int  mode 
)

Create a memory heap or a shared memory segment.

Initializes a memory heap suitable for time-bounded allocation requests of dynamic memory. Memory heaps can be local to the kernel address space, or mapped to user-space.

In their simplest form, heaps are only accessible from kernel space, and are merely usable as regular memory allocators.

Heaps existing in kernel space can be mapped by user-space processes to their own address space provided H_MAPPABLE has been passed into the mode parameter.

By default, heaps support allocation of multiple blocks of memory in an arbitrary order. However, it is possible to ask for single-block management by passing the H_SINGLE flag into the mode parameter, in which case the entire memory space managed by the heap is made available as a unique block. In this mode, all allocation requests made through rt_heap_alloc() will then return the same block address, pointing at the beginning of the heap memory.

H_SHARED is a shorthand for creating shared memory segments transparently accessible from kernel and user-space contexts, which are basically single-block, mappable heaps. By proper use of a common name, all tasks can bind themselves to the same heap and thus share the same memory space, which start address should be subsequently retrieved by a call to rt_heap_alloc().

Parameters
heapThe address of a heap descriptor Xenomai will use to store the heap-related data. This descriptor must always be valid while the heap is active therefore it must be allocated in permanent memory.
nameAn ASCII string standing for the symbolic name of the heap. When non-NULL and non-empty, this string is copied to a safe place into the descriptor, and passed to the registry package if enabled for indexing the created heap. Mappable heaps must be given a valid name.
heapsizeThe size (in bytes) of the block pool which is going to be pre-allocated to the heap. Memory blocks will be claimed and released to this pool. The block pool is not extensible, so this value must be compatible with the highest memory pressure that could be expected. A minimum of 2 * PAGE_SIZE will be enforced for mappable heaps, 2 * XNHEAP_PAGE_SIZE otherwise.
modeThe heap creation mode. The following flags can be OR'ed into this bitmask, each of them affecting the new heap:
  • H_FIFO makes tasks pend in FIFO order on the heap when waiting for available blocks.
  • H_PRIO makes tasks pend in priority order on the heap when waiting for available blocks.
  • H_MAPPABLE causes the heap to be sharable between kernel and user-space contexts. Otherwise, the new heap is only available for kernel-based usage. This flag is implicitly set when the caller is running in user-space. This feature requires the real-time support in user-space to be configured in (CONFIG_XENO_OPT_PERVASIVE).
  • H_SINGLE causes the entire heap space to be managed as a single memory block.
  • H_SHARED is a shorthand for H_MAPPABLE|H_SINGLE, creating a global shared memory segment accessible from both the kernel and user-space contexts.
  • H_DMA causes the block pool associated to the heap to be allocated in physically contiguous memory, suitable for DMA operations with I/O devices. The physical address of the heap can be obtained by a call to rt_heap_inquire().
  • H_DMA32 causes the block pool associated to the heap to be allocated in physically contiguous memory, suitable for DMA32 operations with I/O devices. The physical address of the heap can be obtained by a call to rt_heap_inquire().
  • H_NONCACHED causes the heap not to be cached. This is necessary on platforms such as ARM to share a heap between kernel and user-space. Note that this flag is not compatible with the H_DMA flag.
Returns
0 is returned upon success. Otherwise:
  • -EEXIST is returned if the name is already in use by some registered object.
  • -EINVAL is returned if heapsize is null, greater than the system limit, or name is null or empty for a mappable heap.
  • -ENOMEM is returned if not enough system memory is available to create or register the heap. Additionally, and if H_MAPPABLE has been passed in mode, errors while mapping the block pool in the caller's address space might beget this return code too.
  • -EPERM is returned if this service was called from an invalid context.
  • -ENOSYS is returned if mode specifies H_MAPPABLE, but the real-time support in user-space is unavailable.

Environments:

This service can be called from:

  • Kernel module initialization/cleanup code
  • User-space task (switches to secondary mode)

Rescheduling: possible.

References rt_heap_delete(), xnheap_init(), xnheap_set_label(), xnregistry_enter(), and xnsynch_init().

int rt_heap_delete ( RT_HEAP *  heap)

Delete a real-time heap.

Destroy a heap and release all the tasks currently pending on it. A heap exists in the system since rt_heap_create() has been called to create it, so this service must be called in order to destroy it afterwards.

Parameters
heapThe descriptor address of the affected heap.
Returns
0 is returned upon success. Otherwise:
  • -EINVAL is returned if heap is not a heap descriptor.
  • -EIDRM is returned if heap is a deleted heap descriptor.
  • -EPERM is returned if this service was called from an invalid context.

Environments:

This service can be called from:

  • Kernel module initialization/cleanup code
  • User-space task (switches to secondary mode).

Rescheduling: possible.

Referenced by rt_heap_create().

int rt_heap_free ( RT_HEAP *  heap,
void *  block 
)

Free a block.

This service releases a block to the heap's internal pool. If some task is currently waiting for a block so that it's pending request could be satisfied as a result of the release, it is immediately resumed.

If the heap is defined as a single-block area (i.e. H_SINGLE mode), this service leads to a null-effect and always returns successfully.

Parameters
heapThe address of the heap descriptor to which the block block belong.
blockThe address of the block to free.
Returns
0 is returned upon success, or -EINVAL if block is not a valid block previously allocated by the rt_heap_alloc() service.

Environments:

This service can be called from:

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

Rescheduling: possible.

References xnheap_alloc(), xnheap_free(), xnpod_schedule(), and xnsynch_wakeup_this_sleeper().

int rt_heap_inquire ( RT_HEAP *  heap,
RT_HEAP_INFO info 
)

Inquire about a heap.

Return various information about the status of a given heap.

Parameters
heapThe descriptor address of the inquired heap.
infoThe address of a structure the heap information will be written to.
Returns
0 is returned and status information is written to the structure pointed at by info upon success. Otherwise:
  • -EINVAL is returned if heap is not a message queue descriptor.
  • -EIDRM is returned if heap is a deleted queue descriptor.

Environments:

This service can be called from:

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

Rescheduling: never.

int rt_heap_unbind ( RT_HEAP *  heap)

Unbind from a mappable heap.

This user-space only service unbinds the calling task from the heap object previously retrieved by a call to rt_heap_bind().

Unbinding from a heap when it is no longer needed is especially important in order to properly release the mapping resources used to attach the heap memory to the caller's address space.

Parameters
heapThe address of a heap descriptor to unbind from.
Returns
0 is always returned.

This service can be called from:

  • User-space task.

Rescheduling: never.

Examples:
shared_mem.c.