chapter 9

18
9-1 Chapter 9 Storage Management

Upload: russ

Post on 05-Jan-2016

29 views

Category:

Documents


1 download

DESCRIPTION

Chapter 9. Storage Management. General lists (1). A node may contain a “simple” element or a list. list. 5. 2. null. external pointer. 6. 3. null. internal pointer. 3. null. 4. null. 14. null. The abstract list may be denoted by - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 9

9-1

Chapter 9

Storage Management

Page 2: Chapter 9

9-2

General lists (1) A node may contain a “simple” element

or a list.list 5 2 null

6 3 nullexternal pointer

3 null 4 null

14 null

internal pointer

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".

Page 3: Chapter 9

9-3

#define INTGR 1#define CH 2#define LST 3struct 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;};

General lists (2)

Page 4: Chapter 9

9-4

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.)

Lists and level of languages

Page 5: Chapter 9

9-5

Automatic list management (1)

(I) reference count method

list 11 1 1 null

2 1 null

1 2 null

reference count

list 2

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 of free nodes.

Page 6: Chapter 9

9-6

(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 ).

Automatic list management (2)

Page 7: Chapter 9

9-7

Dynamic memory management

Request for variable-length blocks (see next fig.)The pointer values should remain correctFor example,

before compaction: position 420 contain 340 (pointer, address)

after compaction: position 220 contain 140

Solution: base register

before afterbase register 325 125position 340 15 15

Page 8: Chapter 9

9-8

Alloc.

(1)Free

Alloc.

(2)Free

Alloc.

(3)

Alloc.

(4)

Alloc.

(5)Free

Alloc.

(1)

Alloc.

(2)

Alloc.

(3)

Alloc.

(4)

Alloc.

(5)Free

Alloc.

(1)

Alloc.

(2)

Alloc.

(3)

Alloc.

(4)

Alloc.

(5)

Alloc.

(6)Free

0 125 325 500 750 800 850 950 1023

0 125 300 350 400 500 1023

0 125 300 350 400 500 650 1023

(c)The request for 150 words has been granted.

(b)After compaction.

(a)Before compaction. A block of 150 words is requested.

freepoint=650

freepoint=500

freepoint=950

Page 9: Chapter 9

9-9

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 isgreater than or equal to the amountrequested.

worst-fit:find the largest block (see next fig.)

Page 10: Chapter 9

9-10

Block 1

(Size = 348)

Free

Size = 110

Block 3

(Size = 212)

Free

Size = 354

670458freeblock=3480 1023

Block 1

(Size = 348)

Free

Size = 110

Block 3

(Size = 212)

Size

=54Block 4

(Size = 300)

6704583480 1023724

(a)

(b) allocating 300

Block 1

(Size = 348)

Size =85

Block 5 (size =25)

Block 3

(Size = 212)

Size

=54Block 4

(Size = 300)

6704583480 1023724

(c) first-fit for 25

433

Page 11: Chapter 9

9-11

Block 1

(size = 348)

Free

Size = 110

Block 3

(Size = 212)

Size

=29

Block 5 (Size =

25)

Block 4

(Size = 300)

6704583480 1023724

(d) best-fit for 25

699

Block 1

(size = 348)

Size =40

Block 6 (Size=

70)

Block 3

(Size = 212)

Size

=29

Block 5 (Size =

25)

Block 4

(Size = 300)

6704583480 1023724

(e) best-fit for 70

699358

Page 12: Chapter 9

9-12

Blocks remaining using

Request First-fit Best-fit

Initially 110, 54 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.

First-fit and Best-fit

Page 13: Chapter 9

9-13

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 calledthe i-list. (There are more than one free list.)

Page 14: Chapter 9

9-14

(I) allocating storage request for a block of size n: (1) find the smallest integer such that n2i. (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.

Management in a buddy system (1)

Page 15: Chapter 9

9-15

(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. )

Management in a buddy system (2)

Page 16: Chapter 9

9-16

Free

(Size = 29)

Free

(Size = 28)

Free

(Size = 27)

B1 allocated

(Size = 27)

Free

(Size = 29)

Free

(Size = 28)

Free (Size = 26)

B2 alloc. (Size = 26)

B1 allocated

(Size = 27)

Free

(Size = 29)

Free

(Size = 27)

B5 alloc. (Size = 26)

B4 alloc. (Size = 26)

B3 alloc. (Size = 26)

B2 alloc. (Size = 26)

B1 allocated

(Size = 27)

0

512

768

896

0

512

768

896

0

512

768

896832 832

640

704

9: 08: 5127: 768

9: 08: 5126: 768

9: 07: 512

(a) Allocate 100. (b) Allocate 50. (c) Allocate 50, 50, 50.

Page 17: Chapter 9

9-17

Free

(Size = 29)

Free

(Size = 27)

B5 alloc. (Size = 26)

Free (Size = 26)

Free (Size = 26)

B2 alloc. (Size = 26)

B1 allocated

(Size = 27)

0

512

768

896832

640

704

9: 07: 5126: 704, 768

(d) Free B4, B3.

Free

(Size = 29)

Free

(Size = 27)

B5 alloc. (Size = 26)

Free (Size = 26)

Free (Size = 26)

B2 alloc. (Size = 26)

Free

(Size = 27)

0

512

768

896832

640

704

9: 07: 512, 8966: 704, 768

(e) Free B1.

Free

(Size = 29)

Free

(Size = 28)

Free (Size = 26)

B2 alloc. (Size = 26)

Free

(Size = 27)

0

512

768

896832

9: 08: 5127: 8966: 768

(f) Free B5.

Page 18: Chapter 9

9-18

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

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.

i-block

i-block(i+1)-block

p

P+2i

Starting positions of blocks

2i+1 p, 2i+1 P+2i