1 /* Copyright 2024 Advanced Micro Devices, Inc.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 * OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Authors: AMD
22 *
23 */
24 #include <string.h>
25 #include "vector.h"
26 #include "vpe_priv.h"
27
vpe_vector_create(struct vpe_priv * vpe_priv,size_t element_size,size_t initial_capacity)28 struct vpe_vector *vpe_vector_create(
29 struct vpe_priv *vpe_priv, size_t element_size, size_t initial_capacity)
30 {
31 struct vpe_vector *vector;
32 vector = (struct vpe_vector *)vpe_zalloc(sizeof(struct vpe_vector));
33 if (!vector)
34 return NULL;
35
36 vector->element = vpe_zalloc(initial_capacity * element_size);
37 if (!vector->element) {
38 vpe_free(vector);
39 return NULL;
40 }
41
42 vector->vpe_priv = vpe_priv;
43 vector->num_elements = 0;
44 vector->capacity = initial_capacity;
45 vector->element_size = element_size;
46
47 return vector;
48 }
49
vector_realloc(struct vpe_vector * vector,size_t new_size)50 static struct vpe_vector *vector_realloc(struct vpe_vector *vector, size_t new_size)
51 {
52 struct vpe_priv *vpe_priv = vector->vpe_priv;
53
54 void *new_element = vpe_zalloc(new_size);
55 if (!new_element)
56 return NULL;
57
58 memcpy(new_element, vector->element, vector->num_elements * vector->element_size);
59 vpe_free(vector->element);
60
61 vector->element = new_element;
62 vector->capacity = new_size / vector->element_size;
63
64 return vector;
65 }
66
vpe_vector_get(struct vpe_vector * vector,size_t idx)67 void *vpe_vector_get(struct vpe_vector *vector, size_t idx)
68 {
69 if (!vector)
70 return NULL;
71
72 return (void *)((char *)(vector->element) + (idx * vector->element_size));
73 }
74
vpe_vector_push(struct vpe_vector * vector,void * p_element)75 void vpe_vector_push(struct vpe_vector *vector, void *p_element)
76 {
77 if (!p_element || !vector)
78 return;
79
80 if (vector->num_elements >= vector->capacity) {
81 vector->capacity *= 2;
82 vector = vector_realloc(vector, vector->capacity * vector->element_size);
83 }
84
85 if (!vector)
86 return;
87
88 memcpy((void *)((char *)(vector->element) + (vector->num_elements * vector->element_size)),
89 p_element, vector->element_size);
90 vector->num_elements++;
91 }
92
vpe_vector_clear(struct vpe_vector * vector)93 void vpe_vector_clear(struct vpe_vector *vector)
94 {
95 if (!vector)
96 return;
97
98 vector->num_elements = 0;
99 memset(vector->element, 0, vector->capacity * vector->element_size);
100 }
101
vpe_vector_free(struct vpe_vector * vector)102 void vpe_vector_free(struct vpe_vector *vector)
103 {
104 struct vpe_priv *vpe_priv = vector->vpe_priv;
105
106 vpe_free(vector->element);
107 vector->element = NULL;
108 vpe_free(vector);
109 }
110