generic_linked_list.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (C) 2000,2001 Florian Sander
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #define GENERIC_LINKED_LIST
  19. struct llist_entry
  20. {
  21. struct llist_entry *next;
  22. struct llist_entry *last;
  23. void *data;
  24. };
  25. struct llist_header
  26. {
  27. struct llist_entry *root;
  28. struct llist_entry *last;
  29. int size;
  30. int (*comparedata) (void *, void *);
  31. int (*expmemdata) (void *);
  32. void (*freedata) (void *);
  33. };
  34. static void llist_append(struct llist_header *head, void *data)
  35. {
  36. struct llist_entry *p, *np;
  37. np = nmalloc(sizeof(struct llist_entry));
  38. np->data = NULL;
  39. np->next = NULL;
  40. np->last = NULL;
  41. np->data = data;
  42. for (p = head->root; p && p->next; p = p->next);
  43. if (p) {
  44. p->next = np;
  45. np->last = p;
  46. } else
  47. head->root = np;
  48. head->size++;
  49. }
  50. static void *llist_find(struct llist_header *head, void *key)
  51. {
  52. struct llist_entry *p;
  53. for (p = head->root; p; p = p->next)
  54. if (head->comparedata(p->data, key))
  55. return p->data;
  56. return NULL;
  57. }
  58. static void llist_delete(struct llist_header *head, void *key)
  59. {
  60. struct llist_entry *p, *last;
  61. p = head->root;
  62. last = NULL;
  63. while (p) {
  64. if (head->comparedata(p->data, key)) {
  65. if (last)
  66. last->next = p->next;
  67. else
  68. head->root = p->next;
  69. head->freedata(p->data);
  70. if (p->next)
  71. p->next->last = last;
  72. /* make sure that the getfirst/getnext-loop can
  73. * continue if we are deleting the currently active
  74. * item. If last is NULL, then getnext() will continue
  75. * with the root which should be the correct successor
  76. * of p in this case. */
  77. if (head->last == p)
  78. head->last = last;
  79. nfree(p);
  80. if (last)
  81. p = last->next;
  82. else
  83. p = head->root;
  84. head->size--;
  85. } else {
  86. last = p;
  87. p = p->next;
  88. }
  89. }
  90. }
  91. static int llist_expmem(struct llist_header *head)
  92. {
  93. struct llist_entry *p;
  94. int size = 0;
  95. for (p = head->root; p; p = p->next) {
  96. size += sizeof(struct llist_entry);
  97. size += head->expmemdata(p->data);
  98. }
  99. return size;
  100. }
  101. static void llist_free(struct llist_header *head)
  102. {
  103. struct llist_entry *p, *n;
  104. p = head->root;
  105. while (p) {
  106. n = p->next;
  107. head->freedata(p->data);
  108. nfree(p);
  109. p = n;
  110. }
  111. head->root = NULL;
  112. head->size = 0;
  113. }
  114. static struct llist_entry *llist_getfirst(struct llist_header *head)
  115. {
  116. Assert(head);
  117. head->last = head->root;
  118. if (head->last)
  119. return head->last->data;
  120. else
  121. return NULL;
  122. }
  123. static struct llist_entry *llist_getnext(struct llist_header *head)
  124. {
  125. /* if head->last exists, then we can just proceed to the next
  126. * entry. If it does not exist, then it was the first entry in
  127. * the chain and got deleted while we were looping, so we can
  128. * simply proceed with the root which should be the next item. */
  129. if (head->last)
  130. head->last = head->last->next;
  131. else
  132. head->last = head->root;
  133. if (head->last)
  134. return head->last->data;
  135. else
  136. return NULL;
  137. }