FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
rpmalloc/test/main-override.cc at develop · mjansson/rpmalloc · GitHub
mjansson
/
rpmalloc
Public
Notifications
You must be signed in to change notification settings
Fork
217
Star
2.5k
Code
Issues
8
Pull requests
0
Discussions
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
rpmalloc
/
test
/
main-override.cc
Copy path
More file actions
More file actions
Latest commit
History
History
History
191 lines (163 loc) · 4.02 KB
Breadcrumbs
rpmalloc
/
test
/
main-override.cc
Copy path
File metadata and controls
191 lines (163 loc) · 4.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
SPDX-FileCopyrightText: 2017 Mattias Jansson
* SPDX-License-Identifier: Unlicense OR MIT
*/
#
if
defined(_WIN32) && !defined(_CRT_SECURE_NO_WARNINGS)
#
define
_CRT_SECURE_NO_WARNINGS
#
endif
#
if
defined(__clang__)
#
if
__has_warning("-Wunsafe-buffer-usage")
#
pragma
clang diagnostic ignored "-Wunsafe-buffer-usage"
#
endif
#
endif
#
include
<
rpmalloc.h
>
#
ifdef
_WIN32
#
include
<
rpnew.h
>
#
endif
extern
"
C
"
{
#
include
"
test.h
"
#
include
"
thread.h
"
}
#
include
<
stdio.h
>
#
include
<
string.h
>
#
include
<
stdlib.h
>
#
include
<
memory.h
>
#
include
<
inttypes.h
>
#
if
defined(_WIN32)
extern
"
C
"
void
*
rpvalloc
(
size_t
size);
static
void
*
valloc
(
size_t
size) {
return
rpvalloc
(size);
}
#
endif
#
if
defined(_WIN32) || defined(__APPLE__)
extern
"
C
"
void
*
rppvalloc
(
size_t
size);
static
void
*
pvalloc
(
size_t
size) {
return
rppvalloc
(size);
}
#
else
#
include
<
malloc.h
>
#
if
defined(__linux__) && !defined(__GLIBC__)
//
Non-glibc C libraries (e.g. musl) do not declare the deprecated pvalloc
extern
"
C
"
void
*
rppvalloc
(
size_t
size);
static
void
*
pvalloc
(
size_t
size) {
return
rppvalloc
(size);
}
#
endif
#
endif
extern
"
C
"
int
test_malloc
(
int
print_log);
extern
"
C
"
int
test_free
(
int
print_log);
extern
"
C
"
int
test_malloc_thread
(
void
);
int
test_malloc
(
int
print_log) {
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: valloc 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);
for
(
int
iloop =
0
; iloop <
16
; ++iloop) {
char
* ptr[
1024
];
for
(
int
i =
0
; i <
1024
; ++i) {
ptr[i] =
reinterpret_cast
<
char
*>(
calloc
(
3
,
75
));
if
(!ptr[i])
return
test_fail
(
"
calloc failed
"
);
if
(
rpmalloc_usable_size
(ptr[i]) < (
3
*
75
))
return
test_fail
(
"
calloc usable size invalid
"
);
for
(
unsigned
int
j =
0
; j <
3
*
75
; ++j) {
if
(ptr[i][j])
return
test_fail
(
"
calloc memory not zero
"
);
}
}
for
(
int
i =
0
; i <
1024
; ++i)
free
(ptr[i]);
}
if
(print_log)
printf
(
"
Memory override allocation tests passed
\n
"
);
return
0
;
}
int
test_free
(
int
print_log) {
free
(
rpmalloc
(
371
));
delete
(
new
int
);
delete[]
(
new
int
[
16
]);
free
(
pvalloc
(
1275
));
if
(print_log)
printf
(
"
Memory override free tests passed
\n
"
);
return
0
;
}
static
void
basic_malloc_thread
(
void
* argp) {
(
void
)
sizeof
(argp);
int
res =
test_malloc
(
0
);
if
(res) {
thread_exit
(
static_cast
<
uintptr_t
>(res));
return
;
}
res =
test_free
(
0
);
if
(res) {
thread_exit
(
static_cast
<
uintptr_t
>(res));
return
;
}
thread_exit
(
0
);
}
int
test_malloc_thread
(
void
) {
uintptr_t
thread[
2
];
uintptr_t
threadres[
2
];
thread_arg targ;
memset
(&targ,
0
,
sizeof
(targ));
targ.
fn
= basic_malloc_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
(
"
Memory override thread tests passed
\n
"
);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL