bh_list.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 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. /**
  54. * Remove an elem pointer from list. The list node memory is maintained by list while
  55. * elem memory is the responsibility of list user.
  56. *
  57. * @param list pointer to list.
  58. * @param elem pointer to elem that will be inserted into list.
  59. * @return <code>BH_LIST_ERROR</code> if OK;
  60. * <code>BH_LIST_ERROR</code> if element does not exist in given list.
  61. */
  62. bh_list_status bh_list_remove(bh_list *list, void *elem);
  63. /**
  64. * Get the list length.
  65. *
  66. * @param list pointer to list.
  67. * @return the length of the list.
  68. */
  69. uint32 bh_list_length(bh_list *list);
  70. /**
  71. * Get the first elem in the list.
  72. *
  73. * @param list pointer to list.
  74. * @return pointer to the first node.
  75. */
  76. void* bh_list_first_elem(bh_list *list);
  77. /**
  78. * Get the next elem of given list input elem.
  79. *
  80. * @param node pointer to list node.
  81. * @return pointer to next list node.
  82. */
  83. void* bh_list_elem_next(void *node);
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif /* #ifndef _BH_LIST_H */