1 /**************************************************************************
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
30 #include "util/u_memory.h"
32 #define DEFAULT_ARRAY_SIZE 256
41 static INLINE struct array *array_create(VGint datatype_size)
43 struct array *array = CALLOC_STRUCT(array);
44 array->datatype_size = datatype_size;
46 array->size = DEFAULT_ARRAY_SIZE;
47 array->data = malloc(array->size * array->datatype_size);
53 static INLINE struct array *array_create_size(VGint datatype_size, VGint size)
55 struct array *array = CALLOC_STRUCT(array);
56 array->datatype_size = datatype_size;
59 array->data = malloc(array->size * array->datatype_size);
64 static INLINE void array_destroy(struct array *array)
71 static INLINE void array_resize(struct array *array, int num)
73 VGint size = array->datatype_size * num;
74 void *data = malloc(size);
75 memcpy(data, array->data, array->size * array->datatype_size);
79 array->num_elements = (array->num_elements > num) ? num :
83 static INLINE void array_append_data(struct array *array,
84 const void *data, int num_elements)
88 while (array->num_elements + num_elements > array->size) {
89 array_resize(array, (array->num_elements + num_elements) * 1.5);
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;
97 static INLINE void array_change_data(struct array *array,
102 VGbyte *adata = (VGbyte *)array->data;
103 memcpy(adata + (start_idx * array->datatype_size), data,
104 num_elements * array->datatype_size);
107 static INLINE void array_remove_element(struct array *array,
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;
117 static INLINE void array_reset(struct array *array)
119 array->num_elements = 0;