-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.c
155 lines (124 loc) · 2.95 KB
/
example.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
149
150
151
152
153
154
155
#define _POSIX_C_SOURCE 200112L
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/time.h>
#include "smalloc.h"
#define MAX_ACTIVE_ALLOCS 2048
#define NUM_ROUNDS 60000000
#define ALLOC_SIZE 64
#ifdef SMALLOC_EX_TRACE
# define TRACE_ALLOC(__ptr) printf("a %p\n", (void*)__ptr)
# define TRACE_FREE(__ptr) printf("f %p\n", (void*)__ptr)
#else
# define TRACE_ALLOC(__ptr) do {} while(0)
# define TRACE_FREE(__ptr) do {} while(0)
#endif
void* alloc_page(void* opaque) {
void* page = NULL;
(void)opaque;
if (posix_memalign(&page, 4096, 4096))
errx(EXIT_FAILURE, "posix_memalign() failed");
return page;
}
void free_page(void* opaque, void* page) {
(void)opaque;
free(page);
}
int* gen_rands() {
size_t i;
int* rands;
rands = malloc(NUM_ROUNDS * sizeof(int));
if (!rands)
return NULL;
srand(666);
for (i = 0; i < NUM_ROUNDS; ++i)
rands[i] = rand();
return rands;
}
void run_glibc(int* rands) {
size_t i;
unsigned char volatile* pointers[MAX_ACTIVE_ALLOCS] = {0};
size_t np = 0;
for (i = 0; i < NUM_ROUNDS; ++i) {
unsigned char volatile* ptr = malloc(ALLOC_SIZE);
TRACE_ALLOC(ptr);
ptr[0] = 1;
pointers[np++] = ptr;
if (np == MAX_ACTIVE_ALLOCS) {
int r = (rands[i] % MAX_ACTIVE_ALLOCS) + 1;
for (int v = 1; v <= r; ++v) {
TRACE_FREE((void*)pointers[MAX_ACTIVE_ALLOCS - v]);
free((void*)pointers[MAX_ACTIVE_ALLOCS - v]);
np--;
}
}
}
for (i = 0; i < np; ++i) {
TRACE_FREE((void*)pointers[i]);
free((void*)pointers[i]);
}
}
void run_smalloc(int* rands) {
size_t i;
unsigned char volatile* pointers[MAX_ACTIVE_ALLOCS] = {0};
size_t np = 0;
pa_t pa = {
.alloc_page = alloc_page,
.free_page = free_page,
};
smalloc_t sm;
if (smalloc_init(&sm, &pa))
err(EXIT_FAILURE, "smalloc_init");
for (i = 0; i < NUM_ROUNDS; ++i) {
unsigned char volatile* ptr = smalloc_alloc(&sm, ALLOC_SIZE);
TRACE_ALLOC(ptr);
ptr[0] = 1;
pointers[np++] = ptr;
if (np == MAX_ACTIVE_ALLOCS) {
int r = (rands[i] % MAX_ACTIVE_ALLOCS) + 1;
for (int v = 1; v <= r; ++v) {
TRACE_FREE((void*)pointers[MAX_ACTIVE_ALLOCS - v]);
smalloc_free(&sm, (void*)pointers[MAX_ACTIVE_ALLOCS - v]);
np--;
}
}
}
for (i = 0; i < np; ++i) {
TRACE_FREE((void*)pointers[i]);
smalloc_free(&sm, (void*)pointers[i]);
}
smalloc_release(&sm);
}
int usage(const char* name) {
fprintf(stderr, "%s <smalloc | libc>\n", name);
return EXIT_FAILURE;
}
int parse_args(int argc, const char* argv[]) {
if (argc < 2)
return -1;
if (!strcmp(argv[1], "libc"))
return 0;
if (!strcmp(argv[1], "smalloc"))
return 1;
return -1;
}
int main(int argc, const char* argv[]) {
int* rands;
int use_smalloc;
use_smalloc = parse_args(argc, argv);
if (use_smalloc < 0)
return usage(argv[0]);
rands = gen_rands();
if (!rands)
err(EXIT_FAILURE, "gen_rands()");
if (use_smalloc)
run_smalloc(rands);
else
run_glibc(rands);
free(rands);
return EXIT_SUCCESS;
}