# HG changeset patch
# Parent 2879a3a3daa3219e8c4b523ce104c87941daf698
Make alloca() API both GNU and Microsoft compatible.

* include/malloc.h: Tidy layout; assert copyright.
(_MALLOC_H_): Rename guard macro, for conformance with...
(_MALLOC_H): ...this preferred guard macro naming convention.
(GCC system_header): Add pragma, declaring it as such.
[_BEGIN_C_DECLS, _END_C_DECLS]: Use them.
(_aligned_malloc, _aligned_offset_malloc)
(_aligned_realloc, _aligned_offset_realloc)
(_aligned_recalloc, _aligned_offset_recalloc)
(_aligned_free) [_WIN32_WINNT >= _WIN32_WINNT_WINXP]: Declare them
for this new condition, while also continuing to declare them when...
[__MSVCRT_VERSION__>=__MSVCR70_DLL]: ...this original condition
prevails; update representation, to express it symbolically.
(alloca, _alloca): Factor them out, then include from...
* include/alloca.h: ...this new header file; it reimplements...
(alloca, _alloca): ...these, such that they remain Microsoft
compatible, while adding GNU conformity, either explicitly when
included by user code, or conditionally when included by...
* include/stdlib.h [!__STRICT_ANSI__]: ...this.

diff --git a/mingwrt/include/alloca.h b/mingwrt/include/alloca.h
new file mode 100644
--- /dev/null
+++ b/mingwrt/include/alloca.h
@@ -0,0 +1,67 @@
+/*
+ * alloca.h
+ *
+ * Declarations for the alloca() function API, conforming to both GNU and
+ * Microsoft's implementation conventions.
+ *
+ *
+ * $Id$
+ *
+ * Written by Keith Marshall <keith@users.osdn.me>
+ * Copyright (C) 2018, MinGW.org Project.
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+#ifndef _ALLOCA_H
+#define _ALLOCA_H
+/* Microsoft requires the alloca() API to be declared in <malloc.h>;
+ * GNU declares it in <alloca.h>, with default inclusion by <stdlib.h>
+ * when !__STRICT_ANSI__.  To achieve compatibility with both, we will
+ * define it in the GNU manner, conditionally including this file when
+ * reading <stdlib.h>, and UNCONDITIONALLY including it in <malloc.h>
+ */
+#ifdef __GNUC__
+#pragma GCC system_header
+/* This implementation is unsupported, for any compiler other than GCC,
+ * (which is the standard MinGW compiler, in any case); all MinGW source
+ * may assume that <_mingw.h> has been included, so ensure that it is.
+ */
+#include <_mingw.h>
+
+_BEGIN_C_DECLS
+
+#if defined _GNU_SOURCE || ! defined _NO_OLDNAMES
+/* This is the GNU standard API; it is also compatible with Microsoft's
+ * original, but now deprecated, naming convention.
+ */
+__CRT_ALIAS void *alloca( size_t __n ){ return __builtin_alloca( __n ); }
+#endif	/* _GNU_SOURCE || !_NO_OLDNAMES */
+
+/* This represents the same API, but conforms to Microsoft's currently
+ * preferred naming convention.
+ */
+__CRT_ALIAS void *_alloca( size_t __n ){ return __builtin_alloca( __n ); }
+
+_END_C_DECLS
+
+#endif	/* __GNUC__ */
+#endif	/* !_ALLOCA_H: $RCSfile$: end of file */
diff --git a/mingwrt/include/malloc.h b/mingwrt/include/malloc.h
--- a/mingwrt/include/malloc.h
+++ b/mingwrt/include/malloc.h
@@ -1,103 +1,128 @@
 /*
  * malloc.h
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is a part of the mingw-runtime package.
- * No warranty is given; refer to the file DISCLAIMER within the package.
  *
- * Support for programs which want to use malloc.h to get memory management
- * functions. Unless you absolutely need some of these functions and they are
- * not in the ANSI headers you should use the ANSI standard header files
- * instead.
+ * Declarations for Microsoft's non-standard memory management API.
+ *
+ *
+ * $Id$
+ *
+ * Written by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
+ * Copyright (C) 1997-1999, 2001-2005, 2007, 2018, MinGW.org Project.
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
  *
  */
+#ifndef _MALLOC_H
+#pragma GCC system_header
+#define _MALLOC_H
 
-#ifndef _MALLOC_H_
-#define _MALLOC_H_
-
-/* All the headers include this file. */
-#include <_mingw.h>
-
+/* All MinGW headers assume that <_mingw.h> is included; including
+ * <stdlib.h>, which we also need here, is sufficient to make it so.
+ */
 #include <stdlib.h>
 
 #ifndef RC_INVOKED
 
-/*
- * The structure used to walk through the heap with _heapwalk.
+/* Microsoft stipulate that the alloca() API should be defined in this
+ * header, whereas GNU specify it in its own dedicated header file; to
+ * comply with both, we adopt the GNU stratagem, and then include the
+ * GNU style dedicated header file here.
  */
