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