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