-
Notifications
You must be signed in to change notification settings - Fork 0
/
small_symmetric_worst.c
73 lines (66 loc) · 1.67 KB
/
small_symmetric_worst.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "psumemory.h"
#define NUM 100
typedef struct _test{
int a;
char one;
char two;
} test;
void write_test(test* ptr){
if (ptr == NULL){
return;
}
ptr->a = 1;
ptr->one = 'A';
ptr->two = 'B';
}
void read_test(test* ptr, FILE* f){
if (ptr == NULL){
return;
}
fprintf(f, "This struct contains: %d, %c and %c. \n", ptr->a, ptr->one, ptr->two);
}
int main(){
FILE* f = fopen ("test_output8.txt", "w");
int size;
int sizeOfRegion = 1 << 20;// 1MB
int a = psumeminit(1, sizeOfRegion);
if (a == -1){
fprintf(f, "Initialization failed!\n");
}
for (int i = 0; i < 10; i++){
void* pointer_array[NUM];
for (int i = 0; i < NUM; ++i){
pointer_array[i] = NULL;
}
for (int i = 0; i < NUM; ++i)
{
size = rand()%248 + 8;
test* a = (test*)psumalloc(size);
write_test(a);
read_test(a, f);
if (a == NULL){
fprintf(f, "No.%d : No extra space for allocation in memory!\n", i);
}
else{
pointer_array[i] = a;
fprintf(f, "NO.%d chunk has been allocated. the size is %d bytes \n", i, size);
}
}
for (int i = 0; i < NUM; ++i)
{
int a = psufree(pointer_array[i]);
if (a == 0){
fprintf(f, "No.%d chunk has been freed. \n", i);
}
else{
fprintf(f, "Can not free No.%d chunk. \n", i);
}
}
}
fclose(f);
return 0;
}