1 / 18

Chapter 9

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

russ
Download Presentation

Chapter 9

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9 Storage Management

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

  3. 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; };

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

  5. Automatic list management (1) (I) reference count method list 1 1 1 1 null reference count 2 1 null list 2 1 2 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 of free nodes.

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

  7. 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 beforeafter base register 325 125 position 340 15 15

  8. 0 125 325 500 750 800 850 950 1023 freepoint=950 (a)Before compaction. A block of 150 words is requested. 0 125 300 350 400 500 1023 freepoint=500 (b)After compaction. 0 125 300 350 400 500 1023 650 freepoint=650 (c)The request for 150 words has been granted.

  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 is greater than or equal to the amount requested. • worst-fit: find the largest block (see next fig.)

  10. 458 0 freeblock=348 670 1023 (a) 458 724 0 348 670 1023 (b) allocating 300 433 458 724 0 348 670 1023 (c) first-fit for 25

  11. 458 724 0 348 670 699 1023 (d) best-fit for 25 458 724 0 348 358 670 699 1023 (e) best-fit for 70

  12. First-fit and Best-fit • Freeing storage blocks should be combined with its neighbors.

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

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

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

  16. 0 0 0 512 512 512 640 704 768 768 768 832 832 896 896 896 9: 0 8: 512 7: 768 9: 0 8: 512 6: 768 9: 0 7: 512 (a) Allocate 100. (b) Allocate 50. (c) Allocate 50, 50, 50.

  17. 0 0 0 512 512 512 640 640 704 704 768 768 768 832 832 832 896 896 896 9: 0 8: 512 7: 896 6: 768 9: 0 7: 512 6: 704, 768 9: 0 7: 512, 896 6: 704, 768 (d) Free B4, B3. (e) Free B1. (f) Free B5.

  18. Starting positions of blocks 2i+1 p, 2i+1 P+2i • 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+1)-block P+2i • 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.

More Related