bh_vector.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. /* size of each element */
  14. uint32 size_elem;
  15. /* max element number */
  16. uint32 max_elements;
  17. /* current element num */
  18. uint32 num_elements;
  19. /* vector data allocated */
  20. uint8 *data;
  21. } Vector;
  22. /**
  23. * Initialize vector
  24. *
  25. * @param vector the vector to init
  26. * @param init_length the initial length of the vector
  27. * @param size_elem size of each element
  28. *
  29. * @return true if success, false otherwise
  30. */
  31. bool
  32. bh_vector_init(Vector *vector, uint32 init_length, uint32 size_elem);
  33. /**
  34. * Set element of vector
  35. *
  36. * @param vector the vector to set
  37. * @param index the index of the element to set
  38. * @param elem_buf the element buffer which stores the element data
  39. *
  40. * @return true if success, false otherwise
  41. */
  42. bool
  43. bh_vector_set(Vector *vector, uint32 index, const void *elem_buf);
  44. /**
  45. * Get element of vector
  46. *
  47. * @param vector the vector to get
  48. * @param index the index of the element to get
  49. * @param elem_buf the element buffer to store the element data,
  50. * whose length must be no less than element size
  51. *
  52. * @return true if success, false otherwise
  53. */
  54. bool
  55. bh_vector_get(const Vector *vector, uint32 index, void *elem_buf);
  56. /**
  57. * Insert element of vector
  58. *
  59. * @param vector the vector to insert
  60. * @param index the index of the element to insert
  61. * @param elem_buf the element buffer which stores the element data
  62. *
  63. * @return true if success, false otherwise
  64. */
  65. bool
  66. bh_vector_insert(Vector *vector, uint32 index, const void *elem_buf);
  67. /**
  68. * Append element to the end of vector
  69. *
  70. * @param vector the vector to append
  71. * @param elem_buf the element buffer which stores the element data
  72. *
  73. * @return true if success, false otherwise
  74. */
  75. bool
  76. bh_vector_append(Vector *vector, const void *elem_buf);
  77. /**
  78. * Remove element from vector
  79. *
  80. * @param vector the vector to remove element
  81. * @param index the index of the element to remove
  82. * @param old_elem_buf if not NULL, copies the element data to the buffer
  83. *
  84. * @return true if success, false otherwise
  85. */
  86. bool
  87. bh_vector_remove(Vector *vector, uint32 index, void *old_elem_buf);
  88. /**
  89. * Return the size of the vector
  90. *
  91. * @param vector the vector to get size
  92. *
  93. * @return return the size of the vector
  94. */
  95. uint32
  96. bh_vector_size(const Vector *vector);
  97. /**
  98. * Destroy the vector
  99. *
  100. * @param vector the vector to destroy
  101. *
  102. * @return true if success, false otherwise
  103. */
  104. bool
  105. bh_vector_destroy(Vector *vector);
  106. #ifdef __cplusplus
  107. }
  108. #endif
  109. #endif /* endof _WASM_VECTOR_H */