Sie sind auf Seite 1von 1

Data Structures and Algorithms Special Class LAB-09 Dated: January 04, 2012

Objective:

Understanding of doubly linked list.

Free Space Management with Doubly Linked Lists


Have you ever wondered how the new operator, which is used to create an object in C, C++ and Java which support dynamic allocation of memory. One of the responsibilities of new is to allocate memory space for the newly-created object. Java uses a memory manager to keep track of the memory space (the free space pool) available for creating new objects. The memory manager needs to have mechanisms for allocating space from the free space pool and also for returning space to the pool when an object is no longer being used. In today's lab, we will look at one way of implementing the free space pool, by using a doubly linked list. Your task is to implement a memory manager that is responsible for allocating and de-allocating the blocks of memory. The memory manager is responsible for handling two types of request: 1. MemoryBlock allocate(int size); // allocate a memory block of the given size 2. void free(MemoryBlock block); // free a given memory block; that is, return it to the free list Every memory block has the following structure. public class MemoryBlock { private int address; private int length; MemoryBlock *prev; MemoryBlock *next; } The basic strategy in the allocate method is to scan the free block list to find the first free block which is large enough to satisfy the request. (This is called the "first fit" strategy). If the length of the free block is exactly the same as the requested size, the block is simply removed from the free list and returned to the caller. If the first-fit block is larger than the requested size, it is split into two parts. One part, the same size as the requested size, is returned to the caller. The remainder of that block (with its size appropriately reduced) is left on the free list. If the free list does not contain any block of sufficient size to satisfy the request, allocate should return null. The list should be ordered by address. The free method starts by scanning the free list to find the place where the block being freed belongs. The block should be returned to the free list, combining it with adjacent free blocks if possible. (Two blocks b1 and b2 are adjacent if b1.address + b1.length == b2.address.) The rationale for this is that we want to keep the free blocks as large as possible to handle future allocate requests; otherwise, fragmentation occurs. That is, the free space is split into smaller and smaller blocks until they are too small to be of any use. In programming the free method, there are four possible cases: 1. The block can be combined with the preceding block. In that case, we can simply add the length of the free block to the preceding block. 2. The block can be combined with the succeeding block. Add it to the succeeding block by adjusting both the size and the address of the succeeding block. 3. The block can be combined with both the preceding block and the succeeding block. Combine all three by adding the lengths of the block and the succeeding block to the length of the preceding block, then deleting the succeeding block. 4. The block cannot be combined with either block. Add it to the free list between the predecessor and successor.

Punjab University College Of Information Technology

Page 1 of 1

Das könnte Ihnen auch gefallen