Skip to content

Commit c8898b0

Browse files
authored
[flang][rt] Use allocator registry to allocate the pointer payload (#129992)
pointer allocation is done through `AllocateValidatedPointerPayload`. This function was not updated to use the registered allocators in the descriptor to perform the allocation. This patch makes use of the allocator. The footer word is not set and not checked for allocator other than the default one. The support will likely come in a follow up patch but this will necessitate more functions to be registered to be able to set and get the footer value when the allocation in on the device.
1 parent 5ae19fa commit c8898b0

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

flang-rt/lib/runtime/pointer.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "flang/Runtime/pointer.h"
10+
#include "flang-rt/runtime/allocator-registry.h"
1011
#include "flang-rt/runtime/assign-impl.h"
1112
#include "flang-rt/runtime/derived.h"
1213
#include "flang-rt/runtime/environment.h"
@@ -121,13 +122,15 @@ void RTDEF(PointerAssociateRemapping)(Descriptor &pointer,
121122
}
122123
}
123124

124-
RT_API_ATTRS void *AllocateValidatedPointerPayload(std::size_t byteSize) {
125+
RT_API_ATTRS void *AllocateValidatedPointerPayload(
126+
std::size_t byteSize, int allocatorIdx) {
125127
// Add space for a footer to validate during deallocation.
126128
constexpr std::size_t align{sizeof(std::uintptr_t)};
127129
byteSize = ((byteSize + align - 1) / align) * align;
128130
std::size_t total{byteSize + sizeof(std::uintptr_t)};
129-
void *p{std::malloc(total)};
130-
if (p) {
131+
AllocFct alloc{allocatorRegistry.GetAllocator(allocatorIdx)};
132+
void *p{alloc(total)};
133+
if (p && allocatorIdx == 0) {
131134
// Fill the footer word with the XOR of the ones' complement of
132135
// the base address, which is a value that would be highly unlikely
133136
// to appear accidentally at the right spot.
@@ -151,7 +154,7 @@ int RTDEF(PointerAllocate)(Descriptor &pointer, bool hasStat,
151154
elementBytes = pointer.raw().elem_len = 0;
152155
}
153156
std::size_t byteSize{pointer.Elements() * elementBytes};
154-
void *p{AllocateValidatedPointerPayload(byteSize)};
157+
void *p{AllocateValidatedPointerPayload(byteSize, pointer.GetAllocIdx())};
155158
if (!p) {
156159
return ReturnError(terminator, CFI_ERROR_MEM_ALLOCATION, errMsg, hasStat);
157160
}
@@ -211,6 +214,7 @@ int RTDEF(PointerDeallocate)(Descriptor &pointer, bool hasStat,
211214
return ReturnError(terminator, StatBaseNull, errMsg, hasStat);
212215
}
213216
if (executionEnvironment.checkPointerDeallocation &&
217+
pointer.GetAllocIdx() == kDefaultAllocator &&
214218
!ValidatePointerPayload(pointer.raw())) {
215219
return ReturnError(terminator, StatBadPointerDeallocation, errMsg, hasStat);
216220
}

flang/include/flang/Runtime/pointer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ bool RTDECL(PointerIsAssociatedWith)(
117117

118118
// Fortran POINTERs are allocated with an extra validation word after their
119119
// payloads in order to detect erroneous deallocations later.
120-
RT_API_ATTRS void *AllocateValidatedPointerPayload(std::size_t);
120+
RT_API_ATTRS void *AllocateValidatedPointerPayload(
121+
std::size_t, int allocatorIdx = 0);
121122
RT_API_ATTRS bool ValidatePointerPayload(const ISO::CFI_cdesc_t &);
122123

123124
} // extern "C"

0 commit comments

Comments
 (0)