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.7KB

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