@@ -154,6 +154,15 @@ RT_API_ATTRS std::size_t Descriptor::Elements() const {
154154 return elements;
155155}
156156
157+ RT_API_ATTRS static inline int MapAllocIdx (const Descriptor &desc) {
158+ #ifdef RT_DEVICE_COMPILATION
159+ // Force default allocator in device code.
160+ return kDefaultAllocator ;
161+ #else
162+ return desc.GetAllocIdx ();
163+ #endif
164+ }
165+
157166RT_API_ATTRS int Descriptor::Allocate () {
158167 std::size_t elementBytes{ElementBytes ()};
159168 if (static_cast <std::int64_t >(elementBytes) < 0 ) {
@@ -162,11 +171,10 @@ RT_API_ATTRS int Descriptor::Allocate() {
162171 elementBytes = raw_.elem_len = 0 ;
163172 }
164173 std::size_t byteSize{Elements () * elementBytes};
174+ AllocFct alloc{allocatorRegistry.GetAllocator (MapAllocIdx (*this ))};
165175 // Zero size allocation is possible in Fortran and the resulting
166176 // descriptor must be allocated/associated. Since std::malloc(0)
167177 // result is implementation defined, always allocate at least one byte.
168-
169- AllocFct alloc{allocatorRegistry.GetAllocator (GetAllocIdx ())};
170178 void *p{alloc (byteSize ? byteSize : 1 )};
171179 if (!p) {
172180 return CFI_ERROR_MEM_ALLOCATION;
@@ -209,7 +217,7 @@ RT_API_ATTRS int Descriptor::Deallocate() {
209217 if (!descriptor.base_addr ) {
210218 return CFI_ERROR_BASE_ADDR_NULL;
211219 } else {
212- FreeFct free{allocatorRegistry.GetDeallocator (GetAllocIdx ( ))};
220+ FreeFct free{allocatorRegistry.GetDeallocator (MapAllocIdx (* this ))};
213221 free (descriptor.base_addr );
214222 descriptor.base_addr = nullptr ;
215223 return CFI_SUCCESS;
0 commit comments