revert between 56095 -> 55830 in arch
[AROS.git] / workbench / libs / mesa / src / gallium / state_trackers / vega / util_array.h
blobb2d22f476eedcd82d03087b3f98b5116f366f510
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
15 * of the Software.
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 **************************************************************************/
27 #ifndef UTIL_ARRAY_H
28 #define UTIL_ARRAY_H
30 #include "util/u_memory.h"
32 #define DEFAULT_ARRAY_SIZE 256
34 struct array {
35 VGint datatype_size;
36 void *data;
37 VGint size;
38 VGint num_elements;
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);
49 return array;
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;
58 array->size = size;
59 array->data = malloc(array->size * array->datatype_size);
61 return array;
64 static INLINE void array_destroy(struct array *array)
66 if (array)
67 free(array->data);
68 FREE(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);
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;
83 static INLINE void array_append_data(struct array *array,
84 const void *data, int num_elements)
86 VGbyte *adata;
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,
98 const void *data,
99 int start_idx,
100 int num_elements)
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,
108 int idx)
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;
122 #endif