OSDN Git Service

Make alloca() API both GNU and Microsoft compatible.
authorKeith Marshall <keith@users.osdn.me>
Sun, 7 Oct 2018 19:15:11 +0000 (20:15 +0100)
committerKeith Marshall <keith@users.osdn.me>
Sun, 7 Oct 2018 19:15:11 +0000 (20:15 +0100)
mingwrt/ChangeLog
mingwrt/include/alloca.h [new file with mode: 0644]
mingwrt/include/malloc.h
mingwrt/include/stdlib.h
mingwrt/tests/headers.at

index 4f0509c..d6ff210 100644 (file)
@@ -1,3 +1,27 @@
+2018-10-07  Keith Marshall  <keith@users.osdn.me>
+
+       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.
+       [__MSVCRT_VERSION__ >= 0x0700]: Express symbolically, guarding...
+       (_aligned_malloc, _aligned_offset_malloc)
+       (_aligned_realloc, _aligned_offset_realloc)
+       (_aligned_recalloc, _aligned_offset_recalloc): ...these...
+       [__MSVCRT_VERSION__>=__MSVCR70_DLL]: ...thus.
+       [_BEGIN_C_DECLS, _END_C_DECLS]: Use them.
+       (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.
+
+       * tests/headers.at: Regenerated by "make check".
+       (MINGWRT_AT_PACKAGE_HEADERS): Add alloca.h
+
 2018-09-06  Keith Marshall  <keith@users.osdn.me>
 
        Avoid unwanted GCC warning diagnostic messages.
diff --git a/mingwrt/include/alloca.h b/mingwrt/include/alloca.h
new file mode 100644 (file)
index 0000000..aa65366
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * 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>
+
+/* We must also ensure that the "size_t" type definition is in scope;
+ * we may guarantee this, by selective inclusion from <stddef.h>
+ */
+#define __need_size_t
+#include <stddef.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 */
index c484ad3..3b401e9 100644 (file)
 /*
  * 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 non-standard heap management, and memory allocation
+ * functions.  These augment the standard functions, which are declared
+ * in <stdlib.h>
+ *
+ * $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, this permission notice, and the following
+ * disclaimer, 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 OF 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 control operation, and return information,
+   * when walking the heap using the _heapwalk() function.
+   */
+  int          *_pentry;
+  size_t        _size;
+  int           _useflag;
 } _HEAPINFO;
 
-/* Values for _heapinfo.useflag */
-#define _FREEENTRY 0
-#define _USEDENTRY 1
+/* Status codes returned by _heapwalk()
+ */
+#define _HEAPEMPTY             (-1)
+#define _HEAPOK                (-2)
+#define _HEAPBADBEGIN          (-3)
+#define _HEAPBADNODE           (-4)
+#define _HEAPEND               (-5)
+#define _HEAPBADPTR            (-6)
+
+/* Values returned by _heapwalk(), in the _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)
+/* Maximum size permitted for a heap memory allocation request
+ */
+#define _HEAP_MAXREQ   (0xFFFFFFE0)
 
-/* maximum size of a user request for memory */
-#define _HEAP_MAXREQ  0xFFFFFFE0
+_BEGIN_C_DECLS
 
-#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
-
-#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 _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 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);
-
-/* 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 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 */
-
-/* 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);
-
-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*);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif /* RC_INVOKED */
-
-#endif /* Not _MALLOC_H_ */
+/* The _heap memory allocation functions are supported on WinNT, but not on
+ * Win9X, (on which they always simply set errno to ENOSYS).
+ */
+_CRTIMP __cdecl __MINGW_NOTHROW  int    _heapwalk (_HEAPINFO *);
+
+_CRTIMP __cdecl __MINGW_NOTHROW  int    _heapchk (void);
+_CRTIMP __cdecl __MINGW_NOTHROW  int    _heapmin (void);
+
+_CRTIMP __cdecl __MINGW_NOTHROW  int    _heapset (unsigned int);
+
+_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);
+
+#ifndef _NO_OLDNAMES
+/* Legacy versions of Microsoft runtimes may have supported this alternative
+ * name for the _heapwalk() API.
+ */
+_CRTIMP __cdecl __MINGW_NOTHROW  int     heapwalk (_HEAPINFO *);
+#endif /* !_NO_OLDNAMES */
+
+#if __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; however,
+ * we choose to declare them only for the non-free case, preferring to emulate
+ * them, in terms of libmingwex.a replacement implementations, for consistent
+ * behaviour across ALL MSVCRT.DLL versions.
+ */
+_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_malloc (size_t, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  void *_aligned_realloc (void *, size_t, size_t);
+_CRTIMP __cdecl __MINGW_NOTHROW  void  _aligned_free (void *);
+
+/* Curiously, there are no "calloc()" alike variants of the following pair of
+ * "recalloc()" alike functions; furthermore, neither of these is provided by
+ * any version of pseudo-free MSVCRT.DLL
+ */
+_CRTIMP __cdecl __MINGW_NOTHROW
+void *_aligned_recalloc (void *, size_t, size_t, size_t);
+
+_CRTIMP __cdecl __MINGW_NOTHROW
+void *_aligned_offset_recalloc (void *, size_t, size_t, size_t, size_t);
+
+#endif /* Non-free MSVCR70.DLL, or later */
+
+/* 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_malloc (size_t, size_t);
+
+__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);
+
+__cdecl __MINGW_NOTHROW
+void *__mingw_aligned_realloc (void *, size_t, size_t);
+
+__cdecl __MINGW_NOTHROW
+void  __mingw_aligned_free (void *);
+
+_END_C_DECLS
+
+#endif /* ! RC_INVOKED */
+#endif /* !_MALLOC_H: $RCSfile$: end of file */
index 7ca7eb7..0dd56a6 100644 (file)
@@ -7,7 +7,7 @@
  * $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
@@ -481,6 +481,14 @@ _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);
index 884a456..37eb725 100644 (file)
@@ -37,6 +37,7 @@
 # dynamically, when running "make check"; there is no need to edit it.
 #
 m4_define([MINGWRT_AT_PACKAGE_HEADERS],[dnl
+alloca.h dnl
 assert.h dnl
 complex.h dnl
 conio.h dnl