bh_list.h 2.9 KB

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