bh_list.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "bh_list.h"
  17. #include "bh_assert.h"
  18. #ifdef BH_DEBUG
  19. /**
  20. * Test whehter a pointer value has exist in given list.
  21. *
  22. * @param list pointer to list.
  23. * @param elem pointer to elem that will be inserted into list.
  24. * @return <code>true</code> if the pointer has been in the list;
  25. * <code>false</code> otherwise.
  26. */
  27. BH_STATIC bool bh_list_is_elem_exist(bh_list *list, void *elem);
  28. #endif
  29. bh_list_status bh_list_init(bh_list *list)
  30. {
  31. if (!list)
  32. return BH_LIST_ERROR;
  33. (list->head).next = NULL;
  34. list->len = 0;
  35. return BH_LIST_SUCCESS;
  36. }
  37. bh_list_status _bh_list_insert(bh_list *list, void *elem)
  38. {
  39. bh_list_link *p = NULL;
  40. if (!list || !elem)
  41. return BH_LIST_ERROR;
  42. #ifdef BH_DEBUG
  43. bh_assert (!bh_list_is_elem_exist(list, elem));
  44. #endif
  45. p = (bh_list_link *) elem;
  46. p->next = (list->head).next;
  47. (list->head).next = p;
  48. list->len++;
  49. return BH_LIST_SUCCESS;
  50. }
  51. bh_list_status bh_list_remove(bh_list *list, void *elem)
  52. {
  53. bh_list_link *cur = NULL;
  54. bh_list_link *prev = NULL;
  55. if (!list || !elem)
  56. return BH_LIST_ERROR;
  57. cur = (list->head).next;
  58. while (cur) {
  59. if (cur == elem) {
  60. if (prev)
  61. prev->next = cur->next;
  62. else
  63. (list->head).next = cur->next;
  64. list->len--;
  65. return BH_LIST_SUCCESS;
  66. }
  67. prev = cur;
  68. cur = cur->next;
  69. }
  70. return BH_LIST_ERROR;
  71. }
  72. uint32 bh_list_length(bh_list *list)
  73. {
  74. return (list ? list->len : 0);
  75. }
  76. void* bh_list_first_elem(bh_list *list)
  77. {
  78. return (list ? (list->head).next : NULL);
  79. }
  80. void* bh_list_elem_next(void *node)
  81. {
  82. return (node ? ((bh_list_link *) node)->next : NULL);
  83. }
  84. #ifdef BH_DEBUG
  85. BH_STATIC bool bh_list_is_elem_exist(bh_list *list, void *elem)
  86. {
  87. bh_list_link *p = NULL;
  88. if (!list || !elem) return false;
  89. p = (list->head).next;
  90. while (p && p != elem) p = p->next;
  91. return (p != NULL);
  92. }
  93. #endif