Handle reallocating when out of callback capacity
This commit is contained in:
parent
44ec7317a1
commit
07f96fe511
@ -54,6 +54,7 @@ internal void *alloc(u64 size);
|
||||
internal ESInternal *get_event_system_internal(EventSystem event_system);
|
||||
internal EventSlot *get_event_slots_array(const ESInternal *es);
|
||||
internal u64 *get_free_array(const ESInternal *es);
|
||||
internal void reallocate_system(EventSystem *event_system, ESInternal *es);
|
||||
internal void copy_event_data(const ESInternal *src, ESInternal *dst);
|
||||
internal EventInternal *get_event(const ESInternal *es, Event event);
|
||||
internal EventInternal *get_event_from_offset(const ESInternal *es, u64 offset);
|
||||
@ -172,18 +173,7 @@ Event es_register_event(EventSystem *event_system) {
|
||||
}
|
||||
|
||||
if (es->event_count >= es->event_capacity) {
|
||||
u64 new_callback_capacity = get_largest_capacity(es);
|
||||
EventSystem event_system_new = es_init(es->event_capacity, new_callback_capacity);
|
||||
ESInternal *es_new = get_event_system_internal(event_system_new);
|
||||
|
||||
es_new->id = es->id;
|
||||
es_new->event_count = es->event_count;
|
||||
|
||||
copy_event_data(es, es_new);
|
||||
|
||||
es_deinit(event_system);
|
||||
event_system->id = (u64)es_new;
|
||||
es = es_new;
|
||||
reallocate_system(event_system, es);
|
||||
}
|
||||
|
||||
event.id = ++(es->event_count);
|
||||
@ -207,9 +197,13 @@ void es_deregister_event(EventSystem event_system, Event *event) {
|
||||
|
||||
EventSlot *event_slots_array = get_event_slots_array(es);
|
||||
u64 *free_array = get_free_array(es);
|
||||
EventInternal *ev = get_event(es, *event);
|
||||
|
||||
event_slots_array[event->id].registered = false;
|
||||
free_array[(es->free_count)++] = event->id;
|
||||
free_array[(es->free_count)++] = event->id;
|
||||
|
||||
ev->count = 0;
|
||||
ev->free_count = 0;
|
||||
|
||||
if (event->id == es->event_count) {
|
||||
es->event_count -= 1;
|
||||
@ -245,8 +239,8 @@ EventListener es_add_event_listener(EventSystem *event_system, Event event, Even
|
||||
}
|
||||
|
||||
if (ev->count >= ev->capacity) {
|
||||
// TODO (Abdelrahman): Handle reallocating when out of callbacks
|
||||
goto RETURN_LISTENER;
|
||||
reallocate_system(event_system, es);
|
||||
ev = get_event(es, event);
|
||||
}
|
||||
|
||||
u64 id = ++(ev->count);
|
||||
@ -327,6 +321,21 @@ internal u64 *get_free_array(const ESInternal *es) {
|
||||
return (u64 *)((uptr)es + es->free_offset);
|
||||
}
|
||||
|
||||
internal void reallocate_system(EventSystem *event_system, ESInternal *es) {
|
||||
u64 new_callback_capacity = get_largest_capacity(es);
|
||||
EventSystem event_system_new = es_init(es->event_capacity, new_callback_capacity);
|
||||
ESInternal *es_new = get_event_system_internal(event_system_new);
|
||||
|
||||
es_new->id = es->id;
|
||||
es_new->event_count = es->event_count;
|
||||
|
||||
copy_event_data(es, es_new);
|
||||
|
||||
es_deinit(event_system);
|
||||
event_system->id = event_system_new.id;
|
||||
es = es_new;
|
||||
}
|
||||
|
||||
internal void copy_event_data(const ESInternal *src, ESInternal *dst) {
|
||||
EventSlot *slots_src = get_event_slots_array(src);
|
||||
EventSlot *slots_dst = get_event_slots_array(dst);
|
||||
|
10
src/main.c
10
src/main.c
@ -37,7 +37,7 @@ int main(void) {
|
||||
EventListener listeners[COUNT_EVENTS] = {0};
|
||||
EventCallback callbacks[COUNT_EVENTS] = {window_event_handler, keyboard_event_handler, mouse_event_handler};
|
||||
|
||||
EventSystem es = es_init(COUNT_EVENTS, 64);
|
||||
EventSystem es = es_init(COUNT_EVENTS, 4);
|
||||
|
||||
for (int i = 0; i < COUNT_EVENTS; ++i) {
|
||||
events[i] = es_register_event(&es);
|
||||
@ -71,10 +71,18 @@ int main(void) {
|
||||
es_register_event(&es);
|
||||
}
|
||||
|
||||
printf("AFTER REALLOCATION WHEN OUT OF EVENTS\n\n");
|
||||
es_emit_event(es, events[EVENT_WINDOW], (void *)&window_event);
|
||||
es_emit_event(es, events[EVENT_KEYBOARD], (void *)&keyboard_event);
|
||||
es_emit_event(es, events[EVENT_MOUSE], (void *)&mouse_event);
|
||||
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
es_add_event_listener(&es, events[EVENT_WINDOW], extra_event_handler);
|
||||
}
|
||||
|
||||
printf("AFTER REALLOCATION WHEN OUT OF CALLBACKS\n\n");
|
||||
es_emit_event(es, events[EVENT_WINDOW], (void *)&window_event);
|
||||
|
||||
for (int i = 0; i < COUNT_EVENTS; ++i) {
|
||||
es_remove_event_listener(es, listeners[i]);
|
||||
es_deregister_event(es, &events[i]);
|
||||
|
Loading…
Reference in New Issue
Block a user