GLAST/LAT > DAQ and FSW > FSW > Doxygen Index > PBS / V2-10-13

Constituent: pbs     Tag: linux-gcc


Interface   Data Structures   File List   Data Fields   Globals  

FPA.h File Reference

Fixed Packet Allocator, memory manager that configures and manages a pool of fixed sized packets. More...

#include "PBS/L_pubdefs.h"
#include "PBS/LI_type.h"
#include "PBS/LI_comdefs.h"
#include "PBS/TOC_comdefs.h"

Include dependency graph for FPA.h:

This graph shows which files directly or indirectly include this file:


Data Structures

struct  _FPA_pcb
 Layouts the control structure used by each packet while it is on the free list. More...

Defines

#define FPA_K_PCB_AT_EOP   (-1)
 Convenience symbol indicating that the packet control block, the FORK_pcb, should be placed at the end of the packet (EOP).

Typedefs

typedef enum _FPA_type FPA_type
 Typedef for enum _FPA_type.
typedef _FPA_pcb FPA_pcb
 Typedef for struct _FPA_pcb.
typedef void(* FPA_init_cb )(void *parameter, void *packet, int packet_size, int pcb_offset)
 Function signature for the packet initialization callback routine.
typedef _FPA_fcb FPA_fcb
 Typedef for struct _FPA_fcb.

Enumerations

enum  _FPA_type {
  FPA_K_TYPE_LOCKING_ONLY = RW_K_TYPE_LOCKING_ONLY,
  FPA_K_TYPE_PENDING = RW_K_TYPE_PENDING,
  FPA_K_TYPE_PENDING_WTO = RW_K_TYPE_PENDING_WTO,
  FPA_K_TYPE_PENDING_FIFO = RW_K_TYPE_PENDING_FIFO,
  FPA_K_TYPE_PENDING_FIFO_WTO = RW_K_TYPE_PENDING_FIFO_WTO,
  FPA_K_TYPE_PENDING_PRIORITY = RW_K_TYPE_PENDING_PRIORITY,
  FPA_K_TYPE_PENDING_PRIORITY_WTO = RW_K_TYPE_PENDING_PRIORITY_WTO
}
 Enumerates the FPA types available. More...

Functions

int FPA_fcb_sizeof (void)
 Returns the size of the Fixed Packet Allocator control block.
int FPA_init (FPA_fcb *fcb, FPA_type blocking, void *buffer, int buf_size, int packet_size, int pcb_offset, FPA_init_cb init_routine, void *init_parameter)
 Configures a user supplied piece of memory as a collection of fixed size packets which can be allocated and deallocated.
void * FPA_get (FPA_fcb *fcb)
 Get or allocate a packet from the free list.
void * FPA_getW (FPA_fcb *fcb)
 Get or allocate a packet from the free list with a indefinite blocking.
void * FPA_getW_toc (FPA_fcb *fcb, const TOC *toc)
 Get or allocate a packet from the free list using the timeout specified by TOC.
int FPA_free (FPA_fcb *fcb, void *packet)
 Returns a previously allocated packet to the free list.
int FPA_destroy (FPA_fcb *fcb)
 Releases any resources gathered at initialization time.

Detailed Description

Fixed Packet Allocator, memory manager that configures and manages a pool of fixed sized packets.

Author:
JJRussell - russell@slac.stanford.edu
   CVS $Id: FPA.h,v 1.4 2005/02/14 16:34:50 russell Exp $

SYNOPSIS
This utility allows one to configure a pool of memory as a collection of fixed sized packets. The allocation and deallocation is interlocked, making it safe to use in a multi-threaded environment.

The behaviour when the pool is exhausted is configurable to be either blocking or non-blocking. If blocking is chosen, then the blocking can be in either FIFO or PRIORITY ordering. This is relevant only when multiple allocators use the pool.

An initial design choice was to hide both the control structure for the pool and the packet management structure. The former is an easy decision, the latter caused awkward programming at times, but would increase modularity. The pain is almost exclusively limited to initialization. It was an experiment to see if this worked, but given the rather limited exposure (the packet management structure is currently only LI_node structure, a single 4-byte word), it was decided to keep the FPA control structure hidden, but expose the FPA packet control structure.

