]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/cmemory.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 1997-2016, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
13 * Contains stdlib.h/string.h memory functions
15 * @author Bertrand A. Damiba
17 * Modification History:
19 * Date Name Description
20 * 6/20/98 Bertrand Created.
21 * 05/03/99 stephen Changed from functions to macros.
23 ******************************************************************************
29 #include "unicode/utypes.h"
33 #include "unicode/localpointer.h"
35 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
42 * The C++ standard requires that the source pointer for memcpy() & memmove()
43 * is valid, not NULL, and not at the end of an allocated memory block.
44 * In debug mode, we read one byte from the source point to verify that it's
45 * a valid, readable pointer.
48 U_CAPI
void uprv_checkValidMemory(const void *p
, size_t n
);
50 #define uprv_memcpy(dst, src, size) ( \
51 uprv_checkValidMemory(src, 1), \
52 U_STANDARD_CPP_NAMESPACE memcpy(dst, src, size))
53 #define uprv_memmove(dst, src, size) ( \
54 uprv_checkValidMemory(src, 1), \
55 U_STANDARD_CPP_NAMESPACE memmove(dst, src, size))
59 #define uprv_memcpy(dst, src, size) U_STANDARD_CPP_NAMESPACE memcpy(dst, src, size)
60 #define uprv_memmove(dst, src, size) U_STANDARD_CPP_NAMESPACE memmove(dst, src, size)
66 * Convenience macro to determine the length of a fixed array at compile-time.
67 * @param array A fixed length array
68 * @return The length of the array, in elements
71 #define UPRV_LENGTHOF(array) (int32_t)(sizeof(array)/sizeof((array)[0]))
72 #define uprv_memset(buffer, mark, size) U_STANDARD_CPP_NAMESPACE memset(buffer, mark, size)
73 #define uprv_memcmp(buffer1, buffer2, size) U_STANDARD_CPP_NAMESPACE memcmp(buffer1, buffer2,size)
75 U_CAPI
void * U_EXPORT2
76 uprv_malloc(size_t s
) U_MALLOC_ATTR
U_ALLOC_SIZE_ATTR(1);
78 U_CAPI
void * U_EXPORT2
79 uprv_realloc(void *mem
, size_t size
) U_ALLOC_SIZE_ATTR(2);
84 U_CAPI
void * U_EXPORT2
85 uprv_calloc(size_t num
, size_t size
) U_MALLOC_ATTR
U_ALLOC_SIZE_ATTR2(1,2);
88 * This should align the memory properly on any machine.
89 * This is very useful for the safeClone functions.
98 * Get the least significant bits of a pointer (a memory address).
99 * For example, with a mask of 3, the macro gets the 2 least significant bits,
100 * which will be 0 if the pointer is 32-bit (4-byte) aligned.
102 * ptrdiff_t is the most appropriate integer type to cast to.
103 * size_t should work too, since on most (or all?) platforms it has the same
104 * width as ptrdiff_t.
106 #define U_POINTER_MASK_LSB(ptr, mask) (((ptrdiff_t)(char *)(ptr)) & (mask))
109 * Get the amount of bytes that a pointer is off by from
110 * the previous UAlignedMemory-aligned pointer.
112 #define U_ALIGNMENT_OFFSET(ptr) U_POINTER_MASK_LSB(ptr, sizeof(UAlignedMemory) - 1)
115 * Get the amount of bytes to add to a pointer
116 * in order to get the next UAlignedMemory-aligned address.
118 #define U_ALIGNMENT_OFFSET_UP(ptr) (sizeof(UAlignedMemory) - U_ALIGNMENT_OFFSET(ptr))
121 * Heap clean up function, called from u_cleanup()
122 * Clears any user heap functions from u_setMemoryFunctions()
123 * Does NOT deallocate any remaining allocated memory.
126 cmemory_cleanup(void);
129 * A function called by <TT>uhash_remove</TT>,
130 * <TT>uhash_close</TT>, or <TT>uhash_put</TT> to delete
131 * an existing key or value.
132 * @param obj A key or value stored in a hashtable
133 * @see uprv_deleteUObject
135 typedef void U_CALLCONV
UObjectDeleter(void* obj
);
138 * Deleter for UObject instances.
139 * Works for all subclasses of UObject because it has a virtual destructor.
141 U_CAPI
void U_EXPORT2
142 uprv_deleteUObject(void *obj
);
149 * "Smart pointer" class, deletes memory via uprv_free().
150 * For most methods see the LocalPointerBase base class.
151 * Adds operator[] for array item access.
153 * @see LocalPointerBase
156 class LocalMemory
: public LocalPointerBase
<T
> {
158 using LocalPointerBase
<T
>::operator*;
159 using LocalPointerBase
<T
>::operator->;
161 * Constructor takes ownership.
162 * @param p simple pointer to an array of T items that is adopted
164 explicit LocalMemory(T
*p
=NULL
) : LocalPointerBase
<T
>(p
) {}
165 #if U_HAVE_RVALUE_REFERENCES
167 * Move constructor, leaves src with isNull().
168 * @param src source smart pointer
170 LocalMemory(LocalMemory
<T
> &&src
) U_NOEXCEPT
: LocalPointerBase
<T
>(src
.ptr
) {
175 * Destructor deletes the memory it owns.
178 uprv_free(LocalPointerBase
<T
>::ptr
);
180 #if U_HAVE_RVALUE_REFERENCES
182 * Move assignment operator, leaves src with isNull().
183 * The behavior is undefined if *this and src are the same object.
184 * @param src source smart pointer
187 LocalMemory
<T
> &operator=(LocalMemory
<T
> &&src
) U_NOEXCEPT
{
188 return moveFrom(src
);
192 * Move assignment, leaves src with isNull().
193 * The behavior is undefined if *this and src are the same object.
195 * Can be called explicitly, does not need C++11 support.
196 * @param src source smart pointer
199 LocalMemory
<T
> &moveFrom(LocalMemory
<T
> &src
) U_NOEXCEPT
{
200 delete[] LocalPointerBase
<T
>::ptr
;
201 LocalPointerBase
<T
>::ptr
=src
.ptr
;
207 * @param other other smart pointer
209 void swap(LocalMemory
<T
> &other
) U_NOEXCEPT
{
210 T
*temp
=LocalPointerBase
<T
>::ptr
;
211 LocalPointerBase
<T
>::ptr
=other
.ptr
;
215 * Non-member LocalMemory swap function.
216 * @param p1 will get p2's pointer
217 * @param p2 will get p1's pointer
219 friend inline void swap(LocalMemory
<T
> &p1
, LocalMemory
<T
> &p2
) U_NOEXCEPT
{
223 * Deletes the array it owns,
224 * and adopts (takes ownership of) the one passed in.
225 * @param p simple pointer to an array of T items that is adopted
227 void adoptInstead(T
*p
) {
228 uprv_free(LocalPointerBase
<T
>::ptr
);
229 LocalPointerBase
<T
>::ptr
=p
;
232 * Deletes the array it owns, allocates a new one and reset its bytes to 0.
233 * Returns the new array pointer.
234 * If the allocation fails, then the current array is unchanged and
235 * this method returns NULL.
236 * @param newCapacity must be >0
237 * @return the allocated array pointer, or NULL if the allocation failed
239 inline T
*allocateInsteadAndReset(int32_t newCapacity
=1);
241 * Deletes the array it owns and allocates a new one, copying length T items.
242 * Returns the new array pointer.
243 * If the allocation fails, then the current array is unchanged and
244 * this method returns NULL.
245 * @param newCapacity must be >0
246 * @param length number of T items to be copied from the old array to the new one;
247 * must be no more than the capacity of the old array,
248 * which the caller must track because the LocalMemory does not track it
249 * @return the allocated array pointer, or NULL if the allocation failed
251 inline T
*allocateInsteadAndCopy(int32_t newCapacity
=1, int32_t length
=0);
253 * Array item access (writable).
254 * No index bounds check.
255 * @param i array index
256 * @return reference to the array item
258 T
&operator[](ptrdiff_t i
) const { return LocalPointerBase
<T
>::ptr
[i
]; }
262 inline T
*LocalMemory
<T
>::allocateInsteadAndReset(int32_t newCapacity
) {
264 T
*p
=(T
*)uprv_malloc(newCapacity
*sizeof(T
));
266 uprv_memset(p
, 0, newCapacity
*sizeof(T
));
267 uprv_free(LocalPointerBase
<T
>::ptr
);
268 LocalPointerBase
<T
>::ptr
=p
;
278 inline T
*LocalMemory
<T
>::allocateInsteadAndCopy(int32_t newCapacity
, int32_t length
) {
280 T
*p
=(T
*)uprv_malloc(newCapacity
*sizeof(T
));
283 if(length
>newCapacity
) {
286 uprv_memcpy(p
, LocalPointerBase
<T
>::ptr
, (size_t)length
*sizeof(T
));
288 uprv_free(LocalPointerBase
<T
>::ptr
);
289 LocalPointerBase
<T
>::ptr
=p
;
298 * Simple array/buffer management class using uprv_malloc() and uprv_free().
299 * Provides an internal array with fixed capacity. Can alias another array
302 * The array address is properly aligned for type T. It might not be properly
303 * aligned for types larger than T (or larger than the largest subtype of T).
305 * Unlike LocalMemory and LocalArray, this class never adopts
306 * (takes ownership of) another array.
308 template<typename T
, int32_t stackCapacity
>
309 class MaybeStackArray
{
312 * Default constructor initializes with internal T[stackCapacity] buffer.
314 MaybeStackArray() : ptr(stackArray
), capacity(stackCapacity
), needToRelease(FALSE
) {}
316 * Destructor deletes the array (if owned).
318 ~MaybeStackArray() { releaseArray(); }
320 * Returns the array capacity (number of T items).
321 * @return array capacity
323 int32_t getCapacity() const { return capacity
; }
325 * Access without ownership change.
326 * @return the array pointer
328 T
*getAlias() const { return ptr
; }
330 * Returns the array limit. Simple convenience method.
331 * @return getAlias()+getCapacity()
333 T
*getArrayLimit() const { return getAlias()+capacity
; }
334 // No "operator T *() const" because that can make
335 // expressions like mbs[index] ambiguous for some compilers.
337 * Array item access (const).
338 * No index bounds check.
339 * @param i array index
340 * @return reference to the array item
342 const T
&operator[](ptrdiff_t i
) const { return ptr
[i
]; }
344 * Array item access (writable).
345 * No index bounds check.
346 * @param i array index
347 * @return reference to the array item
349 T
&operator[](ptrdiff_t i
) { return ptr
[i
]; }
351 * Deletes the array (if owned) and aliases another one, no transfer of ownership.
352 * If the arguments are illegal, then the current array is unchanged.
353 * @param otherArray must not be NULL
354 * @param otherCapacity must be >0
356 void aliasInstead(T
*otherArray
, int32_t otherCapacity
) {
357 if(otherArray
!=NULL
&& otherCapacity
>0) {
360 capacity
=otherCapacity
;
365 * Deletes the array (if owned) and allocates a new one, copying length T items.
366 * Returns the new array pointer.
367 * If the allocation fails, then the current array is unchanged and
368 * this method returns NULL.
369 * @param newCapacity can be less than or greater than the current capacity;
371 * @param length number of T items to be copied from the old array to the new one
372 * @return the allocated array pointer, or NULL if the allocation failed
374 inline T
*resize(int32_t newCapacity
, int32_t length
=0);
376 * Gives up ownership of the array if owned, or else clones it,
377 * copying length T items; resets itself to the internal stack array.
378 * Returns NULL if the allocation failed.
379 * @param length number of T items to copy when cloning,
380 * and capacity of the clone when cloning
381 * @param resultCapacity will be set to the returned array's capacity (output-only)
382 * @return the array pointer;
383 * caller becomes responsible for deleting the array
385 inline T
*orphanOrClone(int32_t length
, int32_t &resultCapacity
);
390 T stackArray
[stackCapacity
];
391 void releaseArray() {
396 /* No comparison operators with other MaybeStackArray's. */
397 bool operator==(const MaybeStackArray
& /*other*/) {return FALSE
;}
398 bool operator!=(const MaybeStackArray
& /*other*/) {return TRUE
;}
399 /* No ownership transfer: No copy constructor, no assignment operator. */
400 MaybeStackArray(const MaybeStackArray
& /*other*/) {}
401 void operator=(const MaybeStackArray
& /*other*/) {}
403 // No heap allocation. Use only on the stack.
404 // (Declaring these functions private triggers a cascade of problems:
405 // MSVC insists on exporting an instantiation of MaybeStackArray, which
406 // requires that all functions be defined.
407 // An empty implementation of new() is rejected, it must return a value.
408 // Returning NULL is rejected by gcc for operator new.
409 // The expedient thing is just not to override operator new.
410 // While relatively pointless, heap allocated instances will function.
411 // static void * U_EXPORT2 operator new(size_t size);
412 // static void * U_EXPORT2 operator new[](size_t size);
413 #if U_HAVE_PLACEMENT_NEW
414 // static void * U_EXPORT2 operator new(size_t, void *ptr);
418 template<typename T
, int32_t stackCapacity
>
419 inline T
*MaybeStackArray
<T
, stackCapacity
>::resize(int32_t newCapacity
, int32_t length
) {
421 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
422 ::fprintf(::stderr
,"MaybeStacArray (resize) alloc %d * %lu\n", newCapacity
,sizeof(T
));
424 T
*p
=(T
*)uprv_malloc(newCapacity
*sizeof(T
));
427 if(length
>capacity
) {
430 if(length
>newCapacity
) {
433 uprv_memcpy(p
, ptr
, (size_t)length
*sizeof(T
));
437 capacity
=newCapacity
;
446 template<typename T
, int32_t stackCapacity
>
447 inline T
*MaybeStackArray
<T
, stackCapacity
>::orphanOrClone(int32_t length
, int32_t &resultCapacity
) {
451 } else if(length
<=0) {
454 if(length
>capacity
) {
457 p
=(T
*)uprv_malloc(length
*sizeof(T
));
458 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
459 ::fprintf(::stderr
,"MaybeStacArray (orphan) alloc %d * %lu\n", length
,sizeof(T
));
464 uprv_memcpy(p
, ptr
, (size_t)length
*sizeof(T
));
466 resultCapacity
=length
;
468 capacity
=stackCapacity
;
474 * Variant of MaybeStackArray that allocates a header struct and an array
475 * in one contiguous memory block, using uprv_malloc() and uprv_free().
476 * Provides internal memory with fixed array capacity. Can alias another memory
477 * block or allocate one.
478 * The stackCapacity is the number of T items in the internal memory,
479 * not counting the H header.
480 * Unlike LocalMemory and LocalArray, this class never adopts
481 * (takes ownership of) another memory block.
483 template<typename H
, typename T
, int32_t stackCapacity
>
484 class MaybeStackHeaderAndArray
{
487 * Default constructor initializes with internal H+T[stackCapacity] buffer.
489 MaybeStackHeaderAndArray() : ptr(&stackHeader
), capacity(stackCapacity
), needToRelease(FALSE
) {}
491 * Destructor deletes the memory (if owned).
493 ~MaybeStackHeaderAndArray() { releaseMemory(); }
495 * Returns the array capacity (number of T items).
496 * @return array capacity
498 int32_t getCapacity() const { return capacity
; }
500 * Access without ownership change.
501 * @return the header pointer
503 H
*getAlias() const { return ptr
; }
505 * Returns the array start.
506 * @return array start, same address as getAlias()+1
508 T
*getArrayStart() const { return reinterpret_cast<T
*>(getAlias()+1); }
510 * Returns the array limit.
511 * @return array limit
513 T
*getArrayLimit() const { return getArrayStart()+capacity
; }
515 * Access without ownership change. Same as getAlias().
516 * A class instance can be used directly in expressions that take a T *.
517 * @return the header pointer
519 operator H
*() const { return ptr
; }
521 * Array item access (writable).
522 * No index bounds check.
523 * @param i array index
524 * @return reference to the array item
526 T
&operator[](ptrdiff_t i
) { return getArrayStart()[i
]; }
528 * Deletes the memory block (if owned) and aliases another one, no transfer of ownership.
529 * If the arguments are illegal, then the current memory is unchanged.
530 * @param otherArray must not be NULL
531 * @param otherCapacity must be >0
533 void aliasInstead(H
*otherMemory
, int32_t otherCapacity
) {
534 if(otherMemory
!=NULL
&& otherCapacity
>0) {
537 capacity
=otherCapacity
;
542 * Deletes the memory block (if owned) and allocates a new one,
543 * copying the header and length T array items.
544 * Returns the new header pointer.
545 * If the allocation fails, then the current memory is unchanged and
546 * this method returns NULL.
547 * @param newCapacity can be less than or greater than the current capacity;
549 * @param length number of T items to be copied from the old array to the new one
550 * @return the allocated pointer, or NULL if the allocation failed
552 inline H
*resize(int32_t newCapacity
, int32_t length
=0);
554 * Gives up ownership of the memory if owned, or else clones it,
555 * copying the header and length T array items; resets itself to the internal memory.
556 * Returns NULL if the allocation failed.
557 * @param length number of T items to copy when cloning,
558 * and array capacity of the clone when cloning
559 * @param resultCapacity will be set to the returned array's capacity (output-only)
560 * @return the header pointer;
561 * caller becomes responsible for deleting the array
563 inline H
*orphanOrClone(int32_t length
, int32_t &resultCapacity
);
568 // stackHeader must precede stackArray immediately.
570 T stackArray
[stackCapacity
];
571 void releaseMemory() {
576 /* No comparison operators with other MaybeStackHeaderAndArray's. */
577 bool operator==(const MaybeStackHeaderAndArray
& /*other*/) {return FALSE
;}
578 bool operator!=(const MaybeStackHeaderAndArray
& /*other*/) {return TRUE
;}
579 /* No ownership transfer: No copy constructor, no assignment operator. */
580 MaybeStackHeaderAndArray(const MaybeStackHeaderAndArray
& /*other*/) {}
581 void operator=(const MaybeStackHeaderAndArray
& /*other*/) {}
583 // No heap allocation. Use only on the stack.
584 // (Declaring these functions private triggers a cascade of problems;
585 // see the MaybeStackArray class for details.)
586 // static void * U_EXPORT2 operator new(size_t size);
587 // static void * U_EXPORT2 operator new[](size_t size);
588 #if U_HAVE_PLACEMENT_NEW
589 // static void * U_EXPORT2 operator new(size_t, void *ptr);
593 template<typename H
, typename T
, int32_t stackCapacity
>
594 inline H
*MaybeStackHeaderAndArray
<H
, T
, stackCapacity
>::resize(int32_t newCapacity
,
597 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
598 ::fprintf(::stderr
,"MaybeStackHeaderAndArray alloc %d + %d * %ul\n", sizeof(H
),newCapacity
,sizeof(T
));
600 H
*p
=(H
*)uprv_malloc(sizeof(H
)+newCapacity
*sizeof(T
));
604 } else if(length
>0) {
605 if(length
>capacity
) {
608 if(length
>newCapacity
) {
612 uprv_memcpy(p
, ptr
, sizeof(H
)+(size_t)length
*sizeof(T
));
615 capacity
=newCapacity
;
624 template<typename H
, typename T
, int32_t stackCapacity
>
625 inline H
*MaybeStackHeaderAndArray
<H
, T
, stackCapacity
>::orphanOrClone(int32_t length
,
626 int32_t &resultCapacity
) {
633 } else if(length
>capacity
) {
636 #if U_DEBUG && defined(UPRV_MALLOC_COUNT)
637 ::fprintf(::stderr
,"MaybeStackHeaderAndArray (orphan) alloc %ul + %d * %lu\n", sizeof(H
),length
,sizeof(T
));
639 p
=(H
*)uprv_malloc(sizeof(H
)+length
*sizeof(T
));
643 uprv_memcpy(p
, ptr
, sizeof(H
)+(size_t)length
*sizeof(T
));
645 resultCapacity
=length
;
647 capacity
=stackCapacity
;
654 #endif /* __cplusplus */
655 #endif /* CMEMORY_H */