]> git.cworth.org Git - vogl/blob - src/voglcore/vogl_vector.cpp
Initial vogl checkin
[vogl] / src / voglcore / vogl_vector.cpp
1 /**************************************************************************
2  *
3  * Copyright 2013-2014 RAD Game Tools and Valve Software
4  * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
5  * All Rights Reserved.
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27 // File: vogl_vector.cpp
28 #include "vogl_core.h"
29 #include "vogl_vector.h"
30 #include "vogl_rand.h"
31
32 #include "vogl_color.h"
33 #include "vogl_vec.h"
34
35 namespace vogl
36 {
37     bool elemental_vector::increase_capacity(uint min_new_capacity, bool grow_hint, uint element_size, object_mover pMover, bool nofail)
38     {
39         VOGL_ASSERT(m_size <= m_capacity);
40 #ifdef VOGL_64BIT_POINTERS
41         VOGL_ASSERT(min_new_capacity < (0x400000000ULL / element_size));
42 #else
43         VOGL_ASSERT(min_new_capacity < (0x7FFF0000U / element_size));
44 #endif
45
46         if (m_capacity >= min_new_capacity)
47             return true;
48
49         size_t new_capacity = min_new_capacity;
50
51         if ((grow_hint) && (!math::is_power_of_2(static_cast<uint64_t>(new_capacity))))
52         {
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);
57         }
58
59         VOGL_ASSERT(new_capacity && (new_capacity > m_capacity));
60
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))
63         {
64             if (nofail)
65                 return false;
66
67             char buf[256];
68             sprintf(buf, "%s: Can't increase capacity to %" PRIu64 " items, %" PRIu64 " bytes", VOGL_METHOD_NAME, static_cast<uint64_t>(new_capacity), desired_size64);
69             VOGL_FAIL(buf);
70         }
71
72         const size_t desired_size = static_cast<size_t>(desired_size64);
73
74         size_t actual_size;
75         if (!pMover)
76         {
77             void *new_p = vogl_realloc(m_p, desired_size, &actual_size);
78             if (!new_p)
79             {
80                 if (nofail)
81                     return false;
82
83                 char buf[256];
84                 sprintf(buf, "%s: vogl_realloc() failed allocating %u bytes", VOGL_METHOD_NAME, (uint)desired_size);
85                 VOGL_FAIL(buf);
86             }
87             m_p = new_p;
88         }
89         else
90         {
91             void *new_p = vogl_malloc(desired_size, &actual_size);
92             if (!new_p)
93             {
94                 if (nofail)
95                     return false;
96
97                 char buf[256];
98                 sprintf(buf, "%s: vogl_malloc() failed allocating %u bytes", VOGL_METHOD_NAME, (uint)desired_size);
99                 VOGL_FAIL(buf);
100             }
101
102             (*pMover)(new_p, m_p, m_size);
103
104             if (m_p)
105                 vogl_free(m_p);
106
107             m_p = new_p;
108         }
109
110         if (actual_size > desired_size)
111             m_capacity = static_cast<uint>(actual_size / element_size);
112         else
113             m_capacity = static_cast<uint>(new_capacity);
114
115         return true;
116     }
117
118 } // namespace vogl