2020-01-17 05:51:25 +08:00
|
|
|
//===-- runtime/memory.h ----------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Thin wrapper around malloc()/free() to isolate the dependency,
|
|
|
|
// ease porting, and provide an owning pointer.
|
|
|
|
|
|
|
|
#ifndef FORTRAN_RUNTIME_MEMORY_H_
|
|
|
|
#define FORTRAN_RUNTIME_MEMORY_H_
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace Fortran::runtime {
|
|
|
|
|
|
|
|
class Terminator;
|
|
|
|
|
2020-02-05 08:55:45 +08:00
|
|
|
[[nodiscard]] void *AllocateMemoryOrCrash(
|
|
|
|
const Terminator &, std::size_t bytes);
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A>[[nodiscard]] A &AllocateOrCrash(const Terminator &t) {
|
2020-01-17 05:51:25 +08:00
|
|
|
return *reinterpret_cast<A *>(AllocateMemoryOrCrash(t, sizeof(A)));
|
|
|
|
}
|
|
|
|
void FreeMemory(void *);
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> void FreeMemory(A *p) {
|
2020-01-24 08:59:27 +08:00
|
|
|
FreeMemory(reinterpret_cast<void *>(p));
|
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> void FreeMemoryAndNullify(A *&p) {
|
2020-01-24 08:59:27 +08:00
|
|
|
FreeMemory(p);
|
|
|
|
p = nullptr;
|
|
|
|
}
|
2020-01-17 05:51:25 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> struct New {
|
|
|
|
template <typename... X>
|
2020-02-05 08:55:45 +08:00
|
|
|
[[nodiscard]] A &operator()(const Terminator &terminator, X &&... x) {
|
2020-01-24 08:10:00 +08:00
|
|
|
return *new (AllocateMemoryOrCrash(terminator, sizeof(A)))
|
|
|
|
A{std::forward<X>(x)...};
|
2020-01-17 05:51:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> struct OwningPtrDeleter {
|
2020-01-17 05:51:25 +08:00
|
|
|
void operator()(A *p) { FreeMemory(p); }
|
|
|
|
};
|
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> using OwningPtr = std::unique_ptr<A, OwningPtrDeleter<A>>;
|
2020-01-24 08:59:27 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename A> struct Allocator {
|
2020-01-24 08:59:27 +08:00
|
|
|
using value_type = A;
|
2020-02-05 08:55:45 +08:00
|
|
|
explicit Allocator(const Terminator &t) : terminator{t} {}
|
2020-03-29 12:00:16 +08:00
|
|
|
template <typename B>
|
2020-01-24 08:59:27 +08:00
|
|
|
explicit constexpr Allocator(const Allocator<B> &that) noexcept
|
2020-03-29 12:00:16 +08:00
|
|
|
: terminator{that.terminator} {}
|
2020-01-24 08:59:27 +08:00
|
|
|
Allocator(const Allocator &) = default;
|
|
|
|
Allocator(Allocator &&) = default;
|
|
|
|
[[nodiscard]] constexpr A *allocate(std::size_t n) {
|
|
|
|
return reinterpret_cast<A *>(
|
|
|
|
AllocateMemoryOrCrash(terminator, n * sizeof(A)));
|
|
|
|
}
|
|
|
|
constexpr void deallocate(A *p, std::size_t) { FreeMemory(p); }
|
2020-02-05 08:55:45 +08:00
|
|
|
const Terminator &terminator;
|
2020-01-24 08:59:27 +08:00
|
|
|
};
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::runtime
|
2020-01-17 05:51:25 +08:00
|
|
|
|
2020-03-29 12:00:16 +08:00
|
|
|
#endif // FORTRAN_RUNTIME_MEMORY_H_
|