USAGE/EXAMPLE
Here is how one would typically use this facility. Suppose one wished to manage a pool of packets which contained data which was to be read from an external source. Further suppose that each packet was to be have a fixed header constrained to be the first word of the data packet describing the data type and that this type never changes. This makes the data_type field a candidate for one-time initialization. Since the initializer routine writes the data_type field, the packet control block used to manage the packet when it is on the FPA freelist must not be allowed to occupy the first word. Since, presumably, the body of the structure is filled with fresh data each time, the packet control block can be located anywhere within that section. The choice in the example is to locate it as the first word of the data field.

  struct _MyPacket
    {                       / * Packet consists of a header ...         * /
        int data_type;      / * Header gives the data type              * /
        union _MyData       / * and a body                              * /
        {                     
            FPA_pcb   pcb;  / * Management when packet is not allocated * /
            char  buf[60];  // * Holds the data when allocated          * /
        } body;
    };
    
    FPA_fcb         *fcb;
    char   *buffer[1000];

    fcb = MBA_alloc (FPA_fcb_sizeof ());/ * Allocate the control block * /
    
    FPA_init (fcb,                      / * FPA control block          * /
              buffer,                   / * Memory pool to manage      * /
              sizeof(buffer),           / * Size of the memory pool    * /
              sizeof(struct MyPacket),  / * Size of each packet        * /
              OFFSET_OF(_struct _MyPacket,
                        body.pcb),      / * Byte offset of PCB         * /
              FPA_K_TYPE_BLOCKING,    , / * Blocking, single allocator * /
              (FPA_init_cb)initializer, / * Initialization routine     * /
              (void *)data_type);       / * Data type of each packet   * /
    .
    .
    .
    while (ptr = isDataReady ())
    {
        struct _MyPacket *packet = FPA_getW (fcb);
        copy (packet->body.buf, ptr, sizeof (packet->body.buf));
    }
              
    return;
    }


    
    / * Callback routine to initialize each packet * /
    void initializer (int           data_type,
                      struct MyPacket *packet,
                      int         packet_size,
                      int          pcb_offset)
    {
        packet->data_type = data_type;
    }

Typedef Documentation

FPA_init_cb
 

Function signature for the packet initialization callback routine.

Parameters:
parameter A user provided context parameter
packet The packet being initialized
packet_size The size, in bytes, of the packet.
pcb_offset The offset, in bytes, to the packets management links
The callback routine must take care not to write in the area reserved for the Packet Control Block. The last parameter of the callback pcb_offset and the routine FPA_pcb_sizeof(), allowing the user to determine exactly what memory the packet control block occupies so that this memory can be avoided.

FPA_pcb
 

Typedef for struct _FPA_pcb.

The FPA routines require some storage to manage each packet on the free list. This memory is only used while on the free list and, therefore is available to the user after it a packet has been allocated. Every attempt was made to keep this overhead as small as possible. Currently, this requires only a single 4-byte word.

Warning:
This location is not available to be written by the initialization routine. Technically, the packet's state during hte initialization routine is 'on the free list', so it is unavailable. The FPA creation routine FPA_create() gives the user the flexibility of locating the FORK_pcb anywhere within the packet.

FPA_type
 

Typedef for enum _FPA_type.

This determines how the FPA_getW() and FPA_getW_toc() calls will behave when an allocation is attempted on an empty pool. One can specify non-blocking or two types of blocking,

  • FIFO blocking order, first come, first serve
  • PRIORITY blocking order, by task priority

If a non-blocking type is requested only FPA_get() routine can be called.

Note:
While the current implementation does not make use of whether the one specifies that timeouts be supported, future implementations may, so it is requested that one specifies whether timeouts are to be used or not.


Enumeration Type Documentation

enum _FPA_type
 

Enumerates the FPA types available.

Enumerator:
FPA_K_TYPE_LOCKING_ONLY  Locking mechanism only, no pending mechanism available
FPA_K_TYPE_PENDING  Pending mechanism available, single pender/reader only
FPA_K_TYPE_PENDING_WTO  Pending with timeouts, single pender/reader only
FPA_K_TYPE_PENDING_FIFO  Use FIFO pending order
FPA_K_TYPE_PENDING_FIFO_WTO  Use FIFO pending order with timeouts available
FPA_K_TYPE_PENDING_PRIORITY  Use PRIORITY pending order
FPA_K_TYPE_PENDING_PRIORITY_WTO  Use PRIORITY pending order with timeouts available


Function Documentation

int FPA_destroy FPA_fcb fcb  ) 
 

Releases any resources gathered at initialization time.

Parameters:
fcb The handle of the Fixed Packet Allocator
Returns:
Status
Releases any resources gathered at initialization time. Note that this does not include either releasing the user supplied memory buffer or the control structure itself. Both of those are property of the user and are the responsibility of the user to release them as appropriate.

