Get rid of trailing whitespace in coverage scripts.
[srvx.git] / src / timeq.c
index 3bbbf4640634242e98f4c4146a76fa2ffe201d6f..ba91f6b45080fb69632b71357ce24e5c75382295 100644 (file)
@@ -1,11 +1,12 @@
 /* timeq.c - time-based event queue
  * Copyright 2000-2004 srvx Development Team
  *
- * This program is free software; you can redistribute it and/or modify
+ * This file is part of srvx.
+ *
+ * srvx is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.  Important limitations are
- * listed in the COPYING file that accompanies this software.
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -13,7 +14,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, email srvx-maintainers@srvx.net.
+ * along with srvx; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
  */
 
 #include "common.h"
@@ -32,9 +34,10 @@ timeq_cleanup(void)
 {
     timeq_del(0, 0, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_FUNC|TIMEQ_IGNORE_DATA);
     heap_delete(timeq);
+    timeq = NULL;
 }
 
-void
+static void
 timeq_init(void)
 {
     timeq = heap_new(int_comparator);
@@ -45,6 +48,8 @@ time_t
 timeq_next(void)
 {
     void *time;
+    if (!timeq)
+        return ~0;
     heap_peek(timeq, &time, 0);
     return (time_t)time;
 }
@@ -58,6 +63,8 @@ timeq_add(time_t when, timeq_func func, void *data)
     ent->func = func;
     ent->data = data;
     w = (void*)when;
+    if (!timeq)
+        timeq_init();
     heap_insert(timeq, w, ent);
 }
 
@@ -74,8 +81,8 @@ timeq_del_matching(void *key, void *data, void *extra)
     struct timeq_entry *a = data;
     struct timeq_extra *b = extra;
     if (((b->mask & TIMEQ_IGNORE_WHEN) || ((time_t)key == b->when))
-       && ((b->mask & TIMEQ_IGNORE_FUNC) || (a->func == b->func))
-       && ((b->mask & TIMEQ_IGNORE_DATA) || (a->data == b->data))) {
+        && ((b->mask & TIMEQ_IGNORE_FUNC) || (a->func == b->func))
+        && ((b->mask & TIMEQ_IGNORE_DATA) || (a->data == b->data))) {
         free(data);
         return 1;
     } else {
@@ -91,7 +98,8 @@ timeq_del(time_t when, timeq_func func, void *data, int mask)
     extra.func = func;
     extra.data = data;
     extra.mask = mask;
-    heap_remove_pred(timeq, timeq_del_matching, &extra);
+    if (timeq)
+        heap_remove_pred(timeq, timeq_del_matching, &extra);
 }
 
 unsigned int
@@ -106,12 +114,12 @@ timeq_run(void)
     void *k, *d;
     struct timeq_entry *ent;
     while (heap_size(timeq) > 0) {
-       heap_peek(timeq, &k, &d);
-       if ((time_t)k > now)
+        heap_peek(timeq, &k, &d);
+        if ((time_t)k > now)
             break;
-       ent = d;
-       heap_pop(timeq);
-       ent->func(ent->data);
+        ent = d;
+        heap_pop(timeq);
+        ent->func(ent->data);
         free(ent);
     }
 }