[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Talarm() problem



Kay was right with his complaint about possible memory leaks :-(.
After some heavy networking i had quite some expired timeouts lying
around. Please try this patch. It adds a timeout for expired timeouts,
i.e. if a timout structure is on the expire_list for more than 2
seconds it is disposed automagically.

--- timeout.c~	Sat Sep  3 16:49:56 1994
+++ timeout.c	Wed Sep 14 19:02:16 1994
@@ -34,6 +34,9 @@
 TIMEOUT *tlist = NULL;
 TIMEOUT *expire_list = NULL;
 
+/* Number of ticks after that an expired timeout is considered to be old
+   and disposed automatically.  */
+#define TIMEOUT_EXPIRE_LIMIT 400 /* 2 secs */
 
 static TIMEOUT *
 newtimeout(fromlist)
@@ -72,6 +75,32 @@
 }
 
 static void
+dispose_old_timeouts ()
+{
+  TIMEOUT *t, **prev, *old;
+  long now = *(long *) 0x4ba;
+  short sr = spl7 ();
+
+  for (prev = &expire_list, t = *prev; t; prev = &t->next, t = *prev)
+    {
+      if (t->when < now)
+	{
+	  /* This and the following timeouts are too old. Throw them away. */
+	  *prev = 0;
+	  spl (sr);
+	  while (t)
+	    {
+	      old = t;
+	      t = t->next;
+	      disposetimeout (old);
+	    }
+	  return;
+	}
+    }
+  spl (sr);
+}
+
+static void
 inserttimeout(t, delta)
 	TIMEOUT *t;
 	long delta;
@@ -120,9 +149,11 @@
 {
 	TIMEOUT *t;
 	TIMEOUT **prev;
+	short sr;
 
 	/* Try to reuse an already expired timeout that had the
 	   same function attached */
+	sr = spl7 ();
 	prev = &expire_list;
 	for (t = *prev; t != NULL; prev = &t->next, t = *prev)
 	  if (t->proc == curproc && t->func == func)
@@ -130,6 +161,7 @@
 	      *prev = t->next;
 	      break;
 	    }
+	spl (sr);
 
 	if (t == NULL)
 	  t = newtimeout(0);
@@ -172,9 +204,11 @@
 {
 	TIMEOUT *t;
 	TIMEOUT **prev;
+	short sr;
 
 	/* Try to reuse an already expired timeout that had the
 	   same function attached */
+	sr = spl7 ();
 	prev = &expire_list;
 	for (t = *prev; t != NULL; t = *prev)
 	  {
@@ -185,6 +219,7 @@
 	      }
 	    prev = &t->next;
 	  }
+	spl (sr);
 
 	if (!t)
 	  t = newtimeout(flags & 1);
@@ -402,6 +437,7 @@
 		if (tlist)
 			tlist->when += delta;
 		old->next = expire_list;
+		old->when = *(long *) 0x4ba + TIMEOUT_EXPIRE_LIMIT;
 		expire_list = old;
 		spl(sr);
 /* ++kay: debug output at spl7 hangs the system, so moved it here */
@@ -412,6 +448,8 @@
 		sr = spl7();
 	}
 	spl(sr);
+	/* Now look at the expired timeouts if some are getting old */
+	dispose_old_timeouts ();
 }
 
 /*