From e292bd7797db06a72f6babb2863fb5d1b2e22366 Mon Sep 17 00:00:00 2001 From: Nemo D'ACREMONT Date: Wed, 7 May 2025 09:51:59 +0200 Subject: [PATCH] fix: use list to store children in ufd --- src/utils/ufd.c | 26 +++++++++++++++++--------- src/utils/ufd.h | 1 + 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/utils/ufd.c b/src/utils/ufd.c index ed9f0c6..e5908f8 100644 --- a/src/utils/ufd.c +++ b/src/utils/ufd.c @@ -1,18 +1,20 @@ #include "ufd.h" +#include "debug.h" #include "stdlib.h" #include -void ufd__init(struct ufd_t* ufd, struct context_entry_t* thread) +inline void ufd__init(struct ufd_t* ufd, struct context_entry_t* thread) { TAILQ_INIT(&ufd->children); ufd->thread = thread; ufd->repr = ufd, ufd->parent = NULL; + ufd->child = NULL; } -void ufd__join(struct ufd_t* th1, struct ufd_t* th2) +inline void ufd__join(struct ufd_t* th1, struct ufd_t* th2) { struct ufd_t* a = ufd__find(th1); struct ufd_t* b = ufd__find(th2); @@ -20,29 +22,35 @@ void ufd__join(struct ufd_t* th1, struct ufd_t* th2) if (a != b) { a->parent = th2; + th2->child = a; a->repr = b; - TAILQ_INSERT_TAIL(&a->repr->children, a, link); + TAILQ_INSERT_TAIL(&b->repr->children, a, link); } } -struct ufd_t* ufd__find(struct ufd_t* th) +inline struct ufd_t* ufd__find(struct ufd_t* th) { if (th->repr == th) return th; struct ufd_t* nrepr = ufd__find(th->repr); + TAILQ_REMOVE(&th->repr->children, th, link); + TAILQ_INSERT_TAIL(&nrepr->children, th, link); th->repr = nrepr; return nrepr; } -void ufd__delete(struct ufd_t* th) +inline void ufd__delete(struct ufd_t* th) { - struct ufd_t* child; + struct ufd_t* child = NULL; - TAILQ_FOREACH(child, &th->children, link) { - child->parent = NULL; - child->repr = child; + TAILQ_FOREACH(child, &th->children, link) + { + if (child->parent == th) + child->parent = th->child; + if (child->repr == th) + child->repr = th->child; } } diff --git a/src/utils/ufd.h b/src/utils/ufd.h index 9ba2d38..356ee64 100644 --- a/src/utils/ufd.h +++ b/src/utils/ufd.h @@ -8,6 +8,7 @@ TAILQ_HEAD(ufd_children_t, ufd_t); struct ufd_t { struct ufd_t* parent; struct ufd_t* repr; + struct ufd_t* child; struct context_entry_t* thread; struct ufd_children_t children; TAILQ_ENTRY(ufd_t) link;