bh_list.h 2.6 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_platform.h"
  11. /* List user should embedded bh_list_link into list elem data structure
  12. * definition. And bh_list_link data field should be the first field.
  13. * For example, if we would like to use bh_list for our own data type A,
  14. * A must be defined as a structure like below:
  15. * struct A {
  16. * bh_list_link l;
  17. * ...
  18. * };
  19. *
  20. * bh_list_link is defined as a structure (not typedef void*).
  21. * It will make extend list into bi-direction easy.
  22. */
  23. typedef struct bh_list_link {
  24. struct bh_list_link *next;
  25. } bh_list_link;
  26. typedef struct bh_list {
  27. bh_list_link head;
  28. uint32 len;
  29. } bh_list;
  30. /* list operation return value */
  31. typedef enum bh_list_status {
  32. BH_LIST_SUCCESS = 0,
  33. 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
  43. bh_list_init(bh_list *list);
  44. /**
  45. * Insert an elem pointer into list. The list node memory is maintained by list
  46. * while elem memory is the responsibility of list user.
  47. *
  48. * @param list pointer to list.
  49. * @param elem pointer to elem that will be inserted into list.
  50. * @return <code>BH_LIST_ERROR</code> if OK;
  51. * <code>BH_LIST_ERROR</code> if input is invalid or no memory
  52. * available.
  53. */
  54. bh_list_status
  55. bh_list_insert(bh_list *list, void *elem);
  56. /**
  57. * Remove an elem pointer from list. The list node memory is maintained by list
  58. * while elem memory is the responsibility of list user.
  59. *
  60. * @param list pointer to list.
  61. * @param elem pointer to elem that will be inserted into list.
  62. * @return <code>BH_LIST_ERROR</code> if OK;
  63. * <code>BH_LIST_ERROR</code> if element does not exist in given
  64. * list.
  65. */
  66. bh_list_status
  67. bh_list_remove(bh_list *list, void *elem);
  68. /**
  69. * Get the list length.
  70. *
  71. * @param list pointer to list.
  72. * @return the length of the list.
  73. */
  74. uint32
  75. 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 *
  83. bh_list_first_elem(bh_list *list);
  84. /**
  85. * Get the next elem of given list input elem.
  86. *
  87. * @param node pointer to list node.
  88. * @return pointer to next list node.
  89. */
  90. void *
  91. bh_list_elem_next(void *node);
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. #endif /* #ifndef _BH_LIST_H */