int main()
{
    int src[SIZE];
    int dst[SIZE];
 
    incrementArray( src , dst );

    int x;
    for ( x = 0 ; x < SIZE ; x++ ) {
      src[ x ] = dst[ x ]-1;
    }
  return 0;
}
예제 #2
0
void makedivided(int node)
{
#ifdef DEBUG 
printf("DEBUG makeDivided : starting value of node=%d\n", node);	
#endif

while(node!=0)  // This is a recursive division
	{
node=(node%2==0?(node-1)/2:node/2); // ex- for 16, it becomes 7 and for 17, it becomes 8 

#ifdef DEBUG 
printf("DEBUG makeDivided : node=%d marked as 1 or divided \n", node); 
#endif

tree[node]=1;
//here we are marking the node as divided and it is going away from the pool 
//here we should decrement total_level_free but this is NOT allocation
incrementArray(total_level_used,flevel);
//printf("\n DBG: node used ; flevel= %d, current value=%d",flevel,total_level_alloc[flevel]); 
	}
}
예제 #3
0
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
} 
int main(int argc, const char * argv[]) {
    
    {
        uint32_t ip[] = {2, 1, 8, 9};
        uint32_t ip_sz = static_cast<uint32_t>(sizeof(ip)/sizeof(ip[0]));
        uint32_t op_sz = 0;
        uint32_t *op = incrementArray(ip, ip_sz, op_sz);
        printf("\nIp = ");
        printArray(ip, ip_sz);
        printf("\nOp = ");
        printArray(op, op_sz);
        free(op);
    }
    
    {
        uint32_t ip[] = {9, 9, 9};
        uint32_t ip_sz = static_cast<uint32_t>(sizeof(ip)/sizeof(ip[0]));
        uint32_t op_sz = 0;
        uint32_t *op = incrementArray(ip, ip_sz, op_sz);
        printf("\nIp = ");
        printArray(ip, ip_sz);
        printf("\nOp = ");
        printArray(op, op_sz);
        free(op);
    }
    
    {
        uint32_t ip[] = {1, 9, 0};
        uint32_t ip_sz = static_cast<uint32_t>(sizeof(ip)/sizeof(ip[0]));
        uint32_t op_sz = 0;
        uint32_t *op = incrementArray(ip, ip_sz, op_sz);
        printf("\nIp = ");
        printArray(ip, ip_sz);
        printf("\nOp = ");
        printArray(op, op_sz);
        free(op);
    }
    
    {
        uint32_t ip[] = {3, 3};
        uint32_t ip_sz = static_cast<uint32_t>(sizeof(ip)/sizeof(ip[0]));
        uint32_t op_sz = 0;
        uint32_t *op = incrementArray(ip, ip_sz, op_sz);
        printf("\nIp = ");
        printArray(ip, ip_sz);
        printf("\nOp = ");
        printArray(op, op_sz);
        free(op);
    }
    
    {
        uint32_t ip[] = {9};
        uint32_t ip_sz = static_cast<uint32_t>(sizeof(ip)/sizeof(ip[0]));
        uint32_t op_sz = 0;
        uint32_t *op = incrementArray(ip, ip_sz, op_sz);
        printf("\nIp = ");
        printArray(ip, ip_sz);
        printf("\nOp = ");
        printArray(op, op_sz);
        free(op);
    }
    
    {
        uint32_t ip[] = {1};
        uint32_t ip_sz = static_cast<uint32_t>(sizeof(ip)/sizeof(ip[0]));
        uint32_t op_sz = 0;
        uint32_t *op = incrementArray(ip, ip_sz, op_sz);
        printf("\nIp = ");
        printArray(ip, ip_sz);
        printf("\nOp = ");
        printArray(op, op_sz);
        free(op);
    }
    
    
    return 0;
}