Skip to content

Commit 4ad99d7

Browse files
nwf-msrnwf
authored andcommitted
Downgrade some casts
Do a quick sweep through the codebase to eliminate some reinterpret_cast<>s where less fire power would do just fine.
1 parent 62cce1a commit 4ad99d7

4 files changed

Lines changed: 11 additions & 16 deletions

File tree

src/backend/pagemap.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace snmalloc
130130

131131
// Put pagemap at start of range.
132132
// TODO CHERI capability bound here!
133-
body = reinterpret_cast<T*>(b);
133+
body = static_cast<T*>(b);
134134
body_opt = body;
135135
// Advance by size of pagemap.
136136
// Note that base needs to be aligned to GRANULARITY for the rest of the
@@ -176,8 +176,7 @@ namespace snmalloc
176176
// Begin pagemap at random offset within the additionally allocated space.
177177
static_assert(bits::is_pow2(sizeof(T)), "Next line assumes this.");
178178
size_t offset = get_entropy64<PAL>() & (additional_size - sizeof(T));
179-
auto new_body =
180-
reinterpret_cast<T*>(pointer_offset(new_body_untyped, offset));
179+
auto new_body = pointer_offset<T>(new_body_untyped, offset);
181180

182181
if constexpr (pal_supports<LazyCommit, PAL>)
183182
{
@@ -191,7 +190,7 @@ namespace snmalloc
191190
start_page, pointer_diff(start_page, end_page));
192191
}
193192
#else
194-
auto new_body = reinterpret_cast<T*>(new_body_untyped);
193+
auto new_body = static_cast<T*>(new_body_untyped);
195194
#endif
196195
// Ensure bottom page is committed
197196
// ASSUME: new memory is zeroed.

src/test/func/malloc/malloc.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,12 @@ void check_result(size_t size, size_t align, void* p, int err, bool null)
8888
expected_size);
8989
failed = true;
9090
}
91-
if (
92-
(static_cast<size_t>(reinterpret_cast<uintptr_t>(p) % align) != 0) &&
93-
(size != 0))
91+
if (((address_cast(p) % align) != 0) && (size != 0))
9492
{
9593
INFO("Address is {}, but required to be aligned to {}.\n", p, align);
9694
failed = true;
9795
}
98-
if (
99-
static_cast<size_t>(
100-
reinterpret_cast<uintptr_t>(p) % natural_alignment(size)) != 0)
96+
if ((address_cast(p) % natural_alignment(size)) != 0)
10197
{
10298
INFO(
10399
"Address is {}, but should have natural alignment to {}.\n",

src/test/func/memcpy/func-memcpy.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ extern "C" void abort()
6969
void check_size(size_t size)
7070
{
7171
START_TEST("checking {}-byte memcpy", size);
72-
auto* s = reinterpret_cast<unsigned char*>(my_malloc(size + 1));
73-
auto* d = reinterpret_cast<unsigned char*>(my_malloc(size + 1));
72+
auto* s = static_cast<unsigned char*>(my_malloc(size + 1));
73+
auto* d = static_cast<unsigned char*>(my_malloc(size + 1));
7474
d[size] = 0;
7575
s[size] = 255;
7676
for (size_t start = 0; start < size; start++)
@@ -115,8 +115,8 @@ void check_bounds(size_t size, size_t out_of_bounds)
115115
{
116116
START_TEST(
117117
"memcpy bounds, size {}, {} bytes out of bounds", size, out_of_bounds);
118-
auto* s = reinterpret_cast<unsigned char*>(my_malloc(size));
119-
auto* d = reinterpret_cast<unsigned char*>(my_malloc(size));
118+
auto* s = static_cast<unsigned char*>(my_malloc(size));
119+
auto* d = static_cast<unsigned char*>(my_malloc(size));
120120
for (size_t i = 0; i < size; ++i)
121121
{
122122
s[i] = static_cast<unsigned char>(i);

src/test/perf/memcpy/memcpy.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void shape(size_t size)
3030
// size / alignment) * alignment;
3131
Shape s;
3232
s.object = ThreadAlloc::get().alloc(rsize);
33-
s.dst = reinterpret_cast<unsigned char*>(s.object) + offset;
33+
s.dst = static_cast<unsigned char*>(s.object) + offset;
3434
// Bring into cache the destination of the copy.
3535
memset(s.dst, 0xFF, size);
3636
allocs.push_back(s);
@@ -51,7 +51,7 @@ void test_memcpy(size_t size, void* src, Memcpy mc)
5151
{
5252
for (auto& s : allocs)
5353
{
54-
auto* dst = reinterpret_cast<unsigned char*>(s.dst);
54+
auto* dst = static_cast<unsigned char*>(s.dst);
5555
mc(dst, src, size);
5656
}
5757
}

0 commit comments

Comments
 (0)