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; size_t size; 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,void *data,size_t size,bool direct); int listDelNode(list *lt,listNode *nd); #endif
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)**************** ",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 ",cur); #endif return cur; } int listRelease(list *lt) { #ifdef INFO printf(" ****************Enter listRelease(list =%p)**************** ",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)**************** ",mgr); printf(" mgr->head =%p,mgr->tail =%p,mgr->listlen =%d ",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 ",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 ",lnd,lnd->prev,lnd->next); #endif lnd=lnd->next; } lt=lt->base.next; } } void listDestroy(listmgr *mgr) { #ifdef INFO printf(" ****************Enter listDestroy(listmgr =%p)**************** ",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 ",lt); #endif while(tmp!=NULL) { listNode *t=tmp->next; #ifdef INFO printf(" ->free listNode =%p ",tmp); #endif free(tmp); tmp=t; } free(lt); lt=lt_next; } } listNode *listAddNode(list *lt,void *data,size_t size,bool direct) { printf(" ****************listAddNode(list =%p,data=%p,size =%d,direct =%d)**************** ",lt,data,size,direct); listNode *cur=NULL; if(!lt||size<=0){return NULL;} if((cur=(listNode *)malloc(sizeof(*cur)+size))==NULL){ return NULL; } memset((char *)&cur->buf,'