@@ -64,38 +64,37 @@ uprv_free(void *mem);
64
64
U_CAPI void * U_EXPORT2
65
65
uprv_calloc (size_t num, size_t size) U_MALLOC_ATTR U_ALLOC_SIZE_ATTR2(1 ,2 );
66
66
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
-
77
67
/* *
78
68
* Get the least significant bits of a pointer (a memory address).
79
69
* For example, with a mask of 3, the macro gets the 2 least significant bits,
80
70
* which will be 0 if the pointer is 32-bit (4-byte) aligned.
81
71
*
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.
85
73
*/
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))
87
75
88
76
/* *
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.
97
94
*/
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 ();} ()
99
98
100
99
/* *
101
100
* Heap clean up function, called from u_cleanup()
0 commit comments