#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;ihandles[i]<0 && h<0) h=int(i); } for (unsigned i=0;ilocks[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;ihandles[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; } }