6 #include "lt/lt_slot.h"
7 #include "system/error.h"
8 #include "system/nth_alloc.h"
10 #define INITIAL_FRAME_BUFFER_SIZE 16
21 Lt *lt = nth_alloc(sizeof(Lt));
23 throw_error(ERROR_TYPE_LIBC);
24 goto nth_alloc_lt_fail;
27 lt->frames = nth_alloc(sizeof(Lt_slot*) * INITIAL_FRAME_BUFFER_SIZE);
28 if (lt->frames == NULL) {
29 throw_error(ERROR_TYPE_LIBC);
30 goto nth_alloc_lt_slots_fail;
33 lt->capacity = INITIAL_FRAME_BUFFER_SIZE;
38 nth_alloc_lt_slots_fail:
44 void destroy_lt(Lt *lt)
48 while (lt->size-- > 0) {
49 if (lt->frames[lt->size]) {
50 destroy_lt_slot(lt->frames[lt->size]);
58 void *lt_push(Lt *lt, void *resource, Lt_destroy resource_destroy)
61 assert(resource_destroy);
62 assert(lt != resource);
64 if (resource == NULL) {
68 if (lt->size >= lt->capacity) {
70 if ((lt->frames = realloc(lt->frames, sizeof(Lt_slot*) * lt->capacity)) == NULL) {
71 throw_error(ERROR_TYPE_LIBC);
76 if ((lt->frames[lt->size++] = create_lt_slot(resource, resource_destroy)) == NULL) {
83 void* lt_reset(Lt *lt, void *old_resource, void *new_resource)
88 assert(old_resource != new_resource);
90 for (size_t i = 0; i < lt->size; ++i) {
91 if (lt->frames[i] && lt_slot_contains_resource(lt->frames[i], old_resource)) {
92 lt_slot_reset_resource(lt->frames[i], new_resource);
100 void *lt_release(Lt *lt, void *resource)
105 for (size_t i = 0; i < lt->size; ++i) {
106 if (lt->frames[i] && lt_slot_contains_resource(lt->frames[i], resource)) {
107 release_lt_slot(lt->frames[i]);
108 lt->frames[i] = NULL;
116 void *lt_replace(Lt *lt, void *old_resource, void *new_resource)
119 assert(old_resource);
120 assert(new_resource);
122 for (size_t i = 0; i < lt->size; ++i) {
123 if (lt->frames[i] && lt_slot_contains_resource(lt->frames[i], old_resource)) {
124 lt_slot_replace_resource(lt->frames[i], new_resource);