2 * Copyright 2012-15 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include "dm_services.h"
27 #include "include/vector.h"
29 bool dal_vector_construct(
30 struct vector *vector,
31 struct dc_context *ctx,
35 vector->container = NULL;
37 if (!struct_size || !capacity) {
38 /* Container must be non-zero size*/
43 vector->container = kcalloc(capacity, struct_size, GFP_KERNEL);
44 if (vector->container == NULL)
46 vector->capacity = capacity;
47 vector->struct_size = struct_size;
53 bool dal_vector_presized_costruct(
54 struct vector *vector,
55 struct dc_context *ctx,
62 vector->container = NULL;
64 if (!struct_size || !count) {
65 /* Container must be non-zero size*/
70 vector->container = kcalloc(count, struct_size, GFP_KERNEL);
72 if (vector->container == NULL)
75 /* If caller didn't supply initial value then the default
76 * of all zeros is expected, which is exactly what dal_alloc()
77 * initialises the memory to. */
78 if (NULL != initial_value) {
79 for (i = 0; i < count; ++i)
81 vector->container + i * struct_size,
86 vector->capacity = count;
87 vector->struct_size = struct_size;
88 vector->count = count;
92 struct vector *dal_vector_presized_create(
93 struct dc_context *ctx,
98 struct vector *vector = kzalloc(sizeof(struct vector), GFP_KERNEL);
103 if (dal_vector_presized_costruct(
104 vector, ctx, size, initial_value, struct_size))
112 struct vector *dal_vector_create(
113 struct dc_context *ctx,
115 uint32_t struct_size)
117 struct vector *vector = kzalloc(sizeof(struct vector), GFP_KERNEL);
122 if (dal_vector_construct(vector, ctx, capacity, struct_size))
130 void dal_vector_destruct(
131 struct vector *vector)
133 kfree(vector->container);
135 vector->capacity = 0;
138 void dal_vector_destroy(
139 struct vector **vector)
141 if (vector == NULL || *vector == NULL)
143 dal_vector_destruct(*vector);
148 uint32_t dal_vector_get_count(
149 const struct vector *vector)
151 return vector->count;
154 void *dal_vector_at_index(
155 const struct vector *vector,
158 if (vector->container == NULL || index >= vector->count)
160 return vector->container + (index * vector->struct_size);
163 bool dal_vector_remove_at_index(
164 struct vector *vector,
167 if (index >= vector->count)
170 if (index != vector->count - 1)
172 vector->container + (index * vector->struct_size),
173 vector->container + ((index + 1) * vector->struct_size),
174 (vector->count - index - 1) * vector->struct_size);
180 void dal_vector_set_at_index(
181 const struct vector *vector,
185 void *where = dal_vector_at_index(vector, index);
194 vector->struct_size);
197 static inline uint32_t calc_increased_capacity(
198 uint32_t old_capacity)
200 return old_capacity * 2;
203 bool dal_vector_insert_at(
204 struct vector *vector,
208 uint8_t *insert_address;
210 if (vector->count == vector->capacity) {
211 if (!dal_vector_reserve(
213 calc_increased_capacity(vector->capacity)))
217 insert_address = vector->container + (vector->struct_size * position);
219 if (vector->count && position < vector->count)
221 insert_address + vector->struct_size,
223 vector->struct_size * (vector->count - position));
228 vector->struct_size);
235 bool dal_vector_append(
236 struct vector *vector,
239 return dal_vector_insert_at(vector, item, vector->count);
242 struct vector *dal_vector_clone(
243 const struct vector *vector)
245 struct vector *vec_cloned;
248 /* create new vector */
249 count = dal_vector_get_count(vector);
252 /* when count is 0 we still want to create clone of the vector
254 vec_cloned = dal_vector_create(
257 vector->struct_size);
259 /* Call "presized create" version, independently of how the
260 * original vector was created.
261 * The owner of original vector must know how to treat the new
262 * vector - as "presized" or as "regular".
263 * But from vector point of view it doesn't matter. */
264 vec_cloned = dal_vector_presized_create(vector->ctx, count,
265 NULL,/* no initial value */
266 vector->struct_size);
268 if (NULL == vec_cloned) {
273 /* copy vector's data */
274 memmove(vec_cloned->container, vector->container,
275 vec_cloned->struct_size * vec_cloned->capacity);
280 uint32_t dal_vector_capacity(const struct vector *vector)
282 return vector->capacity;
285 bool dal_vector_reserve(struct vector *vector, uint32_t capacity)
289 if (capacity <= vector->capacity)
292 new_container = krealloc(vector->container,
293 capacity * vector->struct_size, GFP_KERNEL);
296 vector->container = new_container;
297 vector->capacity = capacity;
304 void dal_vector_clear(struct vector *vector)