6 #include "./lt/lt_slot.h"
8 #define INITIAL_FRAME_BUFFER_SIZE 16
19 lt_t *lt = malloc(sizeof(lt_t));
21 throw_error(ERROR_TYPE_LIBC);
25 lt->frames = malloc(sizeof(lt_slot_t*) * INITIAL_FRAME_BUFFER_SIZE);
26 if (lt->frames == NULL) {
27 throw_error(ERROR_TYPE_LIBC);
28 goto malloc_lt_slots_fail;
31 lt->capacity = INITIAL_FRAME_BUFFER_SIZE;
42 void destroy_lt(lt_t *lt)
46 while (lt->size-- > 0) {
47 if (lt->frames[lt->size]) {
48 destroy_lt_slot(lt->frames[lt->size]);
56 void *lt_push(lt_t *lt, void *resource, lt_destroy_t resource_destroy)
59 assert(resource_destroy);
60 assert(lt != resource);
62 if (resource == NULL) {
66 if (lt->size >= lt->capacity) {
68 if ((lt->frames = realloc(lt->frames, sizeof(lt_slot_t*) * lt->capacity)) == NULL) {
69 throw_error(ERROR_TYPE_LIBC);
74 if ((lt->frames[lt->size++] = create_lt_slot(resource, resource_destroy)) == NULL) {
81 void* lt_reset(lt_t *lt, void *old_resource, void *new_resource)
86 assert(old_resource != new_resource);
88 for (size_t i = 0; i < lt->size; ++i) {
89 if (lt->frames[i] && lt_slot_contains_resource(lt->frames[i], old_resource)) {
90 lt_slot_reset_resource(lt->frames[i], new_resource);
98 void *lt_release(lt_t *lt, void *resource)
103 for (size_t i = 0; i < lt->size; ++i) {
104 if (lt->frames[i] && lt_slot_contains_resource(lt->frames[i], resource)) {
105 release_lt_slot(lt->frames[i]);
106 lt->frames[i] = NULL;