changes.
[IRC.git] / Robust / src / Tests / memPool / testMemPool-malloc.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 = malloc( sizeof( struct bar ) );
40     bz = malloc( 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   for( i = 0; i < numThreads; ++i ) {
74
75     pthread_create( &(threads[i]),
76                     &attr,
77                     workerMain,
78                     (void*)i );
79     printf( "." );
80   }
81   
82   printf( "\n" );
83
84   for( i = 0; i < numThreads; ++i ) {
85
86     foos = NULL;
87     pthread_join( threads[i],
88                   (void**)&foos );
89
90     for( j = 0; j < numCyclesPerThread; ++j ) {
91       total += foos[j].z;
92     }
93     free( foos );
94     
95     printf( "+" );
96   }
97   
98   printf( "\nTotal=%d\n", total );
99
100   return 0;
101 }