1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_vector.cpp
28 #include "vogl_core.h"
29 #include "vogl_vector.h"
30 #include "vogl_rand.h"
32 #include "vogl_color.h"
37 bool elemental_vector::increase_capacity(uint min_new_capacity, bool grow_hint, uint element_size, object_mover pMover, bool nofail)
39 VOGL_ASSERT(m_size <= m_capacity);
40 #ifdef VOGL_64BIT_POINTERS
41 VOGL_ASSERT(min_new_capacity < (0x400000000ULL / element_size));
43 VOGL_ASSERT(min_new_capacity < (0x7FFF0000U / element_size));
46 if (m_capacity >= min_new_capacity)
49 size_t new_capacity = min_new_capacity;
51 if ((grow_hint) && (!math::is_power_of_2(static_cast<uint64_t>(new_capacity))))
53 uint64_t new_capacity64 = math::next_pow2(static_cast<uint64_t>(new_capacity));
54 if (new_capacity64 != static_cast<size_t>(new_capacity64))
55 new_capacity64 = min_new_capacity;
56 new_capacity = static_cast<size_t>(new_capacity64);
59 VOGL_ASSERT(new_capacity && (new_capacity > m_capacity));
61 uint64_t desired_size64 = element_size * static_cast<uint64_t>(new_capacity);
62 if ((desired_size64 != static_cast<size_t>(desired_size64)) || (desired_size64 > VOGL_MAX_POSSIBLE_HEAP_BLOCK_SIZE))
68 sprintf(buf, "%s: Can't increase capacity to %" PRIu64 " items, %" PRIu64 " bytes", VOGL_METHOD_NAME, static_cast<uint64_t>(new_capacity), desired_size64);
72 const size_t desired_size = static_cast<size_t>(desired_size64);
77 void *new_p = vogl_realloc(m_p, desired_size, &actual_size);
84 sprintf(buf, "%s: vogl_realloc() failed allocating %u bytes", VOGL_METHOD_NAME, (uint)desired_size);
91 void *new_p = vogl_malloc(desired_size, &actual_size);
98 sprintf(buf, "%s: vogl_malloc() failed allocating %u bytes", VOGL_METHOD_NAME, (uint)desired_size);
102 (*pMover)(new_p, m_p, m_size);
110 if (actual_size > desired_size)
111 m_capacity = static_cast<uint>(actual_size / element_size);
113 m_capacity = static_cast<uint>(new_capacity);