xref: /llvm-project/flang/runtime/allocator-registry.cpp (revision 0def9a923dadc2b2b3dd067eefcef541e475594c)
1 //===-- runtime/allocator-registry.cpp ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flang/Runtime/allocator-registry.h"
10 #include "terminator.h"
11 
12 namespace Fortran::runtime {
13 
14 #ifndef FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS
15 RT_OFFLOAD_VAR_GROUP_BEGIN
16 RT_VAR_ATTRS AllocatorRegistry allocatorRegistry;
17 RT_OFFLOAD_VAR_GROUP_END
18 #endif // FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS
19 
20 RT_OFFLOAD_API_GROUP_BEGIN
21 RT_API_ATTRS void AllocatorRegistry::Register(int pos, Allocator_t allocator) {
22   // pos 0 is reserved for the default allocator and is registered in the
23   // struct ctor.
24   INTERNAL_CHECK(pos > 0 && pos < MAX_ALLOCATOR);
25   allocators[pos] = allocator;
26 }
27 
28 RT_API_ATTRS AllocFct AllocatorRegistry::GetAllocator(int pos) {
29   INTERNAL_CHECK(pos >= 0 && pos < MAX_ALLOCATOR);
30   AllocFct f{allocators[pos].alloc};
31   INTERNAL_CHECK(f != nullptr);
32   return f;
33 }
34 
35 RT_API_ATTRS FreeFct AllocatorRegistry::GetDeallocator(int pos) {
36   INTERNAL_CHECK(pos >= 0 && pos < MAX_ALLOCATOR);
37   FreeFct f{allocators[pos].free};
38   INTERNAL_CHECK(f != nullptr);
39   return f;
40 }
41 RT_OFFLOAD_API_GROUP_END
42 } // namespace Fortran::runtime
43