Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Implementation

This section shows how write the simulation described above. Program gif declares the class Event which is used to keep track of the address of an area and the time at which it is to be freed. Therefore, there are two parts to an event, a VoidPtr and a Time.

   program32085
Program: Event Class Definition

Since events will be put into a priority queue, the Event class is derived from the Association class which is defined in Section gif. An association is an ordered pair comprised of a key and a value. In the case of the Event class, the key is the time of the event and the value is address of the area to be free. The events in a priority queue are prioritized by their times.

The simulation program is embodied in the function StoragePoolTest defined in Program gif. This function takes two arguments, a reference to the storage pool to be tested and an integer which specifies the number of simulation cycles to execute.

   program32098
Program: Simulation Procedure for Exercising a Storage Pool

The variables size and latency are UniformRVs. The class UniformRV is a random number generator defined in Section gif. This class provides a member function called Sample which is used to sample the random number generator. I.e., every time Sample is called, a different (random) result is returned. The random values are uniformly distributed on the interval which is specified in the constructor (lines 3-4).

The StoragePoolTest routine uses a LeftistHeap priority queue (see Section gif) to keep track of scheduled events (line 5).

The body of the main loop directly implements the steps outlined above. First, any events scheduled for the current time step are withdrawn from the priority queue and the associated storage areas are released (lines 10-19).

Second, the random variable size is sampled to determine the size of area to allocate and the random variable latency is sampled to determine when the allocated area should be deleted. An area of the required size is acquired and an event is scheduled in priority queue that will cause the area to be freed after the appropriate amount of time as elapsed.

The StoragePoolTest function given in Program gif was used by the author to create Figures gif, gif and gif. Figure gif shows the condition of a SinglyLinkedPool after tex2html_wrap_inline68383 simulation cycles. Figure gif shows the condition of a DoublyLinkedPool and Figure gif shows the condition of a BuddyPool after exactly the same tex2html_wrap_inline68383 simulation cycles.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.