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.

61 lines
2.0KB

  1. /*
  2. Generic queueing utilities.
  3. Copyright (C) 2002-2005 Robert Lipe, robertlipe@usa.net
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA
  15. */
  16. typedef struct queue {
  17. struct queue* next;
  18. struct queue* prev;
  19. } queue;
  20. void enqueue(queue* new_el, queue* old);
  21. queue* dequeue(queue* element);
  22. void sortqueue(queue* qh, int (*cmp)(const queue*, const queue*));
  23. #define QUEUE_INIT(head) (head)->next = ((head)->prev = head)
  24. #define QUEUE_FIRST(head) ((head)->next)
  25. #define QUEUE_NEXT(element) ((element)->next)
  26. #define QUEUE_LAST(head) ((head)->prev)
  27. #define QUEUE_EMPTY(head) ((head)->next == (head))
  28. #define QUEUE_MOVE(newhead,oldhead) \
  29. if ( (oldhead)->next == (oldhead) ) {\
  30. (newhead)->next = (newhead)->prev = (newhead); \
  31. } \
  32. else { \
  33. (newhead)->next = (oldhead)->next; \
  34. (newhead)->prev = (oldhead)->prev; \
  35. (newhead)->next->prev = (newhead); \
  36. (newhead)->prev->next = (newhead); \
  37. } \
  38. (oldhead)->next = (oldhead)->prev = (oldhead)
  39. #define ENQUEUE_TAIL(listhead, element) \
  40. enqueue(element, (listhead)->prev)
  41. #define ENQUEUE_HEAD(listhead, element) \
  42. enqueue(element, listhead)
  43. #define ENQUEUE_AFTER(predecessor, element) \
  44. enqueue(element, predecessor)
  45. #define QUEUE_FOR_EACH(listhead, element, tmp) \
  46. for ((element) = QUEUE_FIRST(listhead); \
  47. (tmp) = QUEUE_NEXT(element), \
  48. (element) != (listhead); \
  49. (element) = (tmp))