Memory heap services.
[Native RTAI API.]

Collaboration diagram for Memory heap services.:


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.

RTAI 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.


Files

file  heap.c
 This file is part of the RTAI 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.
int rt_heap_delete (RT_HEAP *heap)
 Delete a real-time heap.
int rt_heap_alloc (RT_HEAP *heap, size_t size, RTIME timeout, void **blockp)
 Allocate a block or return the shared memory base.
int rt_heap_free (RT_HEAP *heap, void *block)
 Free a block.
int rt_heap_inquire (RT_HEAP *heap, RT_HEAP_INFO *info)
 Inquire about a heap.
int rt_heap_bind (RT_HEAP *heap, const char *name, RTIME timeout)
 Bind to a shared heap.
int rt_heap_unbind (RT_HEAP *heap)
 Unbind from a shared heap.


Function Documentation

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

Allocate a block or return the shared memory base.

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

Parameters:
heap The descriptor address of the heap to allocate a block from.
size The requested size in bytes of the block. If the heap is shared, this value can be either zero, or the same value given to rt_heap_create(). In any case, the same block covering the entire heap space will always be returned to all callers of this service.
timeout The 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 shared since the entire shared memory space is always available.
blockp A pointer to a memory location which will be written upon success with the address of the allocated block, or the start address of the shared 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 shared (i.e. H_SHARED mode) and size is non-zero but does not match the actual heap size passed to rt_heap_create().

  • -EIDRM is returned if q 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 or scheduler locked).

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 shared.

  • 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 shared heaps never start the rescheduling procedure.

Note:
This service is sensitive to the current operation mode of the system timer, as defined by the rt_timer_start() service. In periodic mode, clock ticks are interpreted as periodic jiffies. In oneshot mode, clock ticks are interpreted as nanoseconds.

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

Bind to a shared heap.

This user-space only service retrieves the uniform descriptor of a given shared RTAI 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:
name A valid NULL-terminated name which identifies the heap to bind to.
heap The address of a heap descriptor retrieved by the operation. Contents of this memory is undefined upon failure.
timeout The 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 or scheduler locked).

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:
This service is sensitive to the current operation mode of the system timer, as defined by the rt_timer_start() service. In periodic mode, clock ticks are interpreted as periodic jiffies. In oneshot mode, clock ticks are interpreted as nanoseconds.

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 space, or shared between kernel and user-space.

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

In the shared case, heaps are used as shared memory segments. All allocation requests made through rt_heap_alloc() will then return the same memory block, which will point at the beginning of the heap memory, and cover the entire heap space. This operating mode is specified by passing the H_SHARED flag into the mode parameter. By the 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:
heap The address of a heap descriptor RTAI 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.
name An 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. Shared heaps must be given a valid name.
heapsize The 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.
mode The 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_SHARED causes the heap to be sharable between kernel and user-space tasks, and make it usable as a shared memory segment. Otherwise, the new heap is only available for kernel-based usage. This flag is implicitely set when the caller is running in user-space. This feature requires the real-time support in user-space to be configured in (CONFIG_OPT_RTAI_FUSION).

  • 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. A 128Kb limit exists for heapsize when this flag is passed.

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 shared heap.

  • -ENOMEM is returned if not enough system memory is available to create or register the heap. Additionally, and if H_SHARED 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_SHARED, 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.

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:
heap The 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 asynchronous context.

Environments:

This service can be called from:

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

Rescheduling: possible.

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 shared (i.e. H_SHARED mode), this service leads to a null-effect and always returns successfully.

Parameters:
heap The address of the heap descriptor to which the block block belong.
block The 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.

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:
heap The descriptor address of the inquired heap.
info The 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 shared 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 more needed is especially important in order to properly release the mapping resources used to attach the shared heap memory to the caller's address space.

Parameters:
heap The address of a heap descriptor to unbind from.
Returns:
0 is always returned.
This service can be called from:

  • User-space task.

Rescheduling: never.


Generated on Sat Sep 3 12:32:55 2005 for RTAI Fusion API by  doxygen 1.4.2