Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

Commit 1ae3ac48 authored by rswindell's avatar rswindell

Created (optional) client reference counter for linked list, incremented and

decremented with listInit() (optionally), listAttach() and listDetach().
listDetach() will automatically free the list when the reference count is 0.
parent 419594dc
......@@ -70,6 +70,9 @@ link_list_t* listInit(link_list_t* list, long flags)
sem_init(&list->sem,0,0);
#endif
if(flags&LINK_LIST_ATTACH)
listAttach(list);
return(list);
}
......@@ -135,6 +138,34 @@ BOOL listFree(link_list_t* list)
return(TRUE);
}
long listAttach(link_list_t* list)
{
if(list==NULL)
return(-1);
MUTEX_LOCK(list);
list->refs++;
MUTEX_UNLOCK(list);
return(list->refs);
}
long listDettach(link_list_t* list)
{
int refs;
if(list==NULL || list->refs<1)
return(-1);
MUTEX_LOCK(list);
if((refs=--list->refs)==0)
listFree(list);
else
MUTEX_UNLOCK(list);
return(refs);
}
void* listSetPrivateData(link_list_t* list, void* p)
{
void* old;
......
......@@ -59,6 +59,7 @@ extern "C" {
#define LINK_LIST_MUTEX (1<<3) /* Mutex-protected linked-list */
#define LINK_LIST_SEMAPHORE (1<<4) /* Semaphore attached to linked-list */
#define LINK_LIST_NODE_LOCKED (1<<5) /* Node is locked */
#define LINK_LIST_ATTACH (1<<6) /* Attach during init */
typedef struct list_node {
void* data; /* pointer to some kind of data */
......@@ -74,6 +75,7 @@ typedef struct link_list {
unsigned long flags; /* private use flags */
long count; /* number of nodes in list */
void* private_data; /* for use by the application only */
long refs; /* reference counter (attached clients) */
#if defined(LINK_LIST_THREADSAFE)
pthread_mutex_t mutex;
sem_t sem;
......@@ -86,6 +88,10 @@ BOOL listFree(link_list_t*);
long listFreeNodes(link_list_t*);
BOOL listFreeNodeData(list_node_t* node);
/* Increment/decrement reference counter (and auto-free when zero), returns -1 on error */
long listAttach(link_list_t*);
long listDetach(link_list_t*);
#if defined(LINK_LIST_THREADSAFE)
BOOL listSemPost(const link_list_t*);
BOOL listSemWait(const link_list_t*);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment