1//===-- runtime/unit-map.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 "unit-map.h"
10
11namespace Fortran::runtime::io {
12
13void UnitMap::Initialize() {
14 if (!isInitialized_) {
15 freeNewUnits_.InitializeState();
16 // Unit number -1 is reserved.
17 // The unit numbers are pushed in reverse order so that the first
18 // ones to be popped will be small and suitable for use as kind=1
19 // integers.
20 for (int j{freeNewUnits_.maxValue}; j > 1; --j) {
21 freeNewUnits_.Add(j);
22 }
23 isInitialized_ = true;
24 }
25}
26
27// See 12.5.6.12 in Fortran 2018. NEWUNIT= unit numbers are negative,
28// and not equal to -1 (or ERROR_UNIT, if it were negative, which it isn't.)
29ExternalFileUnit &UnitMap::NewUnit(const Terminator &terminator) {
30 CriticalSection critical{lock_};
31 Initialize();
32 std::optional<int> n{freeNewUnits_.PopValue()};
33 if (!n) {
34 n = emergencyNewUnit_++;
35 }
36 return Create(-*n, terminator);
37}
38
39ExternalFileUnit *UnitMap::LookUpForClose(int n) {
40 CriticalSection critical{lock_};
41 Chain *previous{nullptr};
42 int hash{Hash(n)};
43 for (Chain *p{bucket_[hash].get()}; p; previous = p, p = p->next.get()) {
44 if (p->unit.unitNumber() == n) {
45 if (previous) {
46 previous->next.swap(p->next);
47 } else {
48 bucket_[hash].swap(p->next);
49 }
50 // p->next.get() == p at this point; the next swap pushes p on closing_
51 closing_.swap(p->next);
52 return &p->unit;
53 }
54 }
55 return nullptr;
56}
57
58void UnitMap::DestroyClosed(ExternalFileUnit &unit) {
59 Chain *p{nullptr};
60 {
61 CriticalSection critical{lock_};
62 Chain *previous{nullptr};
63 for (p = closing_.get(); p; previous = p, p = p->next.get()) {
64 if (&p->unit == &unit) {
65 int n{unit.unitNumber()};
66 if (n <= -2) {
67 freeNewUnits_.Add(-n);
68 }
69 if (previous) {
70 previous->next.swap(p->next);
71 } else {
72 closing_.swap(p->next);
73 }
74 break;
75 }
76 }
77 }
78 if (p) {
79 p->unit.~ExternalFileUnit();
80 FreeMemory(p);
81 }
82}
83
84void UnitMap::CloseAll(IoErrorHandler &handler) {
85 // Extract units from the map so they can be closed
86 // without holding lock_.
87 OwningPtr<Chain> closeList;
88 {
89 CriticalSection critical{lock_};
90 for (int j{0}; j < buckets_; ++j) {
91 while (Chain * p{bucket_[j].get()}) {
92 bucket_[j].swap(p->next); // pops p from head of bucket list
93 closeList.swap(p->next); // pushes p to closeList
94 }
95 }
96 }
97 while (Chain * p{closeList.get()}) {
98 closeList.swap(p->next); // pops p from head of closeList
99 p->unit.CloseUnit(CloseStatus::Keep, handler);
100 p->unit.~ExternalFileUnit();
101 FreeMemory(p);
102 }
103}
104
105void UnitMap::FlushAll(IoErrorHandler &handler) {
106 CriticalSection critical{lock_};
107 for (int j{0}; j < buckets_; ++j) {
108 for (Chain *p{bucket_[j].get()}; p; p = p->next.get()) {
109 p->unit.FlushOutput(handler);
110 }
111 }
112}
113
114ExternalFileUnit *UnitMap::Find(const char *path, std::size_t pathLen) {
115 if (path) {
116 // TODO: Faster data structure
117 for (int j{0}; j < buckets_; ++j) {
118 for (Chain *p{bucket_[j].get()}; p; p = p->next.get()) {
119 if (p->unit.path() && p->unit.pathLength() == pathLen &&
120 std::memcmp(s1: p->unit.path(), s2: path, n: pathLen) == 0) {
121 return &p->unit;
122 }
123 }
124 }
125 }
126 return nullptr;
127}
128
129ExternalFileUnit &UnitMap::Create(int n, const Terminator &terminator) {
130 Chain &chain{*New<Chain>{terminator}(n).release()};
131 chain.next.reset(&chain);
132 bucket_[Hash(n)].swap(chain.next); // pushes new node as list head
133 return chain.unit;
134}
135
136} // namespace Fortran::runtime::io
137

source code of flang/runtime/unit-map.cpp