list.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * list.h
  3. *
  4. * Copyright (C) 2015 Aleksandar Andrejevic <theflash@sdf.lonestar.org>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __MONOLITHIUM_LIST_H__
  20. #define __MONOLITHIUM_LIST_H__
  21. #include "defs.h"
  22. #define LIST_INITIALIZER(name) { &name, &name }
  23. #define DECLARE_LIST(name) list_entry_t name = LIST_INITIALIZER(name)
  24. #define list_put_after list_prepend
  25. #define list_put_before list_append
  26. #define mini_list_put_after mini_list_prepend
  27. #define mini_list_put_before mini_list_append
  28. typedef struct _list_entry_t
  29. {
  30. struct _list_entry_t *next, *prev;
  31. } list_entry_t;
  32. typedef struct _mini_list_entry_t
  33. {
  34. struct _mini_list_entry_t *next;
  35. } mini_list_entry_t;
  36. static inline void list_prepend(list_entry_t *list, list_entry_t *entry)
  37. {
  38. entry->next = list->next;
  39. entry->prev = list;
  40. entry->next->prev = entry;
  41. entry->prev->next = entry;
  42. }
  43. static inline void list_append(list_entry_t *list, list_entry_t *entry)
  44. {
  45. entry->next = list;
  46. entry->prev = list->prev;
  47. entry->next->prev = entry;
  48. entry->prev->next = entry;
  49. }
  50. static inline void list_remove(list_entry_t *entry)
  51. {
  52. entry->next->prev = entry->prev;
  53. entry->prev->next = entry->next;
  54. }
  55. static inline void list_init(list_entry_t *list)
  56. {
  57. list->next = list->prev = list;
  58. }
  59. static inline void list_init_array(list_entry_t *list_array, size_t size)
  60. {
  61. size_t i;
  62. for (i = 0; i < size; i++) list_init(&list_array[i]);
  63. }
  64. static inline void mini_list_prepend(mini_list_entry_t *list, mini_list_entry_t *entry)
  65. {
  66. entry->next = list->next;
  67. list->next = entry;
  68. }
  69. static inline void mini_list_append(mini_list_entry_t *list, mini_list_entry_t *entry)
  70. {
  71. mini_list_entry_t *final = list->next;
  72. while (final->next != list) final = final->next;
  73. final->next = entry;
  74. entry->next = list;
  75. }
  76. static inline void mini_list_remove(mini_list_entry_t *entry)
  77. {
  78. mini_list_entry_t *prev = entry->next;
  79. while (prev->next != entry) prev = prev->next;
  80. prev->next = entry->next;
  81. }
  82. static inline void mini_list_init(mini_list_entry_t *list)
  83. {
  84. list->next = list;
  85. }
  86. static inline void mini_list_init_array(mini_list_entry_t *list_array, size_t size)
  87. {
  88. size_t i;
  89. for (i = 0; i < size; i++) mini_list_init(&list_array[i]);
  90. }
  91. #endif