#include "registry.hpp" #include #include namespace named { namespace { constexpr std::int64_t balance_limit = 1000000000; constexpr std::int64_t delta_limit = 100000; constexpr std::int64_t permit_limit = 8; bool within(std::int64_t value, std::int64_t low, std::int64_t high) { return value >= low && value <= high; } bool proper_name(const std::string& name) { if (name.empty() || name.size() > 12) { return false; } if (name.front() < 'a' || name.front() > 'z') { return false; } for (char character : name) { bool letter = character >= 'a' && character <= 'z'; bool digit = character >= '0' && character <= '9'; if (!letter && !digit && character != '_') { return false; } } return true; } struct Object { bool used = false; bool linked = false; std::string name; std::int64_t identity = 0; std::int64_t permits = 0; std::int64_t balance = 0; std::int64_t handles = 0; std::int64_t leases = 0; std::int64_t commits = 0; }; struct Transaction { bool active = false; std::int64_t handle = -1; int object = -1; std::int64_t pending = 0; }; } const char* spelling(Code code) { switch (code) { 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::array objects{}; std::array handles{}; std::array transactions{}; std::int64_t next = 1; Impl() { handles.fill(-1); } int lookup(const std::string& name) const { for (int index = 0; index < 8; ++index) { const auto& object = objects[index]; if (object.used && object.linked && object.name == name) { return index; } } return -1; } int available() const { for (int index = 0; index < 8; ++index) { if (!objects[index].used) { return index; } } return -1; } Code handle_code(std::int64_t handle) const { if (!within(handle, 0, 15)) { return Code::RANGE; } if (handles[handle] == -1) { return Code::MISSING; } return Code::OK; } Object& object_at(std::int64_t handle) { return objects[handles[handle]]; } const Object& object_at(std::int64_t handle) const { return objects[handles[handle]]; } void collect(int index) { auto& object = objects[index]; if (!object.linked && object.handles == 0) { object = Object{}; } } void return_permit(Transaction& transaction) { auto& object = objects[transaction.object]; object.permits += 1; object.leases -= 1; transaction = Transaction{}; } }; Registry::Registry() : impl(new Impl) { } Registry::~Registry() = default; Code Registry::fresh() { impl.reset(new Impl); return Code::OK; } Code Registry::open(std::int64_t handle, const std::string& name, std::int64_t mode, std::int64_t initial) { if (!within(handle, 0, 15)) { return Code::RANGE; } if (!within(mode, 0, 2)) { return Code::RANGE; } if (!within(initial, 0, permit_limit)) { return Code::RANGE; } if (!proper_name(name)) { return Code::NAME; } if (impl->handles[handle] != -1) { return Code::BUSY; } int index = impl->lookup(name); if (index != -1 && mode == 2) { return Code::EXISTS; } if (index == -1 && mode == 0) { return Code::MISSING; } if (index == -1) { index = impl->available(); if (index == -1) { return Code::FULL; } Object object; object.used = true; object.linked = true; object.name = name; object.identity = impl->next; object.permits = initial; impl->objects[index] = std::move(object); impl->next += 1; } impl->handles[handle] = index; impl->objects[index].handles += 1; return Code::OK; } Code Registry::close(std::int64_t handle) { Code code = impl->handle_code(handle); if (code != Code::OK) { return code; } for (const auto& transaction : impl->transactions) { if (transaction.active && transaction.handle == handle) { return Code::BUSY; } } int index = impl->handles[handle]; impl->handles[handle] = -1; impl->objects[index].handles -= 1; impl->collect(index); return Code::OK; } Code Registry::unlink(const std::string& name) { if (!proper_name(name)) { return Code::NAME; } int index = impl->lookup(name); if (index == -1) { return Code::MISSING; } impl->objects[index].linked = false; impl->collect(index); return Code::OK; } Code Registry::duplicate(std::int64_t source, std::int64_t target) { if (!within(source, 0, 15) || !within(target, 0, 15)) { return Code::RANGE; } Code code = impl->handle_code(source); if (code != Code::OK) { return code; } if (impl->handles[target] != -1) { return Code::BUSY; } int index = impl->handles[source]; impl->handles[target] = index; impl->objects[index].handles += 1; return Code::OK; } Code Registry::wait(std::int64_t handle, std::int64_t count) { if (!within(handle, 0, 15) || !within(count, 0, permit_limit)) { return Code::RANGE; } Code code = impl->handle_code(handle); if (code != Code::OK) { return code; } auto& object = impl->object_at(handle); if (object.permits < count) { return Code::WAIT; } object.permits -= count; return Code::OK; } Code Registry::post(std::int64_t handle, std::int64_t count) { if (!within(handle, 0, 15) || !within(count, 0, permit_limit)) { return Code::RANGE; } Code code = impl->handle_code(handle); if (code != Code::OK) { return code; } auto& object = impl->object_at(handle); if (object.permits + object.leases + count > permit_limit) { return Code::RANGE; } object.permits += count; return Code::OK; } Code Registry::write(std::int64_t handle, std::int64_t value) { if (!within(handle, 0, 15) || !within(value, -balance_limit, balance_limit)) { return Code::RANGE; } Code code = impl->handle_code(handle); if (code != Code::OK) { return code; } auto& object = impl->object_at(handle); if (object.leases != 0) { return Code::BUSY; } object.balance = value; return Code::OK; } Code Registry::move(std::int64_t source, std::int64_t target, std::int64_t count) { if (!within(source, 0, 15)) { return Code::RANGE; } if (!within(target, 0, 15)) { return Code::RANGE; } if (!within(count, 0, permit_limit)) { return Code::RANGE; } Code code = impl->handle_code(source); if (code != Code::OK) { return code; } code = impl->handle_code(target); if (code != Code::OK) { return code; } if (impl->handles[source] == impl->handles[target]) { return Code::OK; } auto& origin = impl->object_at(source); auto& destination = impl->object_at(target); if (origin.permits < count) { return Code::WAIT; } if (destination.permits + destination.leases + count > permit_limit) { return Code::RANGE; } origin.permits -= count; destination.permits += count; return Code::OK; } Code Registry::begin(std::int64_t actor, std::int64_t handle) { if (!within(actor, 0, 7) || !within(handle, 0, 15)) { return Code::RANGE; } auto& transaction = impl->transactions[actor]; if (transaction.active) { return Code::BUSY; } Code code = impl->handle_code(handle); if (code != Code::OK) { return code; } auto& object = impl->object_at(handle); if (object.permits == 0) { return Code::WAIT; } transaction.active = true; transaction.handle = handle; transaction.object = impl->handles[handle]; transaction.pending = 0; object.permits -= 1; object.leases += 1; return Code::OK; } Code Registry::stage(std::int64_t actor, std::int64_t delta) { if (!within(actor, 0, 7) || !within(delta, -delta_limit, delta_limit)) { return Code::RANGE; } auto& transaction = impl->transactions[actor]; if (!transaction.active) { return Code::MISSING; } std::int64_t pending = transaction.pending + delta; if (!within(pending, -balance_limit, balance_limit)) { return Code::RANGE; } transaction.pending = pending; return Code::OK; } Code Registry::commit(std::int64_t actor) { if (!within(actor, 0, 7)) { return Code::RANGE; } auto& transaction = impl->transactions[actor]; if (!transaction.active) { return Code::MISSING; } auto& object = impl->objects[transaction.object]; std::int64_t balance = object.balance + transaction.pending; if (!within(balance, -balance_limit, balance_limit)) { return Code::RANGE; } object.balance = balance; object.commits += 1; impl->return_permit(transaction); return Code::OK; } Code Registry::cancel(std::int64_t actor) { if (!within(actor, 0, 7)) { return Code::RANGE; } auto& transaction = impl->transactions[actor]; if (!transaction.active) { return Code::MISSING; } impl->return_permit(transaction); return Code::OK; } Code Registry::info(std::int64_t handle, ObjectInfo& result) const { Code code = impl->handle_code(handle); if (code != Code::OK) { return code; } const auto& object = impl->object_at(handle); result.identity = object.identity; result.permits = object.permits; result.balance = object.balance; result.handles = object.handles; result.linked = object.linked ? 1 : 0; result.leases = object.leases; result.commits = object.commits; return Code::OK; } Code Registry::lease(std::int64_t actor, LeaseInfo& result) const { if (!within(actor, 0, 7)) { return Code::RANGE; } const auto& transaction = impl->transactions[actor]; result = LeaseInfo{}; if (transaction.active) { result.active = 1; result.handle = transaction.handle; result.identity = impl->objects[transaction.object].identity; result.pending = transaction.pending; } return Code::OK; } Code Registry::named(const std::string& name, ObjectInfo& result) const { if (!proper_name(name)) { return Code::NAME; } int index = impl->lookup(name); if (index == -1) { return Code::MISSING; } const auto& object = impl->objects[index]; result.identity = object.identity; result.permits = object.permits; result.balance = object.balance; result.handles = object.handles; result.linked = object.linked ? 1 : 0; result.leases = object.leases; result.commits = object.commits; return Code::OK; } Statistics Registry::stats() const { Statistics result; result.next_identity = impl->next; for (const auto& object : impl->objects) { if (object.used) { result.objects += 1; result.names += object.linked ? 1 : 0; result.handles += object.handles; result.active += object.leases; } } return result; } }