<s>
Slab	B-General_Concept
allocation	I-General_Concept
is	O
a	O
memory	B-General_Concept
management	I-General_Concept
mechanism	O
intended	O
for	O
the	O
efficient	O
memory	B-General_Concept
allocation	I-General_Concept
of	O
objects	O
.	O
</s>
<s>
In	O
comparison	O
with	O
earlier	O
mechanisms	O
,	O
it	O
reduces	O
fragmentation	B-Architecture
caused	O
by	O
allocations	O
and	O
deallocations	B-General_Concept
.	O
</s>
<s>
Slab	B-General_Concept
allocation	I-General_Concept
was	O
first	O
introduced	O
in	O
the	O
Solaris	B-Application
2.4	O
kernel	B-Operating_System
by	O
Jeff	O
Bonwick	O
.	O
</s>
<s>
It	O
is	O
now	O
widely	O
used	O
by	O
many	O
Unix	O
and	O
Unix-like	O
operating	O
systems	O
including	O
FreeBSD	B-Operating_System
and	O
Linux	B-Operating_System
.	O
</s>
<s>
Slab	B-General_Concept
allocation	I-General_Concept
renders	O
infrequent	O
the	O
very	O
costly	O
practice	O
(	O
in	O
CPU	O
time	O
)	O
of	O
initialization	O
and	O
destruction	O
of	O
kernel	B-Operating_System
data-objects	O
,	O
which	O
can	O
outweigh	O
the	O
cost	O
of	O
allocating	O
memory	O
for	O
them	O
.	O
</s>
<s>
When	O
the	O
kernel	B-Operating_System
creates	O
and	O
deletes	O
objects	O
often	O
,	O
overhead	O
costs	O
of	O
initialization	O
can	O
result	O
in	O
significant	O
performance	O
drops	O
.	O
</s>
<s>
Object	O
caching	O
leads	O
to	O
less	O
frequent	O
invocation	O
of	O
functions	O
which	O
initialize	O
object	O
state	O
:	O
when	O
a	O
slab-allocated	O
object	O
is	O
released	O
after	O
use	O
,	O
the	O
slab	B-General_Concept
allocation	I-General_Concept
system	O
typically	O
keeps	O
it	O
cached	O
(	O
rather	O
than	O
doing	O
the	O
work	O
of	O
destroying	O
it	O
)	O
ready	O
for	O
re-use	O
next	O
time	O
an	O
object	O
of	O
that	O
type	O
is	O
needed	O
(	O
thus	O
avoiding	O
the	O
work	O
of	O
constructing	O
and	O
initialising	O
a	O
new	O
object	O
)	O
.	O
</s>
<s>
With	O
slab	B-General_Concept
allocation	I-General_Concept
,	O
a	O
cache	O
for	O
a	O
certain	O
type	O
or	O
size	O
of	O
data	O
object	O
has	O
a	O
number	O
of	O
pre-allocated	O
"	O
slabs	B-General_Concept
"	O
of	O
memory	O
;	O
within	O
each	O
slab	B-General_Concept
there	O
are	O
memory	O
chunks	O
of	O
fixed	O
size	O
suitable	O
for	O
the	O
objects	O
.	O
</s>
<s>
The	O
slab	B-General_Concept
allocator	I-General_Concept
keeps	O
track	O
of	O
these	O
chunks	O
,	O
so	O
that	O
when	O
it	O
receives	O
a	O
request	O
to	O
allocate	O
memory	O
for	O
a	O
data	O
object	O
of	O
a	O
certain	O
type	O
,	O
usually	O
it	O
can	O
satisfy	O
the	O
request	O
with	O
a	O
free	O
slot	O
(	O
chunk	O
)	O
from	O
an	O
existing	O
slab	B-General_Concept
.	O
</s>
<s>
When	O
the	O
allocator	O
is	O
asked	O
to	O
free	O
the	O
object	O
's	O
memory	O
,	O
it	O
just	O
adds	O
the	O
slot	O
to	O
the	O
containing	O
slab	B-General_Concept
's	O
list	O
of	O
free	O
(	O
unused	O
)	O
slots	O
.	O
</s>
<s>
This	O
process	B-Operating_System
eliminates	O
the	O
need	O
to	O
search	O
for	O
suitable	O
memory	O
space	O
and	O
greatly	O
alleviates	O
memory	B-Architecture
fragmentation	I-Architecture
.	O
</s>
<s>
In	O
this	O
context	O
,	O
a	O
slab	B-General_Concept
is	O
one	O
or	O
more	O
contiguous	O
pages	O
in	O
the	O
memory	O
containing	O
pre-allocated	O
memory	O
chunks	O
.	O
</s>
<s>
Understanding	O
the	O
slab	B-General_Concept
allocation	I-General_Concept
algorithm	O
requires	O
defining	O
and	O
explaining	O
some	O
terms	O
:	O
</s>
<s>
A	O
cache	O
is	O
a	O
storage	O
for	O
a	O
specific	O
type	O
of	O
object	O
,	O
such	O
as	O
semaphores	B-Operating_System
,	O
process	B-Operating_System
descriptors	O
,	O
file	B-Operating_System
objects	O
,	O
etc	O
.	O
</s>
<s>
Slab	B-General_Concept
:	O
slab	B-General_Concept
represents	O
a	O
contiguous	O
piece	O
of	O
memory	O
,	O
usually	O
made	O
of	O
several	O
virtually	O
contiguous	O
pages	O
.	O
</s>
<s>
The	O
slab	B-General_Concept
is	O
the	O
actual	O
container	O
of	O
data	O
associated	O
with	O
objects	O
of	O
the	O
specific	O
kind	O
of	O
the	O
containing	O
cache	O
.	O
</s>
<s>
When	O
a	O
program	O
sets	O
up	O
a	O
cache	O
,	O
it	O
allocates	O
a	O
number	O
of	O
objects	O
to	O
the	O
slabs	B-General_Concept
associated	O
with	O
that	O
cache	O
.	O
</s>
<s>
This	O
number	O
depends	O
on	O
the	O
size	O
of	O
the	O
associated	O
slabs	B-General_Concept
.	O
</s>
<s>
Slabs	B-General_Concept
may	O
exist	O
in	O
one	O
of	O
the	O
following	O
states	O
:	O
</s>
<s>
Initially	O
,	O
the	O
system	O
marks	O
each	O
slab	B-General_Concept
as	O
"	O
empty	O
"	O
.	O
</s>
<s>
When	O
the	O
process	B-Operating_System
calls	O
for	O
a	O
new	O
kernel	B-Operating_System
object	O
,	O
the	O
system	O
tries	O
to	O
find	O
a	O
free	O
location	O
for	O
that	O
object	O
on	O
a	O
partial	O
slab	B-General_Concept
in	O
a	O
cache	O
for	O
that	O
type	O
of	O
object	O
.	O
</s>
<s>
If	O
no	O
such	O
location	O
exists	O
,	O
the	O
system	O
allocates	O
a	O
new	O
slab	B-General_Concept
from	O
contiguous	O
virtual	B-General_Concept
pages	I-General_Concept
and	O
assigns	O
it	O
to	O
a	O
cache	O
.	O
</s>
<s>
The	O
new	O
object	O
gets	O
allocated	O
from	O
this	O
slab	B-General_Concept
,	O
and	O
its	O
location	O
becomes	O
marked	O
as	O
"	O
partial	O
"	O
.	O
</s>
<s>
The	O
allocation	O
takes	O
place	O
quickly	O
,	O
because	O
the	O
system	O
builds	O
the	O
objects	O
in	O
advance	O
and	O
readily	O
allocates	O
them	O
from	O
a	O
slab	B-General_Concept
.	O
</s>
<s>
A	O
slab	B-General_Concept
is	O
the	O
amount	O
by	O
which	O
a	O
cache	O
can	O
grow	O
or	O
shrink	O
.	O
</s>
<s>
It	O
represents	O
one	O
memory	B-General_Concept
allocation	I-General_Concept
to	O
the	O
cache	O
from	O
the	O
machine	O
,	O
and	O
whose	O
size	O
is	O
customarily	O
a	O
multiple	O
of	O
the	O
page	B-General_Concept
size	I-General_Concept
.	O
</s>
<s>
A	O
slab	B-General_Concept
must	O
contain	O
a	O
list	O
of	O
free	O
buffers	B-General_Concept
(	O
or	O
bufctls	O
)	O
,	O
as	O
well	O
as	O
a	O
list	O
of	O
the	O
bufctls	O
that	O
have	O
been	O
allocated	O
(	O
in	O
the	O
case	O
of	O
a	O
large	O
slab	B-General_Concept
size	O
)	O
.	O
</s>
<s>
These	O
are	O
for	O
caches	O
that	O
store	O
objects	O
that	O
are	O
at	O
least	O
1/8	O
of	O
the	O
page	B-General_Concept
size	I-General_Concept
for	O
a	O
given	O
machine	O
.	O
</s>
<s>
The	O
reason	O
for	O
the	O
large	O
slabs	B-General_Concept
having	O
a	O
different	O
layout	O
from	O
the	O
small	O
slabs	B-General_Concept
is	O
that	O
it	O
allows	O
large	O
slabs	B-General_Concept
to	O
pack	O
better	O
into	O
page-size	O
units	O
,	O
which	O
helps	O
with	O
fragmentation	B-Architecture
.	O
</s>
<s>
The	O
slab	B-General_Concept
contains	O
a	O
list	O
of	O
bufctls	O
,	O
which	O
are	O
simply	O
controllers	O
for	O
each	O
buffer	B-General_Concept
that	O
can	O
be	O
allocated	O
(	O
a	O
buffer	B-General_Concept
is	O
the	O
memory	O
that	O
the	O
user	O
of	O
a	O
slab	B-General_Concept
allocator	I-General_Concept
would	O
use	O
)	O
.	O
</s>
<s>
The	O
small	O
slabs	B-General_Concept
contain	O
objects	O
that	O
are	O
less	O
than	O
1/8	O
of	O
the	O
page	B-General_Concept
size	I-General_Concept
for	O
a	O
given	O
machine	O
.	O
</s>
<s>
These	O
small	O
slabs	B-General_Concept
need	O
to	O
be	O
optimized	O
further	O
from	O
the	O
logical	O
layout	O
,	O
by	O
avoiding	O
using	O
bufctls	O
(	O
which	O
would	O
be	O
just	O
as	O
large	O
as	O
the	O
data	O
itself	O
and	O
cause	O
memory	B-General_Concept
usage	I-General_Concept
to	O
be	O
much	O
greater	O
)	O
.	O
</s>
<s>
A	O
small	O
slab	B-General_Concept
is	O
exactly	O
one	O
page	O
,	O
and	O
has	O
a	O
defined	O
structure	O
that	O
allows	O
bufctls	O
to	O
be	O
avoided	O
.	O
</s>
<s>
The	O
last	O
part	O
of	O
the	O
page	O
contains	O
the	O
'	O
slab	B-General_Concept
header	O
 '	O
