Skip to content

Commit 732f225

Browse files
committed
Only reinterpret cast once type is actually placed there
1 parent f1fa072 commit 732f225

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

libc/src/__support/GPU/allocator.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ struct Slab {
226226
// We attempt to place these next to each other.
227227
// TODO: We should coalesce these bits and use the result of `fetch_or` to
228228
// search for free bits in parallel.
229-
for (uint64_t mask = ~0ull; mask; mask = gpu::ballot(lane_mask, !result)) {
229+
for (uint64_t mask = lane_mask; mask;
230+
mask = gpu::ballot(lane_mask, !result)) {
230231
uint32_t id = impl::lane_count(uniform & mask);
231232
uint32_t index =
232233
(gpu::broadcast_value(lane_mask, impl::xorshift32(state)) + id) %
@@ -334,17 +335,17 @@ template <typename T> struct GuardPtr {
334335
cpp::MemoryOrder::RELAXED,
335336
cpp::MemoryOrder::RELAXED)) {
336337
count = cpp::numeric_limits<uint64_t>::max();
337-
T *mem = reinterpret_cast<T *>(impl::rpc_allocate(sizeof(T)));
338+
void *mem = impl::rpc_allocate(sizeof(T));
338339
if (!mem)
339340
return nullptr;
340341
new (mem) T(cpp::forward<Args>(args)...);
341342

342343
cpp::atomic_thread_fence(cpp::MemoryOrder::RELEASE);
343-
ptr.store(mem, cpp::MemoryOrder::RELAXED);
344+
ptr.store(reinterpret_cast<T *>(mem), cpp::MemoryOrder::RELAXED);
344345
cpp::atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
345346
if (!ref.acquire(n, count))
346347
ref.reset(n, count);
347-
return mem;
348+
return reinterpret_cast<T *>(mem);
348349
}
349350

350351
if (!expected || expected == reinterpret_cast<T *>(SENTINEL))

0 commit comments

Comments
 (0)