3 /*-------------------------------------------------------------------------
4 * drawElements Memory Pool Library
5 * --------------------------------
7 * Copyright 2014 The Android Open Source Project
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Memory pool array class.
24 *//*--------------------------------------------------------------------*/
27 #include "deMemPool.h"
31 DE_ARRAY_ELEMENTS_PER_PAGE_LOG2 = 4 /*!< \internal 16 */
34 /*--------------------------------------------------------------------*//*!
36 * \brief Type-independent version of the array template class.
37 *//*--------------------------------------------------------------------*/
38 typedef struct dePoolArray_s
40 deMemPool* pool; /*!< Pool from which all memory is allocated from. */
42 int elementSize; /*!< Size of the element (in bytes). */
43 int numElements; /*!< Number of elements in the array. */
44 int capacity; /*!< Number of allocated elements in the array. */
46 int pageTableCapacity; /*!< Size of the page table. */
47 void** pageTable; /*!< Pointer to the page table. */
52 dePoolArray* dePoolArray_create (deMemPool* pool, int elementSize);
53 deBool dePoolArray_reserve (dePoolArray* arr, int capacity);
54 deBool dePoolArray_setSize (dePoolArray* arr, int size);
56 void dePoolArray_selfTest (void);
60 /*--------------------------------------------------------------------*//*!
61 * \brief Declare a template pool array class.
62 * \param TYPENAME Type name of the declared array.
63 * \param VALUETYPE Type of the value contained in the array.
65 * This macro declares a pool array with all the necessary functions for
66 * operating with it. All allocated memory is taken from the memory pool
67 * given to the constructor.
69 * The array is implemented by having a set of pages (which store the
70 * elements) and a page table with pointers to each of them. The pages
71 * are allocated individually whenever they are needed, but the page
72 * table grows exponentially. This keeps the memory overhead for large
73 * arrays very small. On the other hand, the relative overhead for very
74 * small arrays is prohibitive (the minimum allocation is 16 elements).
76 * The functions for operating the array are:
77 * \todo [petri] Figure out how to comment these in Doxygen-style.
80 * Array* Array_create (deMemPool* pool);
81 * int Array_getNumElements (const Array* array);
82 * deBool Array_reserve (Array* array, int size);
83 * deBool Array_setSize (Array* array, int size);
84 * void Array_reset (Array* array);
85 * Element Array_get (Array* array, int ndx);
86 * deBool Array_set (Array* array, int ndx, Element elem);
87 * deBool Array_pushBack (Array* array, Element elem);
88 * Element Array_popBack (Array* array);
89 * void Array_swap (Array* array, int aNdx, int bNdx);
91 *//*--------------------------------------------------------------------*/
92 #define DE_DECLARE_POOL_ARRAY(TYPENAME, VALUETYPE) \
94 typedef struct TYPENAME##_s \
102 int pageTableCapacity; \
103 DE_PTR_TYPE(VALUETYPE)* pageTable; \
104 } TYPENAME; /* NOLINT(TYPENAME) */ \
106 DE_INLINE TYPENAME* TYPENAME##_create (deMemPool* pool); \
107 DE_INLINE int TYPENAME##_getNumElements (const TYPENAME* arr) DE_UNUSED_FUNCTION; \
108 DE_INLINE deBool TYPENAME##_reserve (DE_PTR_TYPE(TYPENAME) arr, int capacity) DE_UNUSED_FUNCTION; \
109 DE_INLINE deBool TYPENAME##_setSize (DE_PTR_TYPE(TYPENAME) arr, int size) DE_UNUSED_FUNCTION; \
110 DE_INLINE void TYPENAME##_reset (DE_PTR_TYPE(TYPENAME) arr) DE_UNUSED_FUNCTION; \
111 DE_INLINE VALUETYPE TYPENAME##_get (const TYPENAME* arr, int ndx) DE_UNUSED_FUNCTION; \
112 DE_INLINE void TYPENAME##_set (DE_PTR_TYPE(TYPENAME) arr, int ndx, VALUETYPE elem) DE_UNUSED_FUNCTION; \
113 DE_INLINE deBool TYPENAME##_pushBack (DE_PTR_TYPE(TYPENAME) arr, VALUETYPE elem) DE_UNUSED_FUNCTION; \
114 DE_INLINE VALUETYPE TYPENAME##_popBack (DE_PTR_TYPE(TYPENAME) arr) DE_UNUSED_FUNCTION; \
115 DE_INLINE deBool TYPENAME##_copy (DE_PTR_TYPE(TYPENAME) dst, const TYPENAME* src) DE_UNUSED_FUNCTION; \
116 DE_INLINE void TYPENAME##_swap (DE_PTR_TYPE(TYPENAME) arr, int aNdx, int bNdx) DE_UNUSED_FUNCTION; \
118 DE_INLINE TYPENAME* TYPENAME##_create (deMemPool* pool) \
120 return (TYPENAME*)dePoolArray_create(pool, sizeof(VALUETYPE)); \
123 DE_INLINE int TYPENAME##_getNumElements (const TYPENAME* arr) \
125 return arr->numElements; \
128 DE_INLINE deBool TYPENAME##_reserve (DE_PTR_TYPE(TYPENAME) arr, int capacity) \
130 if (capacity > arr->capacity) \
131 return dePoolArray_reserve((dePoolArray*)arr, capacity); \
135 DE_INLINE deBool TYPENAME##_setSize (DE_PTR_TYPE(TYPENAME) arr, int size) \
137 if (size > arr->capacity) \
138 return dePoolArray_setSize((dePoolArray*)arr, size); \
140 arr->numElements = size; \
144 DE_INLINE void TYPENAME##_reset (DE_PTR_TYPE(TYPENAME) arr) \
146 arr->numElements = 0; \
149 DE_INLINE VALUETYPE TYPENAME##_get (const TYPENAME* arr, int ndx) \
151 DE_ASSERT(ndx >= 0 && ndx < arr->numElements); \
153 int pageNdx = (ndx >> DE_ARRAY_ELEMENTS_PER_PAGE_LOG2); \
154 int subNdx = ndx & ((1 << DE_ARRAY_ELEMENTS_PER_PAGE_LOG2) - 1); \
155 return ((VALUETYPE*)arr->pageTable[pageNdx])[subNdx]; \
159 DE_INLINE void TYPENAME##_set (DE_PTR_TYPE(TYPENAME) arr, int ndx, VALUETYPE elem) \
161 DE_ASSERT(ndx >= 0 && ndx < arr->numElements); \
163 int pageNdx = (ndx >> DE_ARRAY_ELEMENTS_PER_PAGE_LOG2); \
164 int subNdx = ndx & ((1 << DE_ARRAY_ELEMENTS_PER_PAGE_LOG2) - 1); \
165 ((VALUETYPE*)arr->pageTable[pageNdx])[subNdx] = elem; \
169 DE_INLINE deBool TYPENAME##_pushBack (DE_PTR_TYPE(TYPENAME) arr, VALUETYPE elem) \
171 if ((arr->numElements + 1 >= arr->capacity) && !TYPENAME##_reserve(arr, arr->numElements + 1)) \
173 arr->numElements++; \
174 TYPENAME##_set(arr, arr->numElements - 1, elem); \
178 DE_INLINE VALUETYPE TYPENAME##_popBack (DE_PTR_TYPE(TYPENAME) arr) \
180 int ndx = arr->numElements - 1; \
181 int pageNdx = (ndx >> DE_ARRAY_ELEMENTS_PER_PAGE_LOG2); \
182 int subNdx = ndx & ((1 << DE_ARRAY_ELEMENTS_PER_PAGE_LOG2) - 1); \
183 DE_ASSERT(arr->numElements > 0); \
184 arr->numElements--; \
185 /* \note We access a value which is out-of-bounds, but we know it to be safe. */ \
186 return ((VALUETYPE*)arr->pageTable[pageNdx])[subNdx]; \
189 DE_INLINE deBool TYPENAME##_copy (DE_PTR_TYPE(TYPENAME) dst, const TYPENAME* src) \
191 DE_ASSERT(dst && src); \
193 int numElements = src->numElements; \
195 if (!TYPENAME##_setSize(dst, numElements)) \
197 for (ndx = 0; ndx < numElements; ndx++) \
198 TYPENAME##_set(dst, ndx, TYPENAME##_get(src, ndx)); \
203 DE_INLINE void TYPENAME##_swap (DE_PTR_TYPE(TYPENAME) arr, int aNdx, int bNdx) \
205 VALUETYPE tmp = TYPENAME##_get(arr, aNdx); \
206 TYPENAME##_set(arr, aNdx, TYPENAME##_get(arr, bNdx)); \
207 TYPENAME##_set(arr, bNdx, tmp); \
210 struct TYPENAME##Dummy_s { int dummy; }
212 /*--------------------------------------------------------------------*//*!
213 * \brief Declare a sort function for an array.
214 * \param TYPENAME Type name of the declared array.
215 * \param VALUETYPE Type of the value contained in the array.
216 * \param SORTNAME Name for this specific sort.
217 * \param CMPFUNC Comparison function for sorting.
219 * This macro declares a sort function for an array declared using
220 * DE_DECLARE_POOL_ARRAY macro.
222 * Sorting algorithm is heap sort since it requires constant amount of
223 * auxiliary space and is in-place sort. Worst-case run-time is O(n log n)
224 * and sort is NOT stable.
226 * CMPFUNC is used to compare elements in array. It must accept two
227 * parameters and return negative integer if first is smaller than, 0 if
228 * both are equal and positive integer if first is larger than second.
230 * The functions for sorting array are:
231 * \todo [petri] Figure out how to comment these in Doxygen-style.
234 * void Array_sortName (Array* array);
235 * void Array_sortNameHeapify (Array* array);
236 * void Array_sortNameShiftDown (Array* array, int start, int end);
238 *//*--------------------------------------------------------------------*/
239 #define DE_DECLARE_POOL_ARRAY_SORT(TYPENAME, VALUETYPE, SORTNAME, CMPFUNC) \
241 DE_INLINE void TYPENAME##_##SORTNAME##ShiftDown (DE_PTR_TYPE(TYPENAME) arr, int startNdx, int endNdx) \
243 int rootNdx = startNdx; \
245 while (rootNdx * 2 + 1 <= endNdx) \
247 int childNdx = rootNdx * 2 + 1; \
249 if ((childNdx + 1 <= endNdx) && (CMPFUNC(TYPENAME##_get(arr, childNdx), TYPENAME##_get(arr, childNdx + 1)) < 0)) \
252 if (CMPFUNC(TYPENAME##_get(arr, rootNdx), TYPENAME##_get(arr, childNdx)) < 0) \
254 TYPENAME##_swap(arr, rootNdx, childNdx); \
255 rootNdx = childNdx; \
262 DE_INLINE void TYPENAME##_##SORTNAME##Heapify (DE_PTR_TYPE(TYPENAME) arr) \
264 int startNdx = (TYPENAME##_getNumElements(arr) - 2) / 2; \
266 while (startNdx >= 0) \
268 TYPENAME##_##SORTNAME##ShiftDown(arr, startNdx, TYPENAME##_getNumElements(arr) - 1); \
273 DE_INLINE void TYPENAME##_##SORTNAME (DE_PTR_TYPE(TYPENAME) arr) \
275 int endNdx = TYPENAME##_getNumElements(arr) - 1; \
277 TYPENAME##_##SORTNAME##Heapify(arr); \
281 TYPENAME##_swap(arr, endNdx, 0); \
283 TYPENAME##_##SORTNAME##ShiftDown(arr, 0, endNdx); \
287 struct TYPENAME##SORTNAME##Dummy_s { int dummy; }
289 /* Basic array types. */
291 DE_DECLARE_POOL_ARRAY(deIntArray, int);
292 DE_DECLARE_POOL_ARRAY(deInt8Array, deInt8);
293 DE_DECLARE_POOL_ARRAY(deUint8Array, deUint8);
294 DE_DECLARE_POOL_ARRAY(deInt16Array, deInt16);
295 DE_DECLARE_POOL_ARRAY(deUint16Array, deUint16);
296 DE_DECLARE_POOL_ARRAY(deInt32Array, deInt32);
297 DE_DECLARE_POOL_ARRAY(deUint32Array, deUint32);
299 #endif /* _DEPOOLARRAY_H */