1 /************************************************************************** 2 * 3 * Copyright 2009 VMware, Inc. All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sub license, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial portions 15 * of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 * 25 **************************************************************************/ 26 27 #ifndef UTIL_ARRAY_H 28 #define UTIL_ARRAY_H 29 30 #include "util/u_memory.h" 31 32 #define DEFAULT_ARRAY_SIZE 256 33 34 struct array { 35 VGint datatype_size; 36 void *data; 37 VGint size; 38 VGint num_elements; 39 }; 40 41 static INLINE struct array *array_create(VGint datatype_size) 42 { 43 struct array *array = CALLOC_STRUCT(array); 44 array->datatype_size = datatype_size; 45 46 array->size = DEFAULT_ARRAY_SIZE; 47 array->data = malloc(array->size * array->datatype_size); 48 49 return array; 50 } 51 52 53 static INLINE struct array *array_create_size(VGint datatype_size, VGint size) 54 { 55 struct array *array = CALLOC_STRUCT(array); 56 array->datatype_size = datatype_size; 57 58 array->size = size; 59 array->data = malloc(array->size * array->datatype_size); 60 61 return array; 62 } 63 64 static INLINE void array_destroy(struct array *array) 65 { 66 if (array) 67 free(array->data); 68 FREE(array); 69 } 70 71 static INLINE void array_resize(struct array *array, int num) 72 { 73 VGint size = array->datatype_size * num; 74 void *data = malloc(size); 75 memcpy(data, array->data, array->size * array->datatype_size); 76 free(array->data); 77 array->data = data; 78 array->size = num; 79 array->num_elements = (array->num_elements > num) ? num : 80 array->num_elements; 81 } 82 83 static INLINE void array_append_data(struct array *array, 84 const void *data, int num_elements) 85 { 86 VGbyte *adata; 87 88 while (array->num_elements + num_elements > array->size) { 89 array_resize(array, (array->num_elements + num_elements) * 1.5); 90 } 91 adata = (VGbyte *)array->data; 92 memcpy(adata + (array->num_elements * array->datatype_size), data, 93 num_elements * array->datatype_size); 94 array->num_elements += num_elements; 95 } 96 97 static INLINE void array_change_data(struct array *array, 98 const void *data, 99 int start_idx, 100 int num_elements) 101 { 102 VGbyte *adata = (VGbyte *)array->data; 103 memcpy(adata + (start_idx * array->datatype_size), data, 104 num_elements * array->datatype_size); 105 } 106 107 static INLINE void array_remove_element(struct array *array, 108 int idx) 109 { 110 VGbyte *adata = (VGbyte *)array->data; 111 memmove(adata + (idx * array->datatype_size), 112 adata + ((idx + 1) * array->datatype_size), 113 (array->num_elements - idx - 1) * array->datatype_size); 114 --array->num_elements; 115 } 116 117 static INLINE void array_reset(struct array *array) 118 { 119 array->num_elements = 0; 120 } 121 122 #endif 123