1
0
mirror of https://github.com/zsh-users/zsh synced 2024-07-08 19:55:44 +00:00

15919: use LinkLists in place of somewhat equivalent code

This commit is contained in:
Clint Adams 2001-10-02 02:35:00 +00:00
parent 89719fbac3
commit 1bbe1d1b0a
4 changed files with 128 additions and 72 deletions

View File

@ -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.

View File

@ -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;
}
@ -275,63 +257,51 @@ tcp_socket(int domain, int type, int protocol, int ztflags)
return sess;
}
static int
ztcp_free_session(Tcp_session sess)
{
zfree(sess, sizeof(struct tcp_session));
return 0;
}
static int
zts_delete(Tcp_session sess)
{
Tcp_session tsess;
LinkNode node;
tsess = zts_head();
node = linknodebydatum(ztcp_sessions, (void *)sess);
if(!sess) return 1;
if (tsess == sess)
if (!node)
{
ztcp_head = sess->next;
free(sess);
return 0;
return 1;
}
while((tsess->next != sess) && (tsess->next)) {
tsess = zts_next(tsess);
}
ztcp_free_session(getdata(node));
remnode(ztcp_sessions, node);
if (!tsess->next) return 1;
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;
}

View File

@ -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;
};

View File

@ -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;
}