vector.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. if (vector->container != NULL)
  114. kfree(vector->container);
  115. vector->count = 0;
  116. vector->capacity = 0;
  117. }
  118. void dal_vector_destroy(
  119. struct vector **vector)
  120. {
  121. if (vector == NULL || *vector == NULL)
  122. return;
  123. dal_vector_destruct(*vector);
  124. kfree(*vector);
  125. *vector = NULL;
  126. }
  127. uint32_t dal_vector_get_count(
  128. const struct vector *vector)
  129. {
  130. return vector->count;
  131. }
  132. void *dal_vector_at_index(
  133. const struct vector *vector,
  134. uint32_t index)
  135. {
  136. if (vector->container == NULL || index >= vector->count)
  137. return NULL;
  138. return vector->container + (index * vector->struct_size);
  139. }
  140. bool dal_vector_remove_at_index(
  141. struct vector *vector,
  142. uint32_t index)
  143. {
  144. if (index >= vector->count)
  145. return false;
  146. if (index != vector->count - 1)
  147. memmove(
  148. vector->container + (index * vector->struct_size),
  149. vector->container + ((index + 1) * vector->struct_size),
  150. (vector->count - index - 1) * vector->struct_size);
  151. vector->count -= 1;
  152. return true;
  153. }
  154. void dal_vector_set_at_index(
  155. const struct vector *vector,
  156. const void *what,
  157. uint32_t index)
  158. {
  159. void *where = dal_vector_at_index(vector, index);
  160. if (!where) {
  161. BREAK_TO_DEBUGGER();
  162. return;
  163. }
  164. memmove(
  165. where,
  166. what,
  167. vector->struct_size);
  168. }
  169. static inline uint32_t calc_increased_capacity(
  170. uint32_t old_capacity)
  171. {
  172. return old_capacity * 2;
  173. }
  174. bool dal_vector_insert_at(
  175. struct vector *vector,
  176. const void *what,
  177. uint32_t position)
  178. {
  179. uint8_t *insert_address;
  180. if (vector->count == vector->capacity) {
  181. if (!dal_vector_reserve(
  182. vector,
  183. calc_increased_capacity(vector->capacity)))
  184. return false;
  185. }
  186. insert_address = vector->container + (vector->struct_size * position);
  187. if (vector->count && position < vector->count)
  188. memmove(
  189. insert_address + vector->struct_size,
  190. insert_address,
  191. vector->struct_size * (vector->count - position));
  192. memmove(
  193. insert_address,
  194. what,
  195. vector->struct_size);
  196. vector->count++;
  197. return true;
  198. }
  199. bool dal_vector_append(
  200. struct vector *vector,
  201. const void *item)
  202. {
  203. return dal_vector_insert_at(vector, item, vector->count);
  204. }
  205. struct vector *dal_vector_clone(
  206. const struct vector *vector)
  207. {
  208. struct vector *vec_cloned;
  209. uint32_t count;
  210. /* create new vector */
  211. count = dal_vector_get_count(vector);
  212. if (count == 0)
  213. /* when count is 0 we still want to create clone of the vector
  214. */
  215. vec_cloned = dal_vector_create(
  216. vector->ctx,
  217. vector->capacity,
  218. vector->struct_size);
  219. else
  220. /* Call "presized create" version, independently of how the
  221. * original vector was created.
  222. * The owner of original vector must know how to treat the new
  223. * vector - as "presized" or as "regular".
  224. * But from vector point of view it doesn't matter. */
  225. vec_cloned = dal_vector_presized_create(vector->ctx, count,
  226. NULL,/* no initial value */
  227. vector->struct_size);
  228. if (NULL == vec_cloned) {
  229. BREAK_TO_DEBUGGER();
  230. return NULL;
  231. }
  232. /* copy vector's data */
  233. memmove(vec_cloned->container, vector->container,
  234. vec_cloned->struct_size * vec_cloned->capacity);
  235. return vec_cloned;
  236. }
  237. uint32_t dal_vector_capacity(const struct vector *vector)
  238. {
  239. return vector->capacity;
  240. }
  241. bool dal_vector_reserve(struct vector *vector, uint32_t capacity)
  242. {
  243. void *new_container;
  244. if (capacity <= vector->capacity)
  245. return true;
  246. new_container = krealloc(vector->container,
  247. capacity * vector->struct_size, GFP_KERNEL);
  248. if (new_container) {
  249. vector->container = new_container;
  250. vector->capacity = capacity;
  251. return true;
  252. }
  253. return false;
  254. }
  255. void dal_vector_clear(struct vector *vector)
  256. {
  257. vector->count = 0;
  258. }