17 Allocation Basic
17 Allocation Basic
Instructors:
Randy
Bryant
and
Dave
O’Hallaron
1
Carnegie Mellon
Today
Basic
concepts
Implicit
free
lists
2
Carnegie Mellon
0
3
Carnegie Mellon
4
Carnegie Mellon
malloc
Example
void foo(int n, int m) {
int i, *p;
6
Carnegie Mellon
7
Carnegie Mellon
Alloca/on
Example
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
p4 = malloc(2)
8
Carnegie Mellon
Constraints
Applica/ons
Can
issue
arbitrary
sequence
of
malloc
and
free
requests
free
request
must
be
to
a
malloc’d
block
Allocators
Can’t
control
number
or
size
of
allocated
blocks
Must
respond
immediately
to
malloc
requests
i.e.,
can’t
reorder
or
buffer
requests
Must
allocate
blocks
from
free
memory
i.e.,
can
only
place
allocated
blocks
in
free
memory
Must
align
blocks
so
they
sa0sfy
all
alignment
requirements
8
byte
alignment
for
GNU
malloc
(libc
malloc)
on
Linux
boxes
Can
manipulate
and
modify
only
free
memory
Can’t
move
the
allocated
blocks
once
they
are
malloc’d
i.e.,
compac0on
is
not
allowed
9
Carnegie Mellon
Throughput:
Number
of
completed
requests
per
unit
0me
Example:
5,000
malloc
calls
and
5,000
free
calls
in
10
seconds
Throughput
is
1,000
opera0ons/second
10
Carnegie Mellon
11
Carnegie Mellon
Fragmenta/on
Poor
memory
u/liza/on
caused
by
fragmenta@on
internal
fragmenta0on
external
fragmenta0on
12
Carnegie Mellon
Internal
Fragmenta/on
For
a
given
block,
internal
fragmenta@on
occurs
if
payload
is
smaller
than
block
size
Block
Internal
Internal
Payload
fragmenta/on
fragmenta/on
Caused
by
Overhead
of
maintaining
heap
data
structures
Padding
for
alignment
purposes
Explicit
policy
decisions
(e.g.,
to
return
a
big
block
to
sa0sfy
a
small
request)
External
Fragmenta/on
Occurs
when
there
is
enough
aggregate
heap
memory,
but
no
single
free
block
is
large
enough
p1 = malloc(4)
p2 = malloc(5)
p3 = malloc(6)
free(p2)
Implementa/on
Issues
How
do
we
know
how
much
memory
to
free
given
just
a
pointer?
What
do
we
do
with
the
extra
space
when
alloca/ng
a
structure
that
is
smaller
than
the
free
block
it
is
placed
in?
How
do
we
pick
a
block
to
use
for
alloca/on
-‐-‐
many
might
fit?
p0
p0 = malloc(4) 5
free(p0)
16
Carnegie Mellon
5 4 6 2
Method 2: Explicit list among the free blocks using pointers
5 4 6 2
Today
Basic
concepts
Implicit
free
lists
18
Carnegie Mellon
Unused
Start
of
8/0
16/1
32/0
16/1
0/1
heap
20
Carnegie Mellon
Can
take
linear
0me
in
total
number
of
blocks
(allocated
and
free)
In
prac0ce
it
can
cause
“splinters”
at
beginning
of
list
Next
fit:
Like
first
fit,
but
search
list
star0ng
where
previous
search
finished
Should
o]en
be
faster
than
first
fit:
avoids
re-‐scanning
unhelpful
blocks
Some
research
suggests
that
fragmenta0on
is
worse
Best
fit:
Search
the
list,
choose
the
best
free
block:
fits,
with
fewest
bytes
le]
over
Keeps
fragments
small—usually
helps
fragmenta0on
Will
typically
run
slower
than
first
fit
21
Carnegie Mellon
4 4 6 2
p
addblock(p, 4)
4 4 4 2 2
4 4 4 2 2
free(p) p
4 4 4 2 2
malloc(5) Oops!
There is enough free space, but the allocator won’t be able to find it
23
Carnegie Mellon
4
4
4
2
2
logically
p
free(p) gone
4
4
6
2
2
void free_block(ptr p) {
*p = *p & -2; // clear allocated flag
next = p + *p; // find next block
if ((*next & 1) == 0)
*p = *p + *next; // add to this block if
} // not allocated
24
Carnegie Mellon
4 4 4 4 6 6 4 4
26
Carnegie Mellon
m1 1 m1 1
m1
1
m1
1
n
1
n
0
n
1
n
0
m2
1
m2
1
m2 1 m2 1
27
Carnegie Mellon
m1 1 m1 1
m1
1
m1
1
n
1
n+m2
0
n
1
m2
0
m2 0 n+m2 0
28
Carnegie Mellon
m1 0 n+m1 0
m1
0
n
1
n
1
n+m1
0
m2
1
m2
1
m2 1 m2 1
29
Carnegie Mellon
m1 0 n+m1+m2 0
m1
0
n
1
n
1
m2
0
m2 0 n+m1+m2 0
30
Carnegie Mellon
31
Carnegie Mellon
Splifng
policy:
When
do
we
go
ahead
and
split
free
blocks?
How
much
internal
fragmenta0on
are
we
willing
to
tolerate?
Coalescing
policy:
Immediate
coalescing:
coalesce
each
0me
free
is
called
Deferred
coalescing:
try
to
improve
performance
of
free
by
deferring
coalescing
un0l
needed.
Examples:
Coalesce
as
you
scan
the
free
list
for
malloc
Coalesce
when
the
amount
of
external
fragmenta0on
reaches
some
threshold
32
Carnegie Mellon