| OLD | NEW |
| 1 /* | 1 /* |
| 2 ****************************************************************************** | 2 ****************************************************************************** |
| 3 * Copyright (C) 1999-2003, International Business Machines Corporation and * | 3 * Copyright (C) 1999-2008, International Business Machines Corporation and * |
| 4 * others. All Rights Reserved. * | 4 * others. All Rights Reserved. * |
| 5 ****************************************************************************** | 5 ****************************************************************************** |
| 6 * Date Name Description | 6 * Date Name Description |
| 7 * 10/22/99 alan Creation. | 7 * 10/22/99 alan Creation. |
| 8 ********************************************************************** | 8 ********************************************************************** |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "uvectr32.h" | 11 #include "uvectr32.h" |
| 12 #include "cmemory.h" | 12 #include "cmemory.h" |
| 13 | 13 |
| 14 U_NAMESPACE_BEGIN | 14 U_NAMESPACE_BEGIN |
| 15 | 15 |
| 16 #define DEFUALT_CAPACITY 8 | 16 #define DEFUALT_CAPACITY 8 |
| 17 | 17 |
| 18 /* | 18 /* |
| 19 * Constants for hinting whether a key is an integer | 19 * Constants for hinting whether a key is an integer |
| 20 * or a pointer. If a hint bit is zero, then the associated | 20 * or a pointer. If a hint bit is zero, then the associated |
| 21 * token is assumed to be an integer. This is needed for iSeries | 21 * token is assumed to be an integer. This is needed for iSeries |
| 22 */ | 22 */ |
| 23 | 23 |
| 24 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector32) | 24 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(UVector32) |
| 25 | 25 |
| 26 UVector32::UVector32(UErrorCode &status) : | 26 UVector32::UVector32(UErrorCode &status) : |
| 27 count(0), | 27 count(0), |
| 28 capacity(0), | 28 capacity(0), |
| 29 maxCapacity(0), |
| 29 elements(NULL) | 30 elements(NULL) |
| 30 { | 31 { |
| 31 _init(DEFUALT_CAPACITY, status); | 32 _init(DEFUALT_CAPACITY, status); |
| 32 } | 33 } |
| 33 | 34 |
| 34 UVector32::UVector32(int32_t initialCapacity, UErrorCode &status) : | 35 UVector32::UVector32(int32_t initialCapacity, UErrorCode &status) : |
| 35 count(0), | 36 count(0), |
| 36 capacity(0), | 37 capacity(0), |
| 38 maxCapacity(0), |
| 37 elements(0) | 39 elements(0) |
| 38 { | 40 { |
| 39 _init(initialCapacity, status); | 41 _init(initialCapacity, status); |
| 40 } | 42 } |
| 41 | 43 |
| 42 | 44 |
| 43 | 45 |
| 44 void UVector32::_init(int32_t initialCapacity, UErrorCode &status) { | 46 void UVector32::_init(int32_t initialCapacity, UErrorCode &status) { |
| 45 // Fix bogus initialCapacity values; avoid malloc(0) | 47 // Fix bogus initialCapacity values; avoid malloc(0) |
| 46 if (initialCapacity < 1) { | 48 if (initialCapacity < 1) { |
| 47 initialCapacity = DEFUALT_CAPACITY; | 49 initialCapacity = DEFUALT_CAPACITY; |
| 48 } | 50 } |
| 51 if (maxCapacity>0 && maxCapacity<initialCapacity) { |
| 52 initialCapacity = maxCapacity; |
| 53 } |
| 49 elements = (int32_t *)uprv_malloc(sizeof(int32_t)*initialCapacity); | 54 elements = (int32_t *)uprv_malloc(sizeof(int32_t)*initialCapacity); |
| 50 if (elements == 0) { | 55 if (elements == 0) { |
| 51 status = U_MEMORY_ALLOCATION_ERROR; | 56 status = U_MEMORY_ALLOCATION_ERROR; |
| 52 } else { | 57 } else { |
| 53 capacity = initialCapacity; | 58 capacity = initialCapacity; |
| 54 } | 59 } |
| 55 } | 60 } |
| 56 | 61 |
| 57 UVector32::~UVector32() { | 62 UVector32::~UVector32() { |
| 58 uprv_free(elements); | 63 uprv_free(elements); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 return i; | 187 return i; |
| 183 } | 188 } |
| 184 } | 189 } |
| 185 return -1; | 190 return -1; |
| 186 } | 191 } |
| 187 | 192 |
| 188 | 193 |
| 189 UBool UVector32::expandCapacity(int32_t minimumCapacity, UErrorCode &status) { | 194 UBool UVector32::expandCapacity(int32_t minimumCapacity, UErrorCode &status) { |
| 190 if (capacity >= minimumCapacity) { | 195 if (capacity >= minimumCapacity) { |
| 191 return TRUE; | 196 return TRUE; |
| 192 } else { | 197 } |
| 193 int32_t newCap = capacity * 2; | 198 if (maxCapacity>0 && minimumCapacity>maxCapacity) { |
| 194 if (newCap < minimumCapacity) { | 199 status = U_BUFFER_OVERFLOW_ERROR; |
| 195 newCap = minimumCapacity; | 200 return FALSE; |
| 196 } | 201 } |
| 197 int32_t* newElems = (int32_t *)uprv_malloc(sizeof(int32_t)*newCap); | 202 int32_t newCap = capacity * 2; |
| 198 if (newElems == 0) { | 203 if (newCap < minimumCapacity) { |
| 199 status = U_MEMORY_ALLOCATION_ERROR; | 204 newCap = minimumCapacity; |
| 200 return FALSE; | 205 } |
| 201 } | 206 if (maxCapacity > 0 && newCap > maxCapacity) { |
| 202 uprv_memcpy(newElems, elements, sizeof(elements[0]) * count); | 207 newCap = maxCapacity; |
| 203 uprv_free(elements); | 208 } |
| 204 elements = newElems; | 209 int32_t* newElems = (int32_t *)uprv_malloc(sizeof(int32_t)*newCap); |
| 205 capacity = newCap; | 210 if (newElems == 0) { |
| 206 return TRUE; | 211 status = U_MEMORY_ALLOCATION_ERROR; |
| 212 return FALSE; |
| 213 } |
| 214 uprv_memcpy(newElems, elements, sizeof(elements[0]) * count); |
| 215 uprv_free(elements); |
| 216 elements = newElems; |
| 217 capacity = newCap; |
| 218 return TRUE; |
| 219 } |
| 220 |
| 221 void UVector32::setMaxCapacity(int32_t limit) { |
| 222 U_ASSERT(limit >= 0); |
| 223 maxCapacity = limit; |
| 224 if (maxCapacity < 0) { |
| 225 maxCapacity = 0; |
| 207 } | 226 } |
| 208 } | 227 } |
| 209 | 228 |
| 210 /** | 229 /** |
| 211 * Change the size of this vector as follows: If newSize is smaller, | 230 * Change the size of this vector as follows: If newSize is smaller, |
| 212 * then truncate the array, possibly deleting held elements for i >= | 231 * then truncate the array, possibly deleting held elements for i >= |
| 213 * newSize. If newSize is larger, grow the array, filling in new | 232 * newSize. If newSize is larger, grow the array, filling in new |
| 214 * slots with NULL. | 233 * slots with NULL. |
| 215 */ | 234 */ |
| 216 void UVector32::setSize(int32_t newSize) { | 235 void UVector32::setSize(int32_t newSize) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 ++count; | 283 ++count; |
| 265 } | 284 } |
| 266 } | 285 } |
| 267 | 286 |
| 268 | 287 |
| 269 | 288 |
| 270 | 289 |
| 271 | 290 |
| 272 U_NAMESPACE_END | 291 U_NAMESPACE_END |
| 273 | 292 |
| OLD | NEW |