summary refs log tree commit diff
diff options
context:
space:
mode:
authorClint Adams <clint@users.sourceforge.net>2001-10-02 02:35:00 +0000
committerClint Adams <clint@users.sourceforge.net>2001-10-02 02:35:00 +0000
commit1bbe1d1b0a60d0a35259f6dc4adf53fb1cbfe759 (patch)
tree0a6d0af71fced8f524ee8faa1392ef8fcf0596f5
parent89719fbac345ea6699d6fd6b5d07dc5d4ec28ea6 (diff)
downloadzsh-1bbe1d1b0a60d0a35259f6dc4adf53fb1cbfe759.tar.gz
zsh-1bbe1d1b0a60d0a35259f6dc4adf53fb1cbfe759.tar.xz
zsh-1bbe1d1b0a60d0a35259f6dc4adf53fb1cbfe759.zip
15919: use LinkLists in place of somewhat equivalent code
-rw-r--r--ChangeLog5
-rw-r--r--Src/Modules/tcp.c91
-rw-r--r--Src/Modules/tcp.h1
-rw-r--r--Src/linklist.c103
4 files changed, 128 insertions, 72 deletions
diff --git a/ChangeLog b/ChangeLog
index de438fa08..86a383796 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2001-10-02  Clint Adams  <clint@zsh.org>
+
+	* 15919: Src/linklist.c, Src/Modules/tcp.c, Src/Modules/tcp.h:
+	use LinkLists in tcp module rather than somewhat equivalent code.
+
 2001-10-01  Peter Stephenson  <pws@csr.com>
 
 	* 15917: Test/C03traps.ztst: test for EXIT trap fix in 15844.
diff --git a/Src/Modules/tcp.c b/Src/Modules/tcp.c
index ae1b21912..405ce2aa6 100644
--- a/Src/Modules/tcp.c
+++ b/Src/Modules/tcp.c
@@ -226,19 +226,7 @@ freehostent(struct hostent *ptr)
 /**/
 #endif /* !HAVE_GETIPNODEBYNAME */
 
-Tcp_session ztcp_head = NULL, ztcp_tail = NULL;
-
-static Tcp_session
-zts_head(void)
-{
-    return ztcp_head;
-}
-
-static Tcp_session
-zts_next(Tcp_session cur)
-{
-    return cur ? cur->next : NULL;
-}
+LinkList ztcp_sessions;
 
 /* "allocate" a tcp_session */
 static Tcp_session
@@ -249,16 +237,10 @@ zts_alloc(int ztflags)
     sess = (Tcp_session)zcalloc(sizeof(struct tcp_session));
     if (!sess) return NULL;
     sess->fd=-1;
-    sess->next=NULL;
     sess->flags=ztflags;
 
-    if (!zts_head()) {
-	ztcp_head = ztcp_tail = sess;
-    }
-    else {
-	ztcp_tail->next = sess;
-	ztcp_tail = sess;
-    }
+    zinsertlinknode(ztcp_sessions, lastnode(ztcp_sessions), (void *)sess);
+
     return sess;
 }
 
@@ -276,62 +258,50 @@ tcp_socket(int domain, int type, int protocol, int ztflags)
 }
 
 static int
-zts_delete(Tcp_session sess)
+ztcp_free_session(Tcp_session sess)
 {
-    Tcp_session tsess;
+    zfree(sess, sizeof(struct tcp_session));
 
-    tsess = zts_head();
+    return 0;
+}
 
