Example #1
0
static void threadcode(int threadidx)
{
	int n;
	unsigned int *toallocptr=threadstuff[threadidx].toalloc;
	void **allocptr=threadstuff[threadidx].allocs;
	unsigned int seed=threadidx;
	usCount start;
	threadstuff[threadidx].done=0;
	/*neddisablethreadcache(0);*/
	THREADSLEEP(100);
	start=GetUsCount();
#ifdef TORTURETEST
	/* A randomised malloc/realloc/free test (torture test) */
	for(n=0; n<RECORDS*100; n++)
	{
		unsigned int r=myrandom(&seed), i;
		i=(int)(r % RECORDS);
		if(!allocptr[i])
		{
			allocptr[i]=mallocs[whichmalloc](r & 0x1FFF);
			threadstuff[threadidx].ops++;
		}
		else if(r & (1<<31))
		{
			allocptr[i]=reallocs[whichmalloc](allocptr[i], r & 0x1FFF);
			threadstuff[threadidx].ops++;
		}
		else
		{
			frees[whichmalloc](allocptr[i]);
			allocptr[i]=0;
		}
	}
	for(n=0; n<RECORDS; n++)
	{
		if(allocptr[n])
		{
			frees[whichmalloc](allocptr[n]);
			allocptr[n]=0;
		}
	}
#else
	/* A simple stack which allocates and deallocates off the top (speed test) */
	for(n=0; n<RECORDS;)
	{
#if 1
		r=myrandom(&seed);
		if(allocptr>threadstuff[threadidx].allocs && (r & 65535)<32760) /*<32760)*/
		{	/* free */
			--toallocptr;
			--allocptr;
			--n;
			frees[whichmalloc](*allocptr);
			*allocptr=0;
		}
		else
#endif
		{
			if(doRealloc && allocptr>threadstuff[threadidx].allocs && (r & 1))
			{
	            allocptr[-1]=reallocs[whichmalloc](allocptr[-1], *toallocptr);
			}
			else
			{
	            allocptr[0]=mallocs[whichmalloc](*toallocptr);
				allocptr++;
			}
			n++;
			toallocptr++;
			threadstuff[threadidx].ops++;
		}
	}
	while(allocptr>threadstuff[threadidx].allocs)
	{
		frees[whichmalloc](*--allocptr);
	}
#endif
	times[threadidx]+=GetUsCount()-start;
	neddisablethreadcache(0);
	threadstuff[threadidx].done=1;
}
Example #2
0
static void threadcode(int threadidx)
{
	int n;
	unsigned int *toallocptr=threadstuff[threadidx].toalloc;
	void **allocptr=threadstuff[threadidx].allocs;
	unsigned int r, seed=threadidx;
	usCount start;
	size_t allocated=0, size;
	threadstuff[threadidx].done=0;
	/*neddisablethreadcache(0);*/
	THREADSLEEP(100);
	start=GetUsCount();
#if 2==TESTTYPE
	/* A randomised malloc/realloc/free test (torture test) */
	for(n=0; n<RECORDS*100; n++)
	{
		static int reallocflip;
		unsigned int i, dorealloc=(reallocflip=!reallocflip);
		r=myrandom(&seed);
		i=(int)(r % RECORDS);
#if TESTCPLUSPLUS
		dorealloc=!(r&(15<<28));
		if(r&(1<<31))
		{   /* Make it two power multiple of less than 512 bytes to
			model frequent C++ new's */
			size=4<<(r & 7);
			dorealloc=0;
		}
		else
#endif
			size=(size_t)(r & (BLOCKSIZE-1));
		if(allocated<MAXMEMORY2 && !allocptr[i])
		{
			if(!(allocptr[i]=mallocs[whichmalloc](size))) abort();
#if TOUCH
			{
				volatile char *mem=(volatile char *)allocptr[i];
				volatile char *end=mem+size;
				for(; mem<end; mem+=4096) *mem;
			}
#endif
			allocated+=memsizes[whichmalloc](allocptr[i]);
			threadstuff[threadidx].ops.mallocs++;
		}
		else if(allocated<MAXMEMORY2 && dorealloc) /* If not TESTCPLUSPLUS, then how often realloc() gets called depends on how small RECORDS is. */
		{
			allocated-=memsizes[whichmalloc](allocptr[i]);
			if(!(allocptr[i]=reallocs[whichmalloc](allocptr[i], size))) abort();
#if TOUCH
			{
				volatile char *mem=(volatile char *)allocptr[i];
				volatile char *end=mem+size;
				for(; mem<end; mem+=4096) *mem;
			}
#endif
			allocated+=memsizes[whichmalloc](allocptr[i]);
			threadstuff[threadidx].ops.reallocs++;
		}
		else if(allocptr[i])
		{
			allocated-=memsizes[whichmalloc](allocptr[i]);
			frees[whichmalloc](allocptr[i]);
			allocptr[i]=0;
			threadstuff[threadidx].ops.frees++;
		}
	}
	for(n=0; n<RECORDS; n++)
	{
		if(allocptr[n])
		{
			allocated-=memsizes[whichmalloc](allocptr[n]);
			frees[whichmalloc](allocptr[n]);
			allocptr[n]=0;
			threadstuff[threadidx].ops.frees++;
		}
	}
	assert(!allocated);
#elif 1==TESTTYPE
	/* A simple stack which allocates and deallocates off the top (speed test) */
	for(n=0; n<RECORDS;)
	{
#if 1
		r=myrandom(&seed);
		if(allocptr>threadstuff[threadidx].allocs && (r & 65535)<32760) /*<32760)*/
		{	/* free */
			--toallocptr;
			--allocptr;
			--n;
			frees[whichmalloc](*allocptr);
			*allocptr=0;
			threadstuff[threadidx].ops.frees++;
		}
		else
#endif
		{
			if(doRealloc && allocptr>threadstuff[threadidx].allocs && (r & 1))
			{
	            if(!(allocptr[-1]=reallocs[whichmalloc](allocptr[-1], *toallocptr))) abort();
#if TOUCH
				{
					volatile char *mem=(volatile char *)allocptr[-1];
					volatile char *end=mem+*toallocptr;
					for(; mem<end; mem+=4096) *mem;
				}
#endif
				threadstuff[threadidx].ops.reallocs++;
			}
			else
			{
	            if(!(allocptr[0]=mallocs[whichmalloc](*toallocptr))) abort();
#if TOUCH
				{
					volatile char *mem=(volatile char *)allocptr[0];
					volatile char *end=mem+*toallocptr;
					for(; mem<end; mem+=4096) *mem;
				}
#endif
				threadstuff[threadidx].ops.mallocs++;
				allocptr++;
			}
			n++;
			toallocptr++;
			/*if(!(threadstuff[threadidx].ops & 0xff))
				nedtrimthreadcache(0,0);*/
		}
	}
	while(allocptr>threadstuff[threadidx].allocs)
	{
		frees[whichmalloc](*--allocptr);
		threadstuff[threadidx].ops.frees++;
	}
#endif
	times[threadidx]+=GetUsCount()-start;
	neddisablethreadcache(0);
	threadstuff[threadidx].done=1;
}