From 1851002fc12173dd9754ece66b134dc3fd5954b8 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Wed, 12 Jan 2022 12:17:29 +1100 Subject: [PATCH] [client] all: remove `ll_walk` and migrate over to `ll_forEachNL` --- client/include/ll.h | 12 ----------- client/renderers/EGL/model.c | 10 ++++++--- client/src/app.c | 39 +++++++++++++++++++++++------------- client/src/ll.c | 31 ---------------------------- client/src/main.c | 5 +++-- client/src/overlay/config.c | 8 ++++++-- client/src/overlay/graphs.c | 16 +++++++++++---- 7 files changed, 53 insertions(+), 68 deletions(-) diff --git a/client/include/ll.h b/client/include/ll.h index ac0e162f..2a4c0c94 100644 --- a/client/include/ll.h +++ b/client/include/ll.h @@ -36,7 +36,6 @@ struct ll { struct ll_item * head; struct ll_item * tail; - struct ll_item * pos; unsigned int count; LG_Lock lock; }; @@ -48,8 +47,6 @@ bool ll_shift (struct ll * list, void ** data); bool ll_peek_head(struct ll * list, void ** data); bool ll_peek_tail(struct ll * list, void ** data); -bool ll_walk (struct ll * list, void ** data); - #define ll_lock(ll) LG_LOCK((ll)->lock) #define ll_unlock(ll) LG_UNLOCK((ll)->lock) @@ -65,13 +62,6 @@ static inline unsigned int ll_count(struct ll * list) return list->count; } -static inline void ll_reset (struct ll * list) -{ - LG_LOCK(list->lock); - list->pos = NULL; - LG_UNLOCK(list->lock); -} - static inline void ll_removeNL(struct ll * list, struct ll_item * item) { --list->count; @@ -87,8 +77,6 @@ static inline void ll_removeNL(struct ll * list, struct ll_item * item) if (item->next) item->next->prev = item->prev; - - list->pos = NULL; } static inline bool ll_removeData(struct ll * list, void * match) diff --git a/client/renderers/EGL/model.c b/client/renderers/EGL/model.c index 434d027f..22df26af 100644 --- a/client/renderers/EGL/model.c +++ b/client/renderers/EGL/model.c @@ -164,18 +164,20 @@ void egl_modelRender(EGL_Model * model) /* buffer the verticies */ struct FloatList * fl; - for(ll_reset(model->verticies); ll_walk(model->verticies, (void **)&fl);) + ll_lock(model->verticies); + ll_forEachNL(model->verticies, item, fl) { glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(GLfloat) * fl->count * 3, fl->v); offset += sizeof(GLfloat) * fl->count * 3; } /* buffer the uvs */ - for(ll_reset(model->verticies); ll_walk(model->verticies, (void **)&fl);) + ll_forEachNL(model->verticies, item, fl) { glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(GLfloat) * fl->count * 2, fl->u); offset += sizeof(GLfloat) * fl->count * 2; } + ll_unlock(model->verticies); /* set up vertex arrays in the VAO */ glEnableVertexAttribArray(0); @@ -199,11 +201,13 @@ void egl_modelRender(EGL_Model * model) /* draw the arrays */ GLint offset = 0; struct FloatList * fl; - for(ll_reset(model->verticies); ll_walk(model->verticies, (void **)&fl);) + ll_lock(model->verticies); + ll_forEachNL(model->verticies, item, fl) { glDrawArrays(GL_TRIANGLE_STRIP, offset, fl->count); offset += fl->count; } + ll_unlock(model->verticies); /* unbind and cleanup */ glBindTexture(GL_TEXTURE_2D, 0); diff --git a/client/src/app.c b/client/src/app.c index 7961284c..9d1c8973 100644 --- a/client/src/app.c +++ b/client/src/app.c @@ -66,15 +66,20 @@ bool app_isOverlayMode(void) if (g_state.overlayInput) return true; + bool result = false; struct Overlay * overlay; - for (ll_reset(g_state.overlays); - ll_walk(g_state.overlays, (void **)&overlay); ) + ll_lock(g_state.overlays); + ll_forEachNL(g_state.overlays, item, overlay) { if (overlay->ops->needs_overlay && overlay->ops->needs_overlay(overlay)) - return true; + { + result = true; + break; + } } + ll_unlock(g_state.overlays); - return false; + return result; } void app_updateCursorPos(double x, double y) @@ -725,8 +730,8 @@ void app_registerOverlay(const struct LG_OverlayOps * ops, const void * params) void app_initOverlays(void) { struct Overlay * overlay; - for (ll_reset(g_state.overlays); - ll_walk(g_state.overlays, (void **)&overlay); ) + ll_lock(g_state.overlays); + ll_forEachNL(g_state.overlays, item, overlay) { if (!overlay->ops->init(&overlay->udata, overlay->params)) { @@ -734,6 +739,7 @@ void app_initOverlays(void) overlay->ops = NULL; } } + ll_unlock(g_state.overlays); } static inline void mergeRect(struct Rect * dest, const struct Rect * a, const struct Rect * b) @@ -778,22 +784,26 @@ static inline LG_DSPointer mapImGuiCursor(ImGuiMouseCursor cursor) bool app_overlayNeedsRender(void) { - struct Overlay * overlay; - if (app_isOverlayMode()) return true; - for (ll_reset(g_state.overlays); - ll_walk(g_state.overlays, (void **)&overlay); ) + bool result = false; + struct Overlay * overlay; + ll_lock(g_state.overlays); + ll_forEachNL(g_state.overlays, item, overlay) { if (!overlay->ops->needs_render) continue; if (overlay->ops->needs_render(overlay->udata, false)) - return true; + { + result = true; + break; + } } + ll_unlock(g_state.overlays); - return false; + return result; } int app_renderOverlay(struct Rect * rects, int maxRects) @@ -832,8 +842,8 @@ render_again: const bool msgModal = overlayMsg_modal(); // render the overlays - for (ll_reset(g_state.overlays); - ll_walk(g_state.overlays, (void **)&overlay); ) + ll_lock(g_state.overlays); + ll_forEachNL(g_state.overlays, item, overlay) { if (msgModal && overlay->ops != &LGOverlayMsg) continue; @@ -875,6 +885,7 @@ render_again: memcpy(overlay->lastRects, buffer, sizeof(struct Rect) * written); overlay->lastRectCount = written; } + ll_unlock(g_state.overlays); if (overlayMode) { diff --git a/client/src/ll.c b/client/src/ll.c index 9642694a..12e0baa1 100644 --- a/client/src/ll.c +++ b/client/src/ll.c @@ -29,7 +29,6 @@ struct ll * ll_new(void) struct ll * list = malloc(sizeof(*list)); list->head = NULL; list->tail = NULL; - list->pos = NULL; list->count = 0; LG_LOCK_INIT(list->lock); return list; @@ -119,33 +118,3 @@ bool ll_peek_tail(struct ll * list, void ** data) return true; } - -bool ll_walk(struct ll * list, void ** data) -{ - LG_LOCK(list->lock); - - if (!list->pos) - { - if (!list->head) - { - LG_UNLOCK(list->lock); - return false; - } - - list->pos = list->head; - } - else - { - if (!list->pos->next) - { - LG_UNLOCK(list->lock); - return false; - } - list->pos = list->pos->next; - } - - *data = list->pos->data; - LG_UNLOCK(list->lock); - - return true; -} diff --git a/client/src/main.c b/client/src/main.c index 6897a599..5140b39f 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -145,12 +145,13 @@ static bool tickTimerFn(void * unused) bool needsRender = false; struct Overlay * overlay; - for (ll_reset(g_state.overlays); - ll_walk(g_state.overlays, (void **)&overlay); ) + ll_lock(g_state.overlays); + ll_forEachNL(g_state.overlays, item, overlay) { if (overlay->ops->tick && overlay->ops->tick(overlay->udata, tickCount)) needsRender = true; } + ll_unlock(g_state.overlays); if (needsRender) app_invalidateWindow(false); diff --git a/client/src/overlay/config.c b/client/src/overlay/config.c index 3ef93b54..841b0754 100644 --- a/client/src/overlay/config.c +++ b/client/src/overlay/config.c @@ -182,7 +182,8 @@ static int config_render(void * udata, bool interactive, struct Rect * windowRec if (igBeginTabItem("Settings", NULL, 0)) { - for (ll_reset(cfg.callbacks); ll_walk(cfg.callbacks, (void **)&cb); ) + ll_lock(cfg.callbacks); + ll_forEachNL(cfg.callbacks, item, cb) { if (!igCollapsingHeader_BoolPtr(cb->title, NULL, 0)) continue; @@ -191,10 +192,12 @@ static int config_render(void * udata, bool interactive, struct Rect * windowRec cb->callback(cb->udata, &id); igPopID(); } + ll_unlock(cfg.callbacks); igEndTabItem(); } - for (ll_reset(cfg.tabCallbacks); ll_walk(cfg.tabCallbacks, (void **)&cb); ) + ll_lock(cfg.tabCallbacks); + ll_forEachNL(cfg.tabCallbacks, item, cb) { if (!igBeginTabItem(cb->title, NULL, 0)) continue; @@ -204,6 +207,7 @@ static int config_render(void * udata, bool interactive, struct Rect * windowRec igPopID(); igEndTabItem(); } + ll_unlock(cfg.tabCallbacks); igEndTabBar(); diff --git a/client/src/overlay/graphs.c b/client/src/overlay/graphs.c index 65d40052..c62a15ce 100644 --- a/client/src/overlay/graphs.c +++ b/client/src/overlay/graphs.c @@ -53,11 +53,13 @@ static void configCallback(void * udata, int * id) igBeginTable("split", 2, 0, (ImVec2){}, 0); GraphHandle graph; - for (ll_reset(gs.graphs); ll_walk(gs.graphs, (void **)&graph); ) + ll_lock(gs.graphs); + ll_forEachNL(gs.graphs, item, graph) { igTableNextColumn(); igCheckbox(graph->name, &graph->enabled); } + ll_unlock(gs.graphs); igEndTable(); } @@ -127,9 +129,12 @@ static int graphs_render(void * udata, bool interactive, GraphHandle graph; int graphCount = 0; - for (ll_reset(gs.graphs); ll_walk(gs.graphs, (void **)&graph); ) + ll_lock(gs.graphs); + ll_forEachNL(gs.graphs, item, graph) + { if (graph->enabled) ++graphCount; + } ImVec2 pos = {0.0f, 0.0f}; igSetNextWindowBgAlpha(0.4f); @@ -152,7 +157,7 @@ static int graphs_render(void * udata, bool interactive, const float height = (winSize.y / graphCount) - igGetStyle()->ItemSpacing.y; - for (ll_reset(gs.graphs); ll_walk(gs.graphs, (void **)&graph); ) + ll_forEachNL(gs.graphs, item, graph) { if (!graph->enabled) continue; @@ -182,6 +187,7 @@ static int graphs_render(void * udata, bool interactive, (ImVec2){ winSize.x, height }, sizeof(float)); }; + ll_unlock(gs.graphs); overlayGetImGuiRect(windowRects); igEnd(); @@ -217,6 +223,8 @@ void overlayGraph_iterate(void (*callback)(GraphHandle handle, const char * name bool * enabled, void * udata), void * udata) { GraphHandle graph; - for (ll_reset(gs.graphs); ll_walk(gs.graphs, (void **)&graph); ) + ll_lock(gs.graphs); + ll_forEachNL(gs.graphs, item, graph) callback(graph, graph->name, &graph->enabled, udata); + ll_unlock(gs.graphs); }