6 #include "lt/lt_slot.h"
7 #include "system/error.h"
9 #define INITIAL_FRAME_BUFFER_SIZE 16
20 lt_t *lt = malloc(sizeof(lt_t));
22 throw_error(ERROR_TYPE_LIBC);
26 lt->frames = malloc(sizeof(lt_slot_t*) * 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_t *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_t *lt, void *resource, lt_destroy_t 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_t*) * 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_t *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_t *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;