adding a test case
[IRC.git] / Robust / src / Tests / memPool / testMemPool.c
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include <pthread.h>
5 #include "memPool.h"
6 #include "config-test.h"
7
8
9 #define numThreads          24
10 #define numCyclesPerThread  500000
11 #define extraBytesInRecords 1000
12
13
14 #ifdef CONFIG_POOLALLOC
15 __thread static MemPool* pool;
16 #endif
17
18
19 struct bar {
20   int x;
21   char takeSpace[extraBytesInRecords];
22 };
23
24 struct baz {
25   int y;
26   char takeSpace[extraBytesInRecords];
27 };
28
29 struct foo {
30   struct bar* br;
31   struct baz* bz;
32   int z;
33   char takeSpace[extraBytesInRecords];
34 };
35
36
37 void* workerMain( void* arg ) {
38
39   INTPTR i = (INTPTR)arg;
40   int j;
41   struct bar* br;
42   struct baz* bz;
43   struct foo* foos = malloc( numCyclesPerThread*sizeof( struct foo ) );
44
45   for( j = 0; j < numCyclesPerThread; ++j ) {
46     br = poolalloc( barPool );
47     bz = poolalloc( bazPool );
48
49     br->x = i + j;
50     bz->y = -4321;
51     
52     foos[j].br = br;
53     foos[j].bz = bz;
54     foos[j].z = foos[j].br->x + foos[j].bz->y;
55
56     poolfree( barPool, foos[j].br );
57     poolfree( bazPool, foos[j].bz );
58   }
59   
60   pthread_exit( foos );
61 }
62
63
64 int main() {
65
66   INTPTR i;
67   int j;
68
69   struct foo* foos;
70
71   pthread_t      threads[numThreads];
72   pthread_attr_t attr;
73
74   int total = 0;
75
76
77 #ifdef CONFIG_POOLALLOC
78   pool = poolcreate( sizeof( struct bar ) );
79 #endif
80
81
82   pthread_attr_init( &attr );
83   pthread_attr_setdetachstate( &attr, 
84                                PTHREAD_CREATE_JOINABLE );
85
86
87   for( i = 0; i < numThreads; ++i ) {
88
89     pthread_create( &(threads[i]),
90                     &attr,
91                     workerMain,
92                     (void*)i );
93     printf( "." );
94   }
95   
96   printf( "\n" );
97
98   for( i = 0; i < numThreads; ++i ) {
99     
100     foos = NULL;
101     pthread_join( threads[i],
102                   (void**)&foos );
103
104     for( j = 0; j < numCyclesPerThread; ++j ) {
105       total += foos[j].z;
106     }
107     free( foos );
108
109     printf( "+" );
110   }
111
112   printf( "\nTotal=%d\n", total );
113
114   return 0;
115 }