From 489e29e887f58eca37a5fefa205ff91a1c772f3c Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 22 May 2014 23:44:56 +0000 Subject: trade sys/queue.h LIST_* for ccan/list ccan/list has branchless add/del operations and (IMHO) a better API. --- bsd/queue_safe.h | 43 ------------------------------------------- bsd/simpleq.h | 10 ---------- cmogstored.h | 8 ++++---- queue_common.c | 8 ++++---- svc.c | 1 + svc_dev.c | 10 +++++----- 6 files changed, 14 insertions(+), 66 deletions(-) delete mode 100644 bsd/queue_safe.h diff --git a/bsd/queue_safe.h b/bsd/queue_safe.h deleted file mode 100644 index 7b5cbfd..0000000 --- a/bsd/queue_safe.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * this file adds *_SAFE macros that may be missing from the stock - * sys/queue.h header. The original (BSD) license terms are - * retained (see below). - */ -#include - -/*- - * Copyright (c) 1991, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ -#ifndef LIST_FOREACH_SAFE -#define LIST_FOREACH_SAFE(var, head, field, tvar) \ - for ((var) = LIST_FIRST((head)); \ - (var) && ((tvar) = LIST_NEXT((var), field), 1); \ - (var) = (tvar)) -#endif /* LIST_FOREACH_SAFE */ - -/* add more as we need them */ diff --git a/bsd/simpleq.h b/bsd/simpleq.h index bce207c..a0b4d99 100644 --- a/bsd/simpleq.h +++ b/bsd/simpleq.h @@ -3,14 +3,6 @@ * sys/queue.h header. The original (BSD) license terms are * retained (see below). */ -#include - -/* - * Some systems have STAILQ instead of SIMPLEQ, and presumably - * some have SIMPLEQ instead of STAILQ. And of course some systems - * have both.. - */ -#ifndef SIMPLEQ_HEAD /*- * Copyright (c) 1991, 1993 @@ -110,5 +102,3 @@ struct { \ #define SIMPLEQ_EMPTY(head) ((head)->sqh_first == NULL) #define SIMPLEQ_FIRST(head) ((head)->sqh_first) #define SIMPLEQ_NEXT(elm, field) ((elm)->field.sqe_next) - -#endif /* missing SIMPLEQ* */ diff --git a/cmogstored.h b/cmogstored.h index 1006fc3..1381f4f 100644 --- a/cmogstored.h +++ b/cmogstored.h @@ -47,8 +47,8 @@ #include #include /* GNU */ #include -#include "bsd/queue_safe.h" #include "bsd/simpleq.h" +#include "ccan/list/list.h" /* gnulib headers */ #include "progname.h" @@ -147,7 +147,7 @@ struct mog_mgmt { struct mog_svc *svc; enum Gc_hash alg; enum mog_mgmt_method mgmt_method; - LIST_ENTRY(mog_mgmt) subscribed; + struct list_node subscribed; }; struct mog_queue; @@ -168,7 +168,7 @@ struct mog_svc { Hash_table *by_st_dev; pthread_mutex_t devstats_lock; struct mog_queue *queue; - LIST_HEAD(mgmt_head, mog_mgmt) devstats_subscribers; + struct list_head devstats_subscribers; SIMPLEQ_ENTRY(mog_svc) qentry; mode_t put_perms; mode_t mkcol_perms; @@ -248,7 +248,7 @@ struct mog_fd; struct mog_queue { int queue_fd; /* epoll or kqueue */ struct mog_thrpool thrpool; - LIST_ENTRY(mog_queue) qbuddies; + struct list_node qbuddies; }; /* accept.c */ diff --git a/queue_common.c b/queue_common.c index b9c2f99..717f6ab 100644 --- a/queue_common.c +++ b/queue_common.c @@ -8,7 +8,7 @@ * access to this should only be called in the main thread, this * is currently not thread safe as there's no need for it. */ -static LIST_HEAD(queue_head, mog_queue) all_queues; +static LIST_HEAD(all_queues); struct mog_queue *mog_queue_init(int queue_fd) { @@ -26,7 +26,7 @@ struct mog_queue *mog_queue_init(int queue_fd) q = &mfd->as.queue; q->queue_fd = queue_fd; memset(&q->thrpool, 0, sizeof(struct mog_thrpool)); - LIST_INSERT_HEAD(&all_queues, q, qbuddies); + list_add(&all_queues, &q->qbuddies); return q; } @@ -36,11 +36,11 @@ void mog_queue_stop(struct mog_queue *keep) struct mog_queue *queue, *tmp; struct mog_fd *mfd; - LIST_FOREACH_SAFE(queue, &all_queues, qbuddies, tmp) { + list_for_each_safe(&all_queues, queue, tmp, qbuddies) { /* keep is usually mog_notify_queue */ if (queue == keep) continue; - LIST_REMOVE(queue, qbuddies); + list_del(&queue->qbuddies); mog_thrpool_quit(&queue->thrpool, queue); mfd = mog_fd_of(queue); mog_fd_put(mfd); diff --git a/svc.c b/svc.c index 86f69db..38f362c 100644 --- a/svc.c +++ b/svc.c @@ -128,6 +128,7 @@ struct mog_svc * mog_svc_new(const char *docroot) svc_once(); svc = xzalloc(sizeof(struct mog_svc)); + list_head_init(&svc->devstats_subscribers); svc->persist_client = 1; svc->docroot = docroot; svc->docroot_fd = fd; diff --git a/svc_dev.c b/svc_dev.c index bb83128..f618a9b 100644 --- a/svc_dev.c +++ b/svc_dev.c @@ -208,7 +208,7 @@ void mog_svc_devstats_subscribe(struct mog_mgmt *mgmt) struct mog_svc *svc = mgmt->svc; CHECK(int, 0, pthread_mutex_lock(&svc->devstats_lock)); - LIST_INSERT_HEAD(&svc->devstats_subscribers, mgmt, subscribed); + list_add(&svc->devstats_subscribers, &mgmt->subscribed); CHECK(int, 0, pthread_mutex_unlock(&svc->devstats_lock)); } @@ -228,7 +228,7 @@ bool mog_svc_devstats_broadcast(void *ent, void *ignored) if (len < 0) goto out; - LIST_FOREACH_SAFE(mgmt, &svc->devstats_subscribers, subscribed, tmp) { + list_for_each_safe(&svc->devstats_subscribers, mgmt, tmp, subscribed) { assert(mgmt->wbuf == NULL && "wbuf not null"); iov.iov_base = buf; iov.iov_len = (size_t)len; @@ -236,7 +236,7 @@ bool mog_svc_devstats_broadcast(void *ent, void *ignored) if (mgmt->wbuf == NULL) continue; /* success */ - LIST_REMOVE(mgmt, subscribed); + list_del(&mgmt->subscribed); mfd = mog_fd_of(mgmt); if (mgmt->wbuf == MOG_WR_ERROR) { assert(mgmt->rbuf == NULL && "would leak rbuf"); @@ -260,10 +260,10 @@ static bool devstats_shutdown_i(void *svcptr, void *ignored) struct mog_fd *mfd; CHECK(int, 0, pthread_mutex_lock(&svc->devstats_lock)); - LIST_FOREACH_SAFE(mgmt, &svc->devstats_subscribers, subscribed, tmp) { + list_for_each_safe(&svc->devstats_subscribers, mgmt, tmp, subscribed) { assert(mgmt->wbuf == NULL && "wbuf not null"); assert(mgmt->rbuf == NULL && "would leak rbuf"); - LIST_REMOVE(mgmt, subscribed); + list_del(&mgmt->subscribed); mfd = mog_fd_of(mgmt); mog_fd_put(mfd); } -- cgit v1.2.3-24-ge0c7