Design and implement Heap, a new abstraction representing area in the memory reserved for dynamic memory allocation.

Question:

Design and implement Heap, a new abstraction representing area in the memory reserved for dynamic memory allocation. For testing, you can assume the capacity of the heap to be 8 KB.
The heap abstraction internally maintains a contiguous space of memory and a free list, which is a collection of 2-tuple (location, size) representing available space. The collection of tuples representing available space starts with a single entry (0, size), where size is the capacity of the heap.

  • The heap abstraction provides four operations: alloc, get, set, and free.
  • The alloc operation takes a single parameter, the size of desired memory space, which is a numeric value in the language. It scans the free list and finds the first available space sufficient for allocation, returns that location, and adjusts the free list to reflect this memory allocation.
    If contiguous space is not available, the alloc operation throws an exception of type InsufficientMemoryException.
  • The get operation takes a single parameter: location, which is a numeric value in the language, and returns the value stored at that location. If the location is in the free list, an exception of type SegmentationFault is raised. If the location is out of bounds of the heap, again an exception of type SegmentationFault is raised.
  • The set operation takes two parameters: location, which is a numeric value in the language, and value, which is also a numeric value to be stored at that location. If the location is in the free list, an exception of type SegmentationFault is raised. If the location is out of bounds of the heap, again an exception of type SegmentationFault is raised. Otherwise, the heap is modified so that the value is stored at the location.
  • The free operation takes two parameters, location and size, both of which are numeric values in the language. If the location is already in the free list, it does nothing. Otherwise, it puts the location in the free list.
Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Question Posted: