6 #include "lt/lt_slot.h"
7 #include "system/error.h"
9 #define INITIAL_FRAME_BUFFER_SIZE 16
20 Lt *lt = malloc(sizeof(Lt));
22 throw_error(ERROR_TYPE_LIBC);
26 lt->frames = malloc(sizeof(Lt_slot*) * INITIAL_FRAME_BUFFER_SIZE);
27 if (lt->frames == NULL) {
28 throw_error(ERROR_TYPE_LIBC);
29 goto malloc_lt_slots_fail;
32 lt->capacity = INITIAL_FRAME_BUFFER_SIZE;
43 void destroy_lt(Lt *lt)
47 while (lt->size-- > 0) {
48 if (lt->frames[lt->size]) {
49 destroy_lt_slot(lt->frames[lt->size]);
57 void *lt_push(Lt *lt, void *resource, Lt_destroy resource_destroy)
60 assert(resource_destroy);
61 assert(lt != resource);
63 if (resource == NULL) {
67 if (lt->size >= lt->capacity) {
69 if ((lt->frames = realloc(lt->frames, sizeof(Lt_slot*) * lt->capacity)) == NULL) {
70 throw_error(ERROR_TYPE_LIBC);
75 if ((lt->frames[lt->size++] = create_lt_slot(resource, resource_destroy)) == NULL) {
82 void* lt_reset(Lt *lt, void *old_resource, void *new_resource)
87 assert(old_resource != new_resource);
89 for (size_t i = 0; i < lt->size; ++i) {
90 if (lt->frames[i] && lt_slot_contains_resource(lt->frames[i], old_resource)) {
91 lt_slot_reset_resource(lt->frames[i], new_resource);
99 void *lt_release(Lt *lt, void *resource)
104 for (size_t i = 0; i < lt->size; ++i) {
105 if (lt->frames[i] && lt_slot_contains_resource(lt->frames[i], resource)) {
106 release_lt_slot(lt->frames[i]);
107 lt->frames[i] = NULL;
115 void *lt_replace(Lt *lt, void *old_resource, void *new_resource)
118 assert(old_resource);
119 assert(new_resource);
121 for (size_t i = 0; i < lt->size; ++i) {
122 if (lt->frames[i] && lt_slot_contains_resource(lt->frames[i], old_resource)) {
123 lt_slot_replace_resource(lt->frames[i], new_resource);