FLANG
allocator-registry.h
1//===-- runtime/allocator-registry.h ----------------------------*- C++ -*-===//
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#ifndef FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_H_
10#define FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_H_
11
12#include "flang/Common/api-attrs.h"
13#include "flang/Runtime/allocator-registry-consts.h"
14#include <cstdlib>
15#include <vector>
16
17#define MAX_ALLOCATOR 7 // 3 bits are reserved in the descriptor.
18
19namespace Fortran::runtime {
20
21using AllocFct = void *(*)(std::size_t);
22using FreeFct = void (*)(void *);
23
24typedef struct Allocator_t {
25 AllocFct alloc{nullptr};
26 FreeFct free{nullptr};
28
29#ifdef RT_DEVICE_COMPILATION
30static RT_API_ATTRS void *MallocWrapper(std::size_t size) {
31 return std::malloc(size);
32}
33static RT_API_ATTRS void FreeWrapper(void *p) { return std::free(p); }
34#endif
35
37#ifdef RT_DEVICE_COMPILATION
38 RT_API_ATTRS constexpr AllocatorRegistry()
39 : allocators{{&MallocWrapper, &FreeWrapper}} {}
40#else
41 constexpr AllocatorRegistry() {
42 allocators[kDefaultAllocator] = {&std::malloc, &std::free};
43 };
44#endif
45 RT_API_ATTRS void Register(int, Allocator_t);
46 RT_API_ATTRS AllocFct GetAllocator(int pos);
47 RT_API_ATTRS FreeFct GetDeallocator(int pos);
48
49 Allocator_t allocators[MAX_ALLOCATOR];
50};
51
52RT_OFFLOAD_VAR_GROUP_BEGIN
53extern RT_VAR_ATTRS AllocatorRegistry allocatorRegistry;
54RT_OFFLOAD_VAR_GROUP_END
55
56} // namespace Fortran::runtime
57
58#endif // FORTRAN_RUNTIME_ALLOCATOR_REGISTRY_H_
Definition: allocator-registry.h:36
Definition: allocator-registry.h:24