libfuncs is collection of code (list, queue, circular buffer, io, logging, etc.). https://georgi.unixsol.org/programs/libfuncs/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list.h 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * List manipulations header file
  3. * Copyright (C) 2006-2010 Unix Solutions Ltd.
  4. *
  5. */
  6. #ifndef LIST_H
  7. # define LIST_H
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #include <pthread.h>
  12. typedef struct LNODE {
  13. struct LNODE *prev;
  14. struct LNODE *next;
  15. void *data;
  16. } LNODE;
  17. typedef struct LIST {
  18. pthread_mutex_t *mutex; // List's lock
  19. struct LNODE *head; // Points to first element of the list
  20. struct LNODE *tail; // Points to last element of the list
  21. unsigned int items; // How many items are in the list
  22. char *name;
  23. } LIST;
  24. // Safe against calling list_del inside
  25. #define list_for_each(list, elem, tmp_el) \
  26. for (elem = (list)->head->next, tmp_el = elem->next; elem != (list)->head && elem->data; elem = tmp_el, tmp_el = elem->next)
  27. #define list_for_each_reverse(list, elem, tmp_el) \
  28. for (elem = (list)->head->prev, tmp_el = elem->prev; elem != (list)->head && elem->data; elem = tmp_el, tmp_el = elem->prev)
  29. // list_del can not be called inside this for
  30. #define list_for_each_unsafe(list, elem) \
  31. for (elem = (list)->head->next; elem != (list)->head && elem->data; elem = elem->next)
  32. #define list_for_each_reverse_unsafe(list, elem) \
  33. for (elem = (list)->head->prev; elem != (list)->head && elem->data; elem = elem->prev)
  34. LIST *list_new (char *listname);
  35. void list_free (LIST **l, void (*l_free)(), void (*l_freep)());
  36. void list_lock (LIST *l);
  37. void list_unlock (LIST *l);
  38. void list_add (LIST *l, void *data);
  39. int list_del (LIST *l, LNODE **node);
  40. int list_del_unlocked (LIST *l, LNODE **node);
  41. int list_del_entry (LIST *l, void *data);
  42. void list_dump(LIST *l);
  43. #ifdef __cplusplus
  44. }
  45. #endif
  46. #endif