cmemory.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 1997-2016, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * File CMEMORY.H
  12. *
  13. * Contains stdlib.h/string.h memory functions
  14. *
  15. * @author Bertrand A. Damiba
  16. *
  17. * Modification History:
  18. *
  19. * Date Name Description
  20. * 6/20/98 Bertrand Created.
  21. * 05/03/99 stephen Changed from functions to macros.
  22. *
  23. ******************************************************************************
  24. */
  25. #ifndef CMEMORY_H
  26. #define CMEMORY_H
  27. #include "unicode/utypes.h"
  28. #include <stddef.h>
  29. #include <string.h>
  30. #include "unicode/localpointer.h"
  31. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  32. #include <stdio.h>
  33. #endif
  34. #define uprv_memcpy(dst, src, size) U_STANDARD_CPP_NAMESPACE memcpy(dst, src, size)
  35. #define uprv_memmove(dst, src, size) U_STANDARD_CPP_NAMESPACE memmove(dst, src, size)
  36. /**
  37. * \def UPRV_LENGTHOF
  38. * Convenience macro to determine the length of a fixed array at compile-time.
  39. * @param array A fixed length array
  40. * @return The length of the array, in elements
  41. * @internal
  42. */
  43. #define UPRV_LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
  44. #define uprv_memset(buffer, mark, size) U_STANDARD_CPP_NAMESPACE memset(buffer, mark, size)
  45. #define uprv_memcmp(buffer1, buffer2, size) U_STANDARD_CPP_NAMESPACE memcmp(buffer1, buffer2,size)
  46. #define uprv_memchr(ptr, value, num) U_STANDARD_CPP_NAMESPACE memchr(ptr, value, num)
  47. U_CAPI void * U_EXPORT2
  48. uprv_malloc(size_t s) U_MALLOC_ATTR U_ALLOC_SIZE_ATTR(1);
  49. U_CAPI void * U_EXPORT2
  50. uprv_realloc(void *mem, size_t size) U_ALLOC_SIZE_ATTR(2);
  51. U_CAPI void U_EXPORT2
  52. uprv_free(void *mem);
  53. U_CAPI void * U_EXPORT2
  54. uprv_calloc(size_t num, size_t size) U_MALLOC_ATTR U_ALLOC_SIZE_ATTR2(1,2);
  55. /**
  56. * Get the least significant bits of a pointer (a memory address).
  57. * For example, with a mask of 3, the macro gets the 2 least significant bits,
  58. * which will be 0 if the pointer is 32-bit (4-byte) aligned.
  59. *
  60. * uintptr_t is the most appropriate integer type to cast to.
  61. */
  62. #define U_POINTER_MASK_LSB(ptr, mask) ((uintptr_t)(ptr) & (mask))
  63. /**
  64. * Create & return an instance of "type" in statically allocated storage.
  65. * e.g.
  66. * static std::mutex *myMutex = STATIC_NEW(std::mutex);
  67. * To destroy an object created in this way, invoke the destructor explicitly, e.g.
  68. * myMutex->~mutex();
  69. * DO NOT use delete.
  70. * DO NOT use with class UMutex, which has specific support for static instances.
  71. *
  72. * STATIC_NEW is intended for use when
  73. * - We want a static (or global) object.
  74. * - We don't want it to ever be destructed, or to explicitly control destruction,
  75. * to avoid use-after-destruction problems.
  76. * - We want to avoid an ordinary heap allocated object,
  77. * to avoid the possibility of memory allocation failures, and
  78. * to avoid memory leak reports, from valgrind, for example.
  79. * This is defined as a macro rather than a template function because each invocation
  80. * must define distinct static storage for the object being returned.
  81. */
  82. #define STATIC_NEW(type) [] () { \
  83. alignas(type) static char storage[sizeof(type)]; \
  84. return new(storage) type();} ()
  85. /**
  86. * Heap clean up function, called from u_cleanup()
  87. * Clears any user heap functions from u_setMemoryFunctions()
  88. * Does NOT deallocate any remaining allocated memory.
  89. */
  90. U_CFUNC UBool
  91. cmemory_cleanup(void);
  92. /**
  93. * A function called by <TT>uhash_remove</TT>,
  94. * <TT>uhash_close</TT>, or <TT>uhash_put</TT> to delete
  95. * an existing key or value.
  96. * @param obj A key or value stored in a hashtable
  97. * @see uprv_deleteUObject
  98. */
  99. typedef void U_CALLCONV UObjectDeleter(void* obj);
  100. /**
  101. * Deleter for UObject instances.
  102. * Works for all subclasses of UObject because it has a virtual destructor.
  103. */
  104. U_CAPI void U_EXPORT2
  105. uprv_deleteUObject(void *obj);
  106. #ifdef __cplusplus
  107. #include <utility>
  108. #include "unicode/uobject.h"
  109. U_NAMESPACE_BEGIN
  110. /**
  111. * "Smart pointer" class, deletes memory via uprv_free().
  112. * For most methods see the LocalPointerBase base class.
  113. * Adds operator[] for array item access.
  114. *
  115. * @see LocalPointerBase
  116. */
  117. template<typename T>
  118. class LocalMemory : public LocalPointerBase<T> {
  119. public:
  120. using LocalPointerBase<T>::operator*;
  121. using LocalPointerBase<T>::operator->;
  122. /**
  123. * Constructor takes ownership.
  124. * @param p simple pointer to an array of T items that is adopted
  125. */
  126. explicit LocalMemory(T *p=NULL) : LocalPointerBase<T>(p) {}
  127. /**
  128. * Move constructor, leaves src with isNull().
  129. * @param src source smart pointer
  130. */
  131. LocalMemory(LocalMemory<T> &&src) U_NOEXCEPT : LocalPointerBase<T>(src.ptr) {
  132. src.ptr=NULL;
  133. }
  134. /**
  135. * Destructor deletes the memory it owns.
  136. */
  137. ~LocalMemory() {
  138. uprv_free(LocalPointerBase<T>::ptr);
  139. }
  140. /**
  141. * Move assignment operator, leaves src with isNull().
  142. * The behavior is undefined if *this and src are the same object.
  143. * @param src source smart pointer
  144. * @return *this
  145. */
  146. LocalMemory<T> &operator=(LocalMemory<T> &&src) U_NOEXCEPT {
  147. uprv_free(LocalPointerBase<T>::ptr);
  148. LocalPointerBase<T>::ptr=src.ptr;
  149. src.ptr=NULL;
  150. return *this;
  151. }
  152. /**
  153. * Swap pointers.
  154. * @param other other smart pointer
  155. */
  156. void swap(LocalMemory<T> &other) U_NOEXCEPT {
  157. T *temp=LocalPointerBase<T>::ptr;
  158. LocalPointerBase<T>::ptr=other.ptr;
  159. other.ptr=temp;
  160. }
  161. /**
  162. * Non-member LocalMemory swap function.
  163. * @param p1 will get p2's pointer
  164. * @param p2 will get p1's pointer
  165. */
  166. friend inline void swap(LocalMemory<T> &p1, LocalMemory<T> &p2) U_NOEXCEPT {
  167. p1.swap(p2);
  168. }
  169. /**
  170. * Deletes the array it owns,
  171. * and adopts (takes ownership of) the one passed in.
  172. * @param p simple pointer to an array of T items that is adopted
  173. */
  174. void adoptInstead(T *p) {
  175. uprv_free(LocalPointerBase<T>::ptr);
  176. LocalPointerBase<T>::ptr=p;
  177. }
  178. /**
  179. * Deletes the array it owns, allocates a new one and reset its bytes to 0.
  180. * Returns the new array pointer.
  181. * If the allocation fails, then the current array is unchanged and
  182. * this method returns NULL.
  183. * @param newCapacity must be >0
  184. * @return the allocated array pointer, or NULL if the allocation failed
  185. */
  186. inline T *allocateInsteadAndReset(int32_t newCapacity=1);
  187. /**
  188. * Deletes the array it owns and allocates a new one, copying length T items.
  189. * Returns the new array pointer.
  190. * If the allocation fails, then the current array is unchanged and
  191. * this method returns NULL.
  192. * @param newCapacity must be >0
  193. * @param length number of T items to be copied from the old array to the new one;
  194. * must be no more than the capacity of the old array,
  195. * which the caller must track because the LocalMemory does not track it
  196. * @return the allocated array pointer, or NULL if the allocation failed
  197. */
  198. inline T *allocateInsteadAndCopy(int32_t newCapacity=1, int32_t length=0);
  199. /**
  200. * Array item access (writable).
  201. * No index bounds check.
  202. * @param i array index
  203. * @return reference to the array item
  204. */
  205. T &operator[](ptrdiff_t i) const { return LocalPointerBase<T>::ptr[i]; }
  206. };
  207. template<typename T>
  208. inline T *LocalMemory<T>::allocateInsteadAndReset(int32_t newCapacity) {
  209. if(newCapacity>0) {
  210. T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
  211. if(p!=NULL) {
  212. uprv_memset(p, 0, newCapacity*sizeof(T));
  213. uprv_free(LocalPointerBase<T>::ptr);
  214. LocalPointerBase<T>::ptr=p;
  215. }
  216. return p;
  217. } else {
  218. return NULL;
  219. }
  220. }
  221. template<typename T>
  222. inline T *LocalMemory<T>::allocateInsteadAndCopy(int32_t newCapacity, int32_t length) {
  223. if(newCapacity>0) {
  224. T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
  225. if(p!=NULL) {
  226. if(length>0) {
  227. if(length>newCapacity) {
  228. length=newCapacity;
  229. }
  230. uprv_memcpy(p, LocalPointerBase<T>::ptr, (size_t)length*sizeof(T));
  231. }
  232. uprv_free(LocalPointerBase<T>::ptr);
  233. LocalPointerBase<T>::ptr=p;
  234. }
  235. return p;
  236. } else {
  237. return NULL;
  238. }
  239. }
  240. /**
  241. * Simple array/buffer management class using uprv_malloc() and uprv_free().
  242. * Provides an internal array with fixed capacity. Can alias another array
  243. * or allocate one.
  244. *
  245. * The array address is properly aligned for type T. It might not be properly
  246. * aligned for types larger than T (or larger than the largest subtype of T).
  247. *
  248. * Unlike LocalMemory and LocalArray, this class never adopts
  249. * (takes ownership of) another array.
  250. *
  251. * WARNING: MaybeStackArray only works with primitive (plain-old data) types.
  252. * It does NOT know how to call a destructor! If you work with classes with
  253. * destructors, consider:
  254. *
  255. * - LocalArray in localpointer.h if you know the length ahead of time
  256. * - MaybeStackVector if you know the length at runtime
  257. */
  258. template<typename T, int32_t stackCapacity>
  259. class MaybeStackArray {
  260. public:
  261. // No heap allocation. Use only on the stack.
  262. static void* U_EXPORT2 operator new(size_t) U_NOEXCEPT = delete;
  263. static void* U_EXPORT2 operator new[](size_t) U_NOEXCEPT = delete;
  264. #if U_HAVE_PLACEMENT_NEW
  265. static void* U_EXPORT2 operator new(size_t, void*) U_NOEXCEPT = delete;
  266. #endif
  267. /**
  268. * Default constructor initializes with internal T[stackCapacity] buffer.
  269. */
  270. MaybeStackArray() : ptr(stackArray), capacity(stackCapacity), needToRelease(FALSE) {}
  271. /**
  272. * Automatically allocates the heap array if the argument is larger than the stack capacity.
  273. * Intended for use when an approximate capacity is known at compile time but the true
  274. * capacity is not known until runtime.
  275. */
  276. MaybeStackArray(int32_t newCapacity) : MaybeStackArray() {
  277. if (capacity < newCapacity) { resize(newCapacity); }
  278. }
  279. /**
  280. * Destructor deletes the array (if owned).
  281. */
  282. ~MaybeStackArray() { releaseArray(); }
  283. /**
  284. * Move constructor: transfers ownership or copies the stack array.
  285. */
  286. MaybeStackArray(MaybeStackArray<T, stackCapacity> &&src) U_NOEXCEPT;
  287. /**
  288. * Move assignment: transfers ownership or copies the stack array.
  289. */
  290. MaybeStackArray<T, stackCapacity> &operator=(MaybeStackArray<T, stackCapacity> &&src) U_NOEXCEPT;
  291. /**
  292. * Returns the array capacity (number of T items).
  293. * @return array capacity
  294. */
  295. int32_t getCapacity() const { return capacity; }
  296. /**
  297. * Access without ownership change.
  298. * @return the array pointer
  299. */
  300. T *getAlias() const { return ptr; }
  301. /**
  302. * Returns the array limit. Simple convenience method.
  303. * @return getAlias()+getCapacity()
  304. */
  305. T *getArrayLimit() const { return getAlias()+capacity; }
  306. // No "operator T *() const" because that can make
  307. // expressions like mbs[index] ambiguous for some compilers.
  308. /**
  309. * Array item access (const).
  310. * No index bounds check.
  311. * @param i array index
  312. * @return reference to the array item
  313. */
  314. const T &operator[](ptrdiff_t i) const { return ptr[i]; }
  315. /**
  316. * Array item access (writable).
  317. * No index bounds check.
  318. * @param i array index
  319. * @return reference to the array item
  320. */
  321. T &operator[](ptrdiff_t i) { return ptr[i]; }
  322. /**
  323. * Deletes the array (if owned) and aliases another one, no transfer of ownership.
  324. * If the arguments are illegal, then the current array is unchanged.
  325. * @param otherArray must not be NULL
  326. * @param otherCapacity must be >0
  327. */
  328. void aliasInstead(T *otherArray, int32_t otherCapacity) {
  329. if(otherArray!=NULL && otherCapacity>0) {
  330. releaseArray();
  331. ptr=otherArray;
  332. capacity=otherCapacity;
  333. needToRelease=FALSE;
  334. }
  335. }
  336. /**
  337. * Deletes the array (if owned) and allocates a new one, copying length T items.
  338. * Returns the new array pointer.
  339. * If the allocation fails, then the current array is unchanged and
  340. * this method returns NULL.
  341. * @param newCapacity can be less than or greater than the current capacity;
  342. * must be >0
  343. * @param length number of T items to be copied from the old array to the new one
  344. * @return the allocated array pointer, or NULL if the allocation failed
  345. */
  346. inline T *resize(int32_t newCapacity, int32_t length=0);
  347. /**
  348. * Gives up ownership of the array if owned, or else clones it,
  349. * copying length T items; resets itself to the internal stack array.
  350. * Returns NULL if the allocation failed.
  351. * @param length number of T items to copy when cloning,
  352. * and capacity of the clone when cloning
  353. * @param resultCapacity will be set to the returned array's capacity (output-only)
  354. * @return the array pointer;
  355. * caller becomes responsible for deleting the array
  356. */
  357. inline T *orphanOrClone(int32_t length, int32_t &resultCapacity);
  358. private:
  359. T *ptr;
  360. int32_t capacity;
  361. UBool needToRelease;
  362. T stackArray[stackCapacity];
  363. void releaseArray() {
  364. if(needToRelease) {
  365. uprv_free(ptr);
  366. }
  367. }
  368. void resetToStackArray() {
  369. ptr=stackArray;
  370. capacity=stackCapacity;
  371. needToRelease=FALSE;
  372. }
  373. /* No comparison operators with other MaybeStackArray's. */
  374. bool operator==(const MaybeStackArray & /*other*/) {return FALSE;}
  375. bool operator!=(const MaybeStackArray & /*other*/) {return TRUE;}
  376. /* No ownership transfer: No copy constructor, no assignment operator. */
  377. MaybeStackArray(const MaybeStackArray & /*other*/) {}
  378. void operator=(const MaybeStackArray & /*other*/) {}
  379. };
  380. template<typename T, int32_t stackCapacity>
  381. icu::MaybeStackArray<T, stackCapacity>::MaybeStackArray(
  382. MaybeStackArray <T, stackCapacity>&& src) U_NOEXCEPT
  383. : ptr(src.ptr), capacity(src.capacity), needToRelease(src.needToRelease) {
  384. if (src.ptr == src.stackArray) {
  385. ptr = stackArray;
  386. uprv_memcpy(stackArray, src.stackArray, sizeof(T) * src.capacity);
  387. } else {
  388. src.resetToStackArray(); // take ownership away from src
  389. }
  390. }
  391. template<typename T, int32_t stackCapacity>
  392. inline MaybeStackArray <T, stackCapacity>&
  393. MaybeStackArray<T, stackCapacity>::operator=(MaybeStackArray <T, stackCapacity>&& src) U_NOEXCEPT {
  394. releaseArray(); // in case this instance had its own memory allocated
  395. capacity = src.capacity;
  396. needToRelease = src.needToRelease;
  397. if (src.ptr == src.stackArray) {
  398. ptr = stackArray;
  399. uprv_memcpy(stackArray, src.stackArray, sizeof(T) * src.capacity);
  400. } else {
  401. ptr = src.ptr;
  402. src.resetToStackArray(); // take ownership away from src
  403. }
  404. return *this;
  405. }
  406. template<typename T, int32_t stackCapacity>
  407. inline T *MaybeStackArray<T, stackCapacity>::resize(int32_t newCapacity, int32_t length) {
  408. if(newCapacity>0) {
  409. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  410. ::fprintf(::stderr,"MaybeStacArray (resize) alloc %d * %lu\n", newCapacity,sizeof(T));
  411. #endif
  412. T *p=(T *)uprv_malloc(newCapacity*sizeof(T));
  413. if(p!=NULL) {
  414. if(length>0) {
  415. if(length>capacity) {
  416. length=capacity;
  417. }
  418. if(length>newCapacity) {
  419. length=newCapacity;
  420. }
  421. uprv_memcpy(p, ptr, (size_t)length*sizeof(T));
  422. }
  423. releaseArray();
  424. ptr=p;
  425. capacity=newCapacity;
  426. needToRelease=TRUE;
  427. }
  428. return p;
  429. } else {
  430. return NULL;
  431. }
  432. }
  433. template<typename T, int32_t stackCapacity>
  434. inline T *MaybeStackArray<T, stackCapacity>::orphanOrClone(int32_t length, int32_t &resultCapacity) {
  435. T *p;
  436. if(needToRelease) {
  437. p=ptr;
  438. } else if(length<=0) {
  439. return NULL;
  440. } else {
  441. if(length>capacity) {
  442. length=capacity;
  443. }
  444. p=(T *)uprv_malloc(length*sizeof(T));
  445. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  446. ::fprintf(::stderr,"MaybeStacArray (orphan) alloc %d * %lu\n", length,sizeof(T));
  447. #endif
  448. if(p==NULL) {
  449. return NULL;
  450. }
  451. uprv_memcpy(p, ptr, (size_t)length*sizeof(T));
  452. }
  453. resultCapacity=length;
  454. resetToStackArray();
  455. return p;
  456. }
  457. /**
  458. * Variant of MaybeStackArray that allocates a header struct and an array
  459. * in one contiguous memory block, using uprv_malloc() and uprv_free().
  460. * Provides internal memory with fixed array capacity. Can alias another memory
  461. * block or allocate one.
  462. * The stackCapacity is the number of T items in the internal memory,
  463. * not counting the H header.
  464. * Unlike LocalMemory and LocalArray, this class never adopts
  465. * (takes ownership of) another memory block.
  466. */
  467. template<typename H, typename T, int32_t stackCapacity>
  468. class MaybeStackHeaderAndArray {
  469. public:
  470. // No heap allocation. Use only on the stack.
  471. static void* U_EXPORT2 operator new(size_t) U_NOEXCEPT = delete;
  472. static void* U_EXPORT2 operator new[](size_t) U_NOEXCEPT = delete;
  473. #if U_HAVE_PLACEMENT_NEW
  474. static void* U_EXPORT2 operator new(size_t, void*) U_NOEXCEPT = delete;
  475. #endif
  476. /**
  477. * Default constructor initializes with internal H+T[stackCapacity] buffer.
  478. */
  479. MaybeStackHeaderAndArray() : ptr(&stackHeader), capacity(stackCapacity), needToRelease(FALSE) {}
  480. /**
  481. * Destructor deletes the memory (if owned).
  482. */
  483. ~MaybeStackHeaderAndArray() { releaseMemory(); }
  484. /**
  485. * Returns the array capacity (number of T items).
  486. * @return array capacity
  487. */
  488. int32_t getCapacity() const { return capacity; }
  489. /**
  490. * Access without ownership change.
  491. * @return the header pointer
  492. */
  493. H *getAlias() const { return ptr; }
  494. /**
  495. * Returns the array start.
  496. * @return array start, same address as getAlias()+1
  497. */
  498. T *getArrayStart() const { return reinterpret_cast<T *>(getAlias()+1); }
  499. /**
  500. * Returns the array limit.
  501. * @return array limit
  502. */
  503. T *getArrayLimit() const { return getArrayStart()+capacity; }
  504. /**
  505. * Access without ownership change. Same as getAlias().
  506. * A class instance can be used directly in expressions that take a T *.
  507. * @return the header pointer
  508. */
  509. operator H *() const { return ptr; }
  510. /**
  511. * Array item access (writable).
  512. * No index bounds check.
  513. * @param i array index
  514. * @return reference to the array item
  515. */
  516. T &operator[](ptrdiff_t i) { return getArrayStart()[i]; }
  517. /**
  518. * Deletes the memory block (if owned) and aliases another one, no transfer of ownership.
  519. * If the arguments are illegal, then the current memory is unchanged.
  520. * @param otherArray must not be NULL
  521. * @param otherCapacity must be >0
  522. */
  523. void aliasInstead(H *otherMemory, int32_t otherCapacity) {
  524. if(otherMemory!=NULL && otherCapacity>0) {
  525. releaseMemory();
  526. ptr=otherMemory;
  527. capacity=otherCapacity;
  528. needToRelease=FALSE;
  529. }
  530. }
  531. /**
  532. * Deletes the memory block (if owned) and allocates a new one,
  533. * copying the header and length T array items.
  534. * Returns the new header pointer.
  535. * If the allocation fails, then the current memory is unchanged and
  536. * this method returns NULL.
  537. * @param newCapacity can be less than or greater than the current capacity;
  538. * must be >0
  539. * @param length number of T items to be copied from the old array to the new one
  540. * @return the allocated pointer, or NULL if the allocation failed
  541. */
  542. inline H *resize(int32_t newCapacity, int32_t length=0);
  543. /**
  544. * Gives up ownership of the memory if owned, or else clones it,
  545. * copying the header and length T array items; resets itself to the internal memory.
  546. * Returns NULL if the allocation failed.
  547. * @param length number of T items to copy when cloning,
  548. * and array capacity of the clone when cloning
  549. * @param resultCapacity will be set to the returned array's capacity (output-only)
  550. * @return the header pointer;
  551. * caller becomes responsible for deleting the array
  552. */
  553. inline H *orphanOrClone(int32_t length, int32_t &resultCapacity);
  554. private:
  555. H *ptr;
  556. int32_t capacity;
  557. UBool needToRelease;
  558. // stackHeader must precede stackArray immediately.
  559. H stackHeader;
  560. T stackArray[stackCapacity];
  561. void releaseMemory() {
  562. if(needToRelease) {
  563. uprv_free(ptr);
  564. }
  565. }
  566. /* No comparison operators with other MaybeStackHeaderAndArray's. */
  567. bool operator==(const MaybeStackHeaderAndArray & /*other*/) {return FALSE;}
  568. bool operator!=(const MaybeStackHeaderAndArray & /*other*/) {return TRUE;}
  569. /* No ownership transfer: No copy constructor, no assignment operator. */
  570. MaybeStackHeaderAndArray(const MaybeStackHeaderAndArray & /*other*/) {}
  571. void operator=(const MaybeStackHeaderAndArray & /*other*/) {}
  572. };
  573. template<typename H, typename T, int32_t stackCapacity>
  574. inline H *MaybeStackHeaderAndArray<H, T, stackCapacity>::resize(int32_t newCapacity,
  575. int32_t length) {
  576. if(newCapacity>=0) {
  577. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  578. ::fprintf(::stderr,"MaybeStackHeaderAndArray alloc %d + %d * %ul\n", sizeof(H),newCapacity,sizeof(T));
  579. #endif
  580. H *p=(H *)uprv_malloc(sizeof(H)+newCapacity*sizeof(T));
  581. if(p!=NULL) {
  582. if(length<0) {
  583. length=0;
  584. } else if(length>0) {
  585. if(length>capacity) {
  586. length=capacity;
  587. }
  588. if(length>newCapacity) {
  589. length=newCapacity;
  590. }
  591. }
  592. uprv_memcpy(p, ptr, sizeof(H)+(size_t)length*sizeof(T));
  593. releaseMemory();
  594. ptr=p;
  595. capacity=newCapacity;
  596. needToRelease=TRUE;
  597. }
  598. return p;
  599. } else {
  600. return NULL;
  601. }
  602. }
  603. template<typename H, typename T, int32_t stackCapacity>
  604. inline H *MaybeStackHeaderAndArray<H, T, stackCapacity>::orphanOrClone(int32_t length,
  605. int32_t &resultCapacity) {
  606. H *p;
  607. if(needToRelease) {
  608. p=ptr;
  609. } else {
  610. if(length<0) {
  611. length=0;
  612. } else if(length>capacity) {
  613. length=capacity;
  614. }
  615. #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
  616. ::fprintf(::stderr,"MaybeStackHeaderAndArray (orphan) alloc %ul + %d * %lu\n", sizeof(H),length,sizeof(T));
  617. #endif
  618. p=(H *)uprv_malloc(sizeof(H)+length*sizeof(T));
  619. if(p==NULL) {
  620. return NULL;
  621. }
  622. uprv_memcpy(p, ptr, sizeof(H)+(size_t)length*sizeof(T));
  623. }
  624. resultCapacity=length;
  625. ptr=&stackHeader;
  626. capacity=stackCapacity;
  627. needToRelease=FALSE;
  628. return p;
  629. }
  630. /**
  631. * A simple memory management class that creates new heap allocated objects (of
  632. * any class that has a public constructor), keeps track of them and eventually
  633. * deletes them all in its own destructor.
  634. *
  635. * A typical use-case would be code like this:
  636. *
  637. * MemoryPool<MyType> pool;
  638. *
  639. * MyType* o1 = pool.create();
  640. * if (o1 != nullptr) {
  641. * foo(o1);
  642. * }
  643. *
  644. * MyType* o2 = pool.create(1, 2, 3);
  645. * if (o2 != nullptr) {
  646. * bar(o2);
  647. * }
  648. *
  649. * // MemoryPool will take care of deleting the MyType objects.
  650. *
  651. * It doesn't do anything more than that, and is intentionally kept minimalist.
  652. */
  653. template<typename T, int32_t stackCapacity = 8>
  654. class MemoryPool : public UMemory {
  655. public:
  656. MemoryPool() : fCount(0), fPool() {}
  657. ~MemoryPool() {
  658. for (int32_t i = 0; i < fCount; ++i) {
  659. delete fPool[i];
  660. }
  661. }
  662. MemoryPool(const MemoryPool&) = delete;
  663. MemoryPool& operator=(const MemoryPool&) = delete;
  664. MemoryPool(MemoryPool&& other) U_NOEXCEPT : fCount(other.fCount),
  665. fPool(std::move(other.fPool)) {
  666. other.fCount = 0;
  667. }
  668. MemoryPool& operator=(MemoryPool&& other) U_NOEXCEPT {
  669. fCount = other.fCount;
  670. fPool = std::move(other.fPool);
  671. other.fCount = 0;
  672. return *this;
  673. }
  674. /**
  675. * Creates a new object of typename T, by forwarding any and all arguments
  676. * to the typename T constructor.
  677. *
  678. * @param args Arguments to be forwarded to the typename T constructor.
  679. * @return A pointer to the newly created object, or nullptr on error.
  680. */
  681. template<typename... Args>
  682. T* create(Args&&... args) {
  683. int32_t capacity = fPool.getCapacity();
  684. if (fCount == capacity &&
  685. fPool.resize(capacity == stackCapacity ? 4 * capacity : 2 * capacity,
  686. capacity) == nullptr) {
  687. return nullptr;
  688. }
  689. return fPool[fCount++] = new T(std::forward<Args>(args)...);
  690. }
  691. /**
  692. * @return Number of elements that have been allocated.
  693. */
  694. int32_t count() const {
  695. return fCount;
  696. }
  697. protected:
  698. int32_t fCount;
  699. MaybeStackArray<T*, stackCapacity> fPool;
  700. };
  701. /**
  702. * An internal Vector-like implementation based on MemoryPool.
  703. *
  704. * Heap-allocates each element and stores pointers.
  705. *
  706. * To append an item to the vector, use emplaceBack.
  707. *
  708. * MaybeStackVector<MyType> vector;
  709. * MyType* element = vector.emplaceBack();
  710. * if (!element) {
  711. * status = U_MEMORY_ALLOCATION_ERROR;
  712. * }
  713. * // do stuff with element
  714. *
  715. * To loop over the vector, use a for loop with indices:
  716. *
  717. * for (int32_t i = 0; i < vector.length(); i++) {
  718. * MyType* element = vector[i];
  719. * }
  720. */
  721. template<typename T, int32_t stackCapacity = 8>
  722. class MaybeStackVector : protected MemoryPool<T, stackCapacity> {
  723. public:
  724. using MemoryPool<T, stackCapacity>::MemoryPool;
  725. using MemoryPool<T, stackCapacity>::operator=;
  726. template<typename... Args>
  727. T* emplaceBack(Args&&... args) {
  728. return this->create(args...);
  729. }
  730. int32_t length() const {
  731. return this->fCount;
  732. }
  733. T** getAlias() {
  734. return this->fPool.getAlias();
  735. }
  736. /**
  737. * Array item access (read-only).
  738. * No index bounds check.
  739. * @param i array index
  740. * @return reference to the array item
  741. */
  742. const T* operator[](ptrdiff_t i) const {
  743. return this->fPool[i];
  744. }
  745. /**
  746. * Array item access (writable).
  747. * No index bounds check.
  748. * @param i array index
  749. * @return reference to the array item
  750. */
  751. T* operator[](ptrdiff_t i) {
  752. return this->fPool[i];
  753. }
  754. /**
  755. * Append all the items from another MaybeStackVector to this one.
  756. */
  757. void appendAll(const MaybeStackVector& other, UErrorCode& status) {
  758. for (int32_t i = 0; i < other.fCount; i++) {
  759. T* item = emplaceBack(*other[i]);
  760. if (!item) {
  761. status = U_MEMORY_ALLOCATION_ERROR;
  762. return;
  763. }
  764. }
  765. }
  766. };
  767. U_NAMESPACE_END
  768. #endif /* __cplusplus */
  769. #endif /* CMEMORY_H */