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 
26 #ifndef __DAL_VECTOR_H__
27 #define __DAL_VECTOR_H__
28 
29 struct vector {
30 	uint8_t *container;
31 	uint32_t struct_size;
32 	uint32_t count;
33 	uint32_t capacity;
34 	struct dc_context *ctx;
35 };
36 
37 bool dal_vector_construct(
38 	struct vector *vector,
39 	struct dc_context *ctx,
40 	uint32_t capacity,
41 	uint32_t struct_size);
42 
43 struct vector *dal_vector_create(
44 	struct dc_context *ctx,
45 	uint32_t capacity,
46 	uint32_t struct_size);
47 
48 /* 'initial_value' is optional. If initial_value not supplied,
49  * each "structure" in the vector will contain zeros by default. */
50 struct vector *dal_vector_presized_create(
51 	struct dc_context *ctx,
52 	uint32_t size,
53 	void *initial_value,
54 	uint32_t struct_size);
55 
56 void dal_vector_destruct(
57 	struct vector *vector);
58 
59 void dal_vector_destroy(
60 	struct vector **vector);
61 
62 uint32_t dal_vector_get_count(
63 	const struct vector *vector);
64 
65 /* dal_vector_insert_at
66  * reallocate container if necessary
67  * then shell items at right and insert
68  * return if the container modified
69  * do not check that index belongs to container
70  * since the function is private and index is going to be calculated
71  * either with by function or as get_count+1 */
72 bool dal_vector_insert_at(
73 	struct vector *vector,
74 	const void *what,
75 	uint32_t position);
76 
77 bool dal_vector_append(
78 	struct vector *vector,
79 	const void *item);
80 
81 /* operator[] */
82 void *dal_vector_at_index(
83 	const struct vector *vector,
84 	uint32_t index);
85 
86 void dal_vector_set_at_index(
87 	const struct vector *vector,
88 	const void *what,
89 	uint32_t index);
90 
91 /* create a clone (copy) of a vector */
92 struct vector *dal_vector_clone(
93 	const struct vector *vector_other);
94 
95 /* dal_vector_remove_at_index
96  * Shifts elements on the right from remove position to the left,
97  * removing an element at position by overwrite means*/
98 bool dal_vector_remove_at_index(
99 	struct vector *vector,
100 	uint32_t index);
101 
102 uint32_t dal_vector_capacity(const struct vector *vector);
103 
104 bool dal_vector_reserve(struct vector *vector, uint32_t capacity);
105 
106 void dal_vector_clear(struct vector *vector);
107 
108 /***************************************************************************
109  * Macro definitions of TYPE-SAFE versions of vector set/get functions.
110  ***************************************************************************/
111 
112 #define DAL_VECTOR_INSERT_AT(vector_type, type_t) \
113 	static bool vector_type##_vector_insert_at( \
114 		struct vector *vector, \
115 		type_t what, \
116 		uint32_t position) \
117 { \
118 	return dal_vector_insert_at(vector, what, position); \
119 }
120 
121 #define DAL_VECTOR_APPEND(vector_type, type_t) \
122 	static bool vector_type##_vector_append( \
123 		struct vector *vector, \
124 		type_t item) \
125 { \
126 	return dal_vector_append(vector, item); \
127 }
128 
129 /* Note: "type_t" is the ONLY token accepted by "checkpatch.pl" and by
130  * "checkcommit" as *return type*.
131  * For uniformity reasons "type_t" is used for all type-safe macro
132  * definitions here. */
133 #define DAL_VECTOR_AT_INDEX(vector_type, type_t) \
134 	static type_t vector_type##_vector_at_index( \
135 		const struct vector *vector, \
136 		uint32_t index) \
137 { \
138 	return dal_vector_at_index(vector, index); \
139 }
140 
141 #define DAL_VECTOR_SET_AT_INDEX(vector_type, type_t) \
142 	static void vector_type##_vector_set_at_index( \
143 		const struct vector *vector, \
144 		type_t what, \
145 		uint32_t index) \
146 { \
147 	dal_vector_set_at_index(vector, what, index); \
148 }
149 
150 #endif /* __DAL_VECTOR_H__ */
151