static inline void segmentalloc(int total_size, int request) { int i, flevel = 0, size = total_size; if (total_size < request) { puts(" * Result:"); puts(" * The system don't have enough free memory"); puts(" * Suggession : Go for VIRTUAL MEMORY"); return; } while (1) { if (request <= size && div2(size) < request) { break; } else { size = div2(size); flevel++; } } for (i = power_2(flevel) - 1; i <= (power_2(flevel + 1) - 2); i++) { if (tree[i] == 0 && place(i)) { tree[i] = request; makedivided(i); puts(" * Result : Successful Allocation"); break; } } if (i == power_2(flevel + 1) - 1) { puts(" * Result :"); puts(" * The system don't have enough free memory"); puts(" * Suggession : Go for VIRTUAL Memory Mode"); } }
int segmentalloc(int totalsize,int request) { #ifdef DEBUG printf("\n Inside the allocation routine:\n"); #endif flevel=0; //everytime I come to allocation I reset flevel to 0 if(request>totalsize) { //check if requested size > total size printf(" * Requesting more than total memory ??? \n"); printf(" * Requested memory size= %d,total memory size=%d ", request,totalsize); printf("\n Press a key to continue..."); getchar(); return 0; // Should I return 0 here ? } // DEBUG let us see the array at beginning while(1) // This infinite loop is safe as we have already checked requesd size vs total size { #ifdef DEBUG printf("Requested memory size= %d, current memory block size being examined=%d ", request,totalsize); #endif if((request<=totalsize) && (request>(totalsize/2))) break; // found the right buddy level to check the availability of memory else { #ifdef DEBUG printf("\n Not yet found the right block; tree level=%d ; current memory block is : %d \n",flevel,totalsize); #endif //go dividing till you reach the right level totalsize/=2; flevel++; } } //end of while #ifdef DEBUG printf("\n correct tree level found=%d ; memory block to be allocated is : %d \n",flevel,totalsize); #endif // Check at level=flevel of the tree from left to right to see where the match is happening for(i=power(2,flevel)-1;i<=(power(2,flevel+1)-2);i++) // this formula is corrected provided root node =0 { #ifdef DEBUG printf("DEBG : In allocation : value of i=%d\n",i); #endif if(tree[i]==0 ) // && place(i) same thing ?? place(i) will return 1 if available { // I am storing block which is allocated incrementNode(); tree[i]=totalsize; //this is also the place to record the no of nodes that are allocated at a tree level incrementArray(total_level_alloc,flevel); #ifdef DEBUG printf("\n DBG: allocated ; flevel= %d, current value=%d",flevel,total_level_alloc[flevel]); #endif makedivided(i); // without this, the allocation will NOT show printf(" \n ......Successful Allocation.......... \n"); break; } //Here it comes out at least once after exhausting this loop if(i==power(2,flevel+1)-1) // first element in the next level { printf(" Result : "); printf("* NOT enough free memory\n"); totalsize=0; } } //end of for #ifdef DEBUG debug_print(); #endif return totalsize; // return the value that has been successfully allocated }