@@ -64,38 +64,37 @@ uprv_free(void *mem);
6464U_CAPI void  * U_EXPORT2
6565uprv_calloc (size_t  num, size_t  size) U_MALLOC_ATTR U_ALLOC_SIZE_ATTR2(1 ,2 );
6666
67- /* *
68-  * This should align the memory properly on any machine. 
69-  * This is very useful for the safeClone functions. 
70-  */  
71- typedef  union  {
72-     long     t1;
73-     double   t2;
74-     void    *t3;
75- } UAlignedMemory;
76- 
7767/* *
7868 * Get the least significant bits of a pointer (a memory address). 
7969 * For example, with a mask of 3, the macro gets the 2 least significant bits, 
8070 * which will be 0 if the pointer is 32-bit (4-byte) aligned. 
8171 * 
82-  * ptrdiff_t is the most appropriate integer type to cast to. 
83-  * size_t should work too, since on most (or all?) platforms it has the same 
84-  * width as ptrdiff_t. 
72+  * uintptr_t is the most appropriate integer type to cast to. 
8573 */  
86- #define  U_POINTER_MASK_LSB (ptr, mask ) ((( ptrdiff_t )( char  *)( ptr) ) & (mask))
74+ #define  U_POINTER_MASK_LSB (ptr, mask ) ((uintptr_t )( ptr) & (mask))
8775
8876/* *
89-  * Get the amount of bytes that a pointer is off by from 
90-  * the previous UAlignedMemory-aligned pointer. 
91-  */  
92- #define  U_ALIGNMENT_OFFSET (ptr ) U_POINTER_MASK_LSB(ptr, sizeof (UAlignedMemory) - 1 )
93- 
94- /* *
95-  * Get the amount of bytes to add to a pointer 
96-  * in order to get the next UAlignedMemory-aligned address. 
77+  * Create & return an instance of "type" in statically allocated storage. 
78+  * e.g. 
79+  *    static std::mutex *myMutex = STATIC_NEW(std::mutex); 
80+  * To destroy an object created in this way, invoke the destructor explicitly, e.g. 
81+  *    myMutex->~mutex(); 
82+  * DO NOT use delete. 
83+  * DO NOT use with class UMutex, which has specific support for static instances. 
84+  * 
85+  * STATIC_NEW is intended for use when 
86+  *   - We want a static (or global) object. 
87+  *   - We don't want it to ever be destructed, or to explicitly control destruction, 
88+  *     to avoid use-after-destruction problems. 
89+  *   - We want to avoid an ordinary heap allocated object, 
90+  *     to avoid the possibility of memory allocation failures, and 
91+  *     to avoid memory leak reports, from valgrind, for example. 
92+  * This is defined as a macro rather than a template function because each invocation 
93+  * must define distinct static storage for the object being returned. 
9794 */  
98- #define  U_ALIGNMENT_OFFSET_UP (ptr ) (sizeof (UAlignedMemory) - U_ALIGNMENT_OFFSET(ptr))
95+ #define  STATIC_NEW (type ) [] () { \
96+     alignas (type) static  char  storage[sizeof (type)]; \
97+     return  new (storage) type ();} ()
9998
10099/* *
101100  *  Heap clean up function, called from u_cleanup() 
0 commit comments