1
- #include <stdlib.h>
2
- #include <stdio.h>
3
1
#include <pthread.h>
4
- #include <time.h>
2
+ #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <time.h>
5
5
6
6
#ifdef BEST_FIT_LOCAL
7
7
#define USE_LOCAL_ALLOCATOR
8
8
#include "best_fit_allocator.h"
9
- #endif // BEST_FIT_LOCAL
9
+ #endif // BEST_FIT_LOCAL
10
10
11
11
#ifdef BEST_FIT_GLOBAL
12
12
#include "best_fit_allocator.h"
13
- #endif // BEST_FIT_GLOBAL
13
+ #endif // BEST_FIT_GLOBAL
14
14
15
15
#ifdef FREE_LIST_LOCAL
16
16
#define USE_LOCAL_ALLOCATOR
21
21
#include "freelist_allocator.h"
22
22
#endif
23
23
24
-
25
24
typedef struct Alloc {
26
- int size ;
27
- int count ;
25
+ int size ;
26
+ int count ;
28
27
} Alloc ;
29
28
30
29
pthread_mutex_t mutex ;
31
30
32
-
33
31
void * doMalloc (void * arg ) {
34
-
35
- Alloc * allocData = (Alloc * )arg ;
36
-
37
- unsigned int seed = time (NULL );
38
-
39
- // Perform N allocations and allocate another N/2 blocks of random size.
40
- for (int i = 0 ; i < allocData -> count * 1.5 ; i ++ )
41
- {
42
- // perform N allocations of random sizes between S and 8*S
43
- int size = (rand_r (& seed ) % ((8 * allocData -> size ) - allocData -> size )) + allocData -> size ;
44
-
45
- // Lock while using queue
46
- pthread_mutex_lock (& mutex );
47
-
48
- char * tmp = (char * )myMalloc (size );
49
- if (tmp == NULL ) {
50
- printf ("Malloc failed, no space available\n" );
51
- destroyAllocator ();
52
- exit (-1 );
53
- }
54
- // randomly(with a 50 % chance)
55
- if (rand_r (& seed ) % 2 ) {
56
- if (tmp != NULL ) {
57
- myFree (tmp );
58
- }
59
- }
60
-
61
- // Unlock for other threads
62
- pthread_mutex_unlock (& mutex );
63
- }
64
-
65
-
66
- return NULL ;
32
+ Alloc * allocData = (Alloc * )arg ;
33
+
34
+ unsigned int seed = time (NULL );
35
+
36
+ // Perform N allocations and allocate another N/2 blocks of random size.
37
+ for (int i = 0 ; i < allocData -> count * 1.5 ; i ++ ) {
38
+ // perform N allocations of random sizes between S and 8*S
39
+ int size = (rand_r (& seed ) % ((8 * allocData -> size ) - allocData -> size )) +
40
+ allocData -> size ;
41
+
42
+ // Lock while using queue
43
+ pthread_mutex_lock (& mutex );
44
+
45
+ char * tmp = (char * )myMalloc (size );
46
+ if (tmp == NULL ) {
47
+ printf ("Malloc failed, no space available\n" );
48
+ destroyAllocator ();
49
+ exit (-1 );
50
+ }
51
+ // randomly(with a 50 % chance)
52
+ if (rand_r (& seed ) % 2 ) {
53
+ if (tmp != NULL ) {
54
+ myFree (tmp );
55
+ }
56
+ }
57
+
58
+ // Unlock for other threads
59
+ pthread_mutex_unlock (& mutex );
60
+ }
61
+
62
+ return NULL ;
67
63
}
68
64
69
- int main (int argc , char * argv [])
70
- {
71
- if (argc < 3 ) {
72
- printf ("Usage: ./membench 8 10000 1024\n" );
73
- return EXIT_FAILURE ;
74
- }
75
-
76
- Alloc allocData ;
77
- int threadCount = atoi (argv [1 ]);
78
- allocData .count = atoi (argv [2 ]);
79
- allocData .size = atoi (argv [3 ]);
80
-
81
- int ret ;
82
- pthread_t thread [threadCount ];
83
-
84
- initAllocator ();
85
-
86
- // init mutex
87
- pthread_mutex_init (& mutex , NULL );
88
-
89
-
90
- // create Threads
91
- for (int i = 0 ; i < threadCount ; i ++ ) {
92
- ret = pthread_create (& thread [i ], NULL , doMalloc , & allocData );
93
- if (ret != 0 )
94
- {
95
- fprintf (stderr , "ERROR %d: Cant create thread\n" , ret );
96
- return EXIT_FAILURE ;
97
- }
98
- }
99
-
100
- // Wait for finish
101
- for (int i = 0 ; i < threadCount ; i ++ ) {
102
- ret = pthread_join (thread [i ], NULL );
103
- if (ret != 0 )
104
- {
105
- fprintf (stderr , "ERROR %d: Cant join thread\n" , ret );
106
- return EXIT_FAILURE ;
107
- }
108
- }
109
-
110
- // remove mutex
111
- pthread_mutex_destroy (& mutex );
112
-
113
-
114
- destroyAllocator ();
115
- return EXIT_SUCCESS ;
65
+ int main (int argc , char * argv []) {
66
+ if (argc < 3 ) {
67
+ printf ("Usage: ./membench 8 10000 1024\n" );
68
+ return EXIT_FAILURE ;
69
+ }
70
+
71
+ Alloc allocData ;
72
+ int threadCount = atoi (argv [1 ]);
73
+ allocData .count = atoi (argv [2 ]);
74
+ allocData .size = atoi (argv [3 ]);
75
+
76
+ int ret ;
77
+ pthread_t thread [threadCount ];
78
+
79
+ initAllocator ();
80
+
81
+ // init mutex
82
+ pthread_mutex_init (& mutex , NULL );
83
+
84
+ // create Threads
85
+ for (int i = 0 ; i < threadCount ; i ++ ) {
86
+ ret = pthread_create (& thread [i ], NULL , doMalloc , & allocData );
87
+ if (ret != 0 ) {
88
+ fprintf (stderr , "ERROR %d: Cant create thread\n" , ret );
89
+ return EXIT_FAILURE ;
90
+ }
91
+ }
92
+
93
+ // Wait for finish
94
+ for (int i = 0 ; i < threadCount ; i ++ ) {
95
+ ret = pthread_join (thread [i ], NULL );
96
+ if (ret != 0 ) {
97
+ fprintf (stderr , "ERROR %d: Cant join thread\n" , ret );
98
+ return EXIT_FAILURE ;
99
+ }
100
+ }
101
+
102
+ // remove mutex
103
+ pthread_mutex_destroy (& mutex );
104
+
105
+ destroyAllocator ();
106
+ return EXIT_SUCCESS ;
116
107
}
0 commit comments