bh_list.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_list.h"
  6. #if BH_DEBUG != 0
  7. /**
  8. * Test whehter a pointer value has exist in given list.
  9. *
  10. * @param list pointer to list.
  11. * @param elem pointer to elem that will be inserted into list.
  12. * @return <code>true</code> if the pointer has been in the list;
  13. * <code>false</code> otherwise.
  14. */
  15. static bool bh_list_is_elem_exist(bh_list *list, void *elem);
  16. #endif
  17. bh_list_status bh_list_init(bh_list *list)
  18. {
  19. if (!list)
  20. return BH_LIST_ERROR;
  21. (list->head).next = NULL;
  22. list->len = 0;
  23. return BH_LIST_SUCCESS;
  24. }
  25. bh_list_status bh_list_insert(bh_list *list, void *elem)
  26. {
  27. bh_list_link *p = NULL;
  28. if (!list || !elem)
  29. return BH_LIST_ERROR;
  30. #if BH_DEBUG != 0
  31. bh_assert (!bh_list_is_elem_exist(list, elem));
  32. #endif
  33. p = (bh_list_link *) elem;
  34. p->next = (list->head).next;
  35. (list->head).next = p;
  36. list->len++;
  37. return BH_LIST_SUCCESS;
  38. }
  39. bh_list_status bh_list_remove(bh_list *list, void *elem)
  40. {
  41. bh_list_link *cur = NULL;
  42. bh_list_link *prev = NULL;
  43. if (!list || !elem)
  44. return BH_LIST_ERROR;
  45. cur = (list->head).next;
  46. while (cur) {
  47. if (cur == elem) {
  48. if (prev)
  49. prev->next = cur->next;
  50. else
  51. (list->head).next = cur->next;
  52. list->len--;
  53. return BH_LIST_SUCCESS;
  54. }
  55. prev = cur;
  56. cur = cur->next;
  57. }
  58. return BH_LIST_ERROR;
  59. }
  60. uint32 bh_list_length(bh_list *list)
  61. {
  62. return (list ? list->len : 0);
  63. }
  64. void* bh_list_first_elem(bh_list *list)
  65. {
  66. return (list ? (list->head).next : NULL);
  67. }
  68. void* bh_list_elem_next(void *node)
  69. {
  70. return (node ? ((bh_list_link *) node)->next : NULL);
  71. }
  72. #if BH_DEBUG != 0
  73. static bool bh_list_is_elem_exist(bh_list *list, void *elem)
  74. {
  75. bh_list_link *p = NULL;
  76. if (!list || !elem) return false;
  77. p = (list->head).next;
  78. while (p && p != elem) p = p->next;
  79. return (p != NULL);
  80. }
  81. #endif