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.

Sec. 12.3 Memory Management 413/** Memory Manager interface */interface MemManADT {}/** Store a record <strong>and</strong> return a h<strong>and</strong>le to it */public MemH<strong>and</strong>le insert(byte[] info);/** Get back a copy of a stored record */public byte[] get(MemH<strong>and</strong>le h);/** Release the space associ<strong>at</strong>ed with a record */public void release(MemH<strong>and</strong>le h);Figure 12.8 A simple ADT for a memory manager.the restricted order of insertions <strong>and</strong> deletions makes this easy to deal with. Butin a language like C++ or Java, programmers can alloc<strong>at</strong>e <strong>and</strong> dealloc<strong>at</strong>e space incomplex ways through use of new. Where does this space come from? This sectiondiscusses memory management techniques for the general problem of h<strong>and</strong>lingspace requests of variable size.The basic model for memory management is th<strong>at</strong> we have a (large) block ofcontiguous memory loc<strong>at</strong>ions, which we will call the memory pool. Periodically,memory requests are issued for some amount of space in the pool. The memorymanager has the job of finding a contiguous block of loc<strong>at</strong>ions of <strong>at</strong> least the requestedsize from somewhere within the memory pool. Honoring such a requestis called a memory alloc<strong>at</strong>ion. The memory manager will typically return somepiece of inform<strong>at</strong>ion th<strong>at</strong> the requester can hold on to so th<strong>at</strong> l<strong>at</strong>er it can recoverthe record th<strong>at</strong> was just stored by the memory manager. This piece of inform<strong>at</strong>ionis called a h<strong>and</strong>le. At some point, space th<strong>at</strong> has been requested might no longerbe needed, <strong>and</strong> this space can be returned to the memory manager so th<strong>at</strong> it can bereused. This is called a memory dealloc<strong>at</strong>ion. The memory manager should thenbe able to reuse this space to s<strong>at</strong>isfy l<strong>at</strong>er memory requests. We can define an ADTfor the memory manager as shown in Figure 12.8.The user of the MemManager ADT provides a pointer (in parameter info) tospace th<strong>at</strong> holds some record or message to be stored or retrieved. This is similarto the Java basic file read/write methods presented in Section 8.4. The fundamentalidea is th<strong>at</strong> the client gives messages to the memory manager for safe keeping. Thememory manager returns a “receipt” for the message in the form of a MemH<strong>and</strong>leobject. Of course to be practical, a MemH<strong>and</strong>le must be much smaller than thetypical message to be stored. The client holds the MemH<strong>and</strong>le object until itwishes to get the message back.Method insert lets the client tell the memory manager the length <strong>and</strong> contentsof the message to be stored. This ADT assumes th<strong>at</strong> the memory manager willremember the length of the message associ<strong>at</strong>ed with a given h<strong>and</strong>le (perhaps in the

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

Saved successfully!

Ooh no, something went wrong!