[Gambas-user] file question

Christopher Brian Jack brian at ...1334...
Wed Oct 11 18:31:12 CEST 2006


On Wed, 11 Oct 2006, James Hatridge wrote:

> Hi all
>
> Is there anyway I can set up a buffer or file so that when its full the oldest
> part is dropped and newst is kept?
>
> The idea I have is a music buffer that holds X minutes of audio when the
> buffer is full it will push out the old and add the new. Is this do-able?
>
> Thanks,
>
> JIM

The solution to your problem is a ring buffer.

To use a ring buffer split your main buffer into sequential chunks,
maintaining a head and tail position in the buffer.  If the head and tail
indices are zero-based (0 indicates the first chunk) then you can use x
MOD NumberOfChunks to get the wraparound effect needed for the index
arithmetic.  The explanation below uses MOD and hence assumes zero-based
indices.

When the buffer is initialized  head=0, tail=0, status="empty"

1.  To add to the buffer first make sure the status is not "full" (throw a
football if it is) then increase the head with head=(head+1) MOD
BufferChunkCount if this makes head=tail then set status to "full" and
throw a football.  If head!=tail then you can go ahead and fill the chunk
now indexed by head.

2.  To pull from the buffer first check that the buffer state is not
"empty" and throw a football if it is.  Transfer the chunk to whatever
needs it then increase the tail by one with tail=(tail+1) MOD
BufferChunkCount.  Appropriately setting the status to "empty" if the
condition tail=head is true after increasing the tail.

3. If the accesses to the buffer may be threaded then the head and tail
check-and-change need to be done within semaphores or with a locking
mechanism to make them atomic/serialized (only one thing accesses the
head/tail indices at a time).  Head needs to bumped before writing the
data and tail must only be bumped after the chunk is transferred to
whatever is using the data.

.=================================================.
|  Christopher BRIAN Jack aka "Gau of the Veldt"  |
+================================================='
| oevna at ...1544...
`=================================================-
Hi Spambots, my email address is sputnik at ...1334...
Hi Humans, my email address uses rot13 cipher





More information about the User mailing list