File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / ntp / ElectricFence / tstheap.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:08:38 2012 UTC (12 years ago) by misho
Branches: ntp, MAIN
CVS tags: v4_2_6p5p0, v4_2_6p5, HEAD
ntp 4.2.6p5

    1: #include <stdlib.h>
    2: #include <stdio.h>
    3: #include <math.h>
    4: #include <limits.h>
    5: #include "efence.h"
    6: 
    7: /*
    8:  * This is a simple program to exercise the allocator. It allocates and frees
    9:  * memory in a pseudo-random fashion. It should run silently, using up time
   10:  * and resources on your system until you stop it or until it has gone
   11:  * through TEST_DURATION (or the argument) iterations of the loop.
   12:  */
   13: 
   14: extern C_LINKAGE double drand48(void); /* For pre-ANSI C systems */
   15: 
   16: #define	POOL_SIZE	1024
   17: #define	LARGEST_BUFFER	30000
   18: #define	TEST_DURATION	1000000
   19: 
   20: void *	pool[POOL_SIZE];
   21: 
   22: #ifdef	FAKE_DRAND48
   23: /*
   24:  * Add -DFAKE_DRAND48 to your compile flags if your system doesn't
   25:  * provide drand48().
   26:  */
   27: 
   28: #ifndef	ULONG_MAX
   29: #define	ULONG_MAX	~(1L)
   30: #endif
   31: 
   32: double
   33: drand48(void)
   34: {
   35: 	return (random() / (double)ULONG_MAX);
   36: }
   37: #endif
   38: 
   39: int
   40: main(int argc, char * * argv)
   41: {
   42: 	int	count = 0;
   43: 	int	duration = TEST_DURATION;
   44: 
   45: 	if ( argc >= 2 )
   46: 		duration = atoi(argv[1]);
   47: 
   48: 	for ( ; count < duration; count++ ) {
   49: 		void * *	element = &pool[(int)(drand48() * POOL_SIZE)];
   50: 		size_t		size = (size_t)(drand48() * (LARGEST_BUFFER + 1));
   51: 
   52: 		if ( *element ) {
   53: 			free( *element );
   54: 			*element = 0;
   55: 		}
   56: 		else if ( size > 0 ) {
   57: 			*element = malloc(size);
   58: 		}
   59: 	}
   60: 	return 0;
   61: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>