#pragma once #include #define HANDLE_MAX 16 typedef struct { uint64_t refcount; Handle *h[HANDLE_MAX]; } HandleSet; /** initialize HandleSets with a refcount of 1 */ HandleSet *hs_init(void); HandleSet *hs_copy(HandleSet *from); void hs_unref(HandleSet *hs); /** finds the first free handle >= start. returns -1 on failure */ hid_t hs_findfree(HandleSet *hs, hid_t start); Handle *hs_get(HandleSet *hs, hid_t id); /** creates a new handle. returns -1 on failure */ hid_t hs_hinit(HandleSet *hs, enum handle_type type, Handle **hp); hid_t hs_dup(HandleSet *hs, hid_t from, hid_t to, int flags); static inline void hs_close(HandleSet *hs, hid_t hid) { hs_dup(hs, -1, hid, 0); } /** Gets a handle and removes the set's reference to it, without decreasing * the refcount. Meant to be used together with hs_put. */ Handle *hs_take(HandleSet *hs, hid_t hid); /* Put a handle into a set, taking the ownership away from the caller. * Doesn't increase the refcount on success, decreases it on failure. */ hid_t hs_put(HandleSet *hs, Handle *h);