Skip to content
Snippets Groups Projects
Commit ec972cd4 authored by Danny Auble's avatar Danny Auble
Browse files

revert whitespace

parent 027f4ab7
No related branches found
No related tags found
No related merge requests found
...@@ -57,7 +57,7 @@ ...@@ -57,7 +57,7 @@
/* /*
** Define slurm-specific aliases for use by plugins, see slurm_xlator.h ** Define slurm-specific aliases for use by plugins, see slurm_xlator.h
** for details. ** for details.
*/ */
strong_alias(list_create, slurm_list_create); strong_alias(list_create, slurm_list_create);
strong_alias(list_destroy, slurm_list_destroy); strong_alias(list_destroy, slurm_list_destroy);
strong_alias(list_is_empty, slurm_list_is_empty); strong_alias(list_is_empty, slurm_list_is_empty);
...@@ -91,17 +91,17 @@ strong_alias(list_install_fork_handlers, slurm_list_install_fork_handlers); ...@@ -91,17 +91,17 @@ strong_alias(list_install_fork_handlers, slurm_list_install_fork_handlers);
#include <unistd.h> #include <unistd.h>
#ifdef WITH_LSD_FATAL_ERROR_FUNC #ifdef WITH_LSD_FATAL_ERROR_FUNC
# undef lsd_fatal_error # undef lsd_fatal_error
extern void lsd_fatal_error(char *file, int line, char *mesg); extern void lsd_fatal_error(char *file, int line, char *mesg);
#else /* !WITH_LSD_FATAL_ERROR_FUNC */ #else /* !WITH_LSD_FATAL_ERROR_FUNC */
# ifndef lsd_fatal_error # ifndef lsd_fatal_error
# include <errno.h> # include <errno.h>
# include <stdio.h> # include <stdio.h>
# include <string.h> # include <string.h>
# define lsd_fatal_error(file, line, mesg) \ # define lsd_fatal_error(file, line, mesg) \
do { \ do { \
fprintf(stderr, "ERROR: [%s:%d] %s: %s\n", \ fprintf(stderr, "ERROR: [%s:%d] %s: %s\n", \
file, line, mesg, strerror(errno)); \ file, line, mesg, strerror(errno)); \
} while (0) } while (0)
# endif /* !lsd_fatal_error */ # endif /* !lsd_fatal_error */
#endif /* !WITH_LSD_FATAL_ERROR_FUNC */ #endif /* !WITH_LSD_FATAL_ERROR_FUNC */
...@@ -112,7 +112,7 @@ extern void lsd_fatal_error(char *file, int line, char *mesg); ...@@ -112,7 +112,7 @@ extern void lsd_fatal_error(char *file, int line, char *mesg);
#ifdef WITH_LSD_NOMEM_ERROR_FUNC #ifdef WITH_LSD_NOMEM_ERROR_FUNC
# undef lsd_nomem_error # undef lsd_nomem_error
extern void * lsd_nomem_error(char *file, int line, char *mesg); extern void * lsd_nomem_error(char *file, int line, char *mesg);
#else /* !WITH_LSD_NOMEM_ERROR_FUNC */ #else /* !WITH_LSD_NOMEM_ERROR_FUNC */
# ifndef lsd_nomem_error # ifndef lsd_nomem_error
# define lsd_nomem_error(file, line, mesg) (NULL) # define lsd_nomem_error(file, line, mesg) (NULL)
...@@ -152,31 +152,31 @@ extern void * lsd_nomem_error(char *file, int line, char *mesg); ...@@ -152,31 +152,31 @@ extern void * lsd_nomem_error(char *file, int line, char *mesg);
****************/ ****************/
struct listNode { struct listNode {
void *data; /* node's data */ void *data; /* node's data */
struct listNode *next; /* next node in list */ struct listNode *next; /* next node in list */
}; };
struct listIterator { struct listIterator {
struct list *list; /* the list being iterated */ struct list *list; /* the list being iterated */
struct listNode *pos; /* the next node to be iterated */ struct listNode *pos; /* the next node to be iterated */
struct listNode **prev; /* addr of 'next' ptr to prv It node */ struct listNode **prev; /* addr of 'next' ptr to prv It node */
struct listIterator *iNext; /* iterator chain for list_destroy() */ struct listIterator *iNext; /* iterator chain for list_destroy() */
#ifndef NDEBUG #ifndef NDEBUG
unsigned int magic; /* sentinel for asserting validity */ unsigned int magic; /* sentinel for asserting validity */
#endif /* !NDEBUG */ #endif /* !NDEBUG */
}; };
struct list { struct list {
struct listNode *head; /* head of the list */ struct listNode *head; /* head of the list */
struct listNode **tail; /* addr of last node's 'next' ptr */ struct listNode **tail; /* addr of last node's 'next' ptr */
struct listIterator *iNext; /* iterator chain for list_destroy() */ struct listIterator *iNext; /* iterator chain for list_destroy() */
ListDelF fDel; /* function to delete node data */ ListDelF fDel; /* function to delete node data */
int count; /* number of nodes in list */ int count; /* number of nodes in list */
#ifdef WITH_PTHREADS #ifdef WITH_PTHREADS
pthread_mutex_t mutex; /* mutex to protect access to list */ pthread_mutex_t mutex; /* mutex to protect access to list */
#endif /* WITH_PTHREADS */ #endif /* WITH_PTHREADS */
#ifndef NDEBUG #ifndef NDEBUG
unsigned int magic; /* sentinel for asserting validity */ unsigned int magic; /* sentinel for asserting validity */
#endif /* !NDEBUG */ #endif /* !NDEBUG */
}; };
...@@ -218,48 +218,48 @@ static pthread_mutex_t list_free_lock = PTHREAD_MUTEX_INITIALIZER; ...@@ -218,48 +218,48 @@ static pthread_mutex_t list_free_lock = PTHREAD_MUTEX_INITIALIZER;
#ifdef WITH_PTHREADS #ifdef WITH_PTHREADS
# define list_mutex_init(mutex) \ # define list_mutex_init(mutex) \
do { \ do { \
int e = pthread_mutex_init(mutex, NULL); \ int e = pthread_mutex_init(mutex, NULL); \
if (e != 0) { \ if (e != 0) { \
errno = e; \ errno = e; \
lsd_fatal_error(__FILE__, __LINE__, "list mutex init"); \ lsd_fatal_error(__FILE__, __LINE__, "list mutex init"); \
abort(); \ abort(); \
} \ } \
} while (0) } while (0)
# define list_mutex_lock(mutex) \ # define list_mutex_lock(mutex) \
do { \ do { \
int e = pthread_mutex_lock(mutex); \ int e = pthread_mutex_lock(mutex); \
if (e != 0) { \ if (e != 0) { \
errno = e; \ errno = e; \
lsd_fatal_error(__FILE__, __LINE__, "list mutex lock"); \ lsd_fatal_error(__FILE__, __LINE__, "list mutex lock"); \
abort(); \ abort(); \
} \ } \
} while (0) } while (0)
# define list_mutex_unlock(mutex) \ # define list_mutex_unlock(mutex) \
do { \ do { \
int e = pthread_mutex_unlock(mutex); \ int e = pthread_mutex_unlock(mutex); \
if (e != 0) { \ if (e != 0) { \
errno = e; \ errno = e; \
lsd_fatal_error(__FILE__, __LINE__, "list mutex unlock"); \ lsd_fatal_error(__FILE__, __LINE__, "list mutex unlock"); \
abort(); \ abort(); \
} \ } \
} while (0) } while (0)
# define list_mutex_destroy(mutex) \ # define list_mutex_destroy(mutex) \
do { \ do { \
int e = pthread_mutex_destroy(mutex); \ int e = pthread_mutex_destroy(mutex); \
if (e != 0) { \ if (e != 0) { \
errno = e; \ errno = e; \
lsd_fatal_error(__FILE__, __LINE__, "list mutex destroy"); \ lsd_fatal_error(__FILE__, __LINE__, "list mutex destroy"); \
abort(); \ abort(); \
} \ } \
} while (0) } while (0)
# ifndef NDEBUG # ifndef NDEBUG
static int list_mutex_is_locked (pthread_mutex_t *mutex); static int list_mutex_is_locked (pthread_mutex_t *mutex);
# endif /* !NDEBUG */ # endif /* !NDEBUG */
#else /* !WITH_PTHREADS */ #else /* !WITH_PTHREADS */
...@@ -280,249 +280,249 @@ static int list_mutex_is_locked (pthread_mutex_t *mutex); ...@@ -280,249 +280,249 @@ static int list_mutex_is_locked (pthread_mutex_t *mutex);
List List
list_create (ListDelF f) list_create (ListDelF f)
{ {
List l; List l;
if (!(l = list_alloc())) if (!(l = list_alloc()))
return(lsd_nomem_error(__FILE__, __LINE__, "list create")); return(lsd_nomem_error(__FILE__, __LINE__, "list create"));
l->head = NULL; l->head = NULL;
l->tail = &l->head; l->tail = &l->head;
l->iNext = NULL; l->iNext = NULL;
l->fDel = f; l->fDel = f;
l->count = 0; l->count = 0;
list_mutex_init(&l->mutex); list_mutex_init(&l->mutex);
assert(l->magic = LIST_MAGIC); /* set magic via assert abuse */ assert(l->magic = LIST_MAGIC); /* set magic via assert abuse */
return(l); return(l);
} }
void void
list_destroy (List l) list_destroy (List l)
{ {
ListIterator i, iTmp; ListIterator i, iTmp;
ListNode p, pTmp; ListNode p, pTmp;
assert(l != NULL); assert(l != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
i = l->iNext; i = l->iNext;
while (i) { while (i) {
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
iTmp = i->iNext; iTmp = i->iNext;
assert(i->magic = ~LIST_MAGIC); /* clear magic via assert abuse */ assert(i->magic = ~LIST_MAGIC); /* clear magic via assert abuse */
list_iterator_free(i); list_iterator_free(i);
i = iTmp; i = iTmp;
} }
p = l->head; p = l->head;
while (p) { while (p) {
pTmp = p->next; pTmp = p->next;
if (p->data && l->fDel) if (p->data && l->fDel)
l->fDel(p->data); l->fDel(p->data);
list_node_free(p); list_node_free(p);
p = pTmp; p = pTmp;
} }
assert(l->magic = ~LIST_MAGIC); /* clear magic via assert abuse */ assert(l->magic = ~LIST_MAGIC); /* clear magic via assert abuse */
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
list_mutex_destroy(&l->mutex); list_mutex_destroy(&l->mutex);
list_free(l); list_free(l);
return; return;
} }
int int
list_is_empty (List l) list_is_empty (List l)
{ {
int n; int n;
assert(l != NULL); assert(l != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
n = l->count; n = l->count;
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(n == 0); return(n == 0);
} }
int int
list_count (List l) list_count (List l)
{ {
int n; int n;
assert(l != NULL); assert(l != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
n = l->count; n = l->count;
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(n); return(n);
} }
void * void *
list_append (List l, void *x) list_append (List l, void *x)
{ {
void *v; void *v;
assert(l != NULL); assert(l != NULL);
assert(x != NULL); assert(x != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
v = list_node_create(l, l->tail, x); v = list_node_create(l, l->tail, x);
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(v); return(v);
} }
int int
list_append_list (List l, List sub) list_append_list (List l, List sub)
{ {
ListIterator itr; ListIterator itr;
void *v; void *v;
int n = 0; int n = 0;
assert(l != NULL); assert(l != NULL);
assert(l->fDel == NULL); assert(l->fDel == NULL);
assert(sub != NULL); assert(sub != NULL);
itr = list_iterator_create(sub); itr = list_iterator_create(sub);
while((v = list_next(itr))) { while((v = list_next(itr))) {
if(list_append(l, v)) if(list_append(l, v))
n++; n++;
else else
break; break;
} }
list_iterator_destroy(itr); list_iterator_destroy(itr);
return n; return n;
} }
int int
list_transfer (List l, List sub) list_transfer (List l, List sub)
{ {
void *v; void *v;
int n = 0; int n = 0;
assert(l != NULL); assert(l != NULL);
assert(sub != NULL); assert(sub != NULL);
assert(l->fDel == sub->fDel); assert(l->fDel == sub->fDel);
while((v = list_pop(sub))) { while((v = list_pop(sub))) {
if(list_append(l, v)) if(list_append(l, v))
n++; n++;
else { else {
if(l->fDel) if(l->fDel)
l->fDel(v); l->fDel(v);
break; break;
}
} }
}
return n; return n;
} }
void * void *
list_prepend (List l, void *x) list_prepend (List l, void *x)
{ {
void *v; void *v;
assert(l != NULL); assert(l != NULL);
assert(x != NULL); assert(x != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
v = list_node_create(l, &l->head, x); v = list_node_create(l, &l->head, x);
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(v); return(v);
} }
void * void *
list_find_first (List l, ListFindF f, void *key) list_find_first (List l, ListFindF f, void *key)
{ {
ListNode p; ListNode p;
void *v = NULL; void *v = NULL;
assert(l != NULL); assert(l != NULL);
assert(f != NULL); assert(f != NULL);
assert(key != NULL); assert(key != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
for (p=l->head; p; p=p->next) { for (p=l->head; p; p=p->next) {
if (f(p->data, key)) { if (f(p->data, key)) {
v = p->data; v = p->data;
break; break;
} }
} }
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(v); return(v);
} }
int int
list_delete_all (List l, ListFindF f, void *key) list_delete_all (List l, ListFindF f, void *key)
{ {
ListNode *pp; ListNode *pp;
void *v; void *v;
int n = 0; int n = 0;
assert(l != NULL); assert(l != NULL);
assert(f != NULL); assert(f != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
pp = &l->head; pp = &l->head;
while (*pp) { while (*pp) {
if (f((*pp)->data, key)) { if (f((*pp)->data, key)) {
if ((v = list_node_destroy(l, pp))) { if ((v = list_node_destroy(l, pp))) {
if (l->fDel) if (l->fDel)
l->fDel(v); l->fDel(v);
n++; n++;
} }
} }
else { else {
pp = &(*pp)->next; pp = &(*pp)->next;
} }
} }
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(n); return(n);
} }
int int
list_for_each (List l, ListForF f, void *arg) list_for_each (List l, ListForF f, void *arg)
{ {
ListNode p; ListNode p;
int n = 0; int n = 0;
assert(l != NULL); assert(l != NULL);
assert(f != NULL); assert(f != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
for (p=l->head; p; p=p->next) { for (p=l->head; p; p=p->next) {
n++; n++;
if (f(p->data, arg) < 0) { if (f(p->data, arg) < 0) {
n = -n; n = -n;
break; break;
} }
} }
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(n); return(n);
} }
int int
list_flush (List l) list_flush (List l)
{ {
ListNode *pp; ListNode *pp;
void *v; void *v;
int n = 0; int n = 0;
assert(l != NULL); assert(l != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
pp = &l->head; pp = &l->head;
while (*pp) { while (*pp) {
if ((v = list_node_destroy(l, pp))) { if ((v = list_node_destroy(l, pp))) {
if (l->fDel) if (l->fDel)
l->fDel(v); l->fDel(v);
n++; n++;
}
} }
list_mutex_unlock(&l->mutex); }
return(n); list_mutex_unlock(&l->mutex);
return(n);
} }
...@@ -531,252 +531,252 @@ list_sort (List l, ListCmpF f) ...@@ -531,252 +531,252 @@ list_sort (List l, ListCmpF f)
{ {
/* Note: Time complexity O(n^2). /* Note: Time complexity O(n^2).
*/ */
ListNode *pp, *ppPrev, *ppPos, pTmp; ListNode *pp, *ppPrev, *ppPos, pTmp;
ListIterator i; ListIterator i;
assert(l != NULL); assert(l != NULL);
assert(f != NULL); assert(f != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
if (l->count > 1) { if (l->count > 1) {
ppPrev = &l->head; ppPrev = &l->head;
pp = &(*ppPrev)->next; pp = &(*ppPrev)->next;
while (*pp) { while (*pp) {
if (f((*pp)->data, (*ppPrev)->data) < 0) { if (f((*pp)->data, (*ppPrev)->data) < 0) {
ppPos = &l->head; ppPos = &l->head;
while (f((*pp)->data, (*ppPos)->data) >= 0) while (f((*pp)->data, (*ppPos)->data) >= 0)
ppPos = &(*ppPos)->next; ppPos = &(*ppPos)->next;
pTmp = (*pp)->next; pTmp = (*pp)->next;
(*pp)->next = *ppPos; (*pp)->next = *ppPos;
*ppPos = *pp; *ppPos = *pp;
*pp = pTmp; *pp = pTmp;
if (ppPrev == ppPos) if (ppPrev == ppPos)
ppPrev = &(*ppPrev)->next; ppPrev = &(*ppPrev)->next;
} }
else { else {
ppPrev = pp; ppPrev = pp;
pp = &(*pp)->next; pp = &(*pp)->next;
} }
} }
l->tail = pp; l->tail = pp;
for (i=l->iNext; i; i=i->iNext) { for (i=l->iNext; i; i=i->iNext) {
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
i->pos = i->list->head; i->pos = i->list->head;
i->prev = &i->list->head; i->prev = &i->list->head;
} }
} }
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return; return;
} }
void * void *
list_push (List l, void *x) list_push (List l, void *x)
{ {
void *v; void *v;
assert(l != NULL); assert(l != NULL);
assert(x != NULL); assert(x != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
v = list_node_create(l, &l->head, x); v = list_node_create(l, &l->head, x);
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(v); return(v);
} }
void * void *
list_pop (List l) list_pop (List l)
{ {
void *v; void *v;
assert(l != NULL); assert(l != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
v = list_node_destroy(l, &l->head); v = list_node_destroy(l, &l->head);
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(v); return(v);
} }
void * void *
list_peek (List l) list_peek (List l)
{ {
void *v; void *v;
assert(l != NULL); assert(l != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
v = (l->head) ? l->head->data : NULL; v = (l->head) ? l->head->data : NULL;
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(v); return(v);
} }
void * void *
list_enqueue (List l, void *x) list_enqueue (List l, void *x)
{ {
void *v; void *v;
assert(l != NULL); assert(l != NULL);
assert(x != NULL); assert(x != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
v = list_node_create(l, l->tail, x); v = list_node_create(l, l->tail, x);
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(v); return(v);
} }
void * void *
list_dequeue (List l) list_dequeue (List l)
{ {
void *v; void *v;
assert(l != NULL); assert(l != NULL);
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
v = list_node_destroy(l, &l->head); v = list_node_destroy(l, &l->head);
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(v); return(v);
} }
ListIterator ListIterator
list_iterator_create (List l) list_iterator_create (List l)
{ {
ListIterator i; ListIterator i;
assert(l != NULL); assert(l != NULL);
if (!(i = list_iterator_alloc())) if (!(i = list_iterator_alloc()))
return(lsd_nomem_error(__FILE__, __LINE__, "list iterator create")); return(lsd_nomem_error(__FILE__, __LINE__, "list iterator create"));
i->list = l; i->list = l;
list_mutex_lock(&l->mutex); list_mutex_lock(&l->mutex);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
i->pos = l->head; i->pos = l->head;
i->prev = &l->head; i->prev = &l->head;
i->iNext = l->iNext; i->iNext = l->iNext;
l->iNext = i; l->iNext = i;
assert(i->magic = LIST_MAGIC); /* set magic via assert abuse */ assert(i->magic = LIST_MAGIC); /* set magic via assert abuse */
list_mutex_unlock(&l->mutex); list_mutex_unlock(&l->mutex);
return(i); return(i);
} }
void void
list_iterator_reset (ListIterator i) list_iterator_reset (ListIterator i)
{ {
assert(i != NULL); assert(i != NULL);
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
list_mutex_lock(&i->list->mutex); list_mutex_lock(&i->list->mutex);
assert(i->list->magic == LIST_MAGIC); assert(i->list->magic == LIST_MAGIC);
i->pos = i->list->head; i->pos = i->list->head;
i->prev = &i->list->head; i->prev = &i->list->head;
list_mutex_unlock(&i->list->mutex); list_mutex_unlock(&i->list->mutex);
return; return;
} }
void void
list_iterator_destroy (ListIterator i) list_iterator_destroy (ListIterator i)
{ {
ListIterator *pi; ListIterator *pi;
assert(i != NULL); assert(i != NULL);
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
list_mutex_lock(&i->list->mutex); list_mutex_lock(&i->list->mutex);
assert(i->list->magic == LIST_MAGIC); assert(i->list->magic == LIST_MAGIC);
for (pi=&i->list->iNext; *pi; pi=&(*pi)->iNext) { for (pi=&i->list->iNext; *pi; pi=&(*pi)->iNext) {
assert((*pi)->magic == LIST_MAGIC); assert((*pi)->magic == LIST_MAGIC);
if (*pi == i) { if (*pi == i) {
*pi = (*pi)->iNext; *pi = (*pi)->iNext;
break; break;
} }
} }
list_mutex_unlock(&i->list->mutex); list_mutex_unlock(&i->list->mutex);
assert(i->magic = ~LIST_MAGIC); /* clear magic via assert abuse */ assert(i->magic = ~LIST_MAGIC); /* clear magic via assert abuse */
list_iterator_free(i); list_iterator_free(i);
return; return;
} }
void * void *
list_next (ListIterator i) list_next (ListIterator i)
{ {
ListNode p; ListNode p;
assert(i != NULL); assert(i != NULL);
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
list_mutex_lock(&i->list->mutex); list_mutex_lock(&i->list->mutex);
assert(i->list->magic == LIST_MAGIC); assert(i->list->magic == LIST_MAGIC);
if ((p = i->pos)) if ((p = i->pos))
i->pos = p->next; i->pos = p->next;
if (*i->prev != p) if (*i->prev != p)
i->prev = &(*i->prev)->next; i->prev = &(*i->prev)->next;
list_mutex_unlock(&i->list->mutex); list_mutex_unlock(&i->list->mutex);
return(p ? p->data : NULL); return(p ? p->data : NULL);
} }
void * void *
list_insert (ListIterator i, void *x) list_insert (ListIterator i, void *x)
{ {
void *v; void *v;
assert(i != NULL); assert(i != NULL);
assert(x != NULL); assert(x != NULL);
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
list_mutex_lock(&i->list->mutex); list_mutex_lock(&i->list->mutex);
assert(i->list->magic == LIST_MAGIC); assert(i->list->magic == LIST_MAGIC);
v = list_node_create(i->list, i->prev, x); v = list_node_create(i->list, i->prev, x);
list_mutex_unlock(&i->list->mutex); list_mutex_unlock(&i->list->mutex);
return(v); return(v);
} }
void * void *
list_find (ListIterator i, ListFindF f, void *key) list_find (ListIterator i, ListFindF f, void *key)
{ {
void *v; void *v;
assert(i != NULL); assert(i != NULL);
assert(f != NULL); assert(f != NULL);
assert(key != NULL); assert(key != NULL);
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
while ((v=list_next(i)) && !f(v,key)) {;} while ((v=list_next(i)) && !f(v,key)) {;}
return(v); return(v);
} }
void * void *
list_remove (ListIterator i) list_remove (ListIterator i)
{ {
void *v = NULL; void *v = NULL;
assert(i != NULL); assert(i != NULL);
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
list_mutex_lock(&i->list->mutex); list_mutex_lock(&i->list->mutex);
assert(i->list->magic == LIST_MAGIC); assert(i->list->magic == LIST_MAGIC);
if (*i->prev != i->pos) if (*i->prev != i->pos)
v = list_node_destroy(i->list, i->prev); v = list_node_destroy(i->list, i->prev);
list_mutex_unlock(&i->list->mutex); list_mutex_unlock(&i->list->mutex);
return(v); return(v);
} }
int int
list_delete_item (ListIterator i) list_delete_item (ListIterator i)
{ {
void *v; void *v;
assert(i != NULL); assert(i != NULL);
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
if ((v = list_remove(i))) { if ((v = list_remove(i))) {
if (i->list->fDel) if (i->list->fDel)
i->list->fDel(v); i->list->fDel(v);
return(1); return(1);
} }
return(0); return(0);
} }
...@@ -788,30 +788,30 @@ list_node_create (List l, ListNode *pp, void *x) ...@@ -788,30 +788,30 @@ list_node_create (List l, ListNode *pp, void *x)
* Returns a ptr to data [x], or NULL if insertion fails. * Returns a ptr to data [x], or NULL if insertion fails.
* This routine assumes the list is already locked upon entry. * This routine assumes the list is already locked upon entry.
*/ */
ListNode p; ListNode p;
ListIterator i; ListIterator i;
assert(l != NULL); assert(l != NULL);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
assert(list_mutex_is_locked(&l->mutex)); assert(list_mutex_is_locked(&l->mutex));
assert(pp != NULL); assert(pp != NULL);
assert(x != NULL); assert(x != NULL);
if (!(p = list_node_alloc())) if (!(p = list_node_alloc()))
return(lsd_nomem_error(__FILE__, __LINE__, "list node create")); return(lsd_nomem_error(__FILE__, __LINE__, "list node create"));
p->data = x; p->data = x;
if (!(p->next = *pp)) if (!(p->next = *pp))
l->tail = &p->next; l->tail = &p->next;
*pp = p; *pp = p;
l->count++; l->count++;
for (i=l->iNext; i; i=i->iNext) { for (i=l->iNext; i; i=i->iNext) {
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
if (i->prev == pp) if (i->prev == pp)
i->prev = &p->next; i->prev = &p->next;
else if (i->pos == p->next) else if (i->pos == p->next)
i->pos = p; i->pos = p;
assert((i->pos == *i->prev) || (i->pos == (*i->prev)->next)); assert((i->pos == *i->prev) || (i->pos == (*i->prev)->next));
} }
return(x); return(x);
} }
...@@ -824,75 +824,75 @@ list_node_destroy (List l, ListNode *pp) ...@@ -824,75 +824,75 @@ list_node_destroy (List l, ListNode *pp)
* or NULL if [*pp] points to the NULL element. * or NULL if [*pp] points to the NULL element.
* This routine assumes the list is already locked upon entry. * This routine assumes the list is already locked upon entry.
*/ */
void *v; void *v;
ListNode p; ListNode p;
ListIterator i; ListIterator i;
assert(l != NULL); assert(l != NULL);
assert(l->magic == LIST_MAGIC); assert(l->magic == LIST_MAGIC);
assert(list_mutex_is_locked(&l->mutex)); assert(list_mutex_is_locked(&l->mutex));
assert(pp != NULL); assert(pp != NULL);
if (!(p = *pp)) if (!(p = *pp))
return(NULL); return(NULL);
v = p->data; v = p->data;
if (!(*pp = p->next)) if (!(*pp = p->next))
l->tail = pp; l->tail = pp;
l->count--; l->count--;
for (i=l->iNext; i; i=i->iNext) { for (i=l->iNext; i; i=i->iNext) {
assert(i->magic == LIST_MAGIC); assert(i->magic == LIST_MAGIC);
if (i->pos == p) if (i->pos == p)
i->pos = p->next, i->prev = pp; i->pos = p->next, i->prev = pp;
else if (i->prev == &p->next) else if (i->prev == &p->next)
i->prev = pp; i->prev = pp;
assert((i->pos == *i->prev) || (i->pos == (*i->prev)->next)); assert((i->pos == *i->prev) || (i->pos == (*i->prev)->next));
} }
list_node_free(p); list_node_free(p);
return(v); return(v);
} }
static List static List
list_alloc (void) list_alloc (void)
{ {
return(list_alloc_aux(sizeof(struct list), &list_free_lists)); return(list_alloc_aux(sizeof(struct list), &list_free_lists));
} }
static void static void
list_free (List l) list_free (List l)
{ {
list_free_aux(l, &list_free_lists); list_free_aux(l, &list_free_lists);
return; return;
} }
static ListNode static ListNode
list_node_alloc (void) list_node_alloc (void)
{ {
return(list_alloc_aux(sizeof(struct listNode), &list_free_nodes)); return(list_alloc_aux(sizeof(struct listNode), &list_free_nodes));
} }
static void static void
list_node_free (ListNode p) list_node_free (ListNode p)
{ {
list_free_aux(p, &list_free_nodes); list_free_aux(p, &list_free_nodes);
return; return;
} }
static ListIterator static ListIterator
list_iterator_alloc (void) list_iterator_alloc (void)
{ {
return(list_alloc_aux(sizeof(struct listIterator), &list_free_iterators)); return(list_alloc_aux(sizeof(struct listIterator), &list_free_iterators));
} }
static void static void
list_iterator_free (ListIterator i) list_iterator_free (ListIterator i)
{ {
list_free_aux(i, &list_free_iterators); list_free_aux(i, &list_free_iterators);
return; return;
} }
...@@ -903,30 +903,30 @@ list_alloc_aux (int size, void *pfreelist) ...@@ -903,30 +903,30 @@ list_alloc_aux (int size, void *pfreelist)
* Memory is added to the freelist in chunks of size LIST_ALLOC. * Memory is added to the freelist in chunks of size LIST_ALLOC.
* Returns a ptr to the object, or NULL if the memory request fails. * Returns a ptr to the object, or NULL if the memory request fails.
*/ */
void **px; void **px;
void **pfree = pfreelist; void **pfree = pfreelist;
void **plast; void **plast;
assert(sizeof(char) == 1); assert(sizeof(char) == 1);
assert(size >= sizeof(void *)); assert(size >= sizeof(void *));
assert(pfreelist != NULL); assert(pfreelist != NULL);
assert(LIST_ALLOC > 0); assert(LIST_ALLOC > 0);
list_mutex_lock(&list_free_lock); list_mutex_lock(&list_free_lock);
if (!*pfree) { if (!*pfree) {
if ((*pfree = xmalloc(LIST_ALLOC * size))) { if ((*pfree = xmalloc(LIST_ALLOC * size))) {
px = *pfree; px = *pfree;
plast = (void **) ((char *) *pfree + ((LIST_ALLOC - 1) * size)); plast = (void **) ((char *) *pfree + ((LIST_ALLOC - 1) * size));
while (px < plast) while (px < plast)
*px = (char *) px + size, px = *px; *px = (char *) px + size, px = *px;
*plast = NULL; *plast = NULL;
} }
} }
if ((px = *pfree)) if ((px = *pfree))
*pfree = *px; *pfree = *px;
else else
errno = ENOMEM; errno = ENOMEM;
list_mutex_unlock(&list_free_lock); list_mutex_unlock(&list_free_lock);
return(px); return(px);
} }
...@@ -936,19 +936,19 @@ list_free_aux (void *x, void *pfreelist) ...@@ -936,19 +936,19 @@ list_free_aux (void *x, void *pfreelist)
/* Frees the object [x], returning it to the freelist [*pfreelist]. /* Frees the object [x], returning it to the freelist [*pfreelist].
*/ */
#ifdef MEMORY_LEAK_DEBUG #ifdef MEMORY_LEAK_DEBUG
xfree(x); xfree(x);
#else #else
void **px = x; void **px = x;
void **pfree = pfreelist; void **pfree = pfreelist;
assert(x != NULL); assert(x != NULL);
assert(pfreelist != NULL); assert(pfreelist != NULL);
list_mutex_lock(&list_free_lock); list_mutex_lock(&list_free_lock);
*px = *pfree; *px = *pfree;
*pfree = px; *pfree = px;
list_mutex_unlock(&list_free_lock); list_mutex_unlock(&list_free_lock);
#endif #endif
return; return;
} }
#ifdef WITH_PTHREADS #ifdef WITH_PTHREADS
...@@ -979,11 +979,11 @@ list_mutex_is_locked (pthread_mutex_t *mutex) ...@@ -979,11 +979,11 @@ list_mutex_is_locked (pthread_mutex_t *mutex)
{ {
/* Returns true if the mutex is locked; o/w, returns false. /* Returns true if the mutex is locked; o/w, returns false.
*/ */
int rc; int rc;
assert(mutex != NULL); assert(mutex != NULL);
rc = pthread_mutex_trylock(mutex); rc = pthread_mutex_trylock(mutex);
return(rc == EBUSY ? 1 : 0); return(rc == EBUSY ? 1 : 0);
} }
#endif /* WITH_PTHREADS */ #endif /* WITH_PTHREADS */
#endif /* !NDEBUG */ #endif /* !NDEBUG */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment