
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#define   RUSAGE_SELF     0
#define   RUSAGE_CHILDREN     -1


void
test1(int n)
{
	int i;
	char *p;

	printf("allocating 12, then 4096 byte structures %d times..\n", n);
	for (i = 0; i < n; i++) {
		p = malloc(12);
		bzero(p, 12);
		p = malloc(4096);
		bzero(p, 4096);
	}
}

void
test2(n)
{
	int i;
	char *p;

	printf("allocating 4108 byte structure %d times..\n", n);
	for (i = 0; i < n; i++) {
		p = malloc(4096 + 12);
		bzero(p, 4096 + 12);
	}
}

void
usage(const char *pn)
{
	printf("error: %s (test1|test2) (number to allocate)\n", pn);
	exit(-1);
}

int
main(int argc, char **argv)
{
	struct rusage ru;
	int n;

	if (argc < 3)
		usage(argv[0]);

	n = atoi(argv[2]);
	if (n < 0)
		usage(argv[0]);

	//printf("n: %d\n", n);

	if (strncmp("test1", argv[1], 5) == 0) {
		test1(n);
	} else if (strncmp("test2", argv[1], 5) == 0) {
		test2(n);
	} else
		usage(argv[0]);

	getrusage(RUSAGE_SELF, &ru);
	printf("RSS: %ld\n", ru.ru_maxrss);
	exit(0);
}

