wasm_vector.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _WASM_VECTOR_H
  17. #define _WASM_VECTOR_H
  18. #include "bh_platform.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define DEFAULT_VECTOR_INIT_SIZE 8
  23. typedef struct Vector {
  24. /* size of each element */
  25. uint32 size_elem;
  26. /* max element number */
  27. uint32 max_elements;
  28. /* current element num */
  29. uint32 num_elements;
  30. /* vector data allocated */
  31. uint8 *data;
  32. } Vector;
  33. /**
  34. * Initialize vector
  35. *
  36. * @param vector the vector to init
  37. * @param init_length the initial length of the vector
  38. * @param size_elem size of each element
  39. *
  40. * @return true if success, false otherwise
  41. */
  42. bool
  43. wasm_vector_init(Vector *vector, uint32 init_length, uint32 size_elem);
  44. /**
  45. * Set element of vector
  46. *
  47. * @param vector the vector to set
  48. * @param index the index of the element to set
  49. * @param elem_buf the element buffer which stores the element data
  50. *
  51. * @return true if success, false otherwise
  52. */
  53. bool
  54. wasm_vector_set(Vector *vector, uint32 index, const void *elem_buf);
  55. /**
  56. * Get element of vector
  57. *
  58. * @param vector the vector to get
  59. * @param index the index of the element to get
  60. * @param elem_buf the element buffer to store the element data,
  61. * whose length must be no less than element size
  62. *
  63. * @return true if success, false otherwise
  64. */
  65. bool
  66. wasm_vector_get(const Vector *vector, uint32 index, void *elem_buf);
  67. /**
  68. * Insert element of vector
  69. *
  70. * @param vector the vector to insert
  71. * @param index the index of the element to insert
  72. * @param elem_buf the element buffer which stores the element data
  73. *
  74. * @return true if success, false otherwise
  75. */
  76. bool
  77. wasm_vector_insert(Vector *vector, uint32 index, const void *elem_buf);
  78. /**
  79. * Append element to the end of vector
  80. *
  81. * @param vector the vector to append
  82. * @param elem_buf the element buffer which stores the element data
  83. *
  84. * @return true if success, false otherwise
  85. */
  86. bool
  87. wasm_vector_append(Vector *vector, const void *elem_buf);
  88. /**
  89. * Remove element from vector
  90. *
  91. * @param vector the vector to remove element
  92. * @param index the index of the element to remove
  93. * @param old_elem_buf if not NULL, copies the element data to the buffer
  94. *
  95. * @return true if success, false otherwise
  96. */
  97. bool
  98. wasm_vector_remove(Vector *vector, uint32 index, void *old_elem_buf);
  99. /**
  100. * Return the size of the vector
  101. *
  102. * @param vector the vector to get size
  103. *
  104. * @return return the size of the vector
  105. */
  106. uint32
  107. wasm_vector_size(const Vector *vector);
  108. /**
  109. * Destroy the vector
  110. *
  111. * @param vector the vector to destroy
  112. *
  113. * @return true if success, false otherwise
  114. */
  115. bool
  116. wasm_vector_destroy(Vector *vector);
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* endof _WASM_VECTOR_H */