11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

416 Chap. 12 Lists <strong>and</strong> Arrays RevisitedFigure 12.11 A doubly linked list of free blocks as seen by the memory manager.Shaded areas represent alloc<strong>at</strong>ed memory. Unshaded areas are part of the freelist.There are two basic approaches to implementing the freelist. The simpler approachis to store the freelist separ<strong>at</strong>ely from the memory pool. In other words,a simple linked-list implement<strong>at</strong>ion such as described in Chapter 4 can be used,where each node of the linked list contains a pointer to a single free block in thememory pool. This is fine if there is space available for the linked list itself, separ<strong>at</strong>efrom the memory pool.The second approach to storing the freelist is more complic<strong>at</strong>ed but saves space.Because the free space is free, it can be used by the memory manager to help it doits job. Th<strong>at</strong> is, the memory manager can temporarily “borrow” space within thefree blocks to maintain its doubly linked list. To do so, each unalloc<strong>at</strong>ed block mustbe large enough to hold these pointers. In addition, it is usually worthwhile to letthe memory manager add a few bytes of space to each reserved block for its ownpurposes. In other words, a request for m bytes of space might result in slightlymore than m bytes being alloc<strong>at</strong>ed by the memory manager, with the extra bytesused by the memory manager itself r<strong>at</strong>her than the requester. We will assume th<strong>at</strong>all memory blocks are organized as shown in Figure 12.12, with space for tags <strong>and</strong>linked list pointers. Here, free <strong>and</strong> reserved blocks are distinguished by a tag bit<strong>at</strong> both the beginning <strong>and</strong> the end of the block, for reasons th<strong>at</strong> will be explained.In addition, both free <strong>and</strong> reserved blocks have a size indic<strong>at</strong>or immedi<strong>at</strong>ely afterthe tag bit <strong>at</strong> the beginning of the block to indic<strong>at</strong>e how large the block is. Freeblocks have a second size indic<strong>at</strong>or immedi<strong>at</strong>ely preceding the tag bit <strong>at</strong> the end ofthe block. Finally, free blocks have left <strong>and</strong> right pointers to their neighbors in thefree block list.The inform<strong>at</strong>ion fields associ<strong>at</strong>ed with each block permit the memory managerto alloc<strong>at</strong>e <strong>and</strong> dealloc<strong>at</strong>e blocks as needed. When a request comes in for m wordsof storage, the memory manager searches the linked list of free blocks until it findsa “suitable” block for alloc<strong>at</strong>ion. How it determines which block is suitable willbe discussed below. If the block contains exactly m words (plus space for the tag<strong>and</strong> size fields), then it is removed from the freelist. If the block (of size k) is largeenough, then the remaining k − m words are reserved as a block on the freelist, inthe current loc<strong>at</strong>ion.When a block F is freed, it must be merged into the freelist. If we do notcare about merging adjacent free blocks, then this is a simple insertion into thedoubly linked list of free blocks. However, we would like to merge adjacent blocks,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!