chiark / gitweb /
Qualify name given to @bres_byname@.
[mLib] / sel.c
diff --git a/sel.c b/sel.c
index 62a61cfc9f5eafdb8858c88ff7eab50c6690f0ad..9babc8426871d86449cfa4a4d1422c4ccc8e8a5e 100644 (file)
--- a/sel.c
+++ b/sel.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: sel.c,v 1.9 2001/02/03 19:07:08 mdw Exp $
+ * $Id: sel.c,v 1.12 2003/05/18 15:10:29 mdw Exp $
  *
  * I/O multiplexing support
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: sel.c,v $
+ * Revision 1.12  2003/05/18 15:10:29  mdw
+ * Remove memory leak.
+ *
+ * Revision 1.11  2003/05/17 10:34:04  mdw
+ *  Tidying and bugfixing.
+ *
+ * Revision 1.10  2001/06/22 19:35:58  mdw
+ * Fix a large number of bugs.
+ *
  * Revision 1.9  2001/02/03 19:07:08  mdw
  * Ensure that timers set to go off in the past don't case a problem.
  *
@@ -62,6 +71,7 @@
 
 /*----- Header files ------------------------------------------------------*/
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
 #include "sel.h"
+#include "sub.h"
 #include "tv.h"
 
+/*----- Data structures ---------------------------------------------------*/
+
+typedef struct sel_pendfile {
+  struct sel_pendfile *next;
+  sel_file *f;
+} pfile;
+
+typedef struct sel_pendtimer {
+  struct sel_pendtimer *next;
+  sel_timer *t;
+} ptimer;
+
 /*----- Main code ---------------------------------------------------------*/
 
 /* --- @sel_init@ --- *
@@ -123,6 +146,7 @@ void sel_initfile(sel_state *s, sel_file *f,
   f->mode = mode;
   f->func = func;
   f->p = p;
+  f->pend = 0;
 }
 
 /* --- @sel_addfile@ --- *
@@ -143,9 +167,9 @@ void sel_addfile(sel_file *f)
   while (*ff && (*ff)->fd > f->fd)
     ff = &(*ff)->next;
   f->next = *ff;
-  f->prev = (sel_file *)ff;
+  f->prev = ff;
   if (*ff)
-    (*ff)->prev = f;
+    (*ff)->prev = &f->next;
   *ff = f;
   FD_SET(f->fd, f->s->fd + f->mode);
 }
@@ -179,10 +203,14 @@ void sel_force(sel_file *f)
 
 void sel_rmfile(sel_file *f)
 {
-  f->prev->next = f->next;
+  *f->prev = f->next;
   if (f->next)
     f->next->prev = f->prev;
   FD_CLR(f->fd, f->s->fd + f->mode);
+  if (f->pend) {
+    f->pend->f = 0;
+    f->pend = 0;
+  }
 }
 
 /* --- @sel_addtimer@ --- *
@@ -204,21 +232,23 @@ void sel_addtimer(sel_state *s, sel_timer *t,
                  void *p)
 {
   sel_timer **tt = &s->timers;
+  { sel_timer *q; for (q = s->timers; q; q = q->next) assert(q != t); }
 
   /* --- Set up the timer block --- */
 
   t->tv = *tv;
   t->func = func;
   t->p = p;
+  t->pend = 0;
 
   /* --- More line noise --- */
   
   while (*tt && TV_CMP(&(*tt)->tv, <, tv))
     tt = &(*tt)->next;
   t->next = *tt;
-  t->prev = (sel_timer *)tt;
+  t->prev = tt;
   if (*tt)
-    (*tt)->prev = t;
+    (*tt)->prev = &t->next;
   *tt = t;
 }
 
@@ -233,9 +263,14 @@ void sel_addtimer(sel_state *s, sel_timer *t,
 
 void sel_rmtimer(sel_timer *t)
 {
-  t->prev->next = t->next;
-  if (t->next)
-    t->next->prev = t->prev;
+  if (t->pend) {
+    t->pend->t = 0;
+    t->pend = 0;
+  } else {
+    *t->prev = t->next;
+    if (t->next)
+      t->next->prev = t->prev;
+  }
 }
 
 /* --- @sel_addhook@ --- *
@@ -258,9 +293,9 @@ void sel_addhook(sel_state *s, sel_hook *h,
   h->after = after;
   h->p = p;
   h->next = s->hooks;
-  h->prev = (sel_hook *)&s->hooks;
+  h->prev = &s->hooks;
   if (s->hooks)
-    s->hooks->prev = h;
+    s->hooks->prev = &h->next;
   s->hooks = h;
 }
 
@@ -277,7 +312,7 @@ void sel_rmhook(sel_hook *h)
 {
   if (h->next)
     h->next->prev = h->prev;
-  h->prev->next = h->next;
+  *h->prev = h->next;
 }
 
 /* --- @sel_fdmerge@ --- *
@@ -385,19 +420,34 @@ int sel_select(sel_state *s)
 
   /* --- Run through the timers --- */
 
-  if (s->timers && TV_CMP(&s->timers->tv, <=, &a.now)) {
-    sel_timer *t, *tt;
-    tt = s->timers;
-    for (t = tt; t && TV_CMP(&t->tv, <=, &a.now); t = t->next)
-      ;
+  if (s->timers) {
+    ptimer *pthead, *pt, **ptt = &pthead;
+    sel_timer *t;
+
+    for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = t->next) {
+      pt = CREATE(ptimer);
+      pt->t = t;
+      t->pend = pt;
+      *ptt = pt;
+      ptt = &pt->next;
+    }
+    *ptt = 0;
     if (t) {
-      t->prev->next = 0;
-      t->prev = (sel_timer *)&s->timers;
+      *t->prev = 0;
+      t->prev = &s->timers;
     }
     s->timers = t;
-    for (t = tt; t; t = tt) {
-      tt = t->next;
-      t->func(&a.now, t->p);
+    while (pthead) {
+      pt = pthead;
+      pthead = pt->next;
+      t = pt->t;
+      if (t) {
+       t->pend = 0;
+       t->next = 0;
+       t->prev = &t->next;
+       t->func(&a.now, t->p);
+      }
+      DESTROY(pt);
     }
   }
 
@@ -412,11 +462,28 @@ int sel_select(sel_state *s)
     int i;
 
     for (i = 0; i < SEL_MODES; i++) {
-      sel_file *f, *ff;
-      for (f = s->files[i]; f; f = ff) {
-       ff = f->next;
-       if (FD_ISSET(f->fd, a.fd + i))
+      pfile *pfhead, *pf, **pff = &pfhead;
+      sel_file *f;
+
+      for (f = s->files[i]; f; f = f->next) {
+       if (!FD_ISSET(f->fd, &a.fd[i]))
+         continue;
+       pf = CREATE(pfile);
+       pf->f = f;
+       f->pend = pf;
+       *pff = pf;
+       pff = &pf->next;
+      }
+      *pff = 0;
+      while (pfhead) {
+       pf = pfhead;
+       pfhead = pf->next;
+       f = pf->f;
+       if (f) {
+         f->pend = 0;
          f->func(f->fd, i, f->p);
+       }
+       DESTROY(pf);
       }
     }
   }