bh_vector.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_VECTOR_H
  6. #define _WASM_VECTOR_H
  7. #include "bh_platform.h"
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11. #define DEFAULT_VECTOR_INIT_SIZE 8
  12. typedef struct Vector {
  13. /* max element number */
  14. size_t max_elems;
  15. /* vector data allocated */
  16. uint8 *data;
  17. /* current element num */
  18. size_t num_elems;
  19. /* size of each element */
  20. size_t size_elem;
  21. void *lock;
  22. } Vector;
  23. /**
  24. * Initialize vector
  25. *
  26. * @param vector the vector to init
  27. * @param init_length the initial length of the vector
  28. * @param size_elem size of each element
  29. *
  30. * @return true if success, false otherwise
  31. */
  32. bool
  33. bh_vector_init(Vector *vector, size_t init_length, size_t size_elem,
  34. bool use_lock);
  35. /**
  36. * Set element of vector
  37. *
  38. * @param vector the vector to set
  39. * @param index the index of the element to set
  40. * @param elem_buf the element buffer which stores the element data
  41. *
  42. * @return true if success, false otherwise
  43. */
  44. bool
  45. bh_vector_set(Vector *vector, uint32 index, const void *elem_buf);
  46. /**
  47. * Get element of vector
  48. *
  49. * @param vector the vector to get
  50. * @param index the index of the element to get
  51. * @param elem_buf the element buffer to store the element data,
  52. * whose length must be no less than element size
  53. *
  54. * @return true if success, false otherwise
  55. */
  56. bool
  57. bh_vector_get(Vector *vector, uint32 index, void *elem_buf);
  58. /**
  59. * Insert element of vector
  60. *
  61. * @param vector the vector to insert
  62. * @param index the index of the element to insert
  63. * @param elem_buf the element buffer which stores the element data
  64. *
  65. * @return true if success, false otherwise
  66. */
  67. bool
  68. bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf);
  69. /**
  70. * Append element to the end of vector
  71. *
  72. * @param vector the vector to append
  73. * @param elem_buf the element buffer which stores the element data
  74. *
  75. * @return true if success, false otherwise
  76. */
  77. bool
  78. bh_vector_append(Vector *vector, const void *elem_buf);
  79. /**
  80. * Remove element from vector
  81. *
  82. * @param vector the vector to remove element
  83. * @param index the index of the element to remove
  84. * @param old_elem_buf if not NULL, copies the element data to the buffer
  85. *
  86. * @return true if success, false otherwise
  87. */
  88. bool
  89. bh_vector_remove(Vector *vector, uint32 index, void *old_elem_buf);
  90. /**
  91. * Return the size of the vector
  92. *
  93. * @param vector the vector to get size
  94. *
  95. * @return return the size of the vector
  96. */
  97. size_t
  98. bh_vector_size(const Vector *vector);
  99. /**
  100. * Destroy the vector
  101. *
  102. * @param vector the vector to destroy
  103. *
  104. * @return true if success, false otherwise
  105. */
  106. bool
  107. bh_vector_destroy(Vector *vector);
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif /* endof _WASM_VECTOR_H */