1 #include <HeapAllocator.h>
5 void FixedHeapAllocator::initializeInfo(void *ptr
)
10 offset
= (char*)top
+PAGEINFOSIZE
;
11 *(short*)((char*)offset
+1) = pageSize
- PAGEINFOSIZE
-BLOCKINFOSIZE
;
14 void FixedHeapAllocator::init(int pSize
,int allocSize
)
17 ptr
= ::calloc(pSize
,1);
20 printError(ErrOS
, "Unable to allocate %d bytes", pSize
);
23 allocateSize
= allocSize
;
31 void* FixedHeapAllocator::allocate()
33 void *allocateptr
, *ptr
, *freeSpaceDetector
, *pageDetector
;
34 short int pageSizeCounter
= 0, add
;
35 short int isFound
= 0;
37 if(isInitialized
!= 1)
39 printError(ErrBadCall
, "Allocator is not initialized");
42 if(allocateSize
> ( pageSize
- PAGEINFOSIZE
-(2*BLOCKINFOSIZE
) ))
44 printf("The requested size is greater than the page Size");
47 //TODO::we will implement mode later for this fixed size allocator
48 //printf("\ninside allocate offset = %u\tpagesize= %d\tusedmemory=%d\tmemoryrequested = %d",offset,pageSize,usedBytes, allocateSize );
49 if( ( usedBytes
+ allocateSize
) > ( pageSize
- PAGEINFOSIZE
) )
51 printDebug(DM_Alloc
, "Current page exhausted. allocating new page");
52 ptr
= calloc(pageSize
,1);
55 printError(ErrOS
, "Unable to allocate %d bytes", pageSize
);
62 offset
= (char *)offset
+allocateSize
;
63 usedBytes
+= allocateSize
;
64 //printf("\nafter allocate offset = %u\tusedmemory=%d\tmemoryrequested = %d\treturn ptr = %u",offset,usedBytes, allocateSize,allocateptr);
68 void FixedHeapAllocator::deAllocate(void *ptr
)
72 void FixedHeapAllocator::destroy()
81 //printf("deallocating %u",temp);
88 cout << "This is the main program";
90 FixedHeapAllocator mAllocator;
91 mAllocator.init(50,10);
96 cout << "enter the choice that u want";
100 // cout << "please enter the memory in bytes that u want to allocate";
102 mAllocator.allocate();
106 cout << "please enter the memory address to deallocate";
116 mAllocator.destroy();