BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file Vector_impl.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include "Vector_header.h"
31  
32 static int Vector_Init (Vector *o, size_t capacity)
33 {
34 if (capacity == 0) {
35 o->elems = NULL;
36 } else {
37 o->elems = BAllocArray(capacity, sizeof(VectorElem));
38 if (!o->elems) {
39 return 0;
40 }
41 }
42 o->capacity = capacity;
43 o->count = 0;
44 return 1;
45 }
46  
47 static void Vector_Free (Vector *o)
48 {
49 BFree(o->elems);
50 }
51  
52 static size_t Vector_Count (Vector *o)
53 {
54 return o->count;
55 }
56  
57 static VectorElem * Vector_Get (Vector *o, size_t index)
58 {
59 ASSERT(index < o->capacity)
60  
61 return &o->elems[index];
62 }
63  
64 static int Vector_Reserve (Vector *o, size_t capacity)
65 {
66 if (capacity > o->capacity) {
67 size_t new_capacity = o->capacity;
68 do {
69 if (new_capacity > SIZE_MAX / 2) {
70 return 0;
71 }
72 new_capacity = (new_capacity == 0) ? 1 : (2 * new_capacity);
73 } while (capacity > new_capacity);
74  
75 VectorElem *new_elems = BAllocArray(new_capacity, sizeof(VectorElem));
76 if (!new_elems) {
77 return 0;
78 }
79  
80 if (o->count > 0) {
81 memcpy(new_elems, o->elems, o->count * sizeof(VectorElem));
82 }
83  
84 BFree(o->elems);
85  
86 o->elems = new_elems;
87 o->capacity = new_capacity;
88 }
89  
90 return 1;
91 }
92  
93 static VectorElem * Vector_Push (Vector *o, size_t *out_index)
94 {
95 if (o->count == SIZE_MAX || !Vector_Reserve(o, o->count + 1)) {
96 return NULL;
97 }
98 if (out_index) {
99 *out_index = o->count;
100 }
101 VectorElem *ptr = &o->elems[o->count];
102 o->count++;
103 return ptr;
104 }
105  
106 static VectorElem * Vector_Pop (Vector *o, size_t *out_index)
107 {
108 ASSERT(o->count > 0)
109  
110 o->count--;
111 if (out_index) {
112 *out_index = o->count;
113 }
114 return &o->elems[o->count];
115 }
116  
117 #include "Vector_footer.h"