1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
Virtual Heap Class
You are to implement a C++ class that functions as a memory heap.
The virtual class v_heap.h has been provided, and must be implemented by your child class. The child class will be named MemHeap and it will be written in the files mem_heap.h and mem_heap.cpp. You must also write a program in main.cpp that thoroughly tests your heap implementation. Use print outs and assert statements accordingly.
In case you are wondering what the point of this is …..........................
This technique has been used by programmers in the past to avoid the overhead of making a call to the operating system to allocate and de-allocate memory. By allocating all the memory upfront, there is only 1 system call to allocate memory, and 1 system call to de-allocate memory, the rest is just pointer manipulation.
Implementing this technique also gives you a small insight into how memory management is handled “under the hood”.
The v_heap.h virtual class has been provided, its content follows.
#ifndef VHEAP_H
#define VHEAP_H
#include <cstdlib>
#define HEAP_SIZE 2048
class VHeap {
protected:
char * vheap;
public:
VHeap()
{
vheap = new char[HEAP_SIZE];
}
virtual void* vnew(size_t size) {}
virtual void* vcnew(size_t size) {}
virtual void vdelete(void* mem) {}
virtual size_t vsizeof(void* mem) {}
virtual void printHeapState() {}
virtual ~VHeap()
{
delete [] vheap;
}
};
#endif // VHEAP_H
Parts of v_heap.h explained
char * vheap;
Pointer to the memory allocated for the heap. You should not use this pointer directly, but rather assign another pointer to the address vheap holds, that way you wont inadvertently lose track of the start address of the heap.
Heap()
{
vheap = new char[HEAP_SIZE];
}
A constructor where the total amount of memory required for the heap is allocated.
virtual void* vnew(size_t size) {}
A user (main.cpp) can use this function to request memory (to be allocated within the heap).
The vnew function will allocate memory to the length of the parameter size, and it will return the address of the start of the allocated memory. The return type is void so as to handle any datatype. As you need to supply the size in bytes, you may find the function sizeof useful.
To use vnew to allocate a int, in main.cpp you would write
int *myNewInt = (int*)heapObj.vnew(sizeof(int));
To use vnew to allocate an array of long's that holds 5 items, in main.cpp you would write
long * myLongArr = (long*)heapObj.vnew(sizeof(long) * 5);
virtual void* vcnew(size_t size) {}
Functions the same as vnew but it also sets the allocated data to all 0's, where as vnew will leave whatever 'Junk' is in the allocated space.
virtual void vdelete(void* mem) {}
A release method where the user can return previously allocated memory back to the heap. When memory is released it should ensure that adjacent, available memory segments are consolidated into a single available segment.
virtual size_t vsizeof(void* mem) {}
Returns the amount of memory, in bytes, allocated at mem.
virtual void printHeapState() {}
A method which describes the in-use / available segments of the heap.
The following is an example of the output from printHeapState() where 'I' indicates areas being used for heap information, 'D' is allocated memory used to hold data and 'F' is for free/unused bytes.
IIIIIIIIIIIIIIIIIIIIIIIIDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDIIIIIIIIIIIIIIII
IIIIIIIIDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDIIIIIIIIIIIIIIIIIIIIIIIIFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
virtual ~VHeap()
{
delete [] vheap;
}
A destructor to release the memory associated with the heap back to the system.
You will also be marked on correct style, ie consistent indenting, appropriate variable naming etc.
Every Function Must have commenting in the style of
/**
* This function is only an example
* and does not actually do anything
*
* It returns nothing
* It takes no parameters
*/
void myFunction()
{
}
Single line commenting should be used for class variables and for non trivial code within functions.
The program must also be accompanied by a report (report.pdf) on how it works, diagrams may be useful for this. Your report need only be a page long (3 pages max) and it must describe the technique you used to implement the virtual heap. It is not meant to be comprehensive documentation, but rather a proof that you understand the problem and its solution.
Note :
You MUST NOT use new , delete , malloc , calloc, or free in your program.
You are to use only the memory that has been allocated by v_heap.h ( char * vheap )
No STL containers are allowed. No static structs or objects allowed.
Tip :
You may find it useful to define a struct and use a struct pointer to index into the heap!
|