Exemple #1
0
void *Tiger_new_array (int length)
{
   	int *p;
	int free=heap.from+heap.size-heap.fromFree;
	int size=sizeof(int)*(4+length);
	if(size>free)
	{
		Tiger_gc();
		free=heap.from+heap.size-heap.fromFree;
		if(size>free)
		{
			printf("Out of memory..\n");
			if(flag == 1)
			{
				FILE *log=fopen("gcLog.txt","r");
				while(fgets(buffer , 100 , log))
					printf("%s\n",buffer);
				fclose(log);
			}
			exit(-1);
		}
	}
	memset(heap.fromFree,0,size);
	p=(int*)heap.fromFree;
	*(p+1)=1;
	*(p+2)=length;
	heap.fromFree+=size;
	return (void*)p;
}
Exemple #2
0
// Try to allocate an object in the "from" space of the Java
// heap. Read Tiger book chapter 13.3 for details on the
// allocation.
// There are two cases to consider:
//   1. If the "from" space has enough space to hold this object, then
//      allocation succeeds, return the apropriate address (look at
//      the above figure, be careful);
//   2. if there is no enough space left in the "from" space, then
//      you should call the function "Tiger_gc()" to collect garbages.
//      and after the collection, there are still two sub-cases:
//        a: if there is enough space, you can do allocations just as case 1; 
//        b: if there is still no enough space, you can just issue
//           an error message ("OutOfMemory") and exit.
//           (However, a production compiler will try to expand
//           the Java heap.)
void *Tiger_new (void *vtable, int size)
{
  // You should write 4 statements for this function.
  // #1: "malloc" a chunk of memory of size "size": 
 	int *p;
	int free=heap.from+heap.size-heap.fromFree;
	if(size>free)
	{
		Tiger_gc();
		free=heap.from+heap.size-heap.fromFree;
		if(size>free)
		{
			printf("Out of memory..\n");
			if(flag == 1)
			{
				FILE *log=fopen("gcLog.txt","r");
				while(fgets(buffer , 100 , log))
					printf("%s\n",buffer);
				fclose(log);
			}
			exit(-1);
		}
	}
	memset(heap.fromFree,0,size);
	p=(int*)heap.fromFree;
	*p=(int)vtable;
	heap.fromFree+=size;
	return (void*)p;
  // #3: set up the "vtable" pointer properly:
}
Exemple #3
0
// Try to allocate an object in the "from" space of the Java
// heap. Read Tiger book chapter 13.3 for details on the
// allocation.
// There are two cases to consider:
//   1. If the "from" space has enough space to hold this object, then
//      allocation succeeds, return the apropriate address (look at
//      the above figure, be careful);
//   2. if there is no enough space left in the "from" space, then
//      you should call the function "Tiger_gc()" to collect garbages.
//      and after the collection, there are still two sub-cases:
//        a: if there is enough space, you can do allocations just as case 1;
//        b: if there is still no enough space, you can just issue
//           an error message ("OutOfMemory") and exit.
//           (However, a production compiler will try to expand
//           the Java heap.)
void *Tiger_new (void *vtable, int size)
{
  // Your code here:
	/*char* t=(char*)malloc(size);
		memset(t,0,size);
		*((int*)t)=(int*)vtable;
		return t;
*/
    if(heap.to-heap.fromFree<size)
    {

        printf("There is %d byte remained,but you need:%d\n",heap.to-heap.fromFree,size);
    	Tiger_gc();
    	 if(heap.to-heap.fromFree<size)
    	 {
    	      printf("Tiger_gc can not collecte enough space...\n");
    	      printf("There is %d byte remained,but you need:%d\n", (int*)(heap.to-heap.fromFree),size);
	    	    exit(1);

    	 }




    }


    	printf("\nthis is Tiger_new--------------\n");
    	printf("malloc size:%d\n",size);
        char* temp=heap.fromFree;
        memset(temp,0,size);
        *(temp+4)=0;
        *(temp+8)=size;
        *(temp+12)=0;
        heap.fromFree+=size;
        *((int*)temp)=(int*)vtable;
        printf("vtable is=%d\n",*(int*)(temp));
        printf("isObj=%d,address=:%d\n",*(temp+4),temp+4);
        printf("length=%d,address=:%d\n",*(temp+8),(temp+8));
        printf("forward=%d,address=:%d\n",*(temp+12),(temp+12));
        printf("malloc finished....------------------\n");
        return temp;




}
Exemple #4
0
// Try to allocate an array object in the "from" space of the Java
// heap. Read Tiger book chapter 13.3 for details on the
// allocation.
// There are two cases to consider:
//   1. If the "from" space has enough space to hold this array object, then
//      allocation succeeds, return the apropriate address (look at
//      the above figure, be careful);
//   2. if there is no enough space left in the "from" space, then
//      you should call the function "Tiger_gc()" to collect garbages.
//      and after the collection, there are still two sub-cases:
//        a: if there is enough space, you can do allocations just as case 1; 
//        b: if there is still no enough space, you can just issue
//           an error message ("OutOfMemory") and exit.
//           (However, a production compiler will try to expand
//           the Java heap.)
//#define  needBytes(length)  Header + sizeof(int)*length
void *Tiger_new_array (int length){
	 int needSpace = needBytes(length);
  if ( FreeSpace < needSpace ){
      Tiger_gc();
      if ( FreeSpace < needSpace ){
          printf("OutOfMemory\n");
          exit(-1);
      }
    }
      memset(heap.fromFree,0,needSpace);
      int *p =(int*) heap.fromFree;
      heap.fromFree += needSpace;
      p[0] = 0;
      p[1] = 1;
      p[2] = length;
      p[3] = 0;
      return (void*)p;
}
Exemple #5
0
// Try to allocate an array object in the "from" space of the Java
// heap. Read Tiger book chapter 13.3 for details on the
// allocation.
// There are two cases to consider:
//   1. If the "from" space has enough space to hold this array object, then
//      allocation succeeds, return the apropriate address (look at
//      the above figure, be careful);
//   2. if there is no enough space left in the "from" space, then
//      you should call the function "Tiger_gc()" to collect garbages.
//      and after the collection, there are still two sub-cases:
//        a: if there is enough space, you can do allocations just as case 1;
//        b: if there is still no enough space, you can just issue
//           an error message ("OutOfMemory") and exit.
//           (However, a production compiler will try to expand
//           the Java heap.)
void *Tiger_new_array (int length)
{
  // Your code here:

/*	int *i=(int *)malloc(length*sizeof(int));
		i[0]=length;
		return i+1;
*/
	 if(heap.to-heap.fromFree<(length*sizeof(int))+16)
	    {
            printf("There is %d byte remained,but you need:%d\n",
             heap.to-heap.fromFree,length*(sizeof(int))+16);
	    	Tiger_gc();
	    	if(heap.to-heap.fromFree<(length*sizeof(int))+16)
	    	{
	    	    printf("Tiger_gc can not collecte enough space...\n");
	    	    printf("There is %d byte remained,but you need:%d\n",
             (int*)(heap.to-heap.fromFree),length*(sizeof(int))+16);
	    	    exit(1);
	    	}


	    }




	    	printf("\nthis is Tiger_new_array-----------------------\n");
	    	printf("malloc size:%d\n",length*(sizeof(int))+16);
	        char* temp=heap.fromFree;
	        memset(temp,0,length*sizeof(int));
	        *temp=NULL;
	        *(temp+4)=1;
	        *((int*)(temp+8))=length;
	        *(temp+12)=0;
	        heap.fromFree+=(length*sizeof(int));

	        printf("isObj=%d,address=:%d\n",*(temp+4),temp+4);
            printf("length=%d,address=:%d\n",*(temp+8),(temp+8));
            printf("forward=%d,address=:%d\n",*(temp+12),(temp+12));
            printf("malloc finished....-------------------------\n");
	        return (temp+16);
}
Exemple #6
0
// Try to allocate an object in the "from" space of the Java
// heap. Read Tiger book chapter 13.3 for details on the
// allocation.
// There are two cases to consider:
//   1. If the "from" space has enough space to hold this object, then
//      allocation succeeds, return the apropriate address (look at
//      the above figure, be careful);
//   2. if there is no enough space left in the "from" space, then
//      you should call the function "Tiger_gc()" to collect garbages.
//      and after the collection, there are still two sub-cases:
//        a: if there is enough space, you can do allocations just as case 1; 
//        b: if there is still no enough space, you can just issue
//           an error message ("OutOfMemory") and exit.
//           (However, a production compiler will try to expand
//           the Java heap.)
//# define  FreeSpace heap.from + heap.size - heap.fromFree
//  return (heap.to - heap.fromFree + 2*heap.size)%(2*heap.size);
void *Tiger_new (void *vtable, int size)
{
  int needSpace = size;
  if ( FreeSpace < needSpace ){
//	  printf("before gc\n");
       Tiger_gc();
       if ( FreeSpace < needSpace ){
            printf("OutOfMemory\n");
            exit(-1);
       }
  }
//  printf("new\n");
  memset(heap.fromFree,0,needSpace);
  int *p = (int*)heap.fromFree;
  heap.fromFree += needSpace;
  p[0] = (int)vtable;
  p[1] = 0;
  p[2] = 0;
  p[3] = 0;
//  p = p + 4;
  return (void*)p;
}