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.

414 Chap. 12 Lists <strong>and</strong> Arrays RevisitedFigure 12.9 Dynamic storage alloc<strong>at</strong>ion model. Memory is made up of a seriesof variable-size blocks, some alloc<strong>at</strong>ed <strong>and</strong> some free. In this example, shadedareas represent memory currently alloc<strong>at</strong>ed <strong>and</strong> unshaded areas represent unusedmemory available for future alloc<strong>at</strong>ion.h<strong>and</strong>le itself), thus method get does not include a length parameter but insteadreturns the length of the message actually stored. Method release allows theclient to tell the memory manager to release the space th<strong>at</strong> stores a given message.When all inserts <strong>and</strong> releases follow a simple p<strong>at</strong>tern, such as last requested,first released (stack order), or first requested, first released (queue order), memorymanagement is fairly easy. We are concerned here with the general case whereblocks of any size might be requested <strong>and</strong> released in any order. This is knownas dynamic storage alloc<strong>at</strong>ion. One example of dynamic storage alloc<strong>at</strong>ion ismanaging free store for a compiler’s runtime environment, such as the systemlevelnew oper<strong>at</strong>ion in Java. Another example is managing main memory in amultitasking oper<strong>at</strong>ing system. Here, a program might require a certain amountof space, <strong>and</strong> the memory manager must keep track of which programs are usingwhich parts of the main memory. Yet another example is the file manager for adisk drive. When a disk file is cre<strong>at</strong>ed, exp<strong>and</strong>ed, or deleted, the file manager mustalloc<strong>at</strong>e or dealloc<strong>at</strong>e disk space.A block of memory or disk space managed in this way is sometimes referred toas a heap. The term “heap” is being used here in a different way than the heap d<strong>at</strong>astructure discussed in Section 5.5. Here “heap” refers to the memory controlled bya dynamic memory management scheme.In the rest of this section, we first study techniques for dynamic memory management.We then tackle the issue of wh<strong>at</strong> to do when no single block of memoryin the memory pool is large enough to honor a given request.12.3.1 Dynamic Storage Alloc<strong>at</strong>ionFor the purpose of dynamic storage alloc<strong>at</strong>ion, we view memory as a single arraywhich, after a series of memory requests <strong>and</strong> releases tends to become broken intoa series of variable-size blocks, where some of the blocks are free <strong>and</strong> some arereserved or already alloc<strong>at</strong>ed to store messages. The memory manager typicallyuses a linked list to keep track of the free blocks, called the freelist, which is usedfor servicing future memory requests. Figure 12.9 illustr<strong>at</strong>es the situ<strong>at</strong>ion th<strong>at</strong> canarise after a series of memory alloc<strong>at</strong>ions <strong>and</strong> dealloc<strong>at</strong>ions.When a memory request is received by the memory manager, some block onthe freelist must be found th<strong>at</strong> is large enough to service the request. If no such

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

Saved successfully!

Ooh no, something went wrong!