1 /*
2 ******************************************************************************
3 * Copyright (C) 1999-2015, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ******************************************************************************
6 */
7 
8 #include "uvectr64.h"
9 #include "cmemory.h"
10 #include "putilimp.h"
11 
12 U_NAMESPACE_BEGIN
13 
14 #define DEFAULT_CAPACITY 8
15 
16 /*
17  * Constants for hinting whether a key is an integer
18  * or a pointer.  If a hint bit is zero, then the associated
19  * token is assumed to be an integer. This is needed for iSeries
20  */
21 
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector64)22 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector64)
23 
24 UVector64::UVector64(UErrorCode &status) :
25     count(0),
26     capacity(0),
27     maxCapacity(0),
28     elements(NULL)
29 {
30     _init(DEFAULT_CAPACITY, status);
31 }
32 
UVector64(int32_t initialCapacity,UErrorCode & status)33 UVector64::UVector64(int32_t initialCapacity, UErrorCode &status) :
34     count(0),
35     capacity(0),
36     maxCapacity(0),
37     elements(0)
38 {
39     _init(initialCapacity, status);
40 }
41 
42 
43 
_init(int32_t initialCapacity,UErrorCode & status)44 void UVector64::_init(int32_t initialCapacity, UErrorCode &status) {
45     // Fix bogus initialCapacity values; avoid malloc(0)
46     if (initialCapacity < 1) {
47         initialCapacity = DEFAULT_CAPACITY;
48     }
49     if (maxCapacity>0 && maxCapacity<initialCapacity) {
50         initialCapacity = maxCapacity;
51     }
52     if (initialCapacity > (int32_t)(INT32_MAX / sizeof(int64_t))) {
53         initialCapacity = uprv_min(DEFAULT_CAPACITY, maxCapacity);
54     }
55     elements = (int64_t *)uprv_malloc(sizeof(int64_t)*initialCapacity);
56     if (elements == 0) {
57         status = U_MEMORY_ALLOCATION_ERROR;
58     } else {
59         capacity = initialCapacity;
60     }
61 }
62 
~UVector64()63 UVector64::~UVector64() {
64     uprv_free(elements);
65     elements = 0;
66 }
67 
68 /**
69  * Assign this object to another (make this a copy of 'other').
70  */
assign(const UVector64 & other,UErrorCode & ec)71 void UVector64::assign(const UVector64& other, UErrorCode &ec) {
72     if (ensureCapacity(other.count, ec)) {
73         setSize(other.count);
74         for (int32_t i=0; i<other.count; ++i) {
75             elements[i] = other.elements[i];
76         }
77     }
78 }
79 
80 
operator ==(const UVector64 & other)81 UBool UVector64::operator==(const UVector64& other) {
82     int32_t i;
83     if (count != other.count) return FALSE;
84     for (i=0; i<count; ++i) {
85         if (elements[i] != other.elements[i]) {
86             return FALSE;
87         }
88     }
89     return TRUE;
90 }
91 
92 
setElementAt(int64_t elem,int32_t index)93 void UVector64::setElementAt(int64_t elem, int32_t index) {
94     if (0 <= index && index < count) {
95         elements[index] = elem;
96     }
97     /* else index out of range */
98 }
99 
insertElementAt(int64_t elem,int32_t index,UErrorCode & status)100 void UVector64::insertElementAt(int64_t elem, int32_t index, UErrorCode &status) {
101     // must have 0 <= index <= count
102     if (0 <= index && index <= count && ensureCapacity(count + 1, status)) {
103         for (int32_t i=count; i>index; --i) {
104             elements[i] = elements[i-1];
105         }
106         elements[index] = elem;
107         ++count;
108     }
109     /* else index out of range */
110 }
111 
removeAllElements(void)112 void UVector64::removeAllElements(void) {
113     count = 0;
114 }
115 
expandCapacity(int32_t minimumCapacity,UErrorCode & status)116 UBool UVector64::expandCapacity(int32_t minimumCapacity, UErrorCode &status) {
117     if (U_FAILURE(status)) {
118         return FALSE;
119     }
120     if (minimumCapacity < 0) {
121         status = U_ILLEGAL_ARGUMENT_ERROR;
122         return FALSE;
123     }
124     if (capacity >= minimumCapacity) {
125         return TRUE;
126     }
127     if (maxCapacity>0 && minimumCapacity>maxCapacity) {
128         status = U_BUFFER_OVERFLOW_ERROR;
129         return FALSE;
130     }
131     if (capacity > (INT32_MAX - 1) / 2) {  // integer overflow check
132         status = U_ILLEGAL_ARGUMENT_ERROR;
133         return FALSE;
134     }
135     int32_t newCap = capacity * 2;
136     if (newCap < minimumCapacity) {
137         newCap = minimumCapacity;
138     }
139     if (maxCapacity > 0 && newCap > maxCapacity) {
140         newCap = maxCapacity;
141     }
142     if (newCap > (int32_t)(INT32_MAX / sizeof(int64_t))) {  // integer overflow check
143         // We keep the original memory contents on bad minimumCapacity/maxCapacity.
144         status = U_ILLEGAL_ARGUMENT_ERROR;
145         return FALSE;
146     }
147     int64_t* newElems = (int64_t *)uprv_realloc(elements, sizeof(int64_t)*newCap);
148     if (newElems == NULL) {
149         // We keep the original contents on the memory failure on realloc.
150         status = U_MEMORY_ALLOCATION_ERROR;
151         return FALSE;
152     }
153     elements = newElems;
154     capacity = newCap;
155     return TRUE;
156 }
157 
setMaxCapacity(int32_t limit)158 void UVector64::setMaxCapacity(int32_t limit) {
159     U_ASSERT(limit >= 0);
160     if (limit < 0) {
161         limit = 0;
162     }
163     if (limit > (int32_t)(INT32_MAX / sizeof(int64_t))) {  // integer overflow check for realloc
164         //  Something is very wrong, don't realloc, leave capacity and maxCapacity unchanged
165         return;
166     }
167     maxCapacity = limit;
168     if (capacity <= maxCapacity || maxCapacity == 0) {
169         // Current capacity is within the new limit.
170         return;
171     }
172 
173     // New maximum capacity is smaller than the current size.
174     // Realloc the storage to the new, smaller size.
175     int64_t* newElems = (int64_t *)uprv_realloc(elements, sizeof(int64_t)*maxCapacity);
176     if (newElems == NULL) {
177         // Realloc to smaller failed.
178         //   Just keep what we had.  No need to call it a failure.
179         return;
180     }
181     elements = newElems;
182     capacity = maxCapacity;
183     if (count > capacity) {
184         count = capacity;
185     }
186 }
187 
188 /**
189  * Change the size of this vector as follows: If newSize is smaller,
190  * then truncate the array, possibly deleting held elements for i >=
191  * newSize.  If newSize is larger, grow the array, filling in new
192  * slots with NULL.
193  */
setSize(int32_t newSize)194 void UVector64::setSize(int32_t newSize) {
195     int32_t i;
196     if (newSize < 0) {
197         return;
198     }
199     if (newSize > count) {
200         UErrorCode ec = U_ZERO_ERROR;
201         if (!ensureCapacity(newSize, ec)) {
202             return;
203         }
204         for (i=count; i<newSize; ++i) {
205             elements[i] = 0;
206         }
207     }
208     count = newSize;
209 }
210 
211 U_NAMESPACE_END
212 
213