From: Keith Marshall Date: Sat, 18 Apr 2020 21:22:36 +0000 (+0100) Subject: Implement ISO-C11/C++17 aligned_alloc() function. X-Git-Tag: wsl-5.3.1-release~2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=ddf570f9827dbbf5ac13f2e1fdb28f69a722fe65;p=mingw%2Fmingw-org-wsl.git Implement ISO-C11/C++17 aligned_alloc() function. --- diff --git a/mingwrt/ChangeLog b/mingwrt/ChangeLog index 716810f..8d0524d 100644 --- a/mingwrt/ChangeLog +++ b/mingwrt/ChangeLog @@ -1,3 +1,14 @@ +2020-04-18 Keith Marshall + + Implement ISO-C11/C++17 aligned_alloc() function. + + * include/stdlib.h (free, realloc): Define as inline aliases for... + (__mingw_free, __mingw_realloc): ...these replacements, respectively. + [__STDC_VERSION__>=201112L || __cplusplus >=201703L] (aligned_alloc): + Implement as inline alias, with libmemalign.a stub, in terms of... + (__mingw_aligned_offset_malloc): ...this; duplicate declaration. + (_GLIBCXX_HAVE_ALIGNED_ALLOC): Define it. + 2020-04-11 Keith Marshall Manpage stylistic consistency updates. diff --git a/mingwrt/include/stdlib.h b/mingwrt/include/stdlib.h index f39e909..d9dac5d 100644 --- a/mingwrt/include/stdlib.h +++ b/mingwrt/include/stdlib.h @@ -574,6 +574,45 @@ _CRTIMP __cdecl __MINGW_NOTHROW void free (void *); __cdecl __MINGW_NOTHROW void *__mingw_realloc (void *, size_t); __cdecl __MINGW_NOTHROW void __mingw_free (void *); +/* Since MinGW's __mingw_free() and __mingw_realloc() are able to + * operate transparently on pointers returned by any of Microsoft's + * heap allocators, except their over-aligned variants, just as they + * operate on pointers returned by MinGW's over-aligned allocators, + * and all of ISO-C11, C++17, and POSIX.1 require this capability, + * always prefer these replacements for free() and realloc(). + */ +__JMPSTUB__(( LIB = memalign, FUNCTION = free )) +__CRT_ALIAS __cdecl __MINGW_NOTHROW void free (void *__ptr) +{ __mingw_free (__ptr); } + +__JMPSTUB__(( LIB = memalign, FUNCTION = realloc )) +__CRT_ALIAS __cdecl __MINGW_NOTHROW void *realloc (void *__ptr, size_t __want) +{ return __mingw_realloc (__ptr, __want); } + +#if __STDC_VERSION__ >= 201112L || __cplusplus >= 201703L +/* ISO-C99 adds support for over-aligned heap memory allocation, by use + * of the aligned_alloc() function, (which was subsequently incorporated + * into ISO-C++17 as std::aligned_alloc()); we may conveniently support + * this by use of MinGW's __mingw_aligned_offset_malloc(), which is + * nominally declared in , and reproduced here: + */ +__cdecl __MINGW_NOTHROW __MINGW_ATTRIB_MALLOC +void *__mingw_aligned_offset_malloc (size_t, size_t, size_t); + +__CRT_ALIAS __LIBIMPL__(( LIB = memalign, FUNCTION = aligned_alloc )) + +__cdecl __MINGW_NOTHROW __MINGW_ATTRIB_MALLOC +void *aligned_alloc (size_t __alignment, size_t __want) +{ return __mingw_aligned_offset_malloc( __want, __alignment, (size_t)(0) ); } + +/* For the ISO-C++17 case, we need to ensure that the feature test + * macro _GLIBCXX_HAVE_ALIGNED_ALLOC is defined, with non-zero value; + * (it is interpreted in , but only for C++17 and later). + */ +#undef _GLIBCXX_HAVE_ALIGNED_ALLOC +#define _GLIBCXX_HAVE_ALIGNED_ALLOC 1 +#endif /* ISO-C11 || ISO-C++17 */ + /* bsearch() and qsort() are declared both here, in , and in * non-ANSI header ; we reproduce these declarations in both, * with no attempt to guard them, so the compiler may verify that they