-
Notifications
You must be signed in to change notification settings - Fork 0
/
HeapTestEngine.c
148 lines (116 loc) · 3.62 KB
/
HeapTestEngine.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//
// HeapTestEngine.c
//
// program to test heap memory allocation
//
// David Bover, April, 2015
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include "heap352.h"
#define BUFF_SIZE 100
// test parameters
int reg_min; // minimum size (bytes) of regular allocations
int reg_max; // maximum size (bytes) of regular allocations
int large_max; // maximum size (bytes) of all allocations
int reg_percent; // percentage of allocations that are regular
int sample_size; // total number of allocation requests
unsigned long *allocs; // array of heap allocation addresses
int nallocs = 0; // number of heap allocations in allocs
/*
function get_parameters()
get the heap test parameters from the file Config.txt
*/
static void get_parameters() {
char line[BUFF_SIZE];
FILE *config;
if (!(config = fopen("Config.txt", "r"))) {
printf("Cannot open configuration file Config.txt\n");
exit(0);
}
fgets(line, BUFF_SIZE, config);
sscanf(line, "%d", ®_min);
fgets(line, BUFF_SIZE, config);
sscanf(line, "%d", ®_max);
fgets(line, BUFF_SIZE, config);
sscanf(line, "%d", &large_max);
fgets(line, BUFF_SIZE, config);
sscanf(line, "%d", ®_percent);
fgets(line, BUFF_SIZE, config);
sscanf(line, "%d", &sample_size);
}
/* function get_size()
generate a random size, based on configuration parameters
*/
static int get_size () {
if (random() % 100 > reg_percent) // a large allocation
return reg_max + random() % (large_max - reg_max);
else // a regular allocation
return reg_min + random() % (reg_max - reg_min);
}
/* function do_allocation()
make a call to malloc352()
*/
static void do_allocation (int sample) {
void *address;
address = malloc352(get_size());
if (address == NULL)
printf("malloc352 fail on sample %d\n", sample);
else {
allocs[nallocs] = (unsigned long)address;
nallocs++;
}
}
/* function do_free()
make a call to free352() for a randomly-selected allocation
*/
static void do_free () {
int index;
unsigned long address;
if (nallocs == 0) return;
index = random() % nallocs;
address = allocs[index];
allocs[index] = allocs[nallocs - 1];
nallocs--;
free352((void *)address);
}
/* function heap_test()
called to run the test engine
*/
void heap_test() {
int sample;
// start the allocation requests
for (sample = 0; sample < sample_size; sample++) {
// start-up phase, sample < sample_size/10, all allocations
if (sample < sample_size/10)
do_allocation(sample);
// build-up phase, sample < sample_size/2,
// calls to free() in proportion to sample
else if (sample < sample_size / 2) {
if (random() % 100 > 200 * sample / sample_size)
// an allocation
do_allocation(sample);
else
// a de-allocation
do_free();
}
// past half way through samples,
// even balance between allocations and de-allocations
else if (random() % 100 > 50)
do_allocation(sample);
else
do_free();
}
// clean up remaining allocations
while (nallocs > 0) do_free();
}
void init_heap_test() {
// seed the random number generator
srand(time(NULL));
// get the test parameters
get_parameters();
// get the start address of the allocs array
// allowing space for the array
allocs = (unsigned long *) sbrk(sample_size * sizeof(unsigned long));
}