#include "BBC/LLI_type.h"
Defines | |
| #define | RNG_K_WTO_NO_WAIT (0) |
| Timeout = 0, ie do not wait. | |
| #define | RNG_K_WTO_FOREVER (-1) |
| Wait forever. | |
Typedefs | |
| typedef _RNG_rcb | RNG_rcb |
| Typedef for RNG buffer control block. | |
| typedef enum _RNG_type | RNG_type |
| Typedef of the enumeration _RNG_type. | |
Enumerations | |
| enum | _RNG_type { RNG_K_TYPE_NON_BLOCKING = LLI_K_TYPE_NON_BLOCKING, RNG_K_TYPE_FIFO_BLOCKING = LLI_K_TYPE_FIFO_BLOCKING, RNG_K_TYPE_PRIORITY_BLOCKING = LLI_K_TYPE_PRIORITY_BLOCKING } |
| The type of blocking to use when an allocation is attempted on an empty pool. More... | |
Functions | |
| void * | RNG_beg (const struct _RNG_rcb *rcb) |
| Query routine to return a pointer to the beginning of the underflow area. More... | |
| void * | RNG_rbeg (const struct _RNG_rcb *rcb) |
| Query routine to return a pointer to the beginning of the ring buffer area. More... | |
| void * | RNG_rend (const struct _RNG_rcb *rcb) |
| Query routine to return a pointer to the end of the ring buffer area. More... | |
| void * | RNG_end (const struct _RNG_rcb *rcb) |
| Query routine to return a pointer to the end of the overflow area. More... | |
| void * | RNG_rd (const struct _RNG_rcb *rcb) |
| Query routine to return a pointer to current read pointer the ring buffer area. More... | |
| void * | RNG_wr (const struct _RNG_rcb *rcb) |
| Query routine to return a pointer to the current write pointer. More... | |
| void * | RNG_shard (const struct _RNG_rcb *rcb) |
| Query routine to return a pointer to current list of shards. More... | |
| void * | RNG_buf (const struct _RNG_rcb *rcb) |
| Query routine to return a pointer to the original buffer. More... | |
| int | RNG_bsize (const struct _RNG_rcb *rcb) |
| Query routine to return the size of the original buffer. More... | |
| int | RNG_destroy (struct _RNG_rcb *rcb) |
| Returns any resources associated with this ring buffer. More... | |
| int | RNG_sizeof_rcb (void) |
| Returns the size of the Ring Contol Block. More... | |
| int | RNG_init (struct _RNG_rcb *rcb, unsigned char *buf, int size, int underflow, int overflow, int alignment, RNG_type blocking) |
| Initializes the control structure for a Ring buffer. More... | |
| void * | RNG_get (struct _RNG_rcb *rcb, int request, const void *chkWrt) |
| Non-blocking allocation request for a specified amount of memory. More... | |
| void * | RNG_grab (struct _RNG_rcb *rcb, int minimum, const void *chkWrt, int *allocated) |
| Non-blocking allocation request for all the remaining contigious memory. More... | |
| void * | RNG_getW (struct _RNG_rcb *rcb, int request, const void *chkWrt, int timeout) |
| Blocking allocation request for a specified amount of memory. More... | |
| void * | RNG_grabW (struct _RNG_rcb *rcb, int minimum, const void *chkWrt, int *allocated, int timeout) |
| Non-blocking allocation request for all the remaining contigious memory. More... | |
| int | RNG_free (struct _RNG_rcb *rcb, const void *packet, int amount) |
| Frees the requested amount of memory from the specified address. More... | |
| void * | RNG_shrink (struct _RNG_rcb *rcb, const void *newWrt, int left) |
| Shrinks the previously allocated packet back to the specified address. More... | |
| int | RNG_reset (struct _RNG_rcb *rcb) |
| If the pool is empty, resets the WR/RD pointers to their initial values. More... | |
| unsigned int | RNG_usec_to_rto (unsigned int usecs) |
| Converts a time, specified in usecs, to a timeout value. | |
The most common usage would be to have the WRITER allocate the memory and fill it. This would then be passed along to another thread which would 'read' or consume the contents, then free the memory.
While ring buffers managers are quick and simply allocators, ring buffers have their limitations. This version supports only in order allocation and deallocation. That is one must deallocate the memory in the same order that it was allocated.
This version of ring buffer management does provide some help when the end of the ring buffer is reached. In order to keep the allocation request contigious in this case, the ring buffer may be configured with an overflow area. The ring buffer management utilities are allowed to use the overflow to satisfy an allocation request, but they never begin an allocation in the overflow area. It is the user's responsibility to specify the overflow area to be as large as the maximum size request that will be made.
The user may also configure an underflow area, which is symmetric to the overflow area, except at the top of the buffer. It's usage is a bit more esoteric. Imagine the situation of wishing to DMA 1000 bytes into a piece of memory allocated by the RNG utility. Circumstances dictate that the memory be contigious within the ring buffer pool. The DMA operation naturally demands continuity. Suppose the current state of the ring buffer is such that one is close the end of the ring buffer pool. The overflow area could be used to satisfy this request. At the end of the DMA operation, the user simply allocates the amount of memory which spilled into the overflow area at the top of the ring buffer and copies into it. The user is now pretty content. The DMA operation proceeded nicely into a chunk of contigious memory and, after the copy operation, the results are contigious in the ring buffer. So what's the problem? It's the copy operation. Suppose the ring buffer only had 4 bytes left at the bottom, leaving the user to copy 996 bytes back to the top. If the allocation could have been made 4 bytes above the top of the ring buffer, then only 4 bytes would have to be copied to the bottom. The underflow provides the memory to implement this scheme.
unsigned char pool[POOL_SIZE];
size = RNG_sizeof_rcb ();
rcb = (struct _RNG_rcb *)malloc (size);
RNG_init (rcb, pool, sizeof(pool), BYTE_ALIGN, RNG_K_TYPE_FIFO_BLOCKING);
.
.
max = 1000; / * Ask for 1000 bytes of memory * /
msg = (unsigned char *)RNG_getW (rcb, max, NULL, RNG_K_WTO_FOREVER);
.
.
/ * Fill this memory, then return the used portion * /
next = fillMemory (msg, max, &left);
RNG_shrink (rcb, next, left);
used = max - left;
.
.
/ * Return the rest of the allocated memory * /
RNG_free (rcb, msg, used);
|
|
The type of blocking to use when an allocation is attempted on an empty pool.
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 two types of blocking, either blocking in FIFO order or PRIORITY order. If a non-blocking type is requested, common practice would be to use the simpler RNG_get() routine, since it never waits. |
|
|
Query routine to return a pointer to the beginning of the underflow area.
|
|
|
Query routine to return the size of the original buffer.
|
|
|
Query routine to return a pointer to the original buffer.
|
|
|
Returns any resources associated with this ring buffer.
|
|
|
Query routine to return a pointer to the end of the overflow area.
|
|
||||||||||||||||
|
Frees the requested amount of memory from the specified address.
|
|
||||||||||||||||
|
Non-blocking allocation request for a specified amount of memory.
|
|
||||||||||||||||||||
|
Blocking allocation request for a specified amount of memory.
|
|
||||||||||||||||||||
|
Non-blocking allocation request for all the remaining contigious memory.
If there is less than the minimum amount of memory in the pool, NULL is returned. |
|
||||||||||||||||||||||||
|
Non-blocking allocation request for all the remaining contigious memory.
If less the timeout period expires or the internal write pointer is inconsistent with the chkWrt argument, NULL is returned. The user may distinguish these cases by calling RNG_wr().
|
|
||||||||||||||||||||||||||||||||
|
Initializes the control structure for a Ring buffer.
|
|
|
Query routine to return a pointer to the beginning of the ring buffer area.
|
|
|
Query routine to return a pointer to current read pointer the ring buffer area.
|
|
|
Query routine to return a pointer to the end of the ring buffer area.
|
|
|
If the pool is empty, resets the WR/RD pointers to their initial values.
|
|
|
Query routine to return a pointer to current list of shards.
|
|
||||||||||||||||
|
Shrinks the previously allocated packet back to the specified address.
|
|
|
Returns the size of the Ring Contol Block.
|
|
|
Query routine to return a pointer to the current write pointer.
|
1.2.13.1 written by Dimitri van Heesch,
© 1997-2001