-
Notifications
You must be signed in to change notification settings - Fork 0
/
small_asymmetric_worst.c
95 lines (86 loc) · 2.08 KB
/
small_asymmetric_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// main.c
// Cmpsc473p2
//
// Created by Chen Chen on 3/21/15.
// Copyright (c) 2015 Chen Chen. All rights reserved.
//
#define NUM 100
#include <stdio.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_output6.txt", "w");
int size;
int sizeOfRegion = 1 << 20;// 1MB
int a = psumeminit(1, sizeOfRegion);
if (a == -1){
fprintf(f, "Initialization failed!\n");
}
void* pointer_array[16][NUM];
for (int i = 0; i < 16; i++){
for (int j = 0; j < NUM; j++){
pointer_array[i][j] = NULL;
}
}
for (int i = 0; i < 16; i++){
for (int j = 0; j < NUM; ++j)
{
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", j);
}
else{
pointer_array[i][j] = a;
fprintf(f, "NO.%d chunk has been allocated, the size is %d bytes\n", j, size);
}
}
}
int half = 16;
while (half != 0){
half /= 2;
int bound;
if (half){
bound = half * 2;
}
else{
bound = 1;
}
for (int i = half; i < bound ; i++){
for (int j = 0; j < NUM; j++)
{
int a = psufree(pointer_array[i][j]);
if (a == 0){
fprintf(f, "No.%d chunk has been freed. \n", j);
}
else{
fprintf(f, "Can not free No.%d chunk. \n", j);
}
}
}
}
fclose(f);
return 0;
}