summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2013-06-22 14:37:33 +0200
committerYorhel <git@yorhel.nl>2013-06-22 14:37:33 +0200
commit38d80827ba30059a9a513c45793f853ed59f067f (patch)
tree6e92e6ffdd06eb0b8070d452e2d387aa3827b5eb
parent8f091f36abb7773f677b681418236bfba9338f73 (diff)
util/list: Remove hlist_* + generalize list_insert() to support append
I wanted the linked list abstraction to be convenient in use, yet result in exactly the same kind of (generated) code as if each list was hand-written. That's the reason for the hlist_*, but I'm starting to doubt whether that optimization is really worth the effort of maintaining two separate APIs. I'm keeping the idea for an slist_ abstraction open, however; Saving memory on pointers in list nodes likely *is* worth the effort. I also figured that, with a tiny change, list_insert_before() could be used as a list_insert_after(), too, so I fixed that and renamed it to a single powerful list_insert().
-rw-r--r--src/hub/chat.c2
-rw-r--r--src/util/list.h31
-rw-r--r--src/util/logfile.c10
3 files changed, 14 insertions, 29 deletions
diff --git a/src/hub/chat.c b/src/hub/chat.c
index e0a753d..d3e28b5 100644
--- a/src/hub/chat.c
+++ b/src/hub/chat.c
@@ -166,7 +166,7 @@ void hub_chat_message(hub_t *h, int64_t group, int64_t from, const char *message
strcpy(m->msg, message);
user_ref(h, from);
- list_insert_before(*g, m, g->head);
+ list_insert(*g, m, g->head);
g->n++;
if((uint32_t)g->n > h->ch.props.ChatLogSize)
glog_pop(h, g);
diff --git a/src/util/list.h b/src/util/list.h
index d3a96be..a479b49 100644
--- a/src/util/list.h
+++ b/src/util/list.h
@@ -31,10 +31,6 @@
* All macros starting with 's' (e.g. slist_ instead of list_) operate on
* singly-linked lists. The nodes only need to have a 'next' pointer.
*
- * All macros containing the 'h' (hlist_, shlist_) flag take only a single
- * pointer variable as list argument rather than a struct. This pointer is
- * treated as the `head' of a list.
- *
* All macros containing the 'p' flag (plist_, splist_) accept an additional
* prefix argument. This argument is prefixed to the names of the 'next' and
* 'prev' pointers.
@@ -53,12 +49,15 @@
*/
-/* Insert an element in the list before _next.
- * To push an element to the head of the list: list_insert_before(l, n, l.head)
+/* Insert an element in the list before _next, or at the tail if _next is NULL.
+ * To insert an element at the head of the list: list_insert(l, n, l.head)
+ * To insert an element at the tail of the list: list_insert(l, n, NULL)
+ * To insert an element before element x: list_insert(l, n, x)
+ * To insert an element after element x: list_insert(l, n, x->next)
*/
-#define list_insert_before(_l, _n, _next) do {\
+#define list_insert(_l, _n, _next) do {\
(_n)->next = (_next);\
- (_n)->prev = (_next) ? (_next)->prev : NULL;\
+ (_n)->prev = (_n)->next ? (_n)->next->prev : (_l).tail;\
if((_n)->next) (_n)->next->prev = (_n);\
else (_l).tail = (_n);\
if((_n)->prev) (_n)->prev->next = (_n);\
@@ -78,21 +77,5 @@
} while(0)
-#define hlist_insert_before(_l, _n, _next) do {\
- (_n)->next = (_next);\
- (_n)->prev = (_next) ? (_next)->prev : NULL;\
- if((_n)->next) (_n)->next->prev = (_n);\
- if((_n)->prev) (_n)->prev->next = (_n);\
- else (_l) = (_n);\
- } while(0)
-
-
-#define hlist_remove(_l, _n) do {\
- if((_n)->next) (_n)->next->prev = (_n)->prev;\
- if((_n)->prev) (_n)->prev->next = (_n)->next;\
- if((_n) == (_l)) (_l) = (_n)->next;\
- } while(0)
-
-
#endif
/* vim: set noet sw=4 ts=4: */
diff --git a/src/util/logfile.c b/src/util/logfile.c
index fed611d..cc1bd75 100644
--- a/src/util/logfile.c
+++ b/src/util/logfile.c
@@ -30,7 +30,9 @@ struct logfile_t {
struct stat st;
};
-static logfile_t *logfile_list;
+static struct {
+ logfile_t *head, *tail;
+} logfile_list;
/* (Re)opens the log file and checks for inode and file size changes. */
@@ -74,14 +76,14 @@ logfile_t *logfile_open(const char *fn) {
l->fn = strdup(fn);
l->fd = -1;
- hlist_insert_before(logfile_list, l, logfile_list);
+ list_insert(logfile_list, l, NULL);
logfile_checkfile(l);
return l;
}
void logfile_close(logfile_t *l) {
- hlist_remove(logfile_list, l);
+ list_remove(logfile_list, l);
if(l->fd >= 0)
close(l->fd);
@@ -126,7 +128,7 @@ static void logfile_reopen(logfile_t *l) {
void logfile_global_reopen() {
logfile_t *l;
- for(l=logfile_list; l; l=l->next)
+ for(l=logfile_list.head; l; l=l->next)
logfile_reopen(l);
}