int FPA_fcb_sizeof void   ) 
 

Returns the size of the Fixed Packet Allocator control block.

Returns:
The size, in bytes of a Fixed Packet Allocator control block.
This routine is the first step in creating a FPA pool. The user first enquires about the size of the control block and then allocates it either from existing piece of memory or from some other allocator like MBA_alloc().

This call is for modularity reasons. The user can learn the size of memory needed by the FPA utility to manage a pool of packets without needing to know the details of how it is laid out. This could have also been achieved by having the FPA initialization routine allocate the control block, but this takes away the freedom of the user to control his own allocation and deallocation

int FPA_free FPA_fcb fcb,
void *  packet
 

Returns a previously allocated packet to the free list.

Parameters:
fcb The handle of the Fixed Packet Allocator.
packet The packet to be freed.
Returns:
The number of outstanding packets
This routine frees, or returns a previously allocated packet to the free list.

void * FPA_get FPA_fcb fcb  ) 
 

Get or allocate a packet from the free list.

Parameters:
fcb The handle of the Fixed Packet Allocator
Returns:
If successful, the address of the allocated packet, else NULL if no packets where available.
This is a non-blocking allocation. See FPA_getW() for a blocking version. While this call is non-blocking, the allocation is fully interlocked.

void * FPA_getW FPA_fcb fcb  ) 
 

Get or allocate a packet from the free list with a indefinite blocking.

Parameters:
fcb The handle of the Fixed Packet Allocator
Returns:
If successful, the address of the allocated packet. NULL is returned on error.
This is a blocking allocation, ie if the pool is exhausted, then the routine blocks indefinitely.

void * FPA_getW_toc FPA_fcb fcb,
const TOC toc
 

Get or allocate a packet from the free list using the timeout specified by TOC.

Parameters:
fcb The handle of the Fixed Packet Allocator.
toc The timeout control structute.
Returns:
If successful, the address of the allocated packet. NULL is returned on error or timeout.
This is a blocking allocation, ie if the pool is exhausted, then the routine blocks until a packet becomes available or the timeout period expires.

int FPA_init FPA_fcb fcb,
FPA_type  type,
void *  buffer,
int  buf_size,
int  packet_size,
int  pcb_offset,
FPA_init_cb  init_routine,
void *  init_parameter
 

Configures a user supplied piece of memory as a collection of fixed size packets which can be allocated and deallocated.

Parameters:
fcb The handle of the Fixed Packet Allocator to be initialized. The user is responsible for supplying the memory for the control structure.
type The type of blocking to be used
buffer A pointer to the user-supplied buffer. This buffer must be 32-bit aligned. The packets will be carved from this memory.
buf_size The size, in bytes, of the user-supplied buffer.
packet_size The size, in bytes, of the packets. This number must be an integral number of 32-bit words. This ensures that all packets will be properly aligned.
pcb_offset Offset, in bytes, to the packet control block. This block is necessary for managing the packet while it is on in the free pool, but is free for the user to do with as he wishes when once the packet has been allocated. This parameter allows the user to control the placement of the control block. This offset is generally specified as 0, in which case the control structure is at the top of the packet, or -1, in which case the links are at the bottom of the packet. The latter case is especially useful if one wishes to initialize the top portion of the packet with a static header of some sort, while the bottom of the packet contains data to be filled in later when the packet is allocated.
init_routine The address of the entry point of a user supplied routine which is called as each new packet is placed on the free list. This parameter may be NULL if no initialization routine is needed. It is the user's responsibility not so use the control area. This is possible, since the user knows the size and controls its placement in the packet. Set FPA_init_cb for the definition of the callback signature.
init_parameter A user supplied parameter which is passed to the callback routine.
This routine is responsible for configuring a user-supplied piece of memory for use as a source of fixed sized packets. As each packet is placed on the free list, an optionally supplied user callback routine allows user initialization of the packets.

Various options allow the user to place where the control structure lives within the packet and determine the blocking style. The most usual choice for the placement of the packet control structure is at the beginning pcb_offset = 0, or at the end pcb_offset = -1, although the user is allowed to specify any offset, provided it is within the packet. The usual blocking style is FPA_K_TYPE_FIFO_BLOCKING.

Warning:
Note that while this Packet Control Block offset pcb_offset is specified in bytes it must be 4 byte aligned.


Generated on Fri Feb 9 02:12:02 2007 by  doxygen 1.4.4