rlist.h代码:
#ifndef __LIST_H #define __LIST_H #include <stdbool.h> #include <pthread.h> #define UT_BASE(TYPE) struct { TYPE *prev; TYPE *next; } #define listSize(p) (p->listlen) #define nodeSize(p) (p->nodelen) #define listHead(p) (p->head) #define listTail(p) (p->tail) typedef struct listNode{ struct listNode *prev; struct listNode *next; short int type; char buf[]; }listNode; typedef struct list { unsigned long nodelen; listNode *head; listNode *tail; UT_BASE(struct list) base; pthread_mutex_t lock; }list; typedef struct listmgr { unsigned int listlen; list *head; list *tail; pthread_mutex_t lock; }listmgr; /* *date:2015-08-29 *author:zhoulin *function:create a double link list *parameter: * direct:if true,add list to head */ list *listCreate(bool direct); int listRelease(list *lt); listNode *listAddNode(list *lt,size_t size,bool direct); int listDelNode(listNode *nd); #endif
2.rlist.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "rlist.h" static listmgr *mgr=NULL; list * listCreate(bool direct) { #ifdef INFO printf(" ****************Enter listCreate(bool =%d)****************\n",direct); #endif list *cur; if(!mgr){ mgr=(listmgr *)malloc(sizeof(*mgr)); pthread_mutex_init(&mgr->lock,NULL); mgr->listlen=0; } if(!(cur=(list *)malloc(sizeof(*cur)))) return NULL; if(!mgr->head){ pthread_mutex_lock(&mgr->lock); cur->base.prev=cur->base.next=NULL; mgr->head=mgr->tail=cur; ++mgr->listlen; pthread_mutex_unlock(&mgr->lock); } else{ if(direct){ cur->base.prev=mgr->head->base.prev; cur->base.next=mgr->head; mgr->head->base.prev=cur; mgr->head=cur; } else{ cur->base.next=mgr->tail->base.next; mgr->tail->base.next=cur; cur->base.prev=mgr->tail; mgr->tail=cur; } pthread_mutex_lock(&mgr->lock); ++mgr->listlen; cur->nodelen=0; cur->head=cur->tail=NULL; pthread_mutex_unlock(&mgr->lock); } #ifdef INFO printf(" ->add list =%p\n",cur); #endif return cur; } int listRelease(list *lt) { #ifdef INFO printf(" ****************Enter listRelease(list =%p)****************\n\n",lt); #endif if(!mgr){return 1;} if(!lt){return 1;} if(mgr->listlen<=0){return 1;} if(lt->base.prev&<->base.next){ lt->base.prev->base.next=lt->base.next; lt->base.next->base.prev=lt->base.prev; } else if(!lt->base.prev&<->base.next){ mgr->head=lt->base.next; mgr->head->base.prev=NULL; } else if(lt->base.prev&&!lt->base.next){ mgr->tail=lt->base.prev; mgr->tail->base.next=NULL; } else{ mgr->head=mgr->tail=NULL; } lt->head=lt->tail=NULL; lt->base.prev=lt->base.next=NULL; listNode *nd=lt->head; while(nd!=NULL) { listNode *nd_next=nd->next; free(nd); nd=nd_next; } free(lt); lt=NULL; pthread_mutex_lock(&mgr->lock); --mgr->listlen; pthread_mutex_unlock(&mgr->lock); return 0; } void listPrt(listmgr *mgr) { #ifdef INFO printf(" ****************Enter listPrt(listmgr =%p)****************\n",mgr); printf(" mgr->head =%p,mgr->tail =%p,mgr->listlen =%d\n",mgr->head,mgr->tail,mgr->listlen); #endif list *lt=mgr->head; while(lt!=NULL) { #ifdef INFO printf(" current list =%p,node of len=%d,prev = %p,next =%p\n",lt,lt->nodelen,lt->base.prev,lt->base.next); #endif listNode *lnd=lt->head; while(lnd!=NULL) { #ifdef INFO printf(" listNode =%p,prev = %p,next =%p\n",lnd,lnd->prev,lnd->next); #endif lnd=lnd->next; } lt=lt->base.next; } } void listDestroy(listmgr *mgr) { #ifdef INFO printf(" ****************Enter listDestroy(listmgr =%p)****************\n",mgr); #endif list *lt=mgr->head; while(lt!=NULL) { list *lt_next=lt->base.next; listNode *tmp=lt->head; #ifdef INFO printf(" ##free list = %p\n",lt); #endif while(tmp!=NULL) { listNode *t=tmp->next; #ifdef INFO printf(" ->free listNode =%p\n",tmp); #endif free(tmp); tmp=t; } free(lt); lt=lt_next; } } int main(void) { printf("@=1\n\n"); list *a0=listCreate(true); listPrt(mgr); listRelease(a0); listPrt(mgr); printf("@=2 head\n\n"); list *a1=listCreate(false); list *a2=listCreate(true); listPrt(mgr); listRelease(a2); listPrt(mgr); printf("@=2 tail\n\n"); a2=listCreate(true); listPrt(mgr); listRelease(a1); listPrt(mgr); printf("@>2 \n\n"); a1=listCreate(false); list *a4=listCreate(true); list *a5=listCreate(false); list *a6=listCreate(true); listPrt(mgr); listRelease(a4); listPrt(mgr); listDestroy(mgr); return 0; }
有不足之处,请大家多指教
时间: 2024-10-16 10:19:36