Skip to content

Commit 40320d2

Browse files
committed
Use unique_ptr, avoid c-style casts.
1 parent 2806389 commit 40320d2

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

offload/libomptarget/interface.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <cstdint>
3131
#include <cstdio>
3232
#include <cstdlib>
33+
#include <memory>
3334

3435
#ifdef OMPT_SUPPORT
3536
using namespace llvm::omp::target::ompt;
@@ -167,13 +168,13 @@ targetData(ident_t *Loc, int64_t DeviceId, int32_t ArgNum, void **ArgsBase,
167168
int Rc = OFFLOAD_SUCCESS;
168169

169170
// Only allocate AttachInfo for targetDataBegin
170-
AttachInfoTy *AttachInfo = nullptr;
171+
std::unique_ptr<AttachInfoTy> AttachInfo;
171172
if (TargetDataFunction == targetDataBegin)
172-
AttachInfo = new AttachInfoTy();
173+
AttachInfo = std::make_unique<AttachInfoTy>();
173174

174175
Rc = TargetDataFunction(Loc, *DeviceOrErr, ArgNum, ArgsBase, Args, ArgSizes,
175-
ArgTypes, ArgNames, ArgMappers, AsyncInfo, AttachInfo,
176-
/*FromMapper=*/false);
176+
ArgTypes, ArgNames, ArgMappers, AsyncInfo,
177+
AttachInfo.get(), /*FromMapper=*/false);
177178

178179
if (Rc == OFFLOAD_SUCCESS) {
179180
// Process deferred ATTACH entries BEFORE synchronization
@@ -184,9 +185,6 @@ targetData(ident_t *Loc, int64_t DeviceId, int32_t ArgNum, void **ArgsBase,
184185
Rc = AsyncInfo.synchronize();
185186
}
186187

187-
if (AttachInfo)
188-
delete AttachInfo;
189-
190188
handleTargetOutcome(Rc == OFFLOAD_SUCCESS, Loc);
191189
}
192190

offload/libomptarget/omptarget.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,10 @@ static int performPointerAttachment(DeviceTy &Device, AsyncInfoTy &AsyncInfo,
399399
int64_t VoidPtrSize = sizeof(void *);
400400
assert(HstPtrSize >= VoidPtrSize && "PointerSize is too small");
401401

402-
uint64_t Delta = (uint64_t)HstPteeBegin - (uint64_t)HstPteeBase;
403-
void *TgtPteeBase = (void *)((uint64_t)TgtPteeBegin - Delta);
402+
uint64_t Delta = reinterpret_cast<uint64_t>(HstPteeBegin) -
403+
reinterpret_cast<uint64_t>(HstPteeBase);
404+
void *TgtPteeBase = reinterpret_cast<void *>(
405+
reinterpret_cast<uint64_t>(TgtPteeBegin) - Delta);
404406

405407
// Add shadow pointer tracking
406408
// TODO: Support shadow-tracking of larger than VoidPtrSize pointers,
@@ -463,7 +465,8 @@ static int performPointerAttachment(DeviceTy &Device, AsyncInfoTy &AsyncInfo,
463465
// And to the remaining bytes, we copy the remaining contents of the host
464466
// descriptor after the initial VoidPtrSize bytes.
465467
uint64_t HstDescriptorFieldsSize = HstPtrSize - VoidPtrSize;
466-
void *HstDescriptorFieldsAddr = (char *)HstPtrAddr + VoidPtrSize;
468+
void *HstDescriptorFieldsAddr =
469+
reinterpret_cast<char *>(HstPtrAddr) + VoidPtrSize;
467470
std::memcpy(DataBuffer + VoidPtrSize, HstDescriptorFieldsAddr,
468471
HstDescriptorFieldsSize);
469472

@@ -613,7 +616,7 @@ int targetDataBegin(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
613616
(PointerTpr.Flags.IsNewEntry ? "" : " not"));
614617
PointerHstPtrBegin = HstPtrBase;
615618
// modify current entry.
616-
HstPtrBase = *(void **)HstPtrBase;
619+
HstPtrBase = *reinterpret_cast<void **>(HstPtrBase);
617620
// No need to update pointee ref count for the first element of the
618621
// subelement that comes from mapper.
619622
UpdateRef =
@@ -655,9 +658,10 @@ int targetDataBegin(ident_t *Loc, DeviceTy &Device, int32_t ArgNum,
655658

656659
if (ArgTypes[I] & OMP_TGT_MAPTYPE_PTR_AND_OBJ && !IsHostPtr) {
657660
int Ret = performPointerAttachment(
658-
Device, AsyncInfo, (void **)PointerHstPtrBegin, HstPtrBase,
659-
HstPtrBegin, (void **)PointerTgtPtrBegin, TgtPtrBegin, sizeof(void *),
660-
PointerTpr);
661+
Device, AsyncInfo, reinterpret_cast<void **>(PointerHstPtrBegin),
662+
HstPtrBase, HstPtrBegin,
663+
reinterpret_cast<void **>(PointerTgtPtrBegin), TgtPtrBegin,
664+
sizeof(void *), PointerTpr);
661665
if (Ret != OFFLOAD_SUCCESS)
662666
return OFFLOAD_FAIL;
663667
}
@@ -746,7 +750,7 @@ int processAttachEntries(DeviceTy &Device, AttachInfoTy &AttachInfo,
746750
++EntryIdx) {
747751
const auto &AttachEntry = AttachInfo.AttachEntries[EntryIdx];
748752

749-
void **HstPtr = (void **)AttachEntry.PointerBase;
753+
void **HstPtr = reinterpret_cast<void **>(AttachEntry.PointerBase);
750754

751755
void *HstPteeBase = *HstPtr;
752756
void *HstPteeBegin = AttachEntry.PointeeBegin;
@@ -767,7 +771,8 @@ int processAttachEntries(DeviceTy &Device, AttachInfoTy &AttachInfo,
767771
void *AllocPtr = Alloc.first;
768772
int64_t AllocSize = Alloc.second;
769773
return Ptr >= AllocPtr &&
770-
Ptr < (void *)((char *)AllocPtr + AllocSize);
774+
Ptr < reinterpret_cast<void *>(
775+
reinterpret_cast<char *>(AllocPtr) + AllocSize);
771776
});
772777
DP("ATTACH entry %zu: %s pointer " DPxMOD " was newly allocated: %s\n",
773778
EntryIdx, PtrName, DPxPTR(Ptr), IsNewlyAllocated ? "yes" : "no");
@@ -823,7 +828,7 @@ int processAttachEntries(DeviceTy &Device, AttachInfoTy &AttachInfo,
823828
if (!PtrTPROpt)
824829
continue;
825830
TargetPointerResultTy &PtrTPR = *PtrTPROpt;
826-
void **TgtPtrBase = (void **)PtrTPR.TargetPointer;
831+
void **TgtPtrBase = reinterpret_cast<void **>(PtrTPR.TargetPointer);
827832

828833
// Get device version of the pointee (e.g., &p[10])
829834
auto PteeTPROpt = LookupTargetPointer(HstPteeBegin, 0, "pointee");

0 commit comments

Comments
 (0)