Skip to content

Commit a4552cb

Browse files
committed
code format
1 parent 8e23df7 commit a4552cb

File tree

24 files changed

+1492
-1664
lines changed

24 files changed

+1492
-1664
lines changed

Assignment10/task1.c

+78-86
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,91 @@
1-
#include <stdlib.h>
2-
#include <stdio.h>
31
#include <pthread.h>
4-
#include <time.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <time.h>
55

66
#include "best_fit_allocator.h"
77

88
typedef struct Alloc {
9-
int size;
10-
int count;
9+
int size;
10+
int count;
1111
} Alloc;
1212

1313
pthread_mutex_t mutex;
1414

15-
1615
void *doMalloc(void *arg) {
17-
18-
Alloc *allocData = (Alloc*)arg;
19-
20-
unsigned int seed = time(NULL);
21-
22-
// Perform N allocations and allocate another N/2 blocks of random size.
23-
for (int i = 0; i < allocData->count * 1.5; i++)
24-
{
25-
// perform N allocations of random sizes between S and 8*S
26-
int size = (rand_r(&seed) % ((8 * allocData->size ) - allocData->size)) + allocData->size;
27-
28-
// Lock while using queue
29-
pthread_mutex_lock(&mutex);
30-
31-
char *tmp = (char*)myMalloc(size);
32-
if (tmp == NULL) {
33-
printf("Malloc failed, no space available\n");
34-
destroyAllocator();
35-
exit(-1);
36-
}
37-
// randomly(with a 50 % chance)
38-
if (rand_r(&seed) % 2) {
39-
if (tmp != NULL) {
40-
myFree(tmp);
41-
}
42-
}
43-
44-
// Unlock for other threads
45-
pthread_mutex_unlock(&mutex);
46-
}
47-
48-
49-
return NULL;
16+
Alloc *allocData = (Alloc *)arg;
17+
18+
unsigned int seed = time(NULL);
19+
20+
// Perform N allocations and allocate another N/2 blocks of random size.
21+
for (int i = 0; i < allocData->count * 1.5; i++) {
22+
// perform N allocations of random sizes between S and 8*S
23+
int size = (rand_r(&seed) % ((8 * allocData->size) - allocData->size)) +
24+
allocData->size;
25+
26+
// Lock while using queue
27+
pthread_mutex_lock(&mutex);
28+
29+
char *tmp = (char *)myMalloc(size);
30+
if (tmp == NULL) {
31+
printf("Malloc failed, no space available\n");
32+
destroyAllocator();
33+
exit(-1);
34+
}
35+
// randomly(with a 50 % chance)
36+
if (rand_r(&seed) % 2) {
37+
if (tmp != NULL) {
38+
myFree(tmp);
39+
}
40+
}
41+
42+
// Unlock for other threads
43+
pthread_mutex_unlock(&mutex);
44+
}
45+
46+
return NULL;
5047
}
5148

52-
int main(int argc, char *argv[])
53-
{
54-
if (argc < 3) {
55-
printf("Usage: ./membench 8 10000 1024\n");
56-
return EXIT_FAILURE;
57-
}
58-
59-
Alloc allocData;
60-
int threadCount = atoi(argv[1]);
61-
allocData.count = atoi(argv[2]);
62-
allocData.size = atoi(argv[3]);
63-
64-
int ret;
65-
pthread_t thread[threadCount];
66-
67-
initAllocator();
68-
69-
// init mutex
70-
pthread_mutex_init(&mutex, NULL);
71-
72-
73-
// create Threads
74-
for (int i = 0; i < threadCount; i++) {
75-
ret = pthread_create(&thread[i], NULL, doMalloc, &allocData);
76-
if (ret != 0)
77-
{
78-
fprintf(stderr, "ERROR %d: Cant create thread\n", ret);
79-
return EXIT_FAILURE;
80-
}
81-
}
82-
83-
// Wait for finish
84-
for (int i = 0; i < threadCount; i++) {
85-
ret = pthread_join(thread[i], NULL);
86-
if (ret != 0)
87-
{
88-
fprintf(stderr, "ERROR %d: Cant join thread\n", ret);
89-
return EXIT_FAILURE;
90-
}
91-
}
92-
93-
// remove mutex
94-
pthread_mutex_destroy(&mutex);
95-
96-
97-
destroyAllocator();
98-
return EXIT_SUCCESS;
49+
int main(int argc, char *argv[]) {
50+
if (argc < 3) {
51+
printf("Usage: ./membench 8 10000 1024\n");
52+
return EXIT_FAILURE;
53+
}
54+
55+
Alloc allocData;
56+
int threadCount = atoi(argv[1]);
57+
allocData.count = atoi(argv[2]);
58+
allocData.size = atoi(argv[3]);
59+
60+
int ret;
61+
pthread_t thread[threadCount];
62+
63+
initAllocator();
64+
65+
// init mutex
66+
pthread_mutex_init(&mutex, NULL);
67+
68+
// create Threads
69+
for (int i = 0; i < threadCount; i++) {
70+
ret = pthread_create(&thread[i], NULL, doMalloc, &allocData);
71+
if (ret != 0) {
72+
fprintf(stderr, "ERROR %d: Cant create thread\n", ret);
73+
return EXIT_FAILURE;
74+
}
75+
}
76+
77+
// Wait for finish
78+
for (int i = 0; i < threadCount; i++) {
79+
ret = pthread_join(thread[i], NULL);
80+
if (ret != 0) {
81+
fprintf(stderr, "ERROR %d: Cant join thread\n", ret);
82+
return EXIT_FAILURE;
83+
}
84+
}
85+
86+
// remove mutex
87+
pthread_mutex_destroy(&mutex);
88+
89+
destroyAllocator();
90+
return EXIT_SUCCESS;
9991
}

Assignment10/task2.c

+80-89
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
#include <stdlib.h>
2-
#include <stdio.h>
31
#include <pthread.h>
4-
#include <time.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <time.h>
55

66
#ifdef BEST_FIT_LOCAL
77
#define USE_LOCAL_ALLOCATOR
88
#include "best_fit_allocator.h"
9-
#endif // BEST_FIT_LOCAL
9+
#endif // BEST_FIT_LOCAL
1010

1111
#ifdef BEST_FIT_GLOBAL
1212
#include "best_fit_allocator.h"
13-
#endif // BEST_FIT_GLOBAL
13+
#endif // BEST_FIT_GLOBAL
1414

1515
#ifdef FREE_LIST_LOCAL
1616
#define USE_LOCAL_ALLOCATOR
@@ -21,96 +21,87 @@
2121
#include "freelist_allocator.h"
2222
#endif
2323

24-
2524
typedef struct Alloc {
26-
int size;
27-
int count;
25+
int size;
26+
int count;
2827
} Alloc;
2928

3029
pthread_mutex_t mutex;
3130

32-
3331
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;
6763
}
6864

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;
116107
}

0 commit comments

Comments
 (0)