7 #include "system/stacktrace.h"
9 #define LT_INITIAL_CAPACITY 8
11 typedef void (*Dtor)(void*);
24 static inline Lt *create_lt(void)
26 return calloc(1, sizeof(Lt));
29 static inline void destroy_lt(Lt *lt)
34 for (Slot *p = lt->slots_end - 1; p >= lt->slots; --p) {
48 #define PUSH_LT(lt, res, dtor) \
49 lt_push(lt, (void*)res, (Dtor)dtor)
51 static inline void *lt_push(Lt *lt, void *res, Dtor dtor)
54 size_t size = (size_t)(lt->slots_end - lt->slots);
55 if (size >= lt->capacity) {
56 if (lt->capacity == 0) {
57 lt->capacity = LT_INITIAL_CAPACITY;
58 lt->slots = calloc(LT_INITIAL_CAPACITY, sizeof(Slot));
59 lt->slots_end = lt->slots;
62 lt->slots = realloc(lt->slots, lt->capacity * sizeof(Slot));
63 lt->slots_end = lt->slots + size;
67 lt->slots_end->res = res;
68 lt->slots_end->dtor = dtor;
74 #define RETURN_LT(lt, result) \
75 return (destroy_lt(lt), result)
77 #define RETURN_LT0(lt) \
83 #define RESET_LT(lt, old_res, new_res) \
84 lt_reset(lt, (void*) old_res, (void*) new_res)
86 static inline void *lt_reset(Lt *lt, void *old_res, void *new_res)
89 trace_assert(old_res != new_res);
91 for(Slot *p = lt->slots; p < lt->slots_end; ++p) {
92 if (p->res == old_res) {
99 trace_assert(0 && "Resource was not found");
104 #define REPLACE_LT(lt, old_res, new_res) \
105 lt_replace(lt, (void *)old_res, (void*)new_res)
107 static inline void *lt_replace(Lt *lt, void *old_res, void *new_res)
110 for(Slot *p = lt->slots; p < lt->slots_end; ++p) {
111 if (p->res == old_res) {
117 trace_assert(0 && "Resource was not found");
121 #define RELEASE_LT(lt, res) \
122 lt_release(lt, (void*)res)
124 static inline void *lt_release(Lt *lt, void *res)
127 for(Slot *p = lt->slots; p < lt->slots_end; ++p) {
129 memmove(p, p + 1, (size_t)(lt->slots_end - p - 1) * sizeof(Slot));
135 trace_assert(0 && "Resource was not found");