| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifndef CMEMORY_H |
| 27 |
#define CMEMORY_H |
| 28 |
|
| 29 |
#include "unicode/utypes.h" |
| 30 |
|
| 31 |
#include <stddef.h> |
| 32 |
#include <string.h> |
| 33 |
#include "unicode/localpointer.h" |
| 34 |
#include "uassert.h" |
| 35 |
|
| 36 |
#if U_DEBUG && defined(UPRV_MALLOC_COUNT) |
| 37 |
#include <stdio.h> |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
#if defined(__clang__) |
| 42 |
#define uprv_memcpy(dst, src, size) UPRV_BLOCK_MACRO_BEGIN { \ |
| 43 |
\ |
| 44 |
_Pragma("clang diagnostic push") \ |
| 45 |
_Pragma("clang diagnostic ignored \"-Waddress\"") \ |
| 46 |
U_ASSERT(dst != NULL); \ |
| 47 |
U_ASSERT(src != NULL); \ |
| 48 |
_Pragma("clang diagnostic pop") \ |
| 49 |
U_STANDARD_CPP_NAMESPACE memcpy(dst, src, size); \ |
| 50 |
} UPRV_BLOCK_MACRO_END |
| 51 |
#define uprv_memmove(dst, src, size) UPRV_BLOCK_MACRO_BEGIN { \ |
| 52 |
\ |
| 53 |
_Pragma("clang diagnostic push") \ |
| 54 |
_Pragma("clang diagnostic ignored \"-Waddress\"") \ |
| 55 |
U_ASSERT(dst != NULL); \ |
| 56 |
U_ASSERT(src != NULL); \ |
| 57 |
_Pragma("clang diagnostic pop") \ |
| 58 |
U_STANDARD_CPP_NAMESPACE memmove(dst, src, size); \ |
| 59 |
} UPRV_BLOCK_MACRO_END |
| 60 |
#elif defined(__GNUC__) |
| 61 |
#define uprv_memcpy(dst, src, size) UPRV_BLOCK_MACRO_BEGIN { \ |
| 62 |
\ |
| 63 |
_Pragma("GCC diagnostic push") \ |
| 64 |
_Pragma("GCC diagnostic ignored \"-Waddress\"") \ |
| 65 |
U_ASSERT(dst != NULL); \ |
| 66 |
U_ASSERT(src != NULL); \ |
| 67 |
_Pragma("GCC diagnostic pop") \ |
| 68 |
U_STANDARD_CPP_NAMESPACE memcpy(dst, src, size); \ |
| 69 |
} UPRV_BLOCK_MACRO_END |
| 70 |
#define uprv_memmove(dst, src, size) UPRV_BLOCK_MACRO_BEGIN { \ |
| 71 |
\ |
| 72 |
_Pragma("GCC diagnostic push") \ |
| 73 |
_Pragma("GCC diagnostic ignored \"-Waddress\"") \ |
| 74 |
U_ASSERT(dst != NULL); \ |
| 75 |
U_ASSERT(src != NULL); \ |
| 76 |
_Pragma("GCC diagnostic pop") \ |
| 77 |
U_STANDARD_CPP_NAMESPACE memmove(dst, src, size); \ |
| 78 |
} UPRV_BLOCK_MACRO_END |
| 79 |
#else |
| 80 |
#define uprv_memcpy(dst, src, size) UPRV_BLOCK_MACRO_BEGIN { \ |
| 81 |
U_ASSERT(dst != NULL); \ |
| 82 |
U_ASSERT(src != NULL); \ |
| 83 |
U_STANDARD_CPP_NAMESPACE memcpy(dst, src, size); \ |
| 84 |
} UPRV_BLOCK_MACRO_END |
| 85 |
#define uprv_memmove(dst, src, size) UPRV_BLOCK_MACRO_BEGIN { \ |
| 86 |
U_ASSERT(dst != NULL); \ |
| 87 |
U_ASSERT(src != NULL); \ |
| 88 |
U_STANDARD_CPP_NAMESPACE memmove(dst, src, size); \ |
| 89 |
} UPRV_BLOCK_MACRO_END |
| 90 |
#endif |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
#define UPRV_LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0])) |
| 100 |
#define uprv_memset(buffer, mark, size) U_STANDARD_CPP_NAMESPACE memset(buffer, mark, size) |
| 101 |
#define uprv_memcmp(buffer1, buffer2, size) U_STANDARD_CPP_NAMESPACE memcmp(buffer1, buffer2,size) |
| 102 |
#define uprv_memchr(ptr, value, num) U_STANDARD_CPP_NAMESPACE memchr(ptr, value, num) |
| 103 |
|
| 104 |
U_CAPI void * U_EXPORT2 |
| 105 |
uprv_malloc(size_t s) U_MALLOC_ATTR U_ALLOC_SIZE_ATTR(1); |
| 106 |
|
| 107 |
U_CAPI void * U_EXPORT2 |
| 108 |
uprv_realloc(void *mem, size_t size) U_ALLOC_SIZE_ATTR(2); |
| 109 |
|
| 110 |
U_CAPI void U_EXPORT2 |
| 111 |
uprv_free(void *mem); |
| 112 |
|
| 113 |
U_CAPI void * U_EXPORT2 |
| 114 |
uprv_calloc(size_t num, size_t size) U_MALLOC_ATTR U_ALLOC_SIZE_ATTR2(1,2); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
#define U_POINTER_MASK_LSB(ptr, mask) ((uintptr_t)(ptr) & (mask)) |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#define STATIC_NEW(type) [] () { \ |
| 145 |
alignas(type) static char storage[sizeof(type)]; \ |
| 146 |
return new(storage) type();} () |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
U_CFUNC UBool |
| 154 |
cmemory_cleanup(void); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
typedef void U_CALLCONV UObjectDeleter(void* obj); |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
U_CAPI void U_EXPORT2 |
| 170 |
uprv_deleteUObject(void *obj); |
| 171 |
|
| 172 |
#ifdef __cplusplus |
| 173 |
|
| 174 |
#include <utility> |
| 175 |
#include "unicode/uobject.h" |
| 176 |
|
| 177 |
U_NAMESPACE_BEGIN |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
template<typename T> |
| 187 |
class LocalMemory : public LocalPointerBase<T> { |
| 188 |
public: |
| 189 |
using LocalPointerBase<T>::operator*; |
| 190 |
using LocalPointerBase<T>::operator->; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
explicit LocalMemory(T *p=nullptr) : LocalPointerBase<T>(p) {} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
LocalMemory(LocalMemory<T> &&src) noexcept : LocalPointerBase<T>(src.ptr) { |
| 201 |
src.ptr=nullptr; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
~LocalMemory() { |
| 207 |
uprv_free(LocalPointerBase<T>::ptr); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
LocalMemory<T> &operator=(LocalMemory<T> &&src) noexcept { |
| 216 |
uprv_free(LocalPointerBase<T>::ptr); |
| 217 |
LocalPointerBase<T>::ptr=src.ptr; |
| 218 |
src.ptr=nullptr; |
| 219 |
return *this; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
void swap(LocalMemory<T> &other) noexcept { |
| 226 |
T *temp=LocalPointerBase<T>::ptr; |
| 227 |
LocalPointerBase<T>::ptr=other.ptr; |
| 228 |
other.ptr=temp; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
friend inline void swap(LocalMemory<T> &p1, LocalMemory<T> &p2) noexcept { |
| 236 |
p1.swap(p2); |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
void adoptInstead(T *p) { |
| 244 |
uprv_free(LocalPointerBase<T>::ptr); |
| 245 |
LocalPointerBase<T>::ptr=p; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
inline T *allocateInsteadAndReset(int32_t newCapacity=1); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
inline T *allocateInsteadAndCopy(int32_t newCapacity=1, int32_t length=0); |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; } |
| 275 |
}; |
| 276 |
|
| 277 |
template<typename T> |
| 278 |
inline T *LocalMemory<T>::allocateInsteadAndReset(int32_t newCapacity) { |
| 279 |
if(newCapacity>0) { |
| 280 |
T *p=(T *)uprv_malloc(newCapacity*sizeof(T)); |
| 281 |
if(p!=nullptr) { |
| 282 |
uprv_memset(p, 0, newCapacity*sizeof(T)); |
| 283 |
uprv_free(LocalPointerBase<T>::ptr); |
| 284 |
LocalPointerBase<T>::ptr=p; |
| 285 |
} |
| 286 |
return p; |
| 287 |
} else { |
| 288 |
return nullptr; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
template<typename T> |
| 294 |
inline T *LocalMemory<T>::allocateInsteadAndCopy(int32_t newCapacity, int32_t length) { |
| 295 |
if(newCapacity>0) { |
| 296 |
T *p=(T *)uprv_malloc(newCapacity*sizeof(T)); |
| 297 |
if(p!=nullptr) { |
| 298 |
if(length>0) { |
| 299 |
if(length>newCapacity) { |
| 300 |
length=newCapacity; |
| 301 |
} |
| 302 |
uprv_memcpy(p, LocalPointerBase<T>::ptr, (size_t)length*sizeof(T)); |
| 303 |
} |
| 304 |
uprv_free(LocalPointerBase<T>::ptr); |
| 305 |
LocalPointerBase<T>::ptr=p; |
| 306 |
} |
| 307 |
return p; |
| 308 |
} else { |
| 309 |
return nullptr; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
template<typename T, int32_t stackCapacity> |
| 332 |
class MaybeStackArray { |
| 333 |
public: |
| 334 |
|
| 335 |
static void* U_EXPORT2 operator new(size_t) noexcept = delete; |
| 336 |
static void* U_EXPORT2 operator new[](size_t) noexcept = delete; |
| 337 |
#if U_HAVE_PLACEMENT_NEW |
| 338 |
static void* U_EXPORT2 operator new(size_t, void*) noexcept = delete; |
| 339 |
#endif |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
MaybeStackArray() : ptr(stackArray), capacity(stackCapacity), needToRelease(false) {} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
MaybeStackArray(int32_t newCapacity, UErrorCode status) : MaybeStackArray() { |
| 351 |
if (U_FAILURE(status)) { |
| 352 |
return; |
| 353 |
} |
| 354 |
if (capacity < newCapacity) { |
| 355 |
if (resize(newCapacity) == nullptr) { |
| 356 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
~MaybeStackArray() { releaseArray(); } |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
MaybeStackArray(MaybeStackArray<T, stackCapacity> &&src) noexcept; |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
MaybeStackArray<T, stackCapacity> &operator=(MaybeStackArray<T, stackCapacity> &&src) noexcept; |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
int32_t getCapacity() const { return capacity; } |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
T *getAlias() const { return ptr; } |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
T *getArrayLimit() const { return getAlias()+capacity; } |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
const T &operator[](ptrdiff_t i) const { return ptr[i]; } |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
T &operator[](ptrdiff_t i) { return ptr[i]; } |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
void aliasInstead(T *otherArray, int32_t otherCapacity) { |
| 410 |
if(otherArray!=nullptr && otherCapacity>0) { |
| 411 |
releaseArray(); |
| 412 |
ptr=otherArray; |
| 413 |
capacity=otherCapacity; |
| 414 |
needToRelease=false; |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
inline T *resize(int32_t newCapacity, int32_t length=0); |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
inline T *orphanOrClone(int32_t length, int32_t &resultCapacity); |
| 439 |
|
| 440 |
protected: |
| 441 |
|
| 442 |
void copyFrom(const MaybeStackArray &src, UErrorCode &status) { |
| 443 |
if (U_FAILURE(status)) { |
| 444 |
return; |
| 445 |
} |
| 446 |
if (this->resize(src.capacity, 0) == nullptr) { |
| 447 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 448 |
return; |
| 449 |
} |
| 450 |
uprv_memcpy(this->ptr, src.ptr, (size_t)capacity * sizeof(T)); |
| 451 |
} |
| 452 |
|
| 453 |
private: |
| 454 |
T *ptr; |
| 455 |
int32_t capacity; |
| 456 |
UBool needToRelease; |
| 457 |
T stackArray[stackCapacity]; |
| 458 |
void releaseArray() { |
| 459 |
if(needToRelease) { |
| 460 |
uprv_free(ptr); |
| 461 |
} |
| 462 |
} |
| 463 |
void resetToStackArray() { |
| 464 |
ptr=stackArray; |
| 465 |
capacity=stackCapacity; |
| 466 |
needToRelease=false; |
| 467 |
} |
| 468 |
|
| 469 |
bool operator==(const MaybeStackArray & ) = delete; |
| 470 |
bool operator!=(const MaybeStackArray & ) = delete; |
| 471 |
|
| 472 |
MaybeStackArray(const MaybeStackArray & ) = delete; |
| 473 |
void operator=(const MaybeStackArray & ) = delete; |
| 474 |
}; |
| 475 |
|
| 476 |
template<typename T, int32_t stackCapacity> |
| 477 |
icu::MaybeStackArray<T, stackCapacity>::MaybeStackArray( |
| 478 |
MaybeStackArray <T, stackCapacity>&& src) noexcept |
| 479 |
: ptr(src.ptr), capacity(src.capacity), needToRelease(src.needToRelease) { |
| 480 |
if (src.ptr == src.stackArray) { |
| 481 |
ptr = stackArray; |
| 482 |
uprv_memcpy(stackArray, src.stackArray, sizeof(T) * src.capacity); |
| 483 |
} else { |
| 484 |
src.resetToStackArray(); |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
template<typename T, int32_t stackCapacity> |
| 489 |
inline MaybeStackArray <T, stackCapacity>& |
| 490 |
MaybeStackArray<T, stackCapacity>::operator=(MaybeStackArray <T, stackCapacity>&& src) noexcept { |
| 491 |
releaseArray(); |
| 492 |
capacity = src.capacity; |
| 493 |
needToRelease = src.needToRelease; |
| 494 |
if (src.ptr == src.stackArray) { |
| 495 |
ptr = stackArray; |
| 496 |
uprv_memcpy(stackArray, src.stackArray, sizeof(T) * src.capacity); |
| 497 |
} else { |
| 498 |
ptr = src.ptr; |
| 499 |
src.resetToStackArray(); |
| 500 |
} |
| 501 |
return *this; |
| 502 |
} |
| 503 |
|
| 504 |
template<typename T, int32_t stackCapacity> |
| 505 |
inline T *MaybeStackArray<T, stackCapacity>::resize(int32_t newCapacity, int32_t length) { |
| 506 |
if(newCapacity>0) { |
| 507 |
#if U_DEBUG && defined(UPRV_MALLOC_COUNT) |
| 508 |
::fprintf(::stderr, "MaybeStackArray (resize) alloc %d * %lu\n", newCapacity, sizeof(T)); |
| 509 |
#endif |
| 510 |
T *p=(T *)uprv_malloc(newCapacity*sizeof(T)); |
| 511 |
if(p!=nullptr) { |
| 512 |
if(length>0) { |
| 513 |
if(length>capacity) { |
| 514 |
length=capacity; |
| 515 |
} |
| 516 |
if(length>newCapacity) { |
| 517 |
length=newCapacity; |
| 518 |
} |
| 519 |
uprv_memcpy(p, ptr, (size_t)length*sizeof(T)); |
| 520 |
} |
| 521 |
releaseArray(); |
| 522 |
ptr=p; |
| 523 |
capacity=newCapacity; |
| 524 |
needToRelease=true; |
| 525 |
} |
| 526 |
return p; |
| 527 |
} else { |
| 528 |
return nullptr; |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
template<typename T, int32_t stackCapacity> |
| 533 |
inline T *MaybeStackArray<T, stackCapacity>::orphanOrClone(int32_t length, int32_t &resultCapacity) { |
| 534 |
T *p; |
| 535 |
if(needToRelease) { |
| 536 |
p=ptr; |
| 537 |
} else if(length<=0) { |
| 538 |
return nullptr; |
| 539 |
} else { |
| 540 |
if(length>capacity) { |
| 541 |
length=capacity; |
| 542 |
} |
| 543 |
p=(T *)uprv_malloc(length*sizeof(T)); |
| 544 |
#if U_DEBUG && defined(UPRV_MALLOC_COUNT) |
| 545 |
::fprintf(::stderr,"MaybeStacArray (orphan) alloc %d * %lu\n", length,sizeof(T)); |
| 546 |
#endif |
| 547 |
if(p==nullptr) { |
| 548 |
return nullptr; |
| 549 |
} |
| 550 |
uprv_memcpy(p, ptr, (size_t)length*sizeof(T)); |
| 551 |
} |
| 552 |
resultCapacity=length; |
| 553 |
resetToStackArray(); |
| 554 |
return p; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
template<typename H, typename T, int32_t stackCapacity> |
| 568 |
class MaybeStackHeaderAndArray { |
| 569 |
public: |
| 570 |
|
| 571 |
static void* U_EXPORT2 operator new(size_t) noexcept = delete; |
| 572 |
static void* U_EXPORT2 operator new[](size_t) noexcept = delete; |
| 573 |
#if U_HAVE_PLACEMENT_NEW |
| 574 |
static void* U_EXPORT2 operator new(size_t, void*) noexcept = delete; |
| 575 |
#endif |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
MaybeStackHeaderAndArray() : ptr(&stackHeader), capacity(stackCapacity), needToRelease(false) {} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
~MaybeStackHeaderAndArray() { releaseMemory(); } |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
int32_t getCapacity() const { return capacity; } |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
H *getAlias() const { return ptr; } |
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
T *getArrayStart() const { return reinterpret_cast<T *>(getAlias()+1); } |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
T *getArrayLimit() const { return getArrayStart()+capacity; } |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
operator H *() const { return ptr; } |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
T &operator[](ptrdiff_t i) { return getArrayStart()[i]; } |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
void aliasInstead(H *otherMemory, int32_t otherCapacity) { |
| 625 |
if(otherMemory!=nullptr && otherCapacity>0) { |
| 626 |
releaseMemory(); |
| 627 |
ptr=otherMemory; |
| 628 |
capacity=otherCapacity; |
| 629 |
needToRelease=false; |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
inline H *resize(int32_t newCapacity, int32_t length=0); |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
inline H *orphanOrClone(int32_t length, int32_t &resultCapacity); |
| 655 |
private: |
| 656 |
H *ptr; |
| 657 |
int32_t capacity; |
| 658 |
UBool needToRelease; |
| 659 |
|
| 660 |
H stackHeader; |
| 661 |
T stackArray[stackCapacity]; |
| 662 |
void releaseMemory() { |
| 663 |
if(needToRelease) { |
| 664 |
uprv_free(ptr); |
| 665 |
} |
| 666 |
} |
| 667 |
|
| 668 |
bool operator==(const MaybeStackHeaderAndArray & ) {return false;} |
| 669 |
bool operator!=(const MaybeStackHeaderAndArray & ) {return true;} |
| 670 |
|
| 671 |
MaybeStackHeaderAndArray(const MaybeStackHeaderAndArray & ) {} |
| 672 |
void operator=(const MaybeStackHeaderAndArray & ) {} |
| 673 |
}; |
| 674 |
|
| 675 |
template<typename H, typename T, int32_t stackCapacity> |
| 676 |
inline H *MaybeStackHeaderAndArray<H, T, stackCapacity>::resize(int32_t newCapacity, |
| 677 |
int32_t length) { |
| 678 |
if(newCapacity>=0) { |
| 679 |
#if U_DEBUG && defined(UPRV_MALLOC_COUNT) |
| 680 |
::fprintf(::stderr,"MaybeStackHeaderAndArray alloc %d + %d * %ul\n", sizeof(H),newCapacity,sizeof(T)); |
| 681 |
#endif |
| 682 |
H *p=(H *)uprv_malloc(sizeof(H)+newCapacity*sizeof(T)); |
| 683 |
if(p!=nullptr) { |
| 684 |
if(length<0) { |
| 685 |
length=0; |
| 686 |
} else if(length>0) { |
| 687 |
if(length>capacity) { |
| 688 |
length=capacity; |
| 689 |
} |
| 690 |
if(length>newCapacity) { |
| 691 |
length=newCapacity; |
| 692 |
} |
| 693 |
} |
| 694 |
uprv_memcpy(p, ptr, sizeof(H)+(size_t)length*sizeof(T)); |
| 695 |
releaseMemory(); |
| 696 |
ptr=p; |
| 697 |
capacity=newCapacity; |
| 698 |
needToRelease=true; |
| 699 |
} |
| 700 |
return p; |
| 701 |
} else { |
| 702 |
return nullptr; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
template<typename H, typename T, int32_t stackCapacity> |
| 707 |
inline H *MaybeStackHeaderAndArray<H, T, stackCapacity>::orphanOrClone(int32_t length, |
| 708 |
int32_t &resultCapacity) { |
| 709 |
H *p; |
| 710 |
if(needToRelease) { |
| 711 |
p=ptr; |
| 712 |
} else { |
| 713 |
if(length<0) { |
| 714 |
length=0; |
| 715 |
} else if(length>capacity) { |
| 716 |
length=capacity; |
| 717 |
} |
| 718 |
#if U_DEBUG && defined(UPRV_MALLOC_COUNT) |
| 719 |
::fprintf(::stderr,"MaybeStackHeaderAndArray (orphan) alloc %ul + %d * %lu\n", sizeof(H),length,sizeof(T)); |
| 720 |
#endif |
| 721 |
p=(H *)uprv_malloc(sizeof(H)+length*sizeof(T)); |
| 722 |
if(p==nullptr) { |
| 723 |
return nullptr; |
| 724 |
} |
| 725 |
uprv_memcpy(p, ptr, sizeof(H)+(size_t)length*sizeof(T)); |
| 726 |
} |
| 727 |
resultCapacity=length; |
| 728 |
ptr=&stackHeader; |
| 729 |
capacity=stackCapacity; |
| 730 |
needToRelease=false; |
| 731 |
return p; |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
template<typename T, int32_t stackCapacity = 8> |
| 758 |
class MemoryPool : public UMemory { |
| 759 |
public: |
| 760 |
MemoryPool() : fCount(0), fPool() {} |
| 761 |
|
| 762 |
~MemoryPool() { |
| 763 |
for (int32_t i = 0; i < fCount; ++i) { |
| 764 |
delete fPool[i]; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
MemoryPool(const MemoryPool&) = delete; |
| 769 |
MemoryPool& operator=(const MemoryPool&) = delete; |
| 770 |
|
| 771 |
MemoryPool(MemoryPool&& other) noexcept : fCount(other.fCount), |
| 772 |
fPool(std::move(other.fPool)) { |
| 773 |
other.fCount = 0; |
| 774 |
} |
| 775 |
|
| 776 |
MemoryPool& operator=(MemoryPool&& other) noexcept { |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
std::swap(fCount, other.fCount); |
| 784 |
std::swap(fPool, other.fPool); |
| 785 |
return *this; |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
template<typename... Args> |
| 796 |
T* create(Args&&... args) { |
| 797 |
int32_t capacity = fPool.getCapacity(); |
| 798 |
if (fCount == capacity && |
| 799 |
fPool.resize(capacity == stackCapacity ? 4 * capacity : 2 * capacity, |
| 800 |
capacity) == nullptr) { |
| 801 |
return nullptr; |
| 802 |
} |
| 803 |
return fPool[fCount++] = new T(std::forward<Args>(args)...); |
| 804 |
} |
| 805 |
|
| 806 |
template <typename... Args> |
| 807 |
T* createAndCheckErrorCode(UErrorCode &status, Args &&... args) { |
| 808 |
if (U_FAILURE(status)) { |
| 809 |
return nullptr; |
| 810 |
} |
| 811 |
T *pointer = this->create(args...); |
| 812 |
if (U_SUCCESS(status) && pointer == nullptr) { |
| 813 |
status = U_MEMORY_ALLOCATION_ERROR; |
| 814 |
} |
| 815 |
return pointer; |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
int32_t count() const { |
| 822 |
return fCount; |
| 823 |
} |
| 824 |
|
| 825 |
protected: |
| 826 |
int32_t fCount; |
| 827 |
MaybeStackArray<T*, stackCapacity> fPool; |
| 828 |
}; |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
template<typename T, int32_t stackCapacity = 8> |
| 851 |
class MaybeStackVector : protected MemoryPool<T, stackCapacity> { |
| 852 |
public: |
| 853 |
template<typename... Args> |
| 854 |
T* emplaceBack(Args&&... args) { |
| 855 |
return this->create(args...); |
| 856 |
} |
| 857 |
|
| 858 |
template <typename... Args> |
| 859 |
T *emplaceBackAndCheckErrorCode(UErrorCode &status, Args &&... args) { |
| 860 |
return this->createAndCheckErrorCode(status, args...); |
| 861 |
} |
| 862 |
|
| 863 |
int32_t length() const { |
| 864 |
return this->fCount; |
| 865 |
} |
| 866 |
|
| 867 |
T** getAlias() { |
| 868 |
return this->fPool.getAlias(); |
| 869 |
} |
| 870 |
|
| 871 |
const T *const *getAlias() const { |
| 872 |
return this->fPool.getAlias(); |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
const T* operator[](ptrdiff_t i) const { |
| 882 |
return this->fPool[i]; |
| 883 |
} |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
T* operator[](ptrdiff_t i) { |
| 892 |
return this->fPool[i]; |
| 893 |
} |
| 894 |
}; |
| 895 |
|
| 896 |
|
| 897 |
U_NAMESPACE_END |
| 898 |
|
| 899 |
#endif |
| 900 |
#endif |