From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27, 11 canonical families), 38 skipped for scope or duplication. Each task has at least one independent contract-edge defect, boundary types rotate, and public docs state trap clauses as plain contract prose. Claude reviewed 63/63 USABLE against current digests. Difficulty is unmeasured. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
69 lines
1.7 KiB
C++
69 lines
1.7 KiB
C++
#include "internal.hpp"
|
|
namespace frames {
|
|
int index_of(const Bank* b,int h) {
|
|
if (!b || h<0 || h>=int(MAX_HANDLES)) return ARG;
|
|
if (b->handles[h]<0) return ABSENT;
|
|
return b->handles[h];
|
|
}
|
|
unsigned alias_count(const Bank& b,int object) {
|
|
unsigned count=0;
|
|
for (int entry:b.handles) {
|
|
if (entry==object) ++count;
|
|
}
|
|
return count;
|
|
}
|
|
int reset(Bank* b) {
|
|
if (!b) return ARG;
|
|
*b=Bank{};
|
|
return OK;
|
|
}
|
|
int create(Bank* b,const Attr* attr,int* out) {
|
|
if (!b || !attr || !out || !valid_attr(*attr)) return ARG;
|
|
int h=-1;
|
|
int object=-1;
|
|
for (unsigned i=0;i<MAX_HANDLES;++i) {
|
|
if (b->handles[i]<0 && h<0) h=int(i);
|
|
}
|
|
for (unsigned i=0;i<MAX_OBJECTS;++i) {
|
|
if (!b->locks[i].used && object<0) object=int(i);
|
|
}
|
|
if (h<0 || object<0) return SPACE;
|
|
Lock fresh;
|
|
fresh.used=true;
|
|
fresh.attr=*attr;
|
|
b->locks[object]=fresh;
|
|
b->handles[h]=object;
|
|
++b->creates;
|
|
++b->revision;
|
|
*out=h;
|
|
return OK;
|
|
}
|
|
int duplicate(Bank* b,int h,int* out) {
|
|
if (!out) return ARG;
|
|
int object=index_of(b,h);
|
|
if (object<0) return object;
|
|
int destination=-1;
|
|
for (unsigned i=0;i<MAX_HANDLES;++i) {
|
|
if (b->handles[i]<0 && destination<0) destination=int(i);
|
|
}
|
|
if (destination<0) return SPACE;
|
|
b->handles[destination]=object;
|
|
++b->duplicates;
|
|
++b->revision;
|
|
*out=destination;
|
|
return OK;
|
|
}
|
|
int close(Bank* b,int h) {
|
|
int object=index_of(b,h);
|
|
if (object<0) return object;
|
|
Lock& lock=b->locks[object];
|
|
bool last=alias_count(*b,object)==1;
|
|
if (last && (!lock.held.empty() || !lock.pending.empty())) return BUSY;
|
|
b->handles[h]=-1;
|
|
if (last) lock=Lock{};
|
|
++b->closes;
|
|
++b->revision;
|
|
return OK;
|
|
}
|
|
}
|