list.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. typedef struct _list_entry_t
  27. {
  28. struct _list_entry_t *next, *prev;
  29. } list_entry_t;
  30. static inline void list_prepend(list_entry_t *list, list_entry_t *entry)
  31. {
  32. entry->next = list->next;
  33. entry->prev = list;
  34. entry->next->prev = entry;
  35. entry->prev->next = entry;
  36. }
  37. static inline void list_append(list_entry_t *list, list_entry_t *entry)
  38. {
  39. entry->next = list;
  40. entry->prev = list->prev;
  41. entry->next->prev = entry;
  42. entry->prev->next = entry;
  43. }
  44. static inline void list_remove(list_entry_t *entry)
  45. {
  46. entry->next->prev = entry->prev;
  47. entry->prev->next = entry->next;
  48. }
  49. static inline void list_init(list_entry_t *list)
  50. {
  51. list->next = list->prev = list;
  52. }
  53. static inline void list_init_array(list_entry_t *list_array, size_t size)
  54. {
  55. size_t i;
  56. for (i = 0; i < size; i++) list_init(&list_array[i]);
  57. }
  58. #endif