xref: /netbsd-src/external/gpl3/gcc.old/dist/libgomp/alloc.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1*8feb0f0bSmrg /* Copyright (C) 2005-2020 Free Software Foundation, Inc.
21debfc3dSmrg    Contributed by Richard Henderson <rth@redhat.com>.
31debfc3dSmrg 
41debfc3dSmrg    This file is part of the GNU Offloading and Multi Processing Library
51debfc3dSmrg    (libgomp).
61debfc3dSmrg 
71debfc3dSmrg    Libgomp is free software; you can redistribute it and/or modify it
81debfc3dSmrg    under the terms of the GNU General Public License as published by
91debfc3dSmrg    the Free Software Foundation; either version 3, or (at your option)
101debfc3dSmrg    any later version.
111debfc3dSmrg 
121debfc3dSmrg    Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
131debfc3dSmrg    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
141debfc3dSmrg    FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
151debfc3dSmrg    more details.
161debfc3dSmrg 
171debfc3dSmrg    Under Section 7 of GPL version 3, you are granted additional
181debfc3dSmrg    permissions described in the GCC Runtime Library Exception, version
191debfc3dSmrg    3.1, as published by the Free Software Foundation.
201debfc3dSmrg 
211debfc3dSmrg    You should have received a copy of the GNU General Public License and
221debfc3dSmrg    a copy of the GCC Runtime Library Exception along with this program;
231debfc3dSmrg    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
241debfc3dSmrg    <http://www.gnu.org/licenses/>.  */
251debfc3dSmrg 
261debfc3dSmrg /* This file contains wrappers for the system allocation routines.  Most
271debfc3dSmrg    places in the OpenMP API do not make any provision for failure, so in
281debfc3dSmrg    general we cannot allow memory allocation to fail.  */
291debfc3dSmrg 
30c0a68be4Smrg #define _GNU_SOURCE
311debfc3dSmrg #include "libgomp.h"
321debfc3dSmrg #include <stdlib.h>
331debfc3dSmrg 
341debfc3dSmrg 
351debfc3dSmrg void *
gomp_malloc(size_t size)361debfc3dSmrg gomp_malloc (size_t size)
371debfc3dSmrg {
381debfc3dSmrg   void *ret = malloc (size);
391debfc3dSmrg   if (ret == NULL)
401debfc3dSmrg     gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
411debfc3dSmrg   return ret;
421debfc3dSmrg }
431debfc3dSmrg 
441debfc3dSmrg void *
gomp_malloc_cleared(size_t size)451debfc3dSmrg gomp_malloc_cleared (size_t size)
461debfc3dSmrg {
471debfc3dSmrg   void *ret = calloc (1, size);
481debfc3dSmrg   if (ret == NULL)
491debfc3dSmrg     gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
501debfc3dSmrg   return ret;
511debfc3dSmrg }
521debfc3dSmrg 
531debfc3dSmrg void *
gomp_realloc(void * old,size_t size)541debfc3dSmrg gomp_realloc (void *old, size_t size)
551debfc3dSmrg {
561debfc3dSmrg   void *ret = realloc (old, size);
571debfc3dSmrg   if (ret == NULL)
581debfc3dSmrg     gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
591debfc3dSmrg   return ret;
601debfc3dSmrg }
61c0a68be4Smrg 
62c0a68be4Smrg void *
gomp_aligned_alloc(size_t al,size_t size)63c0a68be4Smrg gomp_aligned_alloc (size_t al, size_t size)
64c0a68be4Smrg {
65c0a68be4Smrg   void *ret;
66c0a68be4Smrg   if (al < sizeof (void *))
67c0a68be4Smrg     al = sizeof (void *);
68c0a68be4Smrg #ifdef HAVE_ALIGNED_ALLOC
69c0a68be4Smrg   ret = aligned_alloc (al, size);
70c0a68be4Smrg #elif defined(HAVE__ALIGNED_MALLOC)
71c0a68be4Smrg   ret = _aligned_malloc (size, al);
72c0a68be4Smrg #elif defined(HAVE_POSIX_MEMALIGN)
73c0a68be4Smrg   if (posix_memalign (&ret, al, size) != 0)
74c0a68be4Smrg     ret = NULL;
75c0a68be4Smrg #elif defined(HAVE_MEMALIGN)
76c0a68be4Smrg   {
77c0a68be4Smrg     extern void *memalign (size_t, size_t);
78c0a68be4Smrg     ret = memalign (al, size);
79c0a68be4Smrg   }
80c0a68be4Smrg #else
81c0a68be4Smrg   ret = NULL;
82c0a68be4Smrg   if ((al & (al - 1)) == 0 && size)
83c0a68be4Smrg     {
84c0a68be4Smrg       void *p = malloc (size + al);
85c0a68be4Smrg       if (p)
86c0a68be4Smrg 	{
87c0a68be4Smrg 	  void *ap = (void *) (((uintptr_t) p + al) & -al);
88c0a68be4Smrg 	  ((void **) ap)[-1] = p;
89c0a68be4Smrg 	  ret = ap;
90c0a68be4Smrg 	}
91c0a68be4Smrg     }
92c0a68be4Smrg #endif
93c0a68be4Smrg   if (ret == NULL)
94c0a68be4Smrg     gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
95c0a68be4Smrg   return ret;
96c0a68be4Smrg }
97c0a68be4Smrg 
98c0a68be4Smrg void
gomp_aligned_free(void * ptr)99c0a68be4Smrg gomp_aligned_free (void *ptr)
100c0a68be4Smrg {
101c0a68be4Smrg #ifdef GOMP_HAVE_EFFICIENT_ALIGNED_ALLOC
102c0a68be4Smrg   free (ptr);
103c0a68be4Smrg #else
104c0a68be4Smrg   if (ptr)
105c0a68be4Smrg     free (((void **) ptr)[-1]);
106c0a68be4Smrg #endif
107c0a68be4Smrg }
108