1 #include <HeapAllocator.h>
5 void VarHeapAllocator::print()
7 printf("\nTotal number of new allocation happened : %d",newAllocateCounter
);
8 printf("\nTotal number of re allocation happened : %d",reAllocateCounter
);
9 printf("\nTotal number of deallocation happened : %d",deallocateCounter
);
10 printf("\nTotal number of page created : %d\n",pageCounter
);
12 void VarHeapAllocator::initializeInfo(void *ptr
)
17 offset
= (char *)top
+PAGEINFOSIZE
;
18 *(short int *)((char *)offset
+1) = pageSize
- PAGEINFOSIZE
-BLOCKINFOSIZE
;
20 void VarHeapAllocator::init(int size
,int pMode
)
25 void VarHeapAllocator::initSize(int size
)
31 printError(ErrOS
, "unable to allocate memory of %d bytes", size
);
40 void * VarHeapAllocator::allocate(int size
)
42 void *allocateptr
, *ptr
, *freeSpaceDetector
, *pageDetector
;
43 short int pageSizeCounter
= 0, add
;
44 short int isFound
= 0;
46 if(isInitialized
!= 1)
48 printError(ErrBadCall
, "Error:not initialized");
51 if(size
> ( pageSize
- PAGEINFOSIZE
-(2*BLOCKINFOSIZE
) ))
53 printError(ErrBadCall
, "The requested size is greater than the page Size");
58 //printf("\ninside allocate offset = %u\tpagesize= %d\tusedmemory=%d\tmemoryrequested = %d",offset,pageSize,usedBytes, size);
59 if( ( usedBytes
+ size
) > ( pageSize
- PAGEINFOSIZE
) )
61 ptr
= calloc(pageSize
,1);
64 printError(ErrOS
, "unable to allocate memory of %d bytes",pageSize
);
71 offset
= (char *)offset
+size
;
73 //printf("\nafter allocate offset = %u\tusedmemory=%d\tmemoryrequested = %d\treturn ptr = %u",offset,usedBytes, size,allocateptr);
78 //printf("\nCursor is inside mode = 1");
80 int isAllocated
, noOfBytes
;
85 freeSpaceDetector
= (char *)pageDetector
+PAGEINFOSIZE
;
86 pageSizeCounter
+= PAGEINFOSIZE
;
87 isAllocated
= *(char *)freeSpaceDetector
;
88 noOfBytes
= *(short int *)((char *)freeSpaceDetector
+1);
89 //printf("\nStarting from the page:");
90 //printf(":\nfreespacedetector is looking into the pointer: %d\n",freeSpaceDetector);
91 //printf("\nfinding allocation = %d and bytes = %d\n",isAllocated,noOfBytes);
92 while(pageSizeCounter
+noOfBytes
+BLOCKINFOSIZE
< pageSize
)
94 if(isAllocated
== 0 && noOfBytes
>= size
+BLOCKINFOSIZE
)
100 pageSizeCounter
= pageSizeCounter
+noOfBytes
+BLOCKINFOSIZE
;
101 freeSpaceDetector
= (char *)freeSpaceDetector
+noOfBytes
+BLOCKINFOSIZE
;
102 //printf("\nfreespacedetector is looking into the pointer = %u ",freeSpaceDetector);
103 isAllocated
= *(char *)freeSpaceDetector
;
104 noOfBytes
= *(short int *)((char *)freeSpaceDetector
+1);
105 //printf("\nfinding allocated = %d and bytes = %d\n",isAllocated,noOfBytes);
107 //printf("After searching the page (expect last block), pointer is at: %u",freeSpaceDetector);
110 //printf(" searching the last block of the page, at: %u",freeSpaceDetector);
111 if(isFound
== 0 && pageSizeCounter
+noOfBytes
+BLOCKINFOSIZE
== pageSize
)
113 if(isAllocated
== 0 && noOfBytes
>= size
+BLOCKINFOSIZE
)
116 newAllocateCounter
++;
120 pageDetector
= *(void **)pageDetector
;
121 //printf("next page at : %u",pageDetector);
123 if(pageDetector
== NULL
)
125 ptr
= calloc(pageSize
,1);
128 printError(ErrOS
, "unable to allocate memory of %d bytes",pageSize
);
132 newAllocateCounter
++;
134 freeSpaceDetector
= (char *)top
+PAGEINFOSIZE
;
136 *(char *)freeSpaceDetector
= 1;
137 int availMemory
= *(short int *)((char *)freeSpaceDetector
+1);
138 *(short int *)((char *)freeSpaceDetector
+1) = size
;
139 *(short int *)((char *)freeSpaceDetector
+size
+BLOCKINFOSIZE
+1) = availMemory
-size
-BLOCKINFOSIZE
;
140 //printf("\nHere we can get a free spase in side the current page: %d\n",freeSpaceDetector);
141 //printf("\nis allocated == %d and bytes = %d\n",*(char *)freeSpaceDetector,*(short int *)((char *)freeSpaceDetector+1));
142 //printf("\n next block = %u\t allocated == %d and bytes = %d\n",(char *)freeSpaceDetector+size+BLOCKINFOSIZE,*((char *)freeSpaceDetector+size+BLOCKINFOSIZE),*(short int *)((char *)freeSpaceDetector+size+BLOCKINFOSIZE+1) );
146 void VarHeapAllocator::deallocate(void *ptr
)
148 //printf("\nBefore deAllocating the value at pointer is : %d",* (char *)ptr);
150 //printf("\nAfter deAllocating the value at pointer is : %d",* (char *)ptr);
154 void VarHeapAllocator::destroy()
163 //printf("deallocating %u",temp);
172 VarHeapAllocator mAllocator;
173 mAllocator.init(50,1);
178 cout << "enter the choice that u want";
182 cout << "please enter the memory in bytes that u want to allocate";
184 mAllocator.allocate(bsize);
188 cout << "please enter the memory address to deallocate";
198 mAllocator.destroy();