#include <sys/time.h>
#include <sys/resource.h>

#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>

//
// Compile with:  cc nsauto.m -o oTest -lobjc -lpthread
//
@interface TList : NSObject  
{
  int list[100];          
  int size;
}

- (int) addEntry: (int) num;
- print;
- resetSize;

@end

@implementation TList

+ new                  // factory method
{
  self = [super new];
  [self resetSize];
  return self;
}

- (void)dealloc
{
	[super dealloc];
}

- autorelease
{
  return [super autorelease];
}

- (int) addEntry: (int) num
{
  list[size++] = num;
  return size;
}

- print
{
int i;

//	for (i = 0; i < size; ++i)
//		printf ("%i", list[i]);

	return self;                
}

- resetSize
{
  size = 0;
  return self;
}

@end



main()
{
id list;               // id is a new data type for objects.
id pool;
int i;
struct rusage rusage;

	fprintf(stderr," Starting nsauto...\n");

	fprintf(stderr," Creating autorelease pool.\n");
	pool = [NSAutoreleasePool new];

	fprintf(stderr," Adding 10000 list objects to autorelease pool.\n");
	for(i= 0; i < 10000; i++)
		{
		list = [[TList new] autorelease]; 
		[list addEntry: 5];    
		[list addEntry: 6];
		[list addEntry: 3];
		[list print];
		}
	fprintf(stderr,"\n Created and autoreleased 10000 List objects \n");
//	getrusage(RUSAGE_SELF, &rusage);
//	fprintf(stderr," Data seg Mem in use: %d \n", rusage.ru_idrss);
	sleep(7);
    [pool release];

	fprintf(stderr," Releasing Pool and waiting 7 seconds\n");
	sleep(7);

	fprintf(stderr," Creating autorelease pool.\n");
	pool = [NSAutoreleasePool new];
	fprintf(stderr," Adding 5000 list objects to autorelease pool.\n");
	for(i= 0; i < 5000; i++)
		{
		list = [[TList new] autorelease]; 
		[list addEntry: 5];    
		[list addEntry: 6];
		[list addEntry: 3];
		[list print];
		}
	fprintf(stderr,"\n Created and autoreleased 5000 List objects \n");
	sleep(7);
    [pool release];

	fprintf(stderr," Releasing Pool and waiting 7 seconds \n\n");
	sleep(7);
	printf("nsauto test complete\n");
	exit (0);
}