-typedef	struct _heapinfo
-{
-	int*	_pentry;
-	size_t	_size;
-	int	_useflag;
+#include "alloca.h"
+
+typedef
+struct _heapinfo
+{ /* The structure used to walk through the heap with _heapwalk.
+   */
+  int		*_pentry;
+  size_t	 _size;
+  int		 _useflag;
 } _HEAPINFO;
 
-/* Values for _heapinfo.useflag */
-#define _FREEENTRY 0
-#define _USEDENTRY 1
+/* Values for _heapinfo.useflag
+ */
+#define _FREEENTRY		  0
+#define _USEDENTRY		  1
 
-/* Return codes for _heapwalk()  */
-#define _HEAPEMPTY	(-1)
-#define _HEAPOK		(-2)
-#define _HEAPBADBEGIN	(-3)
-#define _HEAPBADNODE	(-4)
-#define _HEAPEND	(-5)
-#define _HEAPBADPTR	(-6)
+/* Return codes for _heapwalk()
+ */
+#define _HEAPEMPTY		(-1)
+#define _HEAPOK 		(-2)
+#define _HEAPBADBEGIN		(-3)
+#define _HEAPBADNODE		(-4)
+#define _HEAPEND		(-5)
+#define _HEAPBADPTR		(-6)
 
-/* maximum size of a user request for memory */
-#define _HEAP_MAXREQ  0xFFFFFFE0
+/* The maximum size permitted in a user request for memory.
+ */
+#define _HEAP_MAXREQ	 0xFFFFFFE0
 
-#ifdef	__cplusplus
-extern "C" {
-#endif
-/*
-   The _heap* memory allocation functions are supported on NT
-   but not W9x. On latter, they always set errno to ENOSYS.
-*/
-_CRTIMP int __cdecl __MINGW_NOTHROW _heapwalk (_HEAPINFO*);
-#ifdef __GNUC__
-#define _alloca(x) __builtin_alloca((x))
-#endif
+_BEGIN_C_DECLS
+
+/* The _heap memory allocation functions are supported on WinNT but,
+ * not on Win9X, (on which they always simply set errno to ENOSYS).
+ */
+_CRTIMP int __cdecl __MINGW_NOTHROW _heapwalk (_HEAPINFO *);
 
 #ifndef	_NO_OLDNAMES
-_CRTIMP int __cdecl __MINGW_NOTHROW heapwalk (_HEAPINFO*);
-#ifdef __GNUC__
-#define alloca(x) __builtin_alloca((x))
-#endif
-#endif	/* Not _NO_OLDNAMES */
+_CRTIMP int __cdecl __MINGW_NOTHROW heapwalk (_HEAPINFO *);
+#endif	/* !_NO_OLDNAMES */
 
-_CRTIMP int __cdecl __MINGW_NOTHROW _heapchk (void);	/* Verify heap integrety. */
-_CRTIMP int __cdecl __MINGW_NOTHROW _heapmin (void);	/* Return unused heap to the OS. */
-_CRTIMP int __cdecl __MINGW_NOTHROW _heapset (unsigned int);
+_CRTIMP __cdecl __MINGW_NOTHROW  int    _heapchk (void);  /* Verify heap integrety. */
+_CRTIMP __cdecl __MINGW_NOTHROW  int    _heapmin (void);  /* Return unused heap to the OS. */
 
-_CRTIMP size_t __cdecl __MINGW_NOTHROW _msize (void*);
-_CRTIMP size_t __cdecl __MINGW_NOTHROW _get_sbh_threshold (void);
-_CRTIMP int __cdecl __MINGW_NOTHROW _set_sbh_threshold (size_t);
-_CRTIMP void* __cdecl __MINGW_NOTHROW _expand (void*, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  int    _heapset (unsigned int);
 
-/* These require msvcr70.dll or higher. */
-#if __MSVCRT_VERSION__ >= 0x0700
-_CRTIMP void * __cdecl __MINGW_NOTHROW _aligned_offset_malloc(size_t, size_t, size_t);
-_CRTIMP void * __cdecl __MINGW_NOTHROW _aligned_offset_realloc(void*, size_t, size_t, size_t);
-_CRTIMP void * __cdecl __MINGW_NOTHROW _aligned_offset_recalloc(void*, size_t, size_t, size_t, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  size_t _msize (void *);
+_CRTIMP __cdecl __MINGW_NOTHROW  size_t _get_sbh_threshold (void);
+_CRTIMP __cdecl __MINGW_NOTHROW  int    _set_sbh_threshold (size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  void  *_expand (void *, size_t);
 
-_CRTIMP void * __cdecl __MINGW_NOTHROW _aligned_malloc (size_t, size_t);
-_CRTIMP void * __cdecl __MINGW_NOTHROW _aligned_realloc (void*, size_t, size_t);
-_CRTIMP void* __cdecl __MINGW_NOTHROW _aligned_recalloc(void*, size_t, size_t, size_t);
-_CRTIMP void __cdecl __MINGW_NOTHROW _aligned_free (void*);
-#endif /* __MSVCRT_VERSION__ >= 0x0700 */
+#if _WIN32_WINNT >= _WIN32_WINNT_WINXP || __MSVCRT_VERSION__ >= __MSVCR70_DLL
+/* First introduced in non-free MSVCR70.DLL, the following were subsequently
+ * made available from MSVCRT.DLL, from the release of WinXP onwards.
+ */
+_CRTIMP __cdecl __MINGW_NOTHROW  void *_aligned_offset_malloc (size_t, size_t, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  void *_aligned_offset_realloc (void *, size_t, size_t, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  void *_aligned_offset_recalloc (void *, size_t, size_t, size_t, size_t);
 
-/* These require libmingwex.a. */
-void * __cdecl __MINGW_NOTHROW __mingw_aligned_offset_malloc (size_t, size_t, size_t);
-void * __cdecl __MINGW_NOTHROW __mingw_aligned_offset_realloc (void*, size_t, size_t, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  void *_aligned_malloc (size_t, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  void *_aligned_realloc (void *, size_t, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  void *_aligned_recalloc (void *, size_t, size_t, size_t);
 
-void * __cdecl __MINGW_NOTHROW __mingw_aligned_malloc (size_t, size_t);
-void * __cdecl __MINGW_NOTHROW __mingw_aligned_realloc (void*, size_t, size_t);
-void __cdecl __MINGW_NOTHROW __mingw_aligned_free (void*);
+_CRTIMP __cdecl __MINGW_NOTHROW  void  _aligned_free (void *);
+#endif	/* WinXP, MSVCR70.DLL, or later */
 
-#ifdef __cplusplus
-}
-#endif
+/* The following emulations are provided in libmingwex.a; they are suitable
+ * for use on any Windows version, irrespective of the limited availability
+ * of the preceding Microsoft implementations.
+ */
+__cdecl __MINGW_NOTHROW  void *__mingw_aligned_offset_malloc (size_t, size_t, size_t);
+__cdecl __MINGW_NOTHROW  void *__mingw_aligned_offset_realloc (void *, size_t, size_t, size_t);
 
-#endif	/* RC_INVOKED */
+__cdecl __MINGW_NOTHROW  void *__mingw_aligned_malloc (size_t, size_t);
+__cdecl __MINGW_NOTHROW  void *__mingw_aligned_realloc (void *, size_t, size_t);
+__cdecl __MINGW_NOTHROW  void  __mingw_aligned_free (void *);
 
-#endif /* Not _MALLOC_H_ */
+_END_C_DECLS
+
+#endif	/* ! RC_INVOKED */
+#endif	/* !_MALLOC_H: $RCSfile$: end of file */
diff --git a/mingwrt/include/stdlib.h b/mingwrt/include/stdlib.h
--- a/mingwrt/include/stdlib.h
+++ b/mingwrt/include/stdlib.h
@@ -5,11 +5,11 @@
  * associated macros, and manifest constant definitions.
  *
  * $Id$
  *
  * Written by Colin Peters <colin@bird.fu.is.saga-u.ac.jp>
- * Copyright (C) 1997-2009, 2011, 2014-2016, MinGW.org Project.
+ * Copyright (C) 1997-2009, 2011, 2014-2016, 2018, MinGW.org Project.
  *
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * to deal in the Software without restriction, including without limitation
@@ -479,10 +479,18 @@ wchar_t *_wfullpath (wchar_t *, const wc
 _CRTIMP __cdecl __MINGW_NOTHROW  int mbtowc (wchar_t *, const char *, size_t);
 
 _CRTIMP __cdecl __MINGW_NOTHROW  int rand (void);
 _CRTIMP __cdecl __MINGW_NOTHROW  void srand (unsigned int);
 
+#ifndef __STRICT_ANSI__
+/* For GNU compatibility, in addition to the standard memory allocation
+ * functions (declared below), we also include the non-standard alloca()
+ * API declarations here, in accordance with GNU convention.
+ */
+# include "alloca.h"
+#endif	/* !__STRICT_ANSI__ */
+
 _CRTIMP __cdecl __MINGW_NOTHROW  void *calloc (size_t, size_t) __MINGW_ATTRIB_MALLOC;
 _CRTIMP __cdecl __MINGW_NOTHROW  void *malloc (size_t) __MINGW_ATTRIB_MALLOC;
 _CRTIMP __cdecl __MINGW_NOTHROW  void *realloc (void *, size_t);
 _CRTIMP __cdecl __MINGW_NOTHROW  void free (void *);
 _CRTIMP __cdecl __MINGW_NOTHROW  void abort (void) __MINGW_ATTRIB_NORETURN;