-    if(!sess) return 1;
+static int
+zts_delete(Tcp_session sess)
+{
+    LinkNode node;
 
-    if (tsess == sess)
-    {
-	ztcp_head = sess->next;
-	free(sess);
-	return 0;
-    }
+    node = linknodebydatum(ztcp_sessions, (void *)sess);
 
-    while((tsess->next != sess) && (tsess->next)) {
-	tsess = zts_next(tsess);
+    if (!node)
+    {
+	return 1;
     }
 
-    if (!tsess->next) return 1;
+    ztcp_free_session(getdata(node));
+    remnode(ztcp_sessions, node);
 
-    if (ztcp_tail == sess)
-	ztcp_tail = tsess;
-    tsess->next = sess->next;
-    free(sess);
     return 0;
-
 }
 
 static Tcp_session
 zts_byfd(int fd)
 {
-    Tcp_session tsess;
-
-    tsess = zts_head();
-
-    while(tsess != NULL) {
-	if (tsess->fd == fd)
-	    return tsess;
-
-	tsess = zts_next(tsess);
-    }
-
+    LinkNode node;
+    
+    for (node = firstnode(ztcp_sessions); node; incnode(node))
+	if (((Tcp_session)getdata(node))->fd == fd)
+	    return (Tcp_session)node;
+    
     return NULL;
 }
 
 static void
 tcp_cleanup(void)
 {
-    Tcp_session sess, prev;
-    
-    for(sess = zts_head(); sess != NULL; sess = zts_next(prev))
-    {
-	prev = sess;
-	tcp_close(sess);
-    }
+    LinkNode node;
+
+    for (node = firstnode(ztcp_sessions); node; incnode(node))
+	tcp_close((Tcp_session)getdata(node));
 }
 
 /**/
@@ -601,8 +571,11 @@ bin_ztcp(char *nam, char **args, char *ops, int func)
     {
 	
 	if (!dargs[0]) {
-	    for(sess = zts_head(); sess != NULL; sess = zts_next(sess))
+	    LinkNode node;
+	    for(node = firstnode(ztcp_sessions); node; incnode(node))
 	    {
+		sess = (Tcp_session)getdata(node);
+
 		if (sess->fd != -1)
 		{
 		    zthost = gethostbyaddr(&(sess->sock.in.sin_addr), sizeof(struct sockaddr_in), AF_INET);
@@ -708,6 +681,7 @@ setup_(Module m)
 int
 boot_(Module m)
 {
+    ztcp_sessions = znewlinklist();
     return !addbuiltins(m->nam, bintab, sizeof(bintab)/sizeof(*bintab));
 }
 
@@ -717,6 +691,7 @@ int
 cleanup_(Module m)
 {
     tcp_cleanup();
+    freelinklist(ztcp_sessions, (FreeFunc) ztcp_free_session);
     return 0;
 }
 
diff --git a/Src/Modules/tcp.h b/Src/Modules/tcp.h
index 05684977f..5d3892861 100644
--- a/Src/Modules/tcp.h
+++ b/Src/Modules/tcp.h
@@ -80,7 +80,6 @@ struct tcp_session {
     int fd;				/* file descriptor */
     union tcp_sockaddr sock;  	/* local address   */
     union tcp_sockaddr peer;  	/* remote address  */
-    Tcp_session next;
     int flags;
 };
 
diff --git a/Src/linklist.c b/Src/linklist.c
index 62a962595..c5a2e2b1a 100644
--- a/Src/linklist.c
+++ b/Src/linklist.c
@@ -33,12 +33,24 @@
 /* Get an empty linked list header */
 
 /**/
-LinkList
+mod_export LinkList
 newlinklist(void)
 {
     LinkList list;
 
-    list = (LinkList) alloc(sizeof *list);
+    list = (LinkList) zhalloc(sizeof *list);
+    list->first = NULL;
+    list->last = (LinkNode) list;
+    return list;
+}
+
+/**/
+mod_export LinkList
+znewlinklist(void)
+{
+    LinkList list;
+
+    list = (LinkList) zalloc(sizeof *list);
     list->first = NULL;
     list->last = (LinkNode) list;
     return list;
@@ -47,13 +59,31 @@ newlinklist(void)
 /* Insert a node in a linked list after a given node */
 
 /**/
-LinkNode
+mod_export LinkNode
 insertlinknode(LinkList list, LinkNode node, void *dat)
 {
     LinkNode tmp, new;
 
     tmp = node->next;
-    node->next = new = (LinkNode) alloc(sizeof *tmp);
+    node->next = new = (LinkNode) zhalloc(sizeof *tmp);
+    new->last = node;
+    new->dat = dat;
+    new->next = tmp;
+    if (tmp)
+	tmp->last = new;
+    else
+	list->last = new;
+    return new;
+}
+
+/**/
+mod_export LinkNode
+zinsertlinknode(LinkList list, LinkNode node, void *dat)
+{
+    LinkNode tmp, new;
+
+    tmp = node->next;
+    node->next = new = (LinkNode) zalloc(sizeof *tmp);
     new->last = node;
     new->dat = dat;
     new->next = tmp;
@@ -67,7 +97,7 @@ insertlinknode(LinkList list, LinkNode node, void *dat)
 /* Insert an already-existing node into a linked list after a given node */
 
 /**/
-LinkNode
+mod_export LinkNode
 uinsertlinknode(LinkList list, LinkNode node, LinkNode new)
 {
     LinkNode tmp = node->next;
@@ -84,7 +114,7 @@ uinsertlinknode(LinkList list, LinkNode node, LinkNode new)
 /* Insert a list in another list */
 
 /**/
-void
+mod_export void
 insertlinklist(LinkList l, LinkNode where, LinkList x)
 {
     LinkNode nx;
@@ -104,7 +134,7 @@ insertlinklist(LinkList l, LinkNode where, LinkList x)
 /* Get top node in a linked list */
 
 /**/
-void *
+mod_export void *
 getlinknode(LinkList list)
 {
     void *dat;
@@ -125,7 +155,7 @@ getlinknode(LinkList list)
 /* Get top node in a linked list without freeing */
 
 /**/
-void *
+mod_export void *
 ugetnode(LinkList list)
 {
     void *dat;
@@ -145,7 +175,7 @@ ugetnode(LinkList list)
 /* Remove a node from a linked list */
 
 /**/
-void *
+mod_export void *
 remnode(LinkList list, LinkNode nd)
 {
     void *dat;
@@ -164,7 +194,7 @@ remnode(LinkList list, LinkNode nd)
 /* Remove a node from a linked list without freeing */
 
 /**/
-void *
+mod_export void *
 uremnode(LinkList list, LinkNode nd)
 {
     void *dat;
@@ -181,7 +211,7 @@ uremnode(LinkList list, LinkNode nd)
 /* Free a linked list */
 
 /**/
-void
+mod_export void
 freelinklist(LinkList list, FreeFunc freefunc)
 {
     LinkNode node, next;
@@ -198,7 +228,7 @@ freelinklist(LinkList list, FreeFunc freefunc)
 /* Count the number of nodes in a linked list */
 
 /**/
-int
+mod_export int
 countlinknodes(LinkList list)
 {
     LinkNode nd;
@@ -209,7 +239,7 @@ countlinknodes(LinkList list)
 }
 
 /**/
-void
+mod_export void
 rolllist(LinkList l, LinkNode nd)
 {
     l->last->next = l->first;
@@ -220,3 +250,50 @@ rolllist(LinkList l, LinkNode nd)
     l->last->next = 0;
 }
 
+/**/
+mod_export LinkList
+newsizedlist(int size)
+{
+    LinkList list;
+    LinkNode node;
+
+    list = (LinkList) zhalloc(sizeof(struct linklist) +
+			      (size * sizeof(struct linknode)));
+
+    list->first = (LinkNode) (list + 1);
+    for (node = list->first; size; size--, node++) {
+	node->last = node - 1;
+	node->next = node + 1;
+    }
+    list->last = node - 1;
+    list->first->last = (LinkNode) list;
+    node[-1].next = NULL;
+
+    return list;
+}
+
+/**/
+mod_export int
+listcontains(LinkList list, void *dat)
+{
+    LinkNode node;
+
+    for (node = firstnode(list); node; incnode(node))
+	if (getdata(node) == dat)
+	    return 1;
+
+    return 0;
+}
+
+/**/
+mod_export LinkNode
+linknodebydatum(LinkList list, void *dat)
+{
+    LinkNode node;
+
+    for (node = firstnode(list); node; incnode(node))
+	if (getdata(node) == dat)
+	    return node;
+
+    return NULL;
+}