vector.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #include "dm_services.h"
  26. #include "include/vector.h"
  27. bool dal_vector_construct(
  28. struct vector *vector,
  29. struct dc_context *ctx,
  30. uint32_t capacity,
  31. uint32_t struct_size)
  32. {
  33. vector->container = NULL;
  34. if (!struct_size || !capacity) {
  35. /* Container must be non-zero size*/
  36. BREAK_TO_DEBUGGER();
  37. return false;
  38. }
  39. vector->container = kzalloc(struct_size * capacity, GFP_KERNEL);
  40. if (vector->container == NULL)
  41. return false;
  42. vector->capacity = capacity;
  43. vector->struct_size = struct_size;
  44. vector->count = 0;
  45. vector->ctx = ctx;
  46. return true;
  47. }
  48. bool dal_vector_presized_costruct(
  49. struct vector *vector,
  50. struct dc_context *ctx,
  51. uint32_t count,
  52. void *initial_value,
  53. uint32_t struct_size)
  54. {
  55. uint32_t i;
  56. vector->container = NULL;
  57. if (!struct_size || !count) {
  58. /* Container must be non-zero size*/
  59. BREAK_TO_DEBUGGER();
  60. return false;
  61. }
  62. vector->container = kzalloc(struct_size * count, GFP_KERNEL);
  63. if (vector->container == NULL)
  64. return false;
  65. /* If caller didn't supply initial value then the default
  66. * of all zeros is expected, which is exactly what dal_alloc()
  67. * initialises the memory to. */
  68. if (NULL != initial_value) {
  69. for (i = 0; i < count; ++i)
  70. memmove(
  71. vector->container + i * struct_size,
  72. initial_value,
  73. struct_size);
  74. }
  75. vector->capacity = count;
  76. vector->struct_size = struct_size;
  77. vector->count = count;
  78. return true;
  79. }
  80. struct vector *dal_vector_presized_create(
  81. struct dc_context *ctx,
  82. uint32_t size,
  83. void *initial_value,
  84. uint32_t struct_size)
  85. {
  86. struct vector *vector = kzalloc(sizeof(struct vector), GFP_KERNEL);
  87. if (vector == NULL)
  88. return NULL;
  89. if (dal_vector_presized_costruct(
  90. vector, ctx, size, initial_value, struct_size))
  91. return vector;
  92. BREAK_TO_DEBUGGER();
  93. kfree(vector);
  94. return NULL;
  95. }
  96. struct vector *dal_vector_create(
  97. struct dc_context *ctx,
  98. uint32_t capacity,
  99. uint32_t struct_size)
  100. {
  101. struct vector *vector = kzalloc(sizeof(struct vector), GFP_KERNEL);
  102. if (vector == NULL)
  103. return NULL;
  104. if (dal_vector_construct(vector, ctx, capacity, struct_size))
  105. return vector;
  106. BREAK_TO_DEBUGGER();
  107. kfree(vector);
  108. return NULL;
  109. }
  110. void dal_vector_destruct(
  111. struct vector *vector)
  112. {
  113. kfree(vector->container);
  114. vector->count = 0;
  115. vector->capacity = 0;
  116. }
  117. void dal_vector_destroy(
  118. struct vector **vector)
  119. {
  120. if (vector == NULL || *vector == NULL)
  121. return;
  122. dal_vector_destruct(*vector);
  123. kfree(*vector);
  124. *vector = NULL;
  125. }
  126. uint32_t dal_vector_get_count(
  127. const struct vector *vector)
  128. {
  129. return vector->count;
  130. }
  131. void *dal_vector_at_index(
  132. const struct vector *vector,
  133. uint32_t index)
  134. {
  135. if (vector->container == NULL || index >= vector->count)
  136. return NULL;
  137. return vector->container + (index * vector->struct_size);
  138. }
  139. bool dal_vector_remove_at_index(
  140. struct vector *vector,
  141. uint32_t index)
  142. {
  143. if (index >= vector->count)
  144. return false;
  145. if (index != vector->count - 1)
  146. memmove(
  147. vector->container + (index * vector->struct_size),
  148. vector->container + ((index + 1) * vector->struct_size),
  149. (vector->count - index - 1) * vector->struct_size);
  150. vector->count -= 1;
  151. return true;
  152. }
  153. void dal_vector_set_at_index(
  154. const struct vector *vector,
  155. const void *what,
  156. uint32_t index)
  157. {
  158. void *where = dal_vector_at_index(vector, index);
  159. if (!where) {
  160. BREAK_TO_DEBUGGER();
  161. return;
  162. }
  163. memmove(
  164. where,
  165. what,
  166. vector->struct_size);
  167. }
  168. static inline uint32_t calc_increased_capacity(
  169. uint32_t old_capacity)
  170. {
  171. return old_capacity * 2;
  172. }
  173. bool dal_vector_insert_at(
  174. struct vector *vector,
  175. const void *what,
  176. uint32_t position)
  177. {
  178. uint8_t *insert_address;
  179. if (vector->count == vector->capacity) {
  180. if (!dal_vector_reserve(
  181. vector,
  182. calc_increased_capacity(vector->capacity)))
  183. return false;
  184. }
  185. insert_address = vector->container + (vector->struct_size * position);
  186. if (vector->count && position < vector->count)
  187. memmove(
  188. insert_address + vector->struct_size,
  189. insert_address,
  190. vector->struct_size * (vector->count - position));
  191. memmove(
  192. insert_address,
  193. what,
  194. vector->struct_size);
  195. vector->count++;
  196. return true;
  197. }
  198. bool dal_vector_append(
  199. struct vector *vector,
  200. const void *item)
  201. {
  202. return dal_vector_insert_at(vector, item, vector->count);
  203. }
  204. struct vector *dal_vector_clone(
  205. const struct vector *vector)
  206. {
  207. struct vector *vec_cloned;
  208. uint32_t count;
  209. /* create new vector */
  210. count = dal_vector_get_count(vector);
  211. if (count == 0)
  212. /* when count is 0 we still want to create clone of the vector
  213. */
  214. vec_cloned = dal_vector_create(
  215. vector->ctx,
  216. vector->capacity,
  217. vector->struct_size);
  218. else
  219. /* Call "presized create" version, independently of how the
  220. * original vector was created.
  221. * The owner of original vector must know how to treat the new
  222. * vector - as "presized" or as "regular".
  223. * But from vector point of view it doesn't matter. */
  224. vec_cloned = dal_vector_presized_create(vector->ctx, count,
  225. NULL,/* no initial value */
  226. vector->struct_size);
  227. if (NULL == vec_cloned) {
  228. BREAK_TO_DEBUGGER();
  229. return NULL;
  230. }
  231. /* copy vector's data */
  232. memmove(vec_cloned->container, vector->container,
  233. vec_cloned->struct_size * vec_cloned->capacity);
  234. return vec_cloned;
  235. }
  236. uint32_t dal_vector_capacity(const struct vector *vector)
  237. {
  238. return vector->capacity;
  239. }
  240. bool dal_vector_reserve(struct vector *vector, uint32_t capacity)
  241. {
  242. void *new_container;
  243. if (capacity <= vector->capacity)
  244. return true;
  245. new_container = krealloc(vector->container,
  246. capacity * vector->struct_size, GFP_KERNEL);
  247. if (new_container) {
  248. vector->container = new_container;
  249. vector->capacity = capacity;
  250. return true;
  251. }
  252. return false;
  253. }
  254. void dal_vector_clear(struct vector *vector)
  255. {
  256. vector->count = 0;
  257. }