forked from mjansson/rpmalloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-override.cc
201 lines (168 loc) · 4.36 KB
/
main-override.cc
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#if defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)
# define _CRT_SECURE_NO_WARNINGS
#endif
#include <rpmalloc.h>
#include <rpnew.h>
#include <thread.h>
#include <test.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <inttypes.h>
extern "C" void* RPMALLOC_CDECL pvalloc(size_t size);
extern "C" void* RPMALLOC_CDECL valloc(size_t size);
static size_t _hardware_threads;
static void
test_initialize(void);
static int
test_fail(const char* reason) {
fprintf(stderr, "FAIL: %s\n", reason);
return -1;
}
static int
test_alloc(void) {
const rpmalloc_config_t* config = rpmalloc_config();
void* p = malloc(371);
if (!p)
return test_fail("malloc failed");
if ((rpmalloc_usable_size(p) < 371) || (rpmalloc_usable_size(p) > (371 + 16)))
return test_fail("usable size invalid (1)");
rpfree(p);
p = new int;
if (!p)
return test_fail("new failed");
if (rpmalloc_usable_size(p) != 16)
return test_fail("usable size invalid (2)");
delete static_cast<int*>(p);
p = new int[16];
if (!p)
return test_fail("new[] failed");
if (rpmalloc_usable_size(p) != 16*sizeof(int))
return test_fail("usable size invalid (3)");
delete[] static_cast<int*>(p);
p = new int[32];
if (!p)
return test_fail("new[] failed");
if (rpmalloc_usable_size(p) != 32*sizeof(int))
return test_fail("usable size invalid (4)");
delete[] static_cast<int*>(p);
p = valloc(873);
if (reinterpret_cast<uintptr_t>(p) & (config->page_size - 1)) {
fprintf(stderr, "FAIL: pvalloc did not align address to page size (%p)\n", p);
return -1;
}
free(p);
p = pvalloc(275);
if (reinterpret_cast<uintptr_t>(p) & (config->page_size - 1)) {
fprintf(stderr, "FAIL: pvalloc did not align address to page size (%p)\n", p);
return -1;
}
if (reinterpret_cast<uintptr_t>(p) < config->page_size) {
fprintf(stderr, "FAIL: pvalloc did not align size to page size (%" PRIu64 ")\n", static_cast<uint64_t>(rpmalloc_usable_size(p)));
return -1;
}
rpfree(p);
printf("Allocation tests passed\n");
return 0;
}
static int
test_free(void) {
free(rpmalloc(371));
free(new int);
free(new int[16]);
free(pvalloc(1275));
printf("Free tests passed\n");
return 0;
}
static void
basic_thread(void* argp) {
(void)sizeof(argp);
int res = test_alloc();
if (res) {
thread_exit(static_cast<uintptr_t>(res));
return;
}
res = test_free();
if (res) {
thread_exit(static_cast<uintptr_t>(res));
return;
}
thread_exit(0);
}
static int
test_thread(void) {
uintptr_t thread[2];
uintptr_t threadres[2];
thread_arg targ;
memset(&targ, 0, sizeof(targ));
targ.fn = basic_thread;
for (int i = 0; i < 2; ++i)
thread[i] = thread_run(&targ);
for (int i = 0; i < 2; ++i) {
threadres[i] = thread_join(thread[i]);
if (threadres[i])
return -1;
}
printf("Thread tests passed\n");
return 0;
}
int
test_run(int argc, char** argv) {
(void)sizeof(argc);
(void)sizeof(argv);
test_initialize();
if (test_alloc())
return -1;
if (test_free())
return -1;
if (test_thread())
return -1;
printf("All tests passed\n");
return 0;
}
#if (defined(__APPLE__) && __APPLE__)
# include <TargetConditionals.h>
# if defined(__IPHONE__) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || (defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR)
# define NO_MAIN 1
# endif
#elif (defined(__linux__) || defined(__linux))
# include <sched.h>
#endif
#if !defined(NO_MAIN)
int
main(int argc, char** argv) {
return test_run(argc, argv);
}
#endif
#ifdef _WIN32
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wnonportable-system-include-path"
#endif
#include <windows.h>
static void
test_initialize(void) {
SYSTEM_INFO system_info;
GetSystemInfo(&system_info);
_hardware_threads = static_cast<size_t>(system_info.dwNumberOfProcessors);
}
#elif (defined(__linux__) || defined(__linux))
static void
test_initialize(void) {
cpu_set_t prevmask, testmask;
CPU_ZERO(&prevmask);
CPU_ZERO(&testmask);
sched_getaffinity(0, sizeof(prevmask), &prevmask); //Get current mask
sched_setaffinity(0, sizeof(testmask), &testmask); //Set zero mask
sched_getaffinity(0, sizeof(testmask), &testmask); //Get mask for all CPUs
sched_setaffinity(0, sizeof(prevmask), &prevmask); //Reset current mask
int num = CPU_COUNT(&testmask);
_hardware_threads = static_cast<size_t>(num > 1 ? num : 1);
}
#else
static void
test_initialize(void) {
_hardware_threads = 1;
}
#endif