Transcript Document

Chapter 9
Storage Management
9-1
General lists (1)

A node may contain a “simple” element or a list.
list
5
external
pointer
2
6
internal pointer
3
null
14
null
4
3
null
null
null
The abstract list may be denoted by
list = (5, (3, (14), ( ), 4), 2, (6, 3))
head(list) = 5 : the value of the first element of "list".
tail(list) = ((3, (14), ( ), 4), 2, (6, 3)) :
a list by removing the first element of "list".9-2
General lists (2)
#define INTGR
1
#define CH
2
#define LST
3
struct nodetype{
int utype; /* utype equals INTGR, CH, or LST */
union{
int intgrinfo; /* utype = INTGR */
char charinfo; /* utype = CH */
struct nodetype *lstinfo; /* utype = LST */
}info;
struct nodetype *next;
};
9-3
Lists and level of languages



LISP has operations for lists as its built-in
operations.
In some sense, language that include lists
as native data types are of "higher
level“ than C.
Similarly, C is of "higher level" than
FORTRAN. (C has structures and unions.)
9-4
Automatic list management (1)
(I) reference count method
list 1
1
1
reference
count
list 2
1
2
1
null
1
2
null
null
reference count: # of pointers to that node.
When the reference count in any node becomes
0, that node can be returned to the available list
9-5
of free nodes.
Automatic list management (2)
(II) garbage collection
Nodes no longer in use remain allocated and
undetected until all available storage has been
allocated. It searches through all the nodes in
the system, identifies those that are no longer
accessible from an external pointer, and
restores the inaccessible nodes to the available
pool.

compaction: moving all the available memory
to one end of memory (when contiguous
available memory is not enough ).
9-6
Dynamic memory management
Request for variable-length blocks (see next fig.)
The pointer values should remain correct
For example,
before compaction: position 420 contain 340 (pointer, address)
after compaction: position 220 contain 140
Solution: base register
base register
position 340
before
325
15
after
125
15
9-7
0
125
Alloc.
(1)
325
500
Alloc.
(2)
Free
750
Free
800
Allo
c.
(3)
Allo
c.
(4)
(a)Before compaction. A block of 150 words is requested.
0
125
Alloc.
(1)
300
Alloc.
(2)
350
Allo
c.
(3)
400
Allo
c.
(4)
125
Alloc.
(1)
300
Alloc.
(2)
950
Allo
c.
(5)
1023
Free
freepoint=950
500
1023
Allo
c.
(5)
Free
freepoint=500
(b)After compaction.
0
850
350
Allo
c.
(3)
400
Allo
c.
(4)
500
1023
650
Allo
Alloc.
c.
(6)
(5)
(c)The request for 150 words has been granted.
Free
freepoint=650
9-8
Methods for allocating storage



first-fit:
find the first free block whose size is
greater than or equal to the amount
requested.
best-fit:
find the smallest free block whose size is
greater than or equal to the amount
requested.
worst-fit:
find the largest block (see next fig.)
9-9
0
458
freeblock=348
Block 1
(Size = 348)
Free
Size = 110
670
1023
Block 3
(Size = 212)
Free
Size = 354
(a)
0
458
348
Block 1
(Size = 348)
Free
Size = 110
670
Block 3
(Size = 212)
724
Size
=54
1023
Block 4
(Size = 300)
(b) allocating 300
348 433
0
Block 1
(Size = 348)
Size
=85
458
Block 5
(size
=25)
670
Block 3
(Size = 212)
724
Size
=54
(c) first-fit for 25
1023
Block 4
(Size = 300)
9-10
0
458
348
Block 1
(size = 348)
Free
Size = 110
670
Block 3
(Size = 212)
699
724
Size Block 5
=29
(Size =
25)
1023
Block 4
(Size = 300)
(d) best-fit for 25
0
348
Block 1
(size = 348)
358
Size
=40
458
Block 6
(Size=
70)
670
Block 3
(Size = 212)
699
Size
=29
724
Block 5
(Size =
25)
1023
Block 4
(Size = 300)
(e) best-fit for 70
9-11
First-fit and Best-fit
Blocks remaining using

Request
Initially
First-fit
110, 54
Best-fit
110, 54
25
85, 54
110, 29
70
15, 54
40, 29
50
15, 4
cannot be
fulfilled
Freeing storage blocks should be
combined with its neighbors.
9-12
The buddy system


Each block is of size 2i, 0  i  m.
A block of size 2i is called an i-block.
The free list containing i-blocks is called
the i-list. (There are more than one free
list.)
9-13
Management in a buddy system (1)
(I) allocating storage
request for a block of size n:
(1) find the smallest integer such that n2i.
(2) allocate an i-block from i-list.
(3) if i-list is empty
take an (i+1)-block from (i+1)-list.
split it into 2 i-blocks.
allocate one of the 2 i-blocks
insert the other into i-list.
(4) if (i+1)-list is empty
action similar to step 3.
 If an (i+1)-block is split into i-blocks B1 and B2,
then B1 and B2 are buddies of each other.
9-14
Management in a buddy system (2)
(II) Freeing storage
(1) if the i-buddy of a newly freed i-block is not
free, then insert it into i-list.
(2) otherwise, two buddies are combined into
an (i+1)-block.
(3) ( two(i+1)-buddies may be combined into
an (i+2)-block. )
9-15
0
0
Free
(Size = 29)
512
512
896
Free
(Size = 27)
B1 allocated
(Size = 27)
9: 0
8: 512
7: 768
(a) Allocate 100.
Free
(Size = 29)
Free
(Size = 29)
Free
(Size = 28)
768
0
512
Free
(Size = 28)
768
Free (Size = 26)
832
B2 alloc. (Size = 26)
896
B1 allocated
(Size = 27)
9: 0
8: 512
6: 768
(b) Allocate 50.
Free
(Size = 27)
640
B5 alloc. (Size = 26)
704
B4 alloc. (Size = 26)
768
B3 alloc. (Size = 26)
832
B2 alloc. (Size = 26)
896
B1 allocated
(Size = 27)
9: 0
7: 512
9-16
(c) Allocate 50, 50, 50.
0
0
Free
(Size = 29)
Free
(Size = 29)
512
512
Free
(Size = 27)
Free
(Size = 29)
512
Free
(Size = 27)
640
704
768
0
Free
(Size = 28)
640
B5 alloc. (Size =
26)
Free (Size = 26)
Free (Size =
26)
832
B2 alloc. (Size = 26)
896
B1 allocated
(Size = 27)
9: 0
7: 512
6: 704, 768
(d) Free B4, B3.
704
768
B5 alloc. (Size = 26)
Free (Size = 26)
Free (Size =
26)
832
B2 alloc. (Size = 26)
896
Free
(Size = 27)
9: 0
7: 512, 896
6: 704, 768
(e) Free B1.
768
Free (Size = 26)
832 B2 alloc. (Size = 26)
896
Free
(Size = 27)
9:
8:
7:
6:
0
512
896
768
9-17
(f) Free B5.
Starting positions of blocks

A block of a given size can start only at some specific
addresses. i-blocks start at locations that are integer
multiples of 2i. Assume an (i+1)-block starts at p
p
i-block
P+2i
(i+1)-block
2i+1 p, 2i+1
P+2i
i-block

Assume an i-block starting at q
If 2i+1|q, then it is the first half of an (i+1)-block and its
buddy is at q+2i.
Otherwise, it is the second half of an (i+1)-block and its
buddy is at q-2i.
9-18