vector.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2012-15 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #ifndef __DAL_VECTOR_H__
  26. #define __DAL_VECTOR_H__
  27. struct vector {
  28. uint8_t *container;
  29. uint32_t struct_size;
  30. uint32_t count;
  31. uint32_t capacity;
  32. struct dc_context *ctx;
  33. };
  34. bool dal_vector_construct(
  35. struct vector *vector,
  36. struct dc_context *ctx,
  37. uint32_t capacity,
  38. uint32_t struct_size);
  39. struct vector *dal_vector_create(
  40. struct dc_context *ctx,
  41. uint32_t capacity,
  42. uint32_t struct_size);
  43. /* 'initial_value' is optional. If initial_value not supplied,
  44. * each "structure" in the vector will contain zeros by default. */
  45. struct vector *dal_vector_presized_create(
  46. struct dc_context *ctx,
  47. uint32_t size,
  48. void *initial_value,
  49. uint32_t struct_size);
  50. void dal_vector_destruct(
  51. struct vector *vector);
  52. void dal_vector_destroy(
  53. struct vector **vector);
  54. uint32_t dal_vector_get_count(
  55. const struct vector *vector);
  56. /* dal_vector_insert_at
  57. * reallocate container if necessary
  58. * then shell items at right and insert
  59. * return if the container modified
  60. * do not check that index belongs to container
  61. * since the function is private and index is going to be calculated
  62. * either with by function or as get_count+1 */
  63. bool dal_vector_insert_at(
  64. struct vector *vector,
  65. const void *what,
  66. uint32_t position);
  67. bool dal_vector_append(
  68. struct vector *vector,
  69. const void *item);
  70. /* operator[] */
  71. void *dal_vector_at_index(
  72. const struct vector *vector,
  73. uint32_t index);
  74. void dal_vector_set_at_index(
  75. const struct vector *vector,
  76. const void *what,
  77. uint32_t index);
  78. /* create a clone (copy) of a vector */
  79. struct vector *dal_vector_clone(
  80. const struct vector *vector_other);
  81. /* dal_vector_remove_at_index
  82. * Shifts elements on the right from remove position to the left,
  83. * removing an element at position by overwrite means*/
  84. bool dal_vector_remove_at_index(
  85. struct vector *vector,
  86. uint32_t index);
  87. uint32_t dal_vector_capacity(const struct vector *vector);
  88. bool dal_vector_reserve(struct vector *vector, uint32_t capacity);
  89. void dal_vector_clear(struct vector *vector);
  90. /***************************************************************************
  91. * Macro definitions of TYPE-SAFE versions of vector set/get functions.
  92. ***************************************************************************/
  93. #define DAL_VECTOR_INSERT_AT(vector_type, type_t) \
  94. static bool vector_type##_vector_insert_at( \
  95. struct vector *vector, \
  96. type_t what, \
  97. uint32_t position) \
  98. { \
  99. return dal_vector_insert_at(vector, what, position); \
  100. }
  101. #define DAL_VECTOR_APPEND(vector_type, type_t) \
  102. static bool vector_type##_vector_append( \
  103. struct vector *vector, \
  104. type_t item) \
  105. { \
  106. return dal_vector_append(vector, item); \
  107. }
  108. /* Note: "type_t" is the ONLY token accepted by "checkpatch.pl" and by
  109. * "checkcommit" as *return type*.
  110. * For uniformity reasons "type_t" is used for all type-safe macro
  111. * definitions here. */
  112. #define DAL_VECTOR_AT_INDEX(vector_type, type_t) \
  113. static type_t vector_type##_vector_at_index( \
  114. const struct vector *vector, \
  115. uint32_t index) \
  116. { \
  117. return dal_vector_at_index(vector, index); \
  118. }
  119. #define DAL_VECTOR_SET_AT_INDEX(vector_type, type_t) \
  120. static void vector_type##_vector_set_at_index( \
  121. const struct vector *vector, \
  122. type_t what, \
  123. uint32_t index) \
  124. { \
  125. dal_vector_set_at_index(vector, what, index); \
  126. }
  127. #endif /* __DAL_VECTOR_H__ */