xref: /dflybsd-src/sys/dev/drm/amd/display/include/vector.h (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2012-15 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  * Authors: AMD
23*b843c749SSergey Zigachev  *
24*b843c749SSergey Zigachev  */
25*b843c749SSergey Zigachev 
26*b843c749SSergey Zigachev #ifndef __DAL_VECTOR_H__
27*b843c749SSergey Zigachev #define __DAL_VECTOR_H__
28*b843c749SSergey Zigachev 
29*b843c749SSergey Zigachev struct vector {
30*b843c749SSergey Zigachev 	uint8_t *container;
31*b843c749SSergey Zigachev 	uint32_t struct_size;
32*b843c749SSergey Zigachev 	uint32_t count;
33*b843c749SSergey Zigachev 	uint32_t capacity;
34*b843c749SSergey Zigachev 	struct dc_context *ctx;
35*b843c749SSergey Zigachev };
36*b843c749SSergey Zigachev 
37*b843c749SSergey Zigachev bool dal_vector_construct(
38*b843c749SSergey Zigachev 	struct vector *vector,
39*b843c749SSergey Zigachev 	struct dc_context *ctx,
40*b843c749SSergey Zigachev 	uint32_t capacity,
41*b843c749SSergey Zigachev 	uint32_t struct_size);
42*b843c749SSergey Zigachev 
43*b843c749SSergey Zigachev struct vector *dal_vector_create(
44*b843c749SSergey Zigachev 	struct dc_context *ctx,
45*b843c749SSergey Zigachev 	uint32_t capacity,
46*b843c749SSergey Zigachev 	uint32_t struct_size);
47*b843c749SSergey Zigachev 
48*b843c749SSergey Zigachev /* 'initial_value' is optional. If initial_value not supplied,
49*b843c749SSergey Zigachev  * each "structure" in the vector will contain zeros by default. */
50*b843c749SSergey Zigachev struct vector *dal_vector_presized_create(
51*b843c749SSergey Zigachev 	struct dc_context *ctx,
52*b843c749SSergey Zigachev 	uint32_t size,
53*b843c749SSergey Zigachev 	void *initial_value,
54*b843c749SSergey Zigachev 	uint32_t struct_size);
55*b843c749SSergey Zigachev 
56*b843c749SSergey Zigachev void dal_vector_destruct(
57*b843c749SSergey Zigachev 	struct vector *vector);
58*b843c749SSergey Zigachev 
59*b843c749SSergey Zigachev void dal_vector_destroy(
60*b843c749SSergey Zigachev 	struct vector **vector);
61*b843c749SSergey Zigachev 
62*b843c749SSergey Zigachev uint32_t dal_vector_get_count(
63*b843c749SSergey Zigachev 	const struct vector *vector);
64*b843c749SSergey Zigachev 
65*b843c749SSergey Zigachev /* dal_vector_insert_at
66*b843c749SSergey Zigachev  * reallocate container if necessary
67*b843c749SSergey Zigachev  * then shell items at right and insert
68*b843c749SSergey Zigachev  * return if the container modified
69*b843c749SSergey Zigachev  * do not check that index belongs to container
70*b843c749SSergey Zigachev  * since the function is private and index is going to be calculated
71*b843c749SSergey Zigachev  * either with by function or as get_count+1 */
72*b843c749SSergey Zigachev bool dal_vector_insert_at(
73*b843c749SSergey Zigachev 	struct vector *vector,
74*b843c749SSergey Zigachev 	const void *what,
75*b843c749SSergey Zigachev 	uint32_t position);
76*b843c749SSergey Zigachev 
77*b843c749SSergey Zigachev bool dal_vector_append(
78*b843c749SSergey Zigachev 	struct vector *vector,
79*b843c749SSergey Zigachev 	const void *item);
80*b843c749SSergey Zigachev 
81*b843c749SSergey Zigachev /* operator[] */
82*b843c749SSergey Zigachev void *dal_vector_at_index(
83*b843c749SSergey Zigachev 	const struct vector *vector,
84*b843c749SSergey Zigachev 	uint32_t index);
85*b843c749SSergey Zigachev 
86*b843c749SSergey Zigachev void dal_vector_set_at_index(
87*b843c749SSergey Zigachev 	const struct vector *vector,
88*b843c749SSergey Zigachev 	const void *what,
89*b843c749SSergey Zigachev 	uint32_t index);
90*b843c749SSergey Zigachev 
91*b843c749SSergey Zigachev /* create a clone (copy) of a vector */
92*b843c749SSergey Zigachev struct vector *dal_vector_clone(
93*b843c749SSergey Zigachev 	const struct vector *vector_other);
94*b843c749SSergey Zigachev 
95*b843c749SSergey Zigachev /* dal_vector_remove_at_index
96*b843c749SSergey Zigachev  * Shifts elements on the right from remove position to the left,
97*b843c749SSergey Zigachev  * removing an element at position by overwrite means*/
98*b843c749SSergey Zigachev bool dal_vector_remove_at_index(
99*b843c749SSergey Zigachev 	struct vector *vector,
100*b843c749SSergey Zigachev 	uint32_t index);
101*b843c749SSergey Zigachev 
102*b843c749SSergey Zigachev uint32_t dal_vector_capacity(const struct vector *vector);
103*b843c749SSergey Zigachev 
104*b843c749SSergey Zigachev bool dal_vector_reserve(struct vector *vector, uint32_t capacity);
105*b843c749SSergey Zigachev 
106*b843c749SSergey Zigachev void dal_vector_clear(struct vector *vector);
107*b843c749SSergey Zigachev 
108*b843c749SSergey Zigachev /***************************************************************************
109*b843c749SSergey Zigachev  * Macro definitions of TYPE-SAFE versions of vector set/get functions.
110*b843c749SSergey Zigachev  ***************************************************************************/
111*b843c749SSergey Zigachev 
112*b843c749SSergey Zigachev #define DAL_VECTOR_INSERT_AT(vector_type, type_t) \
113*b843c749SSergey Zigachev 	static bool vector_type##_vector_insert_at( \
114*b843c749SSergey Zigachev 		struct vector *vector, \
115*b843c749SSergey Zigachev 		type_t what, \
116*b843c749SSergey Zigachev 		uint32_t position) \
117*b843c749SSergey Zigachev { \
118*b843c749SSergey Zigachev 	return dal_vector_insert_at(vector, what, position); \
119*b843c749SSergey Zigachev }
120*b843c749SSergey Zigachev 
121*b843c749SSergey Zigachev #define DAL_VECTOR_APPEND(vector_type, type_t) \
122*b843c749SSergey Zigachev 	static bool vector_type##_vector_append( \
123*b843c749SSergey Zigachev 		struct vector *vector, \
124*b843c749SSergey Zigachev 		type_t item) \
125*b843c749SSergey Zigachev { \
126*b843c749SSergey Zigachev 	return dal_vector_append(vector, item); \
127*b843c749SSergey Zigachev }
128*b843c749SSergey Zigachev 
129*b843c749SSergey Zigachev /* Note: "type_t" is the ONLY token accepted by "checkpatch.pl" and by
130*b843c749SSergey Zigachev  * "checkcommit" as *return type*.
131*b843c749SSergey Zigachev  * For uniformity reasons "type_t" is used for all type-safe macro
132*b843c749SSergey Zigachev  * definitions here. */
133*b843c749SSergey Zigachev #define DAL_VECTOR_AT_INDEX(vector_type, type_t) \
134*b843c749SSergey Zigachev 	static type_t vector_type##_vector_at_index( \
135*b843c749SSergey Zigachev 		const struct vector *vector, \
136*b843c749SSergey Zigachev 		uint32_t index) \
137*b843c749SSergey Zigachev { \
138*b843c749SSergey Zigachev 	return dal_vector_at_index(vector, index); \
139*b843c749SSergey Zigachev }
140*b843c749SSergey Zigachev 
141*b843c749SSergey Zigachev #define DAL_VECTOR_SET_AT_INDEX(vector_type, type_t) \
142*b843c749SSergey Zigachev 	static void vector_type##_vector_set_at_index( \
143*b843c749SSergey Zigachev 		const struct vector *vector, \
144*b843c749SSergey Zigachev 		type_t what, \
145*b843c749SSergey Zigachev 		uint32_t index) \
146*b843c749SSergey Zigachev { \
147*b843c749SSergey Zigachev 	dal_vector_set_at_index(vector, what, index); \
148*b843c749SSergey Zigachev }
149*b843c749SSergey Zigachev 
150*b843c749SSergey Zigachev #endif /* __DAL_VECTOR_H__ */
151