Skip to content

Commit

Permalink
ICU-20875 Include <cstddef> for max_align_t
Browse files Browse the repository at this point in the history
The definition of max_align_t is not guaranteed to be available unless
the appropriate header is included. Since use of <stddef.h> from C++ is
deprecated, that's <cstddef>, and max_align_t is thus defined under the
std namespace rather than in the global namespace.
  • Loading branch information
jmroot authored and jefgen committed Jan 9, 2020
1 parent 09d409f commit a3078fb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
8 changes: 5 additions & 3 deletions icu4c/source/common/uarrsort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* Internal function for sorting arrays.
*/

#include <cstddef>

#include "unicode/utypes.h"
#include "cmemory.h"
#include "uarrsort.h"
Expand All @@ -35,7 +37,7 @@ enum {
};

static constexpr int32_t sizeInMaxAlignTs(int32_t sizeInBytes) {
return (sizeInBytes + sizeof(max_align_t) - 1) / sizeof(max_align_t);
return (sizeInBytes + sizeof(std::max_align_t) - 1) / sizeof(std::max_align_t);
}

/* UComparator convenience implementations ---------------------------------- */
Expand Down Expand Up @@ -139,7 +141,7 @@ static void
insertionSort(char *array, int32_t length, int32_t itemSize,
UComparator *cmp, const void *context, UErrorCode *pErrorCode) {

icu::MaybeStackArray<max_align_t, sizeInMaxAlignTs(STACK_ITEM_SIZE)> v;
icu::MaybeStackArray<std::max_align_t, sizeInMaxAlignTs(STACK_ITEM_SIZE)> v;
if (sizeInMaxAlignTs(itemSize) > v.getCapacity() &&
v.resize(sizeInMaxAlignTs(itemSize)) == nullptr) {
*pErrorCode = U_MEMORY_ALLOCATION_ERROR;
Expand Down Expand Up @@ -233,7 +235,7 @@ static void
quickSort(char *array, int32_t length, int32_t itemSize,
UComparator *cmp, const void *context, UErrorCode *pErrorCode) {
/* allocate two intermediate item variables (x and w) */
icu::MaybeStackArray<max_align_t, sizeInMaxAlignTs(STACK_ITEM_SIZE) * 2> xw;
icu::MaybeStackArray<std::max_align_t, sizeInMaxAlignTs(STACK_ITEM_SIZE) * 2> xw;
if(sizeInMaxAlignTs(itemSize)*2 > xw.getCapacity() &&
xw.resize(sizeInMaxAlignTs(itemSize) * 2) == nullptr) {
*pErrorCode=U_MEMORY_ALLOCATION_ERROR;
Expand Down
8 changes: 5 additions & 3 deletions icu4c/source/common/utext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* created by: Markus W. Scherer
*/

#include <cstddef>

#include "unicode/utypes.h"
#include "unicode/ustring.h"
#include "unicode/unistr.h"
Expand Down Expand Up @@ -566,8 +568,8 @@ enum {
// when a provider asks for a UText to be allocated with extra storage.

struct ExtendedUText {
UText ut;
max_align_t extension;
UText ut;
std::max_align_t extension;
};

static const UText emptyText = UTEXT_INITIALIZER;
Expand All @@ -582,7 +584,7 @@ utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status) {
// We need to heap-allocate storage for the new UText
int32_t spaceRequired = sizeof(UText);
if (extraSpace > 0) {
spaceRequired = sizeof(ExtendedUText) + extraSpace - sizeof(max_align_t);
spaceRequired = sizeof(ExtendedUText) + extraSpace - sizeof(std::max_align_t);
}
ut = (UText *)uprv_malloc(spaceRequired);
if (ut == NULL) {
Expand Down
4 changes: 3 additions & 1 deletion icu4c/source/tools/toolutil/toolutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@

#include <errno.h>

#include <cstddef>

#include "unicode/errorcode.h"
#include "unicode/putil.h"
#include "cmemory.h"
Expand Down Expand Up @@ -243,7 +245,7 @@ struct UToolMemory {
char name[64];
int32_t capacity, maxCapacity, size, idx;
void *array;
alignas(max_align_t) char staticArray[1];
alignas(std::max_align_t) char staticArray[1];
};

U_CAPI UToolMemory * U_EXPORT2
Expand Down

0 comments on commit a3078fb

Please sign in to comment.