#include "registry.hpp" #include #include #include namespace named { namespace { bool bounded(std::int64_t n, std::int64_t low, std::int64_t high) { return low <= n && n <= high; } bool name_ok(const std::string& n) { if (n.empty() || n.size() > 12 || n[0] < 'a' || n[0] > 'z') return false; for (char c : n) { if (!(c >= 'a' && c <= 'z') && !(c >= '0' && c <= '9') && c != '_') return false; } return true; } enum class Kind { OPEN, CLOSE, UNLINK, WAIT, POST, WRITE, BEGIN, STAGE, COMMIT, CANCEL, MOVE }; struct Event { Kind kind; std::int64_t a = 0, b = 0, c = 0; std::string name; }; struct Item { std::string name; ObjectInfo info; }; struct View { std::map objects; std::array handles{}; std::array actors{}; std::int64_t next = 1; std::int64_t find(const std::string& name) const { for (const auto& pair : objects) { if (pair.second.info.linked && pair.second.name == name) return pair.first; } return 0; } Code handle(std::int64_t h) const { if (!bounded(h, 0, 15)) return Code::RANGE; return handles[h] == 0 ? Code::MISSING : Code::OK; } void collect(std::int64_t id) { const auto& o = objects.at(id).info; if (o.handles == 0 && !o.linked) objects.erase(id); } }; View replay(const std::vector& history) { View v; for (const auto& e : history) { switch (e.kind) { case Kind::OPEN: if (v.objects.count(e.b) == 0) { Item item; item.name = e.name; item.info.identity = e.b; item.info.permits = e.c; item.info.linked = 1; v.objects.emplace(e.b, item); v.next = e.b + 1; } v.handles[e.a] = e.b; v.objects.at(e.b).info.handles++; break; case Kind::CLOSE: { auto id = v.handles[e.a]; v.handles[e.a] = 0; v.objects.at(id).info.handles--; v.collect(id); break; } case Kind::UNLINK: v.objects.at(e.a).info.linked = 0; v.collect(e.a); break; case Kind::WAIT: v.objects.at(e.a).info.permits -= e.b; break; case Kind::POST: v.objects.at(e.a).info.permits += e.b; break; case Kind::WRITE: v.objects.at(e.a).info.balance = e.b; break; case Kind::BEGIN: v.actors[e.a] = LeaseInfo{1, e.b, e.c, 0}; v.objects.at(e.c).info.permits--; v.objects.at(e.c).info.leases++; break; case Kind::STAGE: v.actors[e.a].pending += e.b; break; case Kind::COMMIT: case Kind::CANCEL: { auto& lease = v.actors[e.a]; auto& object = v.objects.at(lease.identity).info; if (e.kind == Kind::COMMIT) { object.balance += lease.pending; object.commits++; } object.permits++; object.leases--; lease = LeaseInfo{}; break; } case Kind::MOVE: v.objects.at(e.a).info.permits -= e.c; v.objects.at(e.b).info.permits += e.c; break; } } return v; } } const char* spelling(Code c) { switch (c) { case Code::OK: return "OK"; case Code::RANGE: return "RANGE"; case Code::NAME: return "NAME"; case Code::BUSY: return "BUSY"; case Code::MISSING: return "MISSING"; case Code::EXISTS: return "EXISTS"; case Code::FULL: return "FULL"; case Code::WAIT: return "WAIT"; } return "RANGE"; } struct Registry::Impl { std::vector history; void append(Kind k, std::int64_t a = 0, std::int64_t b = 0, std::int64_t c = 0, const std::string& n = {}) { history.push_back(Event{k, a, b, c, n}); } }; Registry::Registry() : impl(new Impl) {} Registry::~Registry() = default; Code Registry::fresh() { impl->history.clear(); return Code::OK; } Code Registry::open(std::int64_t h, const std::string& n, std::int64_t m, std::int64_t initial) { if (!bounded(h, 0, 15) || !bounded(m, 0, 2) || !bounded(initial, 0, 8)) return Code::RANGE; if (!name_ok(n)) return Code::NAME; auto v = replay(impl->history); if (v.handles[h]) return Code::BUSY; auto id = v.find(n); if (id && m == 2) return Code::EXISTS; if (!id && m == 0) return Code::MISSING; if (!id && v.objects.size() == 8) return Code::FULL; impl->append(Kind::OPEN, h, id ? id : v.next, initial, n); return Code::OK; } Code Registry::duplicate(std::int64_t source, std::int64_t target) { if (!bounded(source, 0, 15) || !bounded(target, 0, 15)) return Code::RANGE; auto v = replay(impl->history); auto code = v.handle(source); if (code != Code::OK) return code; if (v.handles[target]) return Code::BUSY; impl->append(Kind::OPEN, target, v.handles[source]); return Code::OK; } Code Registry::close(std::int64_t h) { auto v = replay(impl->history); auto code = v.handle(h); if (code != Code::OK) return code; for (const auto& t : v.actors) if (t.active && t.handle == h) return Code::BUSY; impl->append(Kind::CLOSE, h); return Code::OK; } Code Registry::unlink(const std::string& n) { if (!name_ok(n)) return Code::NAME; auto v = replay(impl->history); auto id = v.find(n); if (!id) return Code::MISSING; impl->append(Kind::UNLINK, id); return Code::OK; } Code Registry::wait(std::int64_t h, std::int64_t count) { if (!bounded(h, 0, 15) || !bounded(count, 0, 8)) return Code::RANGE; auto v = replay(impl->history); auto code = v.handle(h); if (code != Code::OK) return code; auto id = v.handles[h]; if (v.objects.at(id).info.permits < count) return Code::WAIT; impl->append(Kind::WAIT, id, count); return Code::OK; } Code Registry::post(std::int64_t h, std::int64_t count) { if (!bounded(h, 0, 15) || !bounded(count, 0, 8)) return Code::RANGE; auto v = replay(impl->history); auto code = v.handle(h); if (code != Code::OK) return code; auto id = v.handles[h]; auto o = v.objects.at(id).info; if (o.permits + o.leases + count > 8) return Code::RANGE; impl->append(Kind::POST, id, count); return Code::OK; } Code Registry::write(std::int64_t h, std::int64_t amount) { if (!bounded(h, 0, 15) || !bounded(amount, -1000000000, 1000000000)) return Code::RANGE; auto v = replay(impl->history); auto code = v.handle(h); if (code != Code::OK) return code; auto id = v.handles[h]; if (v.objects.at(id).info.leases) return Code::BUSY; impl->append(Kind::WRITE, id, amount); return Code::OK; } Code Registry::move(std::int64_t s, std::int64_t t, std::int64_t n) { if (!bounded(s, 0, 15) || !bounded(t, 0, 15) || !bounded(n, 0, 8)) return Code::RANGE; auto v = replay(impl->history); auto code = v.handle(s); if (code != Code::OK) return code; code = v.handle(t); if (code != Code::OK) return code; auto a = v.handles[s], b = v.handles[t]; if (a == b) return Code::OK; const auto& first = v.objects.at(a).info; const auto& last = v.objects.at(b).info; if (first.permits < n) return Code::WAIT; if (last.permits + last.leases + n > 8) return Code::RANGE; impl->append(Kind::MOVE, a, b, n); return Code::OK; } Code Registry::begin(std::int64_t a, std::int64_t h) { if (!bounded(a, 0, 7) || !bounded(h, 0, 15)) return Code::RANGE; auto v = replay(impl->history); if (v.actors[a].active) return Code::BUSY; auto code = v.handle(h); if (code != Code::OK) return code; auto id = v.handles[h]; if (!v.objects.at(id).info.permits) return Code::WAIT; impl->append(Kind::BEGIN, a, h, id); return Code::OK; } Code Registry::stage(std::int64_t a, std::int64_t d) { if (!bounded(a, 0, 7) || !bounded(d, -100000, 100000)) return Code::RANGE; auto v = replay(impl->history); if (!v.actors[a].active) return Code::MISSING; if (!bounded(v.actors[a].pending + d, -1000000000, 1000000000)) return Code::RANGE; impl->append(Kind::STAGE, a, d); return Code::OK; } Code Registry::commit(std::int64_t a) { if (!bounded(a, 0, 7)) return Code::RANGE; auto v = replay(impl->history); const auto& t = v.actors[a]; if (!t.active) return Code::MISSING; auto total = v.objects.at(t.identity).info.balance + t.pending; if (!bounded(total, -1000000000, 1000000000)) return Code::RANGE; impl->append(Kind::COMMIT, a); return Code::OK; } Code Registry::cancel(std::int64_t a) { if (!bounded(a, 0, 7)) return Code::RANGE; auto v = replay(impl->history); if (!v.actors[a].active) return Code::MISSING; impl->append(Kind::CANCEL, a); return Code::OK; } Code Registry::info(std::int64_t h, ObjectInfo& result) const { auto v = replay(impl->history); auto code = v.handle(h); if (code != Code::OK) return code; result = v.objects.at(v.handles[h]).info; return Code::OK; } Code Registry::named(const std::string& n, ObjectInfo& result) const { if (!name_ok(n)) return Code::NAME; auto v = replay(impl->history); auto id = v.find(n); if (!id) return Code::MISSING; result = v.objects.at(id).info; return Code::OK; } Code Registry::lease(std::int64_t a, LeaseInfo& result) const { if (!bounded(a, 0, 7)) return Code::RANGE; result = replay(impl->history).actors[a]; return Code::OK; } Statistics Registry::stats() const { auto v = replay(impl->history); Statistics result; result.next_identity = v.next; result.objects = v.objects.size(); for (const auto& pair : v.objects) { const auto& o = pair.second.info; result.names += o.linked; result.handles += o.handles; result.active += o.leases; } return result; } }