[31651] | 1 | /** @file
|
---|
[57004] | 2 | * IPRT - Vector - STL-inspired vector implementation in C.
|
---|
[31651] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[76553] | 6 | * Copyright (C) 2011-2019 Oracle Corporation
|
---|
[31651] | 7 | *
|
---|
| 8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 11 | * General Public License (GPL) as published by the Free Software
|
---|
| 12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
[37829] | 15 | *
|
---|
| 16 | * The contents of this file may alternatively be used under the terms
|
---|
| 17 | * of the Common Development and Distribution License Version 1.0
|
---|
| 18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
| 19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
| 20 | * CDDL are applicable instead of those of the GPL.
|
---|
| 21 | *
|
---|
| 22 | * You may elect to license modified versions of this file under the
|
---|
| 23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[31651] | 24 | */
|
---|
| 25 |
|
---|
[36805] | 26 | /**
|
---|
| 27 | * @todo the right Doxygen tag here
|
---|
| 28 | * This file defines a set of macros which provide a functionality and an
|
---|
| 29 | * interface roughly similar to the C++ STL vector container. To create a
|
---|
| 30 | * vector of a particular type one must first explicitly instantiate such a
|
---|
| 31 | * vector in the source file, e.g.
|
---|
| 32 | * RTVEC_DECL(TopLevels, Window *)
|
---|
| 33 | * without a semi-colon. This macro will define a structure (struct TopLevels)
|
---|
[37423] | 34 | * which contains a dynamically resizeable array of Window * elements. It
|
---|
[36805] | 35 | * will also define a number of inline methods for manipulating the structure,
|
---|
| 36 | * such as
|
---|
| 37 | * Window *TopLevelsPushBack(struct TopLevels *)
|
---|
| 38 | * which adds a new element to the end of the array and returns it, optionally
|
---|
| 39 | * reallocating the array if there is not enough space for the new element.
|
---|
[37423] | 40 | * (This particular method prototype differs from the STL equivalent -
|
---|
[36805] | 41 | * push_back - more than most of the other methods).
|
---|
[37423] | 42 | *
|
---|
[36805] | 43 | * To create a vector, one simply needs to declare the structure, in this case
|
---|
| 44 | * struct TopLevels = RTVEC_INITIALIZER;
|
---|
| 45 | *
|
---|
| 46 | * There are various other macros for declaring vectors with different
|
---|
| 47 | * allocators (e.g. RTVEC_DECL_ALLOCATOR) or with clean-up functions
|
---|
| 48 | * (e.g. RTVEC_DECL_DELETE). See the descriptions of the generic methods and
|
---|
| 49 | * the declarator macros below.
|
---|
| 50 | *
|
---|
| 51 | * One particular use of vectors is to assemble an array of a particular type
|
---|
| 52 | * in heap memory without knowing - or counting - the number of elements in
|
---|
| 53 | * advance. To do this, add the elements onto the array using PushBack, then
|
---|
| 54 | * extract the array from the vector using the (non-STL) Detach method.
|
---|
| 55 | *
|
---|
| 56 | * @note functions in this file are inline to prevent warnings about
|
---|
| 57 | * unused static functions. I assume that in this day and age a
|
---|
| 58 | * compiler makes its own decisions about whether to actually
|
---|
| 59 | * inline a function.
|
---|
[37423] | 60 | * @note since vector structures must be explicitly instanciated unlike the
|
---|
[36805] | 61 | * C++ vector template, care must be taken not to instanciate a
|
---|
| 62 | * particular type twice, e.g. once in a header and once in a code file.
|
---|
| 63 | * Only using vectors in code files and keeping them out of interfaces
|
---|
| 64 | * (or passing them as anonymously) makes it easier to take care of this.
|
---|
| 65 | */
|
---|
[57004] | 66 |
|
---|
[76557] | 67 | #ifndef IPRT_INCLUDED_vector_h
|
---|
| 68 | #define IPRT_INCLUDED_vector_h
|
---|
[76507] | 69 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 70 | # pragma once
|
---|
| 71 | #endif
|
---|
[32258] | 72 |
|
---|
[32324] | 73 | /*******************************************************************************
|
---|
| 74 | * Header Files *
|
---|
| 75 | *******************************************************************************/
|
---|
[32258] | 76 |
|
---|
[36805] | 77 | #include <iprt/assert.h>
|
---|
| 78 | #include <iprt/cdefs.h>
|
---|
[76417] | 79 | #include <iprt/errcore.h>
|
---|
[36805] | 80 | #include <iprt/mem.h> /** @todo Should the caller include this if they need
|
---|
| 81 | * it? */
|
---|
[32324] | 82 |
|
---|
| 83 |
|
---|
[36805] | 84 | /**
|
---|
| 85 | * Generic vector structure
|
---|
| 86 | */
|
---|
| 87 | /** @todo perhaps we should include an additional member for a parameter to
|
---|
| 88 | * three-argument reallocators, so that we can support e.g. mempools? */
|
---|
| 89 | #define RTVEC_DECL_STRUCT(name, type) \
|
---|
| 90 | struct name \
|
---|
| 91 | { \
|
---|
| 92 | /** The number of elements in the vector */ \
|
---|
| 93 | size_t mcElements; \
|
---|
| 94 | /** The current capacity of the vector */ \
|
---|
| 95 | size_t mcCapacity; \
|
---|
| 96 | /** The elements themselves */ \
|
---|
| 97 | type *mpElements; \
|
---|
| 98 | };
|
---|
[31651] | 99 |
|
---|
[36805] | 100 | /** Initialiser for an empty vector structure */
|
---|
| 101 | #define RTVEC_INITIALIZER { 0, 0, NULL }
|
---|
[31651] | 102 |
|
---|
[36805] | 103 | /** The unit by which the vector capacity is increased */
|
---|
| 104 | #define RTVECIMPL_ALLOC_UNIT 16
|
---|
[31651] | 105 |
|
---|
[36805] | 106 | /**
|
---|
| 107 | * Generic method - get the size of a vector
|
---|
| 108 | */
|
---|
| 109 | /** @todo What is the correct way to do doxygen for this sort of macro? */
|
---|
| 110 | #define RTVEC_DECLFN_SIZE(name, type) \
|
---|
| 111 | DECLINLINE(size_t) name ## Size(struct name *pVec) \
|
---|
| 112 | { \
|
---|
| 113 | return(pVec->mcElements); \
|
---|
[32258] | 114 | }
|
---|
[31651] | 115 |
|
---|
[36805] | 116 | /**
|
---|
| 117 | * Generic method - expand a vector
|
---|
| 118 | */
|
---|
| 119 | #define RTVEC_DECLFN_RESERVE(name, type, pfnRealloc) \
|
---|
| 120 | DECLINLINE(int) name ## Reserve(struct name *pVec, size_t cNewCapacity) \
|
---|
| 121 | { \
|
---|
| 122 | void *pvNew; \
|
---|
| 123 | \
|
---|
| 124 | if (cNewCapacity <= pVec->mcCapacity) \
|
---|
| 125 | return VINF_SUCCESS; \
|
---|
| 126 | pvNew = pfnRealloc(pVec->mpElements, cNewCapacity * sizeof(type)); \
|
---|
| 127 | if (!pvNew) \
|
---|
| 128 | return VERR_NO_MEMORY; \
|
---|
| 129 | pVec->mcCapacity = cNewCapacity; \
|
---|
| 130 | pVec->mpElements = (type *)pvNew; \
|
---|
| 131 | return VINF_SUCCESS; \
|
---|
[31651] | 132 | }
|
---|
| 133 |
|
---|
[36805] | 134 | /**
|
---|
| 135 | * Generic method - return a pointer to the first element in the vector.
|
---|
| 136 | */
|
---|
| 137 | #define RTVEC_DECLFN_BEGIN(name, type) \
|
---|
| 138 | DECLINLINE(type *) name ## Begin(struct name *pVec) \
|
---|
| 139 | { \
|
---|
| 140 | return(pVec->mpElements); \
|
---|
[31651] | 141 | }
|
---|
| 142 |
|
---|
[37423] | 143 | /**
|
---|
[36805] | 144 | * Generic method - return a pointer to one past the last element in the
|
---|
| 145 | * vector.
|
---|
| 146 | */
|
---|
| 147 | #define RTVEC_DECLFN_END(name, type) \
|
---|
| 148 | DECLINLINE(type *) name ## End(struct name *pVec) \
|
---|
| 149 | { \
|
---|
| 150 | return(&pVec->mpElements[pVec->mcElements]); \
|
---|
[31651] | 151 | }
|
---|
| 152 |
|
---|
[36805] | 153 | /**
|
---|
| 154 | * Generic method - add a new, uninitialised element onto a vector and return
|
---|
| 155 | * it.
|
---|
| 156 | * @note this method differs from the STL equivalent by letting the caller
|
---|
| 157 | * post-initialise the new element rather than copying it from its
|
---|
| 158 | * argument.
|
---|
| 159 | */
|
---|
| 160 | #define RTVEC_DECLFN_PUSHBACK(name, type) \
|
---|
| 161 | DECLINLINE(type *) name ## PushBack(struct name *pVec) \
|
---|
| 162 | { \
|
---|
| 163 | Assert(pVec->mcElements <= pVec->mcCapacity); \
|
---|
| 164 | if ( pVec->mcElements == pVec->mcCapacity \
|
---|
| 165 | && RT_FAILURE(name ## Reserve(pVec, pVec->mcCapacity \
|
---|
| 166 | + RTVECIMPL_ALLOC_UNIT))) \
|
---|
| 167 | return NULL; \
|
---|
| 168 | ++pVec->mcElements; \
|
---|
| 169 | return &pVec->mpElements[pVec->mcElements - 1]; \
|
---|
[31651] | 170 | }
|
---|
| 171 |
|
---|
[36805] | 172 | /**
|
---|
| 173 | * Generic method - drop the last element from the vector.
|
---|
| 174 | */
|
---|
| 175 | #define RTVEC_DECLFN_POPBACK(name) \
|
---|
| 176 | DECLINLINE(void) name ## PopBack(struct name *pVec) \
|
---|
| 177 | { \
|
---|
| 178 | Assert(pVec->mcElements <= pVec->mcCapacity); \
|
---|
| 179 | --pVec->mcElements; \
|
---|
[31651] | 180 | }
|
---|
| 181 |
|
---|
[36805] | 182 | /**
|
---|
| 183 | * Generic method - drop the last element from the vector, calling a clean-up
|
---|
| 184 | * method first.
|
---|
| 185 | *
|
---|
| 186 | * By taking an adapter function for the element to be dropped as an
|
---|
| 187 | * additional macro parameter we can support clean-up by pointer
|
---|
| 188 | * (pfnAdapter maps T* -> T*) or by value (maps T* -> T). pfnAdapter takes
|
---|
| 189 | * one argument of type @a type * and must return whatever type pfnDelete
|
---|
| 190 | * expects.
|
---|
| 191 | */
|
---|
| 192 | /** @todo find a better name for pfnAdapter? */
|
---|
| 193 | #define RTVEC_DECLFN_POPBACK_DELETE(name, type, pfnDelete, pfnAdapter) \
|
---|
| 194 | DECLINLINE(void) name ## PopBack(struct name *pVec) \
|
---|
| 195 | { \
|
---|
| 196 | Assert(pVec->mcElements <= pVec->mcCapacity); \
|
---|
| 197 | --pVec->mcElements; \
|
---|
| 198 | pfnDelete(pfnAdapter(&pVec->mpElements[pVec->mcElements])); \
|
---|
[31651] | 199 | }
|
---|
| 200 |
|
---|
[37423] | 201 | /**
|
---|
[36805] | 202 | * Generic method - reset a vector to empty.
|
---|
| 203 | * @note This function does not free any memory
|
---|
| 204 | */
|
---|
| 205 | #define RTVEC_DECLFN_CLEAR(name) \
|
---|
| 206 | DECLINLINE(void) name ## Clear(struct name *pVec) \
|
---|
| 207 | { \
|
---|
| 208 | Assert(pVec->mcElements <= pVec->mcCapacity); \
|
---|
| 209 | pVec->mcElements = 0; \
|
---|
[31651] | 210 | }
|
---|
| 211 |
|
---|
[36805] | 212 | /**
|
---|
| 213 | * Generic method - reset a vector to empty, calling a clean-up method on each
|
---|
| 214 | * element first.
|
---|
| 215 | * @note See @a RTVEC_DECLFN_POPBACK_DELETE for an explanation of pfnAdapter
|
---|
| 216 | * @note This function does not free any memory
|
---|
| 217 | * @note The cleanup function is currently called on the elements from first
|
---|
| 218 | * to last. The testcase expects this.
|
---|
| 219 | */
|
---|
| 220 | #define RTVEC_DECLFN_CLEAR_DELETE(name, pfnDelete, pfnAdapter) \
|
---|
| 221 | DECLINLINE(void) name ## Clear(struct name *pVec) \
|
---|
| 222 | { \
|
---|
| 223 | size_t i; \
|
---|
| 224 | \
|
---|
| 225 | Assert(pVec->mcElements <= pVec->mcCapacity); \
|
---|
| 226 | for (i = 0; i < pVec->mcElements; ++i) \
|
---|
| 227 | pfnDelete(pfnAdapter(&pVec->mpElements[i])); \
|
---|
| 228 | pVec->mcElements = 0; \
|
---|
[31651] | 229 | }
|
---|
| 230 |
|
---|
[37423] | 231 | /**
|
---|
[36805] | 232 | * Generic method - detach the array contained inside a vector and reset the
|
---|
| 233 | * vector to empty.
|
---|
| 234 | * @note This function does not free any memory
|
---|
| 235 | */
|
---|
| 236 | #define RTVEC_DECLFN_DETACH(name, type) \
|
---|
| 237 | DECLINLINE(type *) name ## Detach(struct name *pVec) \
|
---|
| 238 | { \
|
---|
| 239 | type *pArray = pVec->mpElements; \
|
---|
| 240 | \
|
---|
| 241 | Assert(pVec->mcElements <= pVec->mcCapacity); \
|
---|
| 242 | pVec->mcElements = 0; \
|
---|
| 243 | pVec->mpElements = NULL; \
|
---|
| 244 | pVec->mcCapacity = 0; \
|
---|
| 245 | return pArray; \
|
---|
[32114] | 246 | }
|
---|
[31651] | 247 |
|
---|
[36805] | 248 | /** Common declarations for all vector types */
|
---|
| 249 | #define RTVEC_DECL_COMMON(name, type, pfnRealloc) \
|
---|
| 250 | RTVEC_DECL_STRUCT(name, type) \
|
---|
| 251 | RTVEC_DECLFN_SIZE(name, type) \
|
---|
| 252 | RTVEC_DECLFN_RESERVE(name, type, pfnRealloc) \
|
---|
| 253 | RTVEC_DECLFN_BEGIN(name, type) \
|
---|
| 254 | RTVEC_DECLFN_END(name, type) \
|
---|
| 255 | RTVEC_DECLFN_PUSHBACK(name, type) \
|
---|
| 256 | RTVEC_DECLFN_DETACH(name, type)
|
---|
[31651] | 257 |
|
---|
[32258] | 258 | /**
|
---|
[36805] | 259 | * Declarator macro - declare a vector type
|
---|
| 260 | * @param name the name of the C struct type describing the vector as
|
---|
| 261 | * well as the prefix of the functions for manipulating it
|
---|
| 262 | * @param type the type of the objects contained in the vector
|
---|
| 263 | * @param pfnRealloc the memory reallocation function used for expanding the
|
---|
| 264 | * vector
|
---|
[32114] | 265 | */
|
---|
[36805] | 266 | #define RTVEC_DECL_ALLOCATOR(name, type, pfnRealloc) \
|
---|
| 267 | RTVEC_DECL_COMMON(name, type, pfnRealloc) \
|
---|
| 268 | RTVEC_DECLFN_POPBACK(name) \
|
---|
| 269 | RTVEC_DECLFN_CLEAR(name)
|
---|
[32114] | 270 |
|
---|
[32258] | 271 | /**
|
---|
[36805] | 272 | * Generic method - inline id mapping delete adapter function - see the
|
---|
| 273 | * explanation of pfnAdapter in @a RTVEC_DECLFN_POPBACK_DELETE.
|
---|
[32258] | 274 | */
|
---|
[36805] | 275 | #define RTVEC_DECLFN_DELETE_ADAPTER_ID(name, type) \
|
---|
| 276 | DECLINLINE(type *) name ## DeleteAdapterId(type *arg) \
|
---|
| 277 | { \
|
---|
| 278 | return arg; \
|
---|
| 279 | }
|
---|
[32114] | 280 |
|
---|
[32258] | 281 | /**
|
---|
[36805] | 282 | * Generic method - inline pointer-to-value mapping delete adapter function -
|
---|
| 283 | * see the explanation of pfnAdapter in @a RTVEC_DECLFN_POPBACK_DELETE.
|
---|
[32258] | 284 | */
|
---|
[36805] | 285 | #define RTVEC_DECLFN_DELETE_ADAPTER_TO_VALUE(name, type) \
|
---|
| 286 | DECLINLINE(type) name ## DeleteAdapterToValue(type *arg) \
|
---|
| 287 | { \
|
---|
| 288 | return *arg; \
|
---|
| 289 | }
|
---|
[32114] | 290 |
|
---|
[32258] | 291 | /**
|
---|
[36805] | 292 | * Declarator macro - declare a vector type with a cleanup callback to be used
|
---|
| 293 | * when elements are dropped from the vector. The callback takes a pointer to
|
---|
| 294 | * @a type,
|
---|
| 295 | * NOT a value of type @a type.
|
---|
| 296 | * @param name the name of the C struct type describing the vector as
|
---|
| 297 | * well as the prefix of the functions for manipulating it
|
---|
| 298 | * @param type the type of the objects contained in the vector
|
---|
| 299 | * @param pfnRealloc the memory reallocation function used for expanding the
|
---|
| 300 | * vector
|
---|
| 301 | * @param pfnDelete the cleanup callback function - signature
|
---|
| 302 | * void pfnDelete(type *)
|
---|
[32258] | 303 | */
|
---|
[36805] | 304 | #define RTVEC_DECL_ALLOCATOR_DELETE(name, type, pfnRealloc, pfnDelete) \
|
---|
| 305 | RTVEC_DECL_COMMON(name, type, pfnRealloc) \
|
---|
| 306 | RTVEC_DECLFN_DELETE_ADAPTER_ID(name, type) \
|
---|
| 307 | RTVEC_DECLFN_POPBACK_DELETE(name, type, pfnDelete, \
|
---|
| 308 | name ## DeleteAdapterId) \
|
---|
| 309 | RTVEC_DECLFN_CLEAR_DELETE(name, pfnDelete, name ## DeleteAdapterId)
|
---|
[31651] | 310 |
|
---|
[32258] | 311 | /**
|
---|
[36805] | 312 | * Declarator macro - declare a vector type with a cleanup callback to be used
|
---|
| 313 | * when elements are dropped from the vector. The callback takes a parameter
|
---|
| 314 | * of type @a type, NOT a pointer to @a type.
|
---|
| 315 | * @param name the name of the C struct type describing the vector as
|
---|
| 316 | * well as the prefix of the functions for manipulating it
|
---|
| 317 | * @param type the type of the objects contained in the vector
|
---|
| 318 | * @param pfnRealloc the memory reallocation function used for expanding the
|
---|
| 319 | * vector
|
---|
| 320 | * @param pfnDelete the cleanup callback function - signature
|
---|
| 321 | * void pfnDelete(type)
|
---|
[32258] | 322 | */
|
---|
[36805] | 323 | #define RTVEC_DECL_ALLOCATOR_DELETE_BY_VALUE(name, type, pfnRealloc, \
|
---|
| 324 | pfnDelete) \
|
---|
| 325 | RTVEC_DECL_COMMON(name, type, pfnRealloc) \
|
---|
| 326 | RTVEC_DECLFN_DELETE_ADAPTER_TO_VALUE(name, type) \
|
---|
| 327 | RTVEC_DECLFN_POPBACK_DELETE(name, type, pfnDelete, \
|
---|
| 328 | name ## DeleteAdapterToValue) \
|
---|
| 329 | RTVEC_DECLFN_CLEAR_DELETE(name, pfnDelete, \
|
---|
| 330 | name ## DeleteAdapterToValue)
|
---|
[31651] | 331 |
|
---|
[32258] | 332 | /**
|
---|
[36805] | 333 | * Inline wrapper around RTMemRealloc macro to get a function usable as a
|
---|
| 334 | * callback.
|
---|
[32258] | 335 | */
|
---|
[36805] | 336 | DECLINLINE(void *) rtvecReallocDefTag(void *pv, size_t cbNew)
|
---|
| 337 | {
|
---|
| 338 | return RTMemRealloc(pv, cbNew);
|
---|
| 339 | }
|
---|
[31651] | 340 |
|
---|
[32258] | 341 | /**
|
---|
[36805] | 342 | * Declarator macro - declare a vector type (see @a RTVEC_DECL_ALLOCATOR)
|
---|
| 343 | * using RTMemRealloc as a memory allocator
|
---|
| 344 | * @param name the name of the C struct type describing the vector as
|
---|
| 345 | * well as the prefix of the functions for manipulating it
|
---|
| 346 | * @param type the type of the objects contained in the vector
|
---|
[32258] | 347 | */
|
---|
[36805] | 348 | #define RTVEC_DECL(name, type) \
|
---|
| 349 | RTVEC_DECL_ALLOCATOR(name, type, rtvecReallocDefTag)
|
---|
[31651] | 350 |
|
---|
[32258] | 351 | /**
|
---|
[36805] | 352 | * Declarator macro - declare a vector type with a cleanup by pointer callback
|
---|
| 353 | * (see @a RTVEC_DECL_ALLOCATOR_DELETE) using RTMemRealloc as a memory
|
---|
| 354 | * allocator
|
---|
| 355 | * @param name the name of the C struct type describing the vector as
|
---|
| 356 | * well as the prefix of the functions for manipulating it
|
---|
| 357 | * @param type the type of the objects contained in the vector
|
---|
| 358 | * @param pfnDelete the cleanup callback function - signature
|
---|
| 359 | * void pfnDelete(type *)
|
---|
[32258] | 360 | */
|
---|
[36805] | 361 | #define RTVEC_DECL_DELETE(name, type, pfnDelete) \
|
---|
| 362 | RTVEC_DECL_ALLOCATOR_DELETE(name, type, rtvecReallocDefTag, pfnDelete)
|
---|
[31651] | 363 |
|
---|
[32258] | 364 | /**
|
---|
[36805] | 365 | * Declarator macro - declare a vector type with a cleanup by value callback
|
---|
| 366 | * (see @a RTVEC_DECL_ALLOCATOR_DELETE_BY_VALUE) using RTMemRealloc as a memory
|
---|
| 367 | * allocator
|
---|
| 368 | * @param name the name of the C struct type describing the vector as
|
---|
| 369 | * well as the prefix of the functions for manipulating it
|
---|
| 370 | * @param type the type of the objects contained in the vector
|
---|
| 371 | * @param pfnDelete the cleanup callback function - signature
|
---|
| 372 | * void pfnDelete(type)
|
---|
[32114] | 373 | */
|
---|
[36805] | 374 | #define RTVEC_DECL_DELETE_BY_VALUE(name, type, pfnDelete) \
|
---|
| 375 | RTVEC_DECL_ALLOCATOR_DELETE_BY_VALUE(name, type, rtvecReallocDefTag, \
|
---|
| 376 | pfnDelete)
|
---|
[32114] | 377 |
|
---|
[76585] | 378 | #endif /* !IPRT_INCLUDED_vector_h */
|
---|
[57004] | 379 |
|
---|