From 3ac2da2044a902f3c21f84fdd0d119032dd5f3ad Mon Sep 17 00:00:00 2001 From: Andy Heninger Date: Fri, 26 Jul 2019 14:14:59 -0700 Subject: [PATCH] ICU-20048 Remove use of std::align() for compat with g++ 4.9. --- icu4c/source/common/ucnv.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/icu4c/source/common/ucnv.cpp b/icu4c/source/common/ucnv.cpp index 8f94a2834cc..856be708785 100644 --- a/icu4c/source/common/ucnv.cpp +++ b/icu4c/source/common/ucnv.cpp @@ -225,13 +225,16 @@ ucnv_safeClone(const UConverter* cnv, void *stackBuffer, int32_t *pBufferSize, U } } - /* Adjust (if necessary) the stackBuffer pointer to be aligned correctly for a UConverter. + * TODO(Jira ICU-20736) Redo this using std::align() once g++4.9 compatibility is no longer needed. */ - size_t altStackBufferSize = stackBufferSize; // Incompatible types for passing by reference. if (stackBuffer) { - if (std::align(alignof(UConverter), bufferSizeNeeded, stackBuffer, altStackBufferSize)) { - stackBufferSize = static_cast(altStackBufferSize); + uintptr_t p = reinterpret_cast(stackBuffer); + uintptr_t aligned_p = (p + alignof(UConverter) - 1) & ~(alignof(UConverter) - 1); + ptrdiff_t pointerAdjustment = aligned_p - p; + if (bufferSizeNeeded + pointerAdjustment <= stackBufferSize) { + stackBuffer = reinterpret_cast(aligned_p); + stackBufferSize -= pointerAdjustment; } else { /* prevent using the stack buffer but keep the size > 0 so that we do not just preflight */ stackBufferSize = 1;