GLAST / LAT > DAQ and FSW > FSW > Doxygen Index> IPBS / V0-0-2 > pbs / i845e
#include <IPBS/RW_type.h>
#include <IPBS/TOC_comdefs.h>
#include <IPBS/TOV.h>
Classes | |
struct | _RNG_dsc |
This describes all the static properties of the ring buffer. More... | |
Defines | |
#define | RNG_K_PTR_ERROR (1) |
Returned by either the RNG_get() or RNG_grab routines when an internal error is detected. | |
#define | RNG_PTR_IS_VALID(ptr) ( (unsigned int)ptr > RNG_K_PTR_ERROR) |
Checks that the returned pointer is valid. | |
#define | RNG_PTR_IS_ERROR(ptr) ( (unsigned int)ptr == RNG_K_PTR_ERROR) |
Checks if the pointer is indicates failure to allocate because of an error. | |
Typedefs | |
typedef enum _RNG_type | RNG_type |
Typedef for enum _RNG_type. | |
typedef struct _RNG_dsc | RNG_dsc |
Typedef for struct _RNG_dsc. | |
Enumerations | |
enum | _RNG_type { RNG_K_TYPE_LOCKING_ONLY = RW_K_TYPE_LOCKING_ONLY, RNG_K_TYPE_PENDING = RW_K_TYPE_PENDING, RNG_K_TYPE_PENDING_WTO = RW_K_TYPE_PENDING_WTO } |
An enumeration of the types of ring buffers that can be created. More... | |
Functions | |
const RNG_dsc * | RNG_dsc_get (const RNG_rcb *rcb) |
Query routine to return a pointer to the ring buffer description structure. | |
int | RNG_sizeof_rcb (void) |
Returns the size of the Ring Contol Block. | |
int | RNG_init (RNG_rcb *rcb, RNG_type type, void *buffer, int size, int underflow, int overflow, int alignment) |
Initializes the control structure for a Ring buffer. | |
int | RNG_destroy (RNG_rcb *rcb) |
Returns any resources associated with this ring buffer. | |
void * | RNG_get (RNG_rcb *rcb, int request, const void *write) |
Non-blocking allocation request for a specified amount of memory. | |
void * | RNG_getW (RNG_rcb *rcb, int request, const void *write) |
Blocking allocation request for a specified amount of memory. | |
void * | RNG_getW_toc (RNG_rcb *rcb, int request, const void *check_Wr, const TOC *toc) |
Blocking allocation request for a specified amount of memory, with timeout. | |
void * | RNG_grab (RNG_rcb *rcb, int minimum, const void *write, int *allocated) |
Non-blocking allocation request for all the remaining contigious memory, i.e. a greedy form of RNG_get. | |
void * | RNG_grabW (RNG_rcb *rcb, int minimum, const void *write, int *allocated) |
Blocking allocation request for all the remaining contigious memory, i.e. a greedy form of RNG_getW. | |
void * | RNG_grabW_toc (RNG_rcb *rcb, int minimum, const void *check_wr, int *allocated, const TOC *toc) |
Blocking allocation request for all the remaining contigious memory with a timeout, i.e. a greedy form of RNG_getWtos. | |
int | RNG_free (RNG_rcb *rcb, const void *packet, int amount) |
Frees the requested amount of memory from the specified address. | |
void * | RNG_shrink (RNG_rcb *rcb, const void *write, int left) |
Shrinks the previously allocated packet back to the specified address. | |
int | RNG_reset (RNG_rcb *rcb) |
If the ring buffer is empty, reset the read and write pointers to their initial positions. This can only be called if the user knows that the buffer is empty. |
CVS $Id: RNG.h,v 1.2 2011/03/25 21:15:01 saxton Exp $
SYNOPSIS
Implements a set of routines for managing a single writer / single reader ring buffer. The routines are thread and interrupt level safe, although one should not use the blocking forms RNG_getW() and RNG_grabW() nor any of their timeout variations in interrupt routines. While nothing in the code prohibits multiple writers and readers, using a ring buffer in this fashion runs is not natural. Ring buffers are really just a software implementation of a FIFO, which, by its nature, is a single writer, single reader object.
#define RNG_K_PTR_ERROR (1) |
Returned by either the RNG_get() or RNG_grab routines when an internal error is detected.
Referenced by rng_allocate().
#define RNG_PTR_IS_ERROR | ( | ptr | ) | ( (unsigned int)ptr == RNG_K_PTR_ERROR) |
Checks if the pointer is indicates failure to allocate because of an error.
ptr | The pointer to check |
0,the | pointer is not an error condition pointer | |
1,the | pointer is an error condition pointer |
#define RNG_PTR_IS_VALID | ( | ptr | ) | ( (unsigned int)ptr > RNG_K_PTR_ERROR) |
Checks that the returned pointer is valid.
ptr | The pointer to check |
0,if | the pointer is invalid | |
1,if | the pointer is valid |
This macro checks that the pointer is not one of these values. This attempts to address the age old problem of how to indicate an error condition without resorting to an additional parameter for a status value.
Typedef for struct _RNG_dsc.
This structure describes all the static properties of the ring buffer.
The ring buffer composed of three pieces carved out of a user provided block of memory. The three pieces are
Underflow area Main pool Overflow area
Picturally the ring buffer looks like
+---------------+ beg | | | Underflow | | | +---------------+ rbeg | | | Main | | Ring | | Buffer | | | +---------------+ rend | | | Underflow | | | +---------------+ end
As is common practice, the pointers to the beginning of the areas point at the real beginning and the pointers to the ending of the areas point one past the actual last address so that sizes are trivially calculated.
As a convenience the address of the initial buffer, its size and the requested ring buffer type are also kept in this structure. This may be useful to the user when he later attempts to free this memory.
Note that due to alignment rounding the initial buffer address and size may not match the ring buffer's beginning address and size.
Typedef for enum _RNG_type.
This determines how the ring allocators, RNG_getW() and RNG_grabW(), calls will behave when an allocation is attempted on an empty pool. One can specify non-blocking or blocking and with and without timeouts.
Note that there is no choice as to the blocking types as one sees in the LLI facility. This is because the blocking type affects only the behavior when multiple tasks are being blocked. Given the single writer / single reader nature of the ring buffer, the issue what to do when multiple tasking are pending never comes up.
If a non-blocking type is requested, common practice would be to use the simpler RNG_get() routine, since it never waits. The RNG_getWxxx and RNG_grabWxxx should not be used in this case, although their behavior is currently defined to degenerate to the RNG_get and RNG_grab, respectively.
enum _RNG_type |
int RNG_destroy | ( | RNG_rcb * | rcb | ) |
Returns any resources associated with this ring buffer.
rcb | The Ring Control Block |
Status | Frees any internally gathered resources associated with the specified ring buffer. The rcb is no longer usable as ring buffer after this call. |
References _RNG_rcb::rw, and RW_destroy().
Query routine to return a pointer to the ring buffer description structure.
rcb | The ring control block |
A | pointer to ring buffer description |
Here is a overview of the elements and there use.
beg This a pointer to the top of the underflow portion of the buffer. If if there is no underflow area, this will be the same as rbeg. Because of alignment reason, this pointer may or may not agree with the original buffer pointer passed in by the user.
rbeg Pointer to the top of the ring buffer proper.
rend Pointer to the bottom of the ring buffer proper. Also, by definition, the start of the overflow area.
end Pointer to the bottom of the overflow area.
msk The alignment mask, for shorts = 1, ints = 3, long long = 7
buf The original buffer pointer passed into RCB_init by the user. This routine along with bufsize allows the user to recover the original address and size of the pool of memory the RNG utilities where requested to manage. This may be useful when closing the ring buffer, allowing the user to return the managed memory to where ever it came from.
bufsize The size of the original buffer
References _RNG_rcb::dsc.
int RNG_free | ( | RNG_rcb * | rcb, | |
const void * | packet, | |||
int | amount | |||
) |
Frees the requested amount of memory from the specified address.
rcb | The Ring Control Block | |
packet | The pointer where the free begins | |
amount | The amount of memory to free |
0 | Successful | |
-1 | Packet being freed is not contigous with the previous packet being freed | |
-2 | Packet being freed extended into the shard area. Since this area is owned by the ring buffer and cannot belong to the user, this is a user specification error. |
If fails, the current read pointer is returned. The only practical reason for failing is that the packet being freed is does not match the current read pointer. Currently there is no sanity check to prevent the user from freeing more memory than is actually allocated. That is fairly expensive and, the thought is, if one does this, the next free is going to fail. This is a little dicey, but see how this works. If it doesn't will add the proper sanity check.
There is one case where overfreeing is detected, that is when the packet being freed extends into the user area. As compared with detecting an arbritary overfree, this one is relatively cheap, so it is done.
References _RNG_dsc::beg, _RNG_rcb::dsc, _RNG_rcb::dyn, _RNG_dsc::end, _RNG_dsc::msk, _RNG_dyn::que, _RNG_dyn::rd, _RNG_dyn::rdx, _RNG_dsc::rend, ROUND_TO, _RNG_rcb::rw, RW__wake(), _RNG_rcb::size, and _RNG_dyn::wr.
void* RNG_get | ( | RNG_rcb * | rcb, | |
int | request, | |||
const void * | write | |||
) |
Non-blocking allocation request for a specified amount of memory.
rcb | The Ring Control Block | |
request | The size of the request, in bytes. | |
write | The end of the previously allocated block. Given the loose nature of ring buffers, this is used as an integrity check. If one is feeling brave, this may be specified as NULL. Doing this will skip the integrity checking. |
References _RNG_rcb::dyn, rng_allocate(), TOC_NOWAIT, and _RNG_dyn::wrx.
void* RNG_getW | ( | RNG_rcb * | rcb, | |
int | request, | |||
const void * | write | |||
) |
Blocking allocation request for a specified amount of memory.
rcb | The Ring Control Block | |
request | The size of the request, in bytes. | |
write | The end of the previously allocated block. Given the loose nature of ring buffers, this is used as an integrity check. If one is feeling brave, this may be specified as NULL. Doing this will skip the integrity checking. |
If the write pointer is inconsistent with the internal write pointer, NULL is returned. The user can distinguish these two cases by calling RNG_wr(), to fetch the current value of the write pointer. The user may also live dangerously and specify NULL for the write argument, in which case the consistency check is bypassed.
References _RNG_rcb::dyn, rng_allocate(), TOC_FOREVER, and _RNG_dyn::wrx.
Blocking allocation request for a specified amount of memory, with timeout.
rcb | The Ring Control Block | |
request | The size of the request, in bytes | |
write | The end of the previously allocated block. Given the loose nature of ring buffers, this is used as an integrity check. If one is feeling brave, this may be specified as NULL. Doing this will skip the integrity checking. | |
toc | The timeout control structure. This specifies the timeout value. TOC_NOWAIT and TOC_FOREVER may be specified here, although in this case the user should consider using RNG_get or RNG_getW. |
If the write pointer is inconsistent with the internal write pointer, NULL is returned. The user can distinguish these two cases by calling RNG_wr(), to fetch the current value of the write pointer. The user may also live dangerously and specify NULL for the write argument, in which case the consistency check is bypassed.
References _RNG_rcb::dyn, rng_allocate(), and _RNG_dyn::wrx.
void* RNG_grab | ( | RNG_rcb * | rcb, | |
int | minimum, | |||
const void * | write, | |||
int * | allocated | |||
) |
Non-blocking allocation request for all the remaining contigious memory, i.e. a greedy form of RNG_get.
rcb | The Ring Control Block | |
minimum | The minimum size requested, in bytes. | |
write | The end of the previously allocated block. Given the loose nature of ring buffers, this is used as an integrity check. If one is feeling brave, this may be specified as NULL. Doing this will skip the integrity checking. | |
allocated | Pointer to receive the actual amount, in bytes, that was allocated. |
If there is less than the minimum amount of memory in the pool, NULL is returned.
References rng_allocate(), and TOC_NOWAIT.
void* RNG_grabW | ( | RNG_rcb * | rcb, | |
int | minimum, | |||
const void * | write, | |||
int * | allocated | |||
) |
Blocking allocation request for all the remaining contigious memory, i.e. a greedy form of RNG_getW.
rcb | The Ring Control Block | |
minimum | The minimum size requested, in bytes. | |
write | The end of the previously allocated block. Given the loose nature of ring buffers, this is used as an integrity check. If one is feeling brave, this may be specified as NULL. Doing this will skip the integrity checking. | |
allocated | Pointer to receive the actual amount, in bytes, that was allocated. |
If there is less than the minimum amount of memory in the pool, RNG_grabW waits until the memory shows up. NULL is only on an internal error.
References rng_allocate(), and TOC_FOREVER.
void* RNG_grabW_toc | ( | RNG_rcb * | rcb, | |
int | minimum, | |||
const void * | write, | |||
int * | allocated, | |||
const TOC * | toc | |||
) |
Blocking allocation request for all the remaining contigious memory with a timeout, i.e. a greedy form of RNG_getWtos.
rcb | The Ring Control Block | |
minimum | The minimum size of the request, in bytes. | |
write | The end of the previously allocated block. Given the loose nature of ring buffers, this is used as an integrity check. If one is feeling brave, this may be specified as NULL. Doing this will skip the integrity checking. | |
allocated | Returned as the amount of memory actually allocated. | |
toc | The timeout control structure. This specifies the timeout value. TOC_NOWAIT and TOC_FOREVER may be specified here, although in this case the user should consider using RNG_grab or RNG_grabW. |
If the write pointer is inconsistent with the internal write pointer, NULL is returned. The user can distinguish these two cases by calling RNG_wr(), to fetch the current value of the write pointer. The user may also live dangerously and specify NULL for the write argument, in which case the consistency check is bypassed.
References rng_allocate().
int RNG_init | ( | RNG_rcb * | rcb, | |
RNG_type | type, | |||
void * | buffer, | |||
int | size, | |||
int | underflow, | |||
int | overflow, | |||
int | alignment | |||
) |
Initializes the control structure for a Ring buffer.
rcb | Pointer to the ring buffer control block structure to be initialized. | |
type | Indicates the type of ring buffer. This mainly controls the blocking. If insufficient memory is available, the allocation calls will block until it becomes available or a timeout occurs. | |
buffer | The buffer to be managed | |
size | The total size of the buffer (in bytes) to be managed | |
underflow | The size (in bytes) to be assigned to the underflow area. This area is an area immediately preceeding the ring buffer area. |
overflow | The size (in bytes) to be assigned to the overflow area. This area carved from the end buf. The useful property of the overflow area is that while memory may be allocated from the overflow area, an initial allocation never begins in the overflow area. This allows some continuity when the wrap-around point of the ring buffer is encountered. |
alignment | The byte alignment, expressed in binary powers of 2, eg
|
Status | Initializes a Ring Control Block to manage a pool of memory. The memory is carved into three pieces, an underflow area, a ring buffer area, and an overflow area. The detailed use and meaning of these areas is described elsewhere. |
References _RNG_dsc::beg, _RNG_dsc::buf, _RNG_dsc::bufsize, _RNG_rcb::dsc, _RNG_rcb::dyn, _RNG_dsc::end, _RNG_dsc::msk, _RNG_dyn::que, _RNG_dsc::rbeg, _RNG_dyn::rd, _RNG_dyn::rdx, _RNG_dsc::rend, ROUND_TO, _RNG_rcb::rw, RW_init(), _RNG_rcb::size, _RNG_dsc::type, _RNG_dyn::wr, and _RNG_dyn::wrx.
int RNG_reset | ( | RNG_rcb * | rcb | ) |
If the ring buffer is empty, reset the read and write pointers to their initial positions. This can only be called if the user knows that the buffer is empty.
rcb | The Ring Control Block |
So, one may ask, why not just reset the pointers to the empty state any time the ring buffer is totally empty. The answer is that this destroys any history that the ring buffer may accumulate. Imagine the case where one allocates some memory and then immediately frees it. The current implementation would deliver a different memory block each time. In a debugging mode, one could go back and examine this memory for some clues. If the buffer pointers where to be continually reset, this the memory would always be allocated from the top of the pool, and this debugging feature would be lost.
One possible alternative would be to make this resetting a optional configuration parameter on the ring buffer initialization. That way this would happen automatically.
References _RNG_dsc::beg, _RNG_rcb::dsc, _RNG_rcb::dyn, _RNG_dsc::end, _RNG_dyn::rd, _RNG_dyn::rdx, _RNG_rcb::size, _RNG_dyn::wr, and _RNG_dyn::wrx.
void* RNG_shrink | ( | RNG_rcb * | rcb, | |
const void * | write, | |||
int | left | |||
) |
Shrinks the previously allocated packet back to the specified address.
rcb | The Ring Control Block | |
write | The address to shrink to. The current write pointer, up to alignment factors, is moved to this address. | |
left | The unused amount, in bytes, of the previously allocated packet. This value + write addresss must match the current write pointer. If not, error. Note that the amount being returned must be consistent with the alignment factors. Concretely stated, if one demands 8-byte alignment, one cannot return in units less than 8-bytes. |
Typically one would allocate a block of memory, and fill it by advancing the pointer returned and decreasing the amount left. After filling the buffer, one would then call RNG_shrink to return the unused portion. The parameters to RNG_shrink will be the address of the next location to be written and the amount left.
Note also that this has no consequences on waking anyone up who may be waiting for memory. By definition, this is a single allocator system. Since, RNG_shrink is really just extension of the allocation process, no one can be waiting.
EXAMPLE
-------
amount = 100; ptr = RNG_get (rcb, amount, prv); if (ptr == NULL) perror ("No memory\n"); *ptr++ = 0xdeadbeef; amount -= 1; *ptr++ = 0xabadbabe; amount -= 1; chk = RNG_shrink (rcb, ptr, amount); if (chk == NULL) perror ("Bad shrink\n");
References _RNG_rcb::dsc, _RNG_rcb::dyn, _RNG_dsc::msk, _RNG_dyn::que, _RNG_dyn::rd, _RNG_dsc::rend, _RNG_rcb::size, _RNG_dyn::wr, and _RNG_dyn::wrx.
int RNG_sizeof_rcb | ( | void | ) |
Returns the size of the Ring Contol Block.
The | size of a Ring Control Block |