<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
,	O
circular	B-Data_Structure
queue	I-Data_Structure
,	O
cyclic	B-Data_Structure
buffer	I-Data_Structure
or	O
ring	B-Data_Structure
buffer	I-Data_Structure
is	O
a	O
data	B-General_Concept
structure	I-General_Concept
that	O
uses	O
a	O
single	O
,	O
fixed-size	O
buffer	B-General_Concept
as	O
if	O
it	O
were	O
connected	O
end-to-end	O
.	O
</s>
<s>
This	O
structure	O
lends	O
itself	O
easily	O
to	O
buffering	O
data	B-General_Concept
streams	I-General_Concept
.	O
</s>
<s>
There	O
were	O
early	O
circular	B-Data_Structure
buffer	I-Data_Structure
implementations	O
in	O
hardware	O
.	O
</s>
<s>
A	O
circular	B-Data_Structure
buffer	I-Data_Structure
first	O
starts	O
out	O
empty	O
and	O
has	O
a	O
set	O
length	O
.	O
</s>
<s>
In	O
the	O
diagram	O
below	O
is	O
a	O
7-element	O
buffer	B-General_Concept
:	O
</s>
<s>
Assume	O
that	O
1	O
is	O
written	O
in	O
the	O
center	O
of	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
(	O
the	O
exact	O
starting	O
location	O
is	O
not	O
important	O
in	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
)	O
:	O
</s>
<s>
Then	O
assume	O
that	O
two	O
more	O
elements	O
are	O
added	O
to	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
2	O
&	O
3	O
which	O
get	O
put	O
after	O
1	O
:	O
</s>
<s>
If	O
two	O
elements	O
are	O
removed	O
,	O
the	O
two	O
oldest	O
values	O
inside	O
of	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
would	O
be	O
removed	O
.	O
</s>
<s>
Circular	B-Data_Structure
buffers	I-Data_Structure
use	O
FIFO	B-Operating_System
(	O
first	B-Operating_System
in	I-Operating_System
,	I-Operating_System
first	I-Operating_System
out	I-Operating_System
)	O
logic	O
.	O
</s>
<s>
In	O
the	O
example	O
,	O
1	O
&	O
2	O
were	O
the	O
first	O
to	O
enter	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
,	O
they	O
are	O
the	O
first	O
to	O
be	O
removed	O
,	O
leaving	O
3	O
inside	O
of	O
the	O
buffer	B-General_Concept
.	O
</s>
<s>
If	O
the	O
buffer	B-General_Concept
has	O
7	O
elements	O
,	O
then	O
it	O
is	O
completely	O
full	O
:	O
</s>
<s>
A	O
property	O
of	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
is	O
that	O
when	O
it	O
is	O
full	O
and	O
a	O
subsequent	O
write	O
is	O
performed	O
,	O
then	O
it	O
starts	O
overwriting	O
the	O
oldest	O
data	O
.	O
</s>
<s>
Alternatively	O
,	O
the	O
routines	O
that	O
manage	O
the	O
buffer	B-General_Concept
could	O
prevent	O
overwriting	O
the	O
data	O
and	O
return	O
an	O
error	O
or	O
raise	O
an	O
exception	B-General_Concept
.	O
</s>
<s>
Whether	O
or	O
not	O
data	O
is	O
overwritten	O
is	O
up	O
to	O
the	O
semantics	O
of	O
the	O
buffer	B-General_Concept
routines	O
or	O
the	O
application	O
using	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
.	O
</s>
<s>
Finally	O
,	O
if	O
two	O
elements	O
are	O
now	O
removed	O
then	O
what	O
would	O
be	O
returned	O
is	O
not	O
3	O
&	O
4	O
but	O
A	O
&	O
B	O
because	O
A	O
&	O
B	O
overwrote	O
the	O
3	O
&	O
the	O
4	O
yielding	O
the	O
buffer	B-General_Concept
with	O
:	O
</s>
<s>
The	O
useful	O
property	O
of	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
is	O
that	O
it	O
does	O
not	O
need	O
to	O
have	O
its	O
elements	O
shuffled	O
around	O
when	O
one	O
is	O
consumed	O
.	O
</s>
<s>
In	O
other	O
words	O
,	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
is	O
well-suited	O
as	O
a	O
FIFO	B-Operating_System
(	O
first	B-Operating_System
in	I-Operating_System
,	I-Operating_System
first	I-Operating_System
out	I-Operating_System
)	O
buffer	B-General_Concept
while	O
a	O
standard	O
,	O
non-circular	O
buffer	B-General_Concept
is	O
well	O
suited	O
as	O
a	O
LIFO	B-Application
(	O
last	O
in	O
,	O
first	O
out	O
)	O
buffer	B-General_Concept
.	O
</s>
<s>
Circular	O
buffering	O
makes	O
a	O
good	O
implementation	O
strategy	O
for	O
a	O
queue	B-Application
that	O
has	O
fixed	O
maximum	O
size	O
.	O
</s>
<s>
Should	O
a	O
maximum	O
size	O
be	O
adopted	O
for	O
a	O
queue	B-Application
,	O
then	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
is	O
a	O
completely	O
ideal	O
implementation	O
;	O
all	O
queue	B-Application
operations	O
are	O
constant	O
time	O
.	O
</s>
<s>
However	O
,	O
expanding	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
requires	O
shifting	O
memory	O
,	O
which	O
is	O
comparatively	O
costly	O
.	O
</s>
<s>
For	O
arbitrarily	O
expanding	O
queues	B-Application
,	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
approach	O
may	O
be	O
preferred	O
instead	O
.	O
</s>
<s>
In	O
some	O
situations	O
,	O
overwriting	O
circular	B-Data_Structure
buffer	I-Data_Structure
can	O
be	O
used	O
,	O
e.g.	O
</s>
<s>
If	O
the	O
buffer	B-General_Concept
is	O
used	O
as	O
the	O
bounded	O
buffer	B-General_Concept
in	O
the	O
producer	O
–	O
consumer	O
problem	O
then	O
it	O
is	O
probably	O
desired	O
for	O
the	O
producer	O
(	O
e.g.	O
,	O
an	O
audio	O
generator	O
)	O
to	O
overwrite	O
old	O
data	O
if	O
the	O
consumer	O
(	O
e.g.	O
,	O
the	O
sound	B-Device
card	I-Device
)	O
is	O
unable	O
to	O
momentarily	O
keep	O
up	O
.	O
</s>
<s>
Also	O
,	O
the	O
LZ77	O
family	O
of	O
lossless	O
data	O
compression	O
algorithms	O
operates	O
on	O
the	O
assumption	O
that	O
strings	O
seen	O
more	O
recently	O
in	O
a	O
data	B-General_Concept
stream	I-General_Concept
are	O
more	O
likely	O
to	O
occur	O
soon	O
in	O
the	O
stream	O
.	O
</s>
<s>
Implementations	O
store	O
the	O
most	O
recent	O
data	O
in	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
.	O
</s>
<s>
A	O
circular	B-Data_Structure
buffer	I-Data_Structure
can	O
be	O
implemented	O
using	O
a	O
pointer	O
and	O
three	O
integers	O
:	O
</s>
<s>
This	O
image	O
shows	O
a	O
partially	O
full	O
buffer	B-General_Concept
with	O
Length	O
=	O
7	O
:	O
</s>
<s>
This	O
image	O
shows	O
a	O
full	O
buffer	B-General_Concept
with	O
four	O
elements	O
(	O
numbers	O
1	O
through	O
4	O
)	O
having	O
been	O
overwritten	O
:	O
</s>
<s>
The	O
circular	B-Data_Structure
buffer	I-Data_Structure
write	O
operation	O
writes	O
an	O
element	O
to	O
the	O
end	O
index	O
position	O
and	O
the	O
end	O
index	O
is	O
incremented	O
to	O
the	O
next	O
buffer	B-General_Concept
position	O
.	O
</s>
<s>
The	O
circular	B-Data_Structure
buffer	I-Data_Structure
read	O
operation	O
reads	O
an	O
element	O
from	O
the	O
start	O
index	O
position	O
and	O
the	O
start	O
index	O
is	O
incremented	O
to	O
the	O
next	O
buffer	B-General_Concept
position	O
.	O
</s>
<s>
The	O
start	O
and	O
end	O
indexes	O
are	O
not	O
enough	O
to	O
tell	O
buffer	B-General_Concept
full	O
or	O
empty	O
state	O
while	O
also	O
utilizing	O
all	O
buffer	B-General_Concept
slots	O
,	O
but	O
can	O
if	O
the	O
buffer	B-General_Concept
only	O
has	O
a	O
maximum	O
in-use	O
size	O
of	O
Length	O
-	O
1	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
buffer	B-General_Concept
is	O
empty	O
if	O
the	O
start	O
and	O
end	O
indexes	O
are	O
equal	O
and	O
full	O
when	O
the	O
in-use	O
size	O
is	O
Length	O
-	O
1	O
.	O
</s>
<s>
Function	O
put( )	O
puts	O
an	O
item	O
in	O
the	O
buffer	B-General_Concept
,	O
function	O
get( )	O
gets	O
an	O
item	O
from	O
the	O
buffer	B-General_Concept
:	O
</s>
<s>
A	O
circular-buffer	O
implementation	O
may	O
be	O
optimized	O
by	O
mapping	B-Operating_System
the	O
underlying	O
buffer	B-General_Concept
to	O
two	O
contiguous	O
regions	O
of	O
virtual	B-Architecture
memory	I-Architecture
.	O
</s>
<s>
(	O
Naturally	O
,	O
the	O
underlying	O
buffer	B-General_Concept
‘	O
s	O
length	O
must	O
then	O
equal	O
some	O
multiple	O
of	O
the	O
system	O
’s	O
page	B-General_Concept
size	I-General_Concept
.	O
)	O
</s>
<s>
Reading	O
from	O
and	O
writing	O
to	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
may	O
then	O
be	O
carried	O
out	O
with	O
greater	O
efficiency	O
by	O
means	O
of	O
direct	O
memory	O
access	O
;	O
those	O
accesses	O
which	O
fall	O
beyond	O
the	O
end	O
of	O
the	O
first	O
virtual-memory	O
region	O
will	O
automatically	O
wrap	O
around	O
to	O
the	O
beginning	O
of	O
the	O
underlying	O
buffer	B-General_Concept
.	O
</s>
<s>
When	O
the	O
read	O
offset	O
is	O
advanced	O
into	O
the	O
second	O
virtual-memory	O
region	O
,	O
both	O
offsets	O
—	O
read	O
and	O
write	O
—	O
are	O
decremented	O
by	O
the	O
length	O
of	O
the	O
underlying	O
buffer	B-General_Concept
.	O
</s>
<s>
Perhaps	O
the	O
most	O
common	O
version	O
of	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
uses	O
8-bit	O
bytes	O
as	O
elements	O
.	O
</s>
<s>
Some	O
implementations	O
of	O
the	O
circular	B-Data_Structure
buffer	I-Data_Structure
use	O
fixed-length	O
elements	O
that	O
are	O
bigger	O
than	O
8-bit	O
bytes	O
—	O
16-bit	O
integers	O
for	O
audio	O
buffers	B-General_Concept
,	O
53-byte	O
ATM	O
cells	O
for	O
telecom	O
buffers	B-General_Concept
,	O
etc	O
.	O
</s>
<s>
Each	O
item	O
is	O
contiguous	O
and	O
has	O
the	O
correct	O
data	B-Application
alignment	I-Application
,	O
so	O
software	O
reading	O
and	O
writing	O
these	O
values	O
can	O
be	O
faster	O
than	O
software	O
that	O
handles	O
non-contiguous	O
and	O
non-aligned	O
values	O
.	O
</s>
<s>
Ping-pong	O
buffering	O
can	O
be	O
considered	O
a	O
very	O
specialized	O
circular	B-Data_Structure
buffer	I-Data_Structure
with	O
exactly	O
two	O
large	O
fixed-length	O
elements	O
.	O
</s>
<s>
The	O
bip	O
buffer	B-General_Concept
(	O
bipartite	O
buffer	B-General_Concept
)	O
is	O
very	O
similar	O
to	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
,	O
except	O
it	O
always	O
returns	O
contiguous	O
blocks	O
which	O
can	O
be	O
variable	O
length	O
.	O
</s>
<s>
This	O
offers	O
nearly	O
all	O
the	O
efficiency	O
advantages	O
of	O
a	O
circular	B-Data_Structure
buffer	I-Data_Structure
while	O
maintaining	O
the	O
ability	O
for	O
the	O
buffer	B-General_Concept
to	O
be	O
used	O
in	O
APIs	O
that	O
only	O
accept	O
contiguous	O
blocks	O
.	O
</s>
<s>
Fixed-sized	O
compressed	O
circular	B-Data_Structure
buffers	I-Data_Structure
use	O
an	O
alternative	O
indexing	O
strategy	O
based	O
on	O
elementary	O
number	O
theory	O
to	O
maintain	O
a	O
fixed-sized	O
compressed	O
representation	O
of	O
the	O
entire	O
data	O
sequence	O
.	O
</s>
