commit 357b5f535a3c83fdfd58f54f1fec0c4a445bb92a
parent 86e462daaed62f3197f442dfa3cbabed26ea886d
Author: Kyle Milz <kyle@0x30.net>
Date: Sat, 16 Jul 2016 09:42:25 -0600
lib: keep tu chain sorted
Diffstat:
1 file changed, 16 insertions(+), 18 deletions(-)
diff --git a/lib/runtime.c b/lib/runtime.c
@@ -30,9 +30,8 @@
#include "runtime.h"
-static struct citrun_node *nodes_head;
-static struct citrun_node *nodes_tail;
-static uint64_t nodes_total;
+static struct citrun_node *nodes_head;
+static uint64_t nodes_total;
static void *relay_thread(void *);
@@ -42,20 +41,29 @@ static void *relay_thread(void *);
void
citrun_node_add(struct citrun_node *n)
{
+ struct citrun_node *walk = nodes_head;
+
/* Used for double buffering line counts. */
n->old_lines = calloc(n->size, sizeof(uint64_t));
if (n->old_lines == NULL)
err(1, "calloc");
- if (nodes_head == NULL) {
- assert(nodes_tail == NULL);
+ nodes_total++;
+
+ /* If the list is empty or we need to replace the list head */
+ if (nodes_head == NULL || nodes_head->size >= n->size) {
+ n->next = nodes_head;
nodes_head = n;
- nodes_tail = n;
return;
}
- nodes_tail->next = n;
- nodes_tail = n;
+ /* Search for a next element that n->size is greater than */
+ while (walk->next != NULL && walk->next->size < n->size)
+ walk = walk->next;
+
+ /* Splice in the new element */
+ n->next = walk->next;
+ walk->next = n;
}
/*
@@ -64,17 +72,7 @@ citrun_node_add(struct citrun_node *n)
void
citrun_start()
{
- struct citrun_node *w;
pthread_t tid;
-
- /*
- * Count nodes once. Changing this after program start is not supported
- * at the moment (dlopen(), dlclose() of instrumented libs).
- */
- nodes_total = 0;
- for (w = nodes_head; w != NULL; w = w->next)
- ++nodes_total;
-
pthread_create(&tid, NULL, relay_thread, NULL);
}