code reorg and moving files to csql base directory
[csql.git] / src / base / VarHeapAllocator.cxx
blob046242773bd8ce76fe6b40fcd575a997548996d2
1 #include <HeapAllocator.h>
2 #include <ErrorType.h>
3 #include <Debug.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)
14 *(void **)ptr = top;
15 top = ptr;
16 usedBytes = 0;
17 offset = (char *)top+PAGEINFOSIZE;
18 *(short int *)((char *)offset+1) = pageSize- PAGEINFOSIZE-BLOCKINFOSIZE;
20 void VarHeapAllocator::init(int size,int pMode)
22 mode = pMode;
23 initSize(size);
25 void VarHeapAllocator::initSize(int size)
27 void *ptr;
28 ptr = calloc(size,1);
29 if(ptr == NULL)
31 printError(ErrOS, "unable to allocate memory of %d bytes", size);
32 return;
34 pageSize = size;
35 initializeInfo(ptr);
36 isInitialized = 1;
37 pageCounter++;
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");
49 return NULL;
51 if(size > ( pageSize - PAGEINFOSIZE-(2*BLOCKINFOSIZE) ))
53 printError(ErrBadCall, "The requested size is greater than the page Size");
54 return NULL;
56 if(mode ==0 )
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);
62 if(ptr == NULL)
64 printError(ErrOS, "unable to allocate memory of %d bytes",pageSize);
65 return NULL;
67 pageCounter++;
68 initializeInfo(ptr);
70 allocateptr = offset;
71 offset = (char *)offset+size;
72 usedBytes += size;
73 //printf("\nafter allocate offset = %u\tusedmemory=%d\tmemoryrequested = %d\treturn ptr = %u",offset,usedBytes, size,allocateptr);
74 return (allocateptr);
76 else if(mode == 1)
78 //printf("\nCursor is inside mode = 1");
79 pageDetector = top;
80 int isAllocated, noOfBytes;
81 while(pageDetector)
83 isFound = 0;
84 pageSizeCounter = 0;
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)
96 isFound = 1;
97 reAllocateCounter++;
98 break;
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);
108 if(isFound == 1)
109 break;
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)
115 isFound = 1;
116 newAllocateCounter++;
117 break;
120 pageDetector = *(void **)pageDetector;
121 //printf("next page at : %u",pageDetector);
123 if(pageDetector == NULL)
125 ptr = calloc(pageSize,1);
126 if(ptr == NULL)
128 printError(ErrOS, "unable to allocate memory of %d bytes",pageSize);
129 return NULL;
131 initializeInfo(ptr);
132 newAllocateCounter++;
133 pageCounter++;
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);
149 *(char *)ptr = 0;
150 //printf("\nAfter deAllocating the value at pointer is : %d",* (char *)ptr);
151 deallocateCounter++;
154 void VarHeapAllocator::destroy()
156 void *ptr, *temp;
157 ptr = top;
158 unsigned int add;
159 while(ptr)
161 temp = ptr;
162 ptr = *(void **)ptr;
163 //printf("deallocating %u",temp);
164 free(temp);
169 int main()
171 int bsize;
172 VarHeapAllocator mAllocator;
173 mAllocator.init(50,1);
174 int i = 0, choice;
175 unsigned int ptr;
176 while(true)
178 cout << "enter the choice that u want";
179 cin>> choice;
180 if(choice == 1)
182 cout << "please enter the memory in bytes that u want to allocate";
183 cin >> bsize;
184 mAllocator.allocate(bsize);
186 else if(choice == 2)
188 cout << "please enter the memory address to deallocate";
189 cin >> ptr;
191 else
193 // exit(1);
194 break;
198 mAllocator.destroy();