objc-list.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Generic single linked list to keep various information
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Contributed by Kresten Krab Thorup.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC 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. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #ifndef __GNU_OBJC_LIST_H
  21. #define __GNU_OBJC_LIST_H
  22. struct objc_list
  23. {
  24. void *head;
  25. struct objc_list *tail;
  26. };
  27. /* Return a cons cell produced from (head . tail). */
  28. static inline struct objc_list*
  29. list_cons (void* head, struct objc_list* tail)
  30. {
  31. struct objc_list* cell;
  32. cell = (struct objc_list*)objc_malloc (sizeof (struct objc_list));
  33. cell->head = head;
  34. cell->tail = tail;
  35. return cell;
  36. }
  37. /* Remove the element at the head by replacing it by its
  38. successor. */
  39. static inline void
  40. list_remove_head (struct objc_list** list)
  41. {
  42. if ((*list)->tail)
  43. {
  44. /* Fetch next. */
  45. struct objc_list* tail = (*list)->tail;
  46. /* Copy next to list head. */
  47. *(*list) = *tail;
  48. /* Free next. */
  49. objc_free (tail);
  50. }
  51. else
  52. {
  53. /* Inly one element in list. */
  54. objc_free (*list);
  55. (*list) = 0;
  56. }
  57. }
  58. /* Map FUNCTION over all elements in LIST. */
  59. static inline void
  60. list_mapcar (struct objc_list* list, void(*function)(void*))
  61. {
  62. while (list)
  63. {
  64. (*function) (list->head);
  65. list = list->tail;
  66. }
  67. }
  68. /* Free list (backwards recursive). */
  69. static inline void
  70. list_free (struct objc_list* list)
  71. {
  72. if(list)
  73. {
  74. list_free (list->tail);
  75. objc_free (list);
  76. }
  77. }
  78. #endif /* not __GNU_OBJC_LIST_H */