[flang-commits] [flang] 98d576c - [flang] Improve API for runtime allocator (I/O runtime work part 3)
peter klausler via flang-commits
flang-commits at lists.llvm.org
Fri Jul 3 08:38:24 PDT 2020
Author: peter klausler
Date: 2020-07-03T08:37:40-07:00
New Revision: 98d576c78f88b6d7191335c9774313c191ef7539
URL: https://github.com/llvm/llvm-project/commit/98d576c78f88b6d7191335c9774313c191ef7539
DIFF: https://github.com/llvm/llvm-project/commit/98d576c78f88b6d7191335c9774313c191ef7539.diff
LOG: [flang] Improve API for runtime allocator (I/O runtime work part 3)
New<A> used to return an A&; now it returns an OwningPtr<A>
to force better ownership tracking of allocations. Its API
has also been split into New<A> and SizedNew<A> to allow
allocations with a size override.
Reviewed By: tskeith
Differential Revision: https://reviews.llvm.org/D83108
Added:
Modified:
flang/runtime/file.cpp
flang/runtime/io-api.cpp
flang/runtime/memory.h
flang/runtime/unit-map.cpp
flang/runtime/unit.cpp
Removed:
################################################################################
diff --git a/flang/runtime/file.cpp b/flang/runtime/file.cpp
index 677b9675f47a..fdd7ea64c201 100644
--- a/flang/runtime/file.cpp
+++ b/flang/runtime/file.cpp
@@ -352,7 +352,7 @@ bool OpenFile::RawSeekToEnd() {
int OpenFile::PendingResult(const Terminator &terminator, int iostat) {
int id{nextId_++};
- pending_.reset(&New<Pending>{}(terminator, id, iostat, std::move(pending_)));
+ pending_ = New<Pending>{terminator}(id, iostat, std::move(pending_));
return id;
}
} // namespace Fortran::runtime::io
diff --git a/flang/runtime/io-api.cpp b/flang/runtime/io-api.cpp
index db228e5bbf21..bbb7eb2863c6 100644
--- a/flang/runtime/io-api.cpp
+++ b/flang/runtime/io-api.cpp
@@ -28,9 +28,10 @@ Cookie BeginInternalArrayListIO(const Descriptor &descriptor,
void ** /*scratchArea*/, std::size_t /*scratchBytes*/,
const char *sourceFile, int sourceLine) {
Terminator oom{sourceFile, sourceLine};
- return &New<InternalListIoStatementState<DIR>>{}(
- oom, descriptor, sourceFile, sourceLine)
- .ioStatementState();
+ return &New<InternalListIoStatementState<DIR>>{oom}(
+ descriptor, sourceFile, sourceLine)
+ .release()
+ ->ioStatementState();
}
Cookie IONAME(BeginInternalArrayListOutput)(const Descriptor &descriptor,
@@ -52,9 +53,10 @@ Cookie BeginInternalArrayFormattedIO(const Descriptor &descriptor,
const char *format, std::size_t formatLength, void ** /*scratchArea*/,
std::size_t /*scratchBytes*/, const char *sourceFile, int sourceLine) {
Terminator oom{sourceFile, sourceLine};
- return &New<InternalFormattedIoStatementState<DIR>>{}(
- oom, descriptor, format, formatLength, sourceFile, sourceLine)
- .ioStatementState();
+ return &New<InternalFormattedIoStatementState<DIR>>{oom}(
+ descriptor, format, formatLength, sourceFile, sourceLine)
+ .release()
+ ->ioStatementState();
}
Cookie IONAME(BeginInternalArrayFormattedOutput)(const Descriptor &descriptor,
@@ -78,9 +80,10 @@ Cookie BeginInternalFormattedIO(
void ** /*scratchArea*/, std::size_t /*scratchBytes*/,
const char *sourceFile, int sourceLine) {
Terminator oom{sourceFile, sourceLine};
- return &New<InternalFormattedIoStatementState<DIR>>{}(oom, internal,
- internalLength, format, formatLength, sourceFile, sourceLine)
- .ioStatementState();
+ return &New<InternalFormattedIoStatementState<DIR>>{oom}(
+ internal, internalLength, format, formatLength, sourceFile, sourceLine)
+ .release()
+ ->ioStatementState();
}
Cookie IONAME(BeginInternalFormattedOutput)(char *internal,
@@ -234,8 +237,9 @@ Cookie IONAME(BeginClose)(
} else {
// CLOSE(UNIT=bad unit) is just a no-op
Terminator oom{sourceFile, sourceLine};
- return &New<NoopCloseStatementState>{}(oom, sourceFile, sourceLine)
- .ioStatementState();
+ return &New<NoopCloseStatementState>{oom}(sourceFile, sourceLine)
+ .release()
+ ->ioStatementState();
}
}
diff --git a/flang/runtime/memory.h b/flang/runtime/memory.h
index b8e84952a99f..f21b237f3905 100644
--- a/flang/runtime/memory.h
+++ b/flang/runtime/memory.h
@@ -32,20 +32,32 @@ template <typename A> void FreeMemoryAndNullify(A *&p) {
p = nullptr;
}
-template <typename A> struct New {
- template <typename... X>
- [[nodiscard]] A &operator()(const Terminator &terminator, X &&... x) {
- return *new (AllocateMemoryOrCrash(terminator, sizeof(A)))
- A{std::forward<X>(x)...};
- }
-};
-
template <typename A> struct OwningPtrDeleter {
void operator()(A *p) { FreeMemory(p); }
};
template <typename A> using OwningPtr = std::unique_ptr<A, OwningPtrDeleter<A>>;
+template <typename A> class SizedNew {
+public:
+ explicit SizedNew(const Terminator &terminator) : terminator_{terminator} {}
+ template <typename... X>
+ [[nodiscard]] OwningPtr<A> operator()(std::size_t bytes, X &&... x) {
+ return OwningPtr<A>{new (AllocateMemoryOrCrash(terminator_, bytes))
+ A{std::forward<X>(x)...}};
+ }
+
+private:
+ const Terminator &terminator_;
+};
+
+template <typename A> struct New : public SizedNew<A> {
+ using SizedNew<A>::SizedNew;
+ template <typename... X> [[nodiscard]] OwningPtr<A> operator()(X &&... x) {
+ return SizedNew<A>::operator()(sizeof(A), std::forward<X>(x)...);
+ }
+};
+
template <typename A> struct Allocator {
using value_type = A;
explicit Allocator(const Terminator &t) : terminator{t} {}
diff --git a/flang/runtime/unit-map.cpp b/flang/runtime/unit-map.cpp
index 4e58cf590c2a..5cbbf059d5f8 100644
--- a/flang/runtime/unit-map.cpp
+++ b/flang/runtime/unit-map.cpp
@@ -64,7 +64,7 @@ void UnitMap::CloseAll(IoErrorHandler &handler) {
}
ExternalFileUnit &UnitMap::Create(int n, const Terminator &terminator) {
- Chain &chain{New<Chain>{}(terminator, n)};
+ Chain &chain{*New<Chain>{terminator}(n).release()};
chain.next.reset(&chain);
bucket_[Hash(n)].swap(chain.next); // pushes new node as list head
return chain.unit;
diff --git a/flang/runtime/unit.cpp b/flang/runtime/unit.cpp
index 81035ab6fcde..2eee142081a5 100644
--- a/flang/runtime/unit.cpp
+++ b/flang/runtime/unit.cpp
@@ -95,7 +95,7 @@ UnitMap &ExternalFileUnit::GetUnitMap() {
return *unitMap;
}
Terminator terminator{__FILE__, __LINE__};
- unitMap = &New<UnitMap>{}(terminator);
+ unitMap = New<UnitMap>{terminator}().release();
ExternalFileUnit &out{ExternalFileUnit::LookUpOrCreate(6, terminator)};
out.Predefine(1);
out.set_mayRead(false);
More information about the flang-commits
mailing list