https://github.com/Unipisa/CMM
Raw File
Tip revision: 0e186db83a043dcfe9c666dd4c90f9e8d1b9234e authored by Giuseppe Attardi on 27 October 1994, 11:26:20 UTC
1.1 -
Tip revision: 0e186db
test2.C
/* The following program tests the ability of the garbage collector to correct
   derived pointers and pointers in the non-garbage collected heap.
*/

/* Externals */

#include <stdio.h>
#include <stdlib.h>
#include "cmm.H"

/* Cell type to build a list with. */

struct  cell : GcObject  {
  cell  *next;
  int* value1;
  int  value2;
  cell();
  void traverse();
};

void cell::traverse()
{
  CmmHeap *heap = CmmHeap::heap;
  heap->scavenge((GcObject **)&next);
  heap->scavenge((GcObject **)&value1);
}

cell::cell()
{
  next = 0;
  value1 = 0;
}

typedef  cell* cellptr;

/* Dynamic array of cellptr's that is not garbage collected */

struct  cella  {
  cellptr ptr[50000];
};

gcheap  dummy(1048576, 2147483647, 1048576, 35, 30,
	       GCHEAPROOTS+GCMEM+GCSTATS);

main()
{
	cella*  pointers = new cella;
	cellptr  cl = 0, cp;

	/* Allocate 50000 cells referenced by an array in the non-gc heap */
	for  (int i = 0; i < 50000; i++)  {
	   cp = new cell;
	   pointers->ptr[i] = cp;
	   cp->value1 = 0;
	   cp->value2 = i;
	}

	/* Make a list of 50000 cells that each point into themseleves. */
	for  (i = 0; i < 50000; i++)  {
	   cp = new cell;
	   cp = new cell;
	   cp = new cell;
	   cp = new cell;
	   cp->next = cl;
	   cp->value1 = &cp->value2;
	   cp->value2 = i;
	   cl = cp;
	}

	/* Verify that objects referenced by pointers still exist */
	for  (i = 0; i < 50000; i++)  {
	   if  (pointers->ptr[i]->value2 != i)  abort();
	}

	/* Verify that cell list is correct correct */
	for  (i = 0; i < 50000; i++)  {
	   if  (cl->value2 != *(cl->value1))  abort();
	   cl = cl->next;
	}
	exit(0);
}
back to top