VirtualBox

Changeset 32114 in vbox


Ignore:
Timestamp:
Aug 31, 2010 8:59:15 AM (14 years ago)
Author:
vboxsync
Message:

Main/linux: fixes to vector container code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/vector.h

    r31663 r32114  
    2828#define VECTOR_ELEMENT_SIZE sizeof(VECTOR_TYPE)
    2929/** The unit by which the vector capacity is increased */
    30 #define VECTOR_ALLOC_UNIT VECTOR_ELEMENT_SIZE * 16
     30#define VECTOR_ALLOC_UNIT 16
    3131
    3232#ifndef VECTOR_TYPE
     
    6565} VECTOR_TYPENAME;
    6666
     67/** Non-constant iterator over the vector */
     68typedef VECTOR_TYPE *VECTOR_PUBLIC(iterator);
     69
    6770/*** Private methods ***/
    6871
     
    7881    size_t cNewCap = pSelf->mCapacity - pSelf->mBegin + VECTOR_ALLOC_UNIT;
    7982    size_t cOffEnd = pSelf->mEnd - pSelf->mBegin;
    80     void *pRealloc = VECTOR_ALLOCATOR(pSelf->mBegin, cNewCap);
     83    void *pRealloc = VECTOR_ALLOCATOR(pSelf->mBegin,
     84                                      cNewCap * VECTOR_ELEMENT_SIZE);
    8185    if (!pRealloc)
    8286        return 0;
    8387    pSelf->mBegin = (VECTOR_TYPE *)pRealloc;
    8488    pSelf->mEnd = pSelf->mBegin + cOffEnd;
    85     pSelf->mCapacity = pSelf->mBegin + cNewCap / VECTOR_ELEMENT_SIZE;
     89    pSelf->mCapacity = pSelf->mBegin + cNewCap;
    8690    memset(pSelf->mEnd, 0, pSelf->mCapacity - pSelf->mEnd);
    8791    return 1;
     
    144148}
    145149
    146 /*** Iterators ***/
    147 
    148 /** Non-constant iterator over the vector */
    149 typedef VECTOR_TYPE *VECTOR_PUBLIC(iterator);
     150/** Get the special "begin" iterator for a vector */
     151static inline const VECTOR_PUBLIC(iterator) *VECTOR_PUBLIC(begin)
     152                                                 (VECTOR_TYPENAME *pSelf)
     153{
     154    return &pSelf->mBegin;
     155}
     156
     157/** Get the special "end" iterator for a vector */
     158static inline const VECTOR_PUBLIC(iterator) *VECTOR_PUBLIC(end)
     159                                                 (VECTOR_TYPENAME *pSelf)
     160{
     161    return &pSelf->mEnd;
     162}
     163
     164/** A structure with pointers to class operations to save too much use of long
     165 * identifiers in source code.  Use like:
     166 *   <vector_type>_op_table *pOps = &<vector_type>_ops;
     167 * and then
     168 *   pOps->init(&my_vector);
     169 */
     170typedef const struct VECTOR_INTERNAL(op_table)
     171{
     172    int    (*init)      (VECTOR_TYPENAME *pSelf);
     173    void   (*cleanup)   (VECTOR_TYPENAME *pSelf);
     174    int    (*push_back) (VECTOR_TYPENAME *pSelf, VECTOR_TYPE *pElement);
     175    void   (*clear)     (VECTOR_TYPENAME *pSelf);
     176    size_t (*size)      (VECTOR_TYPENAME *pSelf);
     177    const VECTOR_PUBLIC(iterator) *
     178           (*begin)     (VECTOR_TYPENAME *pSelf);
     179    const VECTOR_PUBLIC(iterator) *
     180           (*end)       (VECTOR_TYPENAME *pSelf);
     181} VECTOR_PUBLIC(op_table);
     182
     183static VECTOR_PUBLIC(op_table) VECTOR_PUBLIC(ops) =
     184{
     185    VECTOR_PUBLIC(init),
     186    VECTOR_PUBLIC(cleanup),
     187    VECTOR_PUBLIC(push_back),
     188    VECTOR_PUBLIC(clear),
     189    VECTOR_PUBLIC(size),
     190    VECTOR_PUBLIC(begin),
     191    VECTOR_PUBLIC(end)
     192};
     193
     194/*** Iterator methods ***/
    150195
    151196/** Initialise a newly allocated iterator */
     
    166211{
    167212    ++*pSelf;
    168 }
    169 
    170 /** Get the special "begin" iterator for a vector */
    171 static inline const VECTOR_PUBLIC(iterator) *VECTOR_PUBLIC(begin)
    172                                                  (VECTOR_TYPENAME *pSelf)
    173 {
    174     return &pSelf->mBegin;
    175 }
    176 
    177 /** Get the special "end" iterator for a vector */
    178 static inline const VECTOR_PUBLIC(iterator) *VECTOR_PUBLIC(end)
    179                                                  (VECTOR_TYPENAME *pSelf)
    180 {
    181     return &pSelf->mEnd;
    182213}
    183214
     
    196227    return *pFirst == *pSecond;
    197228}
     229
     230/** A structure with pointers to iterator operations to save too much use of
     231 * long identifiers in source code.  Use like:
     232 *   <vector_type>_iter_op_table *pOps = &<vector_type>_iter_ops;
     233 * and then
     234 *   pOps->init(&my_iter, &other_iter);
     235 */
     236typedef const struct VECTOR_INTERNAL(iter_op_table)
     237{
     238    void         (*init)    (VECTOR_PUBLIC(iterator) *pSelf,
     239                             const VECTOR_PUBLIC(iterator) *pOther);
     240    VECTOR_TYPE *(*target)  (VECTOR_PUBLIC(iterator) *pSelf);
     241    void         (*incr)    (VECTOR_PUBLIC(iterator) *pSelf);
     242    int          (*lt) (const VECTOR_PUBLIC(iterator) *pFirst,
     243                             const VECTOR_PUBLIC(iterator) *pSecond);
     244    int          (*eq) (const VECTOR_PUBLIC(iterator) *pFirst,
     245                             const VECTOR_PUBLIC(iterator) *pSecond);
     246} VECTOR_PUBLIC(iter_op_table);
     247
     248static VECTOR_PUBLIC(iter_op_table) VECTOR_PUBLIC(iter_ops) =
     249{
     250    VECTOR_PUBLIC(iter_init),
     251    VECTOR_PUBLIC(iter_target),
     252    VECTOR_PUBLIC(iter_incr),
     253    VECTOR_PUBLIC(iter_lt),
     254    VECTOR_PUBLIC(iter_eq)
     255};
    198256
    199257/* We need to undefine anything we have defined (and for convenience we also
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette