bh_list.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifndef _BH_LIST_H
  17. #define _BH_LIST_H
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #include "bh_types.h" /*For bool type*/
  22. #include "bh_platform.h"
  23. /* List user should embedded bh_list_link into list elem data structure
  24. * definition. And bh_list_link data field should be the first field.
  25. * For example, if we would like to use bh_list for our own data type A,
  26. * A must be defined as a structure like below:
  27. * struct A {
  28. * bh_list_link l;
  29. * ...
  30. * };
  31. *
  32. * bh_list_link is defined as a structure (not typedef void*).
  33. * It will make extend list into bi-direction easy.
  34. */
  35. typedef struct _bh_list_link {
  36. struct _bh_list_link *next;
  37. } bh_list_link;
  38. typedef struct _bh_list {
  39. bh_list_link head;
  40. uint32 len;
  41. } bh_list;
  42. /* Beihai list operation return value */
  43. typedef enum _bh_list_status {
  44. BH_LIST_SUCCESS = 0, BH_LIST_ERROR = -1
  45. } bh_list_status;
  46. /**
  47. * Initialize a list.
  48. *
  49. * @param list pointer to list.
  50. * @return <code>BH_LIST_ERROR</code> if OK;
  51. * <code>BH_LIST_ERROR</code> if list pointer is NULL.
  52. */
  53. bh_list_status bh_list_init(bh_list *list);
  54. /**
  55. * Insert an elem pointer into list. The list node memory is maintained by list while
  56. * elem memory is the responsibility of list user.
  57. *
  58. * @param list pointer to list.
  59. * @param elem pointer to elem that will be inserted into list.
  60. * @return <code>BH_LIST_ERROR</code> if OK;
  61. * <code>BH_LIST_ERROR</code> if input is invalid or no memory available.
  62. */
  63. extern bh_list_status _bh_list_insert(bh_list *list, void *elem);
  64. #ifdef _INSTRUMENT_TEST_ENABLED
  65. extern bh_list_status bh_list_insert_instr(bh_list *list, void *elem, const char*func_name);
  66. #define bh_list_insert(list, elem) bh_list_insert_instr(list, elem, __FUNCTION__)
  67. #else
  68. #define bh_list_insert _bh_list_insert
  69. #endif
  70. /**
  71. * Remove an elem pointer from list. The list node memory is maintained by list while
  72. * elem memory is the responsibility of list user.
  73. *
  74. * @param list pointer to list.
  75. * @param elem pointer to elem that will be inserted into list.
  76. * @return <code>BH_LIST_ERROR</code> if OK;
  77. * <code>BH_LIST_ERROR</code> if element does not exist in given list.
  78. */
  79. bh_list_status bh_list_remove(bh_list *list, void *elem);
  80. /**
  81. * Get the list length.
  82. *
  83. * @param list pointer to list.
  84. * @return the length of the list.
  85. */
  86. uint32 bh_list_length(bh_list *list);
  87. /**
  88. * Get the first elem in the list.
  89. *
  90. * @param list pointer to list.
  91. * @return pointer to the first node.
  92. */
  93. void* bh_list_first_elem(bh_list *list);
  94. /**
  95. * Get the next elem of given list input elem.
  96. *
  97. * @param node pointer to list node.
  98. * @return pointer to next list node.
  99. */
  100. void* bh_list_elem_next(void *node);
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif /* #ifndef _BH_LIST_H */