,	O
which	O
is	O
the	O
information	O
needed	O
to	O
retain	O
the	O
slab	B-General_Concept
.	O
</s>
<s>
Starting	O
at	O
the	O
first	O
address	O
of	O
that	O
page	O
,	O
there	O
are	O
as	O
many	O
buffers	B-General_Concept
as	O
can	O
be	O
allocated	O
without	O
running	O
into	O
the	O
slab	B-General_Concept
header	O
at	O
the	O
end	O
of	O
the	O
page	O
.	O
</s>
<s>
Instead	O
of	O
using	O
bufctls	O
,	O
we	O
use	O
the	O
buffers	B-General_Concept
themselves	O
to	O
retain	O
the	O
free	O
list	O
links	O
.	O
</s>
<s>
This	O
allows	O
the	O
small	O
slab	B-General_Concept
's	O
bufctl	O
to	O
be	O
bypassed	O
.	O
</s>
<s>
Linux	B-Operating_System
(	O
introduced	O
in	O
kernel	B-Operating_System
2.2	O
,	O
it	O
's	O
now	O
one	O
of	O
three	O
memory	B-General_Concept
allocator	I-General_Concept
implementations	O
together	O
with	O
SLOB	B-Application
and	O
SLUB	B-Application
.	O
</s>
<s>
The	O
three	O
allocators	O
provides	O
a	O
kind	O
of	O
front-end	O
to	O
the	O
zoned	B-General_Concept
buddy	I-General_Concept
allocator	I-General_Concept
for	O
those	O
sections	O
of	O
the	O
kernel	B-Operating_System
that	O
require	O
more	O
flexible	O
memory	B-General_Concept
allocation	I-General_Concept
than	O
the	O
standard	O
4KB	O
page	B-General_Concept
size	I-General_Concept
)	O
.	O
</s>
