intl/icu/source/common/uvectr64.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial