100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
243 lines
8.2 KiB
C++
243 lines
8.2 KiB
C++
#include "minor_router.hpp"
|
|
#include <algorithm>
|
|
#include <set>
|
|
namespace minorio {
|
|
int Router::allocate() const
|
|
{
|
|
int fd = 3;
|
|
while (fd <= 18 && descriptors_.count(fd)) ++fd;
|
|
return fd == 19 ? -24 : fd;
|
|
}
|
|
void Router::collect()
|
|
{
|
|
std::set<unsigned> used_streams;
|
|
for (const auto &fd : descriptors_) used_streams.insert(fd.second);
|
|
for (auto it = streams_.begin(); it != streams_.end();) {
|
|
if (!used_streams.count(it->first)) it = streams_.erase(it);
|
|
else ++it;
|
|
}
|
|
std::set<unsigned> used_objects;
|
|
for (const auto &route : routes_) used_objects.insert(route.second);
|
|
for (const auto &stream : streams_) used_objects.insert(stream.second.object);
|
|
for (auto it = objects_.begin(); it != objects_.end();) {
|
|
if (!used_objects.count(it->first)) it = objects_.erase(it);
|
|
else ++it;
|
|
}
|
|
}
|
|
int Router::access(int fd, int need) const
|
|
{
|
|
auto it = descriptors_.find(fd);
|
|
if (it == descriptors_.end()) return -9;
|
|
const auto &stream = streams_.at(it->second);
|
|
if (need && !(stream.mode & need)) return -13;
|
|
return 0;
|
|
}
|
|
int Router::create(int minor, std::uint64_t capacity)
|
|
{
|
|
if (minor < 0 || minor > 7 || !capacity || capacity > 4096) return -22;
|
|
if (routes_.count(minor)) return -17;
|
|
const unsigned key = next_object_++;
|
|
objects_.emplace(key, Object{minor, ++versions_[minor], capacity, {}, true});
|
|
routes_.emplace(minor, key);
|
|
return 0;
|
|
}
|
|
int Router::remove(int minor)
|
|
{
|
|
if (minor < 0 || minor > 7) return -22;
|
|
auto it = routes_.find(minor);
|
|
if (it == routes_.end()) return -2;
|
|
objects_.at(it->second).linked = false;
|
|
routes_.erase(it);
|
|
collect();
|
|
return 0;
|
|
}
|
|
int Router::open(int minor, int mode, int append)
|
|
{
|
|
if (minor < 0 || minor > 7 || mode < 1 || mode > 3 || append < 0 || append > 1) return -22;
|
|
if (!routes_.count(minor)) return -2;
|
|
const int fd = allocate();
|
|
if (fd < 0) return fd;
|
|
const unsigned key = next_stream_++;
|
|
streams_.emplace(key, Stream{routes_.at(minor), 0, mode, append});
|
|
descriptors_.emplace(fd, key);
|
|
return fd;
|
|
}
|
|
int Router::duplicate(int fd)
|
|
{
|
|
const int error = access(fd, 0);
|
|
if (error) return error;
|
|
const int result = allocate();
|
|
if (result < 0) return result;
|
|
descriptors_.emplace(result, descriptors_.at(fd));
|
|
return result;
|
|
}
|
|
int Router::close(int fd)
|
|
{
|
|
if (!descriptors_.erase(fd)) return -9;
|
|
collect();
|
|
return 0;
|
|
}
|
|
int Router::set_append(int fd, int enabled)
|
|
{
|
|
const int error = access(fd, 0);
|
|
if (error) return error;
|
|
if (enabled != 0 && enabled != 1) return -22;
|
|
streams_.at(descriptors_.at(fd)).append = enabled;
|
|
return 0;
|
|
}
|
|
int Router::get(const Object &object, std::uint64_t at, std::uint64_t count, Bytes &out)
|
|
{
|
|
out.clear();
|
|
for (std::uint64_t i = at; i < object.bytes.size() && out.size() < count; ++i) {
|
|
out.push_back(object.bytes[static_cast<std::size_t>(i)]);
|
|
}
|
|
return static_cast<int>(out.size());
|
|
}
|
|
int Router::put(Object &object, std::uint64_t at, const Bytes &data)
|
|
{
|
|
const auto amount = std::min<std::uint64_t>(data.size(), object.limit - at);
|
|
for (std::uint64_t i = 0; i < amount; ++i) {
|
|
const auto index = static_cast<std::size_t>(at + i);
|
|
while (object.bytes.size() <= index) object.bytes.push_back(0);
|
|
object.bytes[index] = data[static_cast<std::size_t>(i)];
|
|
}
|
|
return static_cast<int>(amount);
|
|
}
|
|
int Router::read(int fd, std::uint64_t count, Bytes &out)
|
|
{
|
|
const int error = access(fd, 1);
|
|
if (error) return error;
|
|
auto &stream = streams_.at(descriptors_.at(fd));
|
|
const int amount = get(objects_.at(stream.object), stream.position, count, out);
|
|
stream.position += static_cast<unsigned>(amount);
|
|
return amount;
|
|
}
|
|
int Router::write(int fd, const Bytes &data)
|
|
{
|
|
const int error = access(fd, 2);
|
|
if (error) return error;
|
|
if (data.size() > 4096) return -22;
|
|
if (data.empty()) return 0;
|
|
auto &stream = streams_.at(descriptors_.at(fd));
|
|
auto &object = objects_.at(stream.object);
|
|
const auto at = stream.append ? object.bytes.size() : stream.position;
|
|
const int amount = put(object, at, data);
|
|
stream.position = at + static_cast<unsigned>(amount);
|
|
return amount;
|
|
}
|
|
int Router::pread(int fd, std::uint64_t offset, std::uint64_t count, Bytes &out)
|
|
{
|
|
const int error = access(fd, 1);
|
|
if (error) return error;
|
|
const auto &object = objects_.at(streams_.at(descriptors_.at(fd)).object);
|
|
if (offset > object.limit) return -22;
|
|
return get(object, offset, count, out);
|
|
}
|
|
int Router::pwrite(int fd, std::uint64_t offset, const Bytes &data)
|
|
{
|
|
const int error = access(fd, 2);
|
|
if (error) return error;
|
|
auto &object = objects_.at(streams_.at(descriptors_.at(fd)).object);
|
|
if (offset > object.limit || data.size() > 4096) return -22;
|
|
return put(object, offset, data);
|
|
}
|
|
int Router::seek(int fd, int whence, std::int64_t delta)
|
|
{
|
|
const int error = access(fd, 0);
|
|
if (error) return error;
|
|
if (whence < 0 || whence > 2) return -22;
|
|
auto &stream = streams_.at(descriptors_.at(fd));
|
|
const auto &object = objects_.at(stream.object);
|
|
const std::int64_t base = static_cast<std::int64_t>(whence == 0 ? 0 : whence == 1 ? stream.position : object.bytes.size());
|
|
if (delta < -base || delta > static_cast<std::int64_t>(object.limit) - base) return -22;
|
|
stream.position = static_cast<std::uint64_t>(base + delta);
|
|
return static_cast<int>(stream.position);
|
|
}
|
|
int Router::resize(int fd, std::uint64_t length)
|
|
{
|
|
const int error = access(fd, 2);
|
|
if (error) return error;
|
|
auto &object = objects_.at(streams_.at(descriptors_.at(fd)).object);
|
|
if (length > object.limit) return -22;
|
|
while (object.bytes.size() > length) object.bytes.pop_back();
|
|
while (object.bytes.size() < length) object.bytes.push_back(0);
|
|
return 0;
|
|
}
|
|
int Router::clear(int fd)
|
|
{
|
|
const int error = access(fd, 2);
|
|
if (error) return error;
|
|
objects_.at(streams_.at(descriptors_.at(fd)).object).bytes = {};
|
|
return 0;
|
|
}
|
|
DeviceInfo Router::info(unsigned key) const
|
|
{
|
|
const auto &object = objects_.at(key);
|
|
DeviceInfo value{object.minor, object.generation, object.limit, object.bytes.size(), 0, 0};
|
|
for (const auto &stream : streams_) if (stream.second.object == key) ++value.sessions;
|
|
for (const auto &fd : descriptors_) if (streams_.at(fd.second).object == key) ++value.handles;
|
|
return value;
|
|
}
|
|
SessionInfo Router::info(int fd) const
|
|
{
|
|
const unsigned key = descriptors_.at(fd);
|
|
const auto &stream = streams_.at(key);
|
|
const auto &object = objects_.at(stream.object);
|
|
std::uint64_t aliases = 0;
|
|
for (const auto &entry : descriptors_) if (entry.second == key) ++aliases;
|
|
return {fd, object.minor, object.generation, object.limit, object.bytes.size(), stream.position,
|
|
stream.mode, stream.append, object.linked ? 1 : 0, aliases};
|
|
}
|
|
int Router::device(int minor, DeviceInfo &out) const
|
|
{
|
|
if (minor < 0 || minor > 7) return -22;
|
|
if (!routes_.count(minor)) return -2;
|
|
out = info(routes_.at(minor));
|
|
return 0;
|
|
}
|
|
int Router::session(int fd, SessionInfo &out) const
|
|
{
|
|
const int error = access(fd, 0);
|
|
if (error) return error;
|
|
out = info(fd);
|
|
return 0;
|
|
}
|
|
int Router::peek(int minor, std::uint64_t offset, std::uint64_t count, Bytes &out) const
|
|
{
|
|
if (minor < 0 || minor > 7) return -22;
|
|
if (!routes_.count(minor)) return -2;
|
|
const auto &object = objects_.at(routes_.at(minor));
|
|
if (offset > object.limit) return -22;
|
|
return get(object, offset, count, out);
|
|
}
|
|
void Router::devices(std::vector<DeviceInfo> &out) const
|
|
{
|
|
out.clear();
|
|
for (const auto &route : routes_) out.push_back(info(route.second));
|
|
}
|
|
void Router::sessions(std::vector<SessionInfo> &out) const
|
|
{
|
|
out.clear();
|
|
for (const auto &fd : descriptors_) out.push_back(info(fd.first));
|
|
}
|
|
Counts Router::counts() const
|
|
{
|
|
Counts result{routes_.size(), objects_.size() - routes_.size(), streams_.size(), descriptors_.size(), 0, 0};
|
|
for (const auto &object : objects_) {
|
|
result.bytes += object.second.bytes.size();
|
|
result.capacity += object.second.limit;
|
|
}
|
|
return result;
|
|
}
|
|
void Router::reset()
|
|
{
|
|
descriptors_.clear();
|
|
streams_.clear();
|
|
routes_.clear();
|
|
objects_.clear();
|
|
versions_.clear();
|
|
next_object_ = 1;
|
|
next_stream_ = 1;
|
|
}
|
|
}
|