about summary refs log tree commit diff
path: root/Src/Modules
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 /Src/Modules
parent89719fbac345ea6699d6fd6b5d07dc5d4ec28ea6 (diff)
downloadzsh-1bbe1d1b0a60d0a35259f6dc4adf53fb1cbfe759.tar.gz
zsh-1bbe1d1b0a60d0a35259f6dc4adf53fb1cbfe759.tar.xz
zsh-1bbe1d1b0a60d0a35259f6dc4adf53fb1cbfe759.zip
15919: use LinkLists in place of somewhat equivalent code
Diffstat (limited to 'Src/Modules')
-rw-r--r--Src/Modules/tcp.c91
-rw-r--r--Src/Modules/tcp.h1
2 files changed, 33 insertions, 59 deletions
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;
 };