1*b1e83836Smrg /* Copyright (C) 2005-2022 Free Software Foundation, Inc.
24fee23f9Smrg Contributed by Richard Henderson <rth@redhat.com>.
34fee23f9Smrg
44d5abbe8Smrg This file is part of the GNU Offloading and Multi Processing Library
54d5abbe8Smrg (libgomp).
64fee23f9Smrg
74fee23f9Smrg Libgomp is free software; you can redistribute it and/or modify it
84fee23f9Smrg under the terms of the GNU General Public License as published by
94fee23f9Smrg the Free Software Foundation; either version 3, or (at your option)
104fee23f9Smrg any later version.
114fee23f9Smrg
124fee23f9Smrg Libgomp is distributed in the hope that it will be useful, but WITHOUT ANY
134fee23f9Smrg WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
144fee23f9Smrg FOR A PARTICULAR PURPOSE. See the GNU General Public License for
154fee23f9Smrg more details.
164fee23f9Smrg
174fee23f9Smrg Under Section 7 of GPL version 3, you are granted additional
184fee23f9Smrg permissions described in the GCC Runtime Library Exception, version
194fee23f9Smrg 3.1, as published by the Free Software Foundation.
204fee23f9Smrg
214fee23f9Smrg You should have received a copy of the GNU General Public License and
224fee23f9Smrg a copy of the GCC Runtime Library Exception along with this program;
234fee23f9Smrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
244fee23f9Smrg <http://www.gnu.org/licenses/>. */
254fee23f9Smrg
264fee23f9Smrg /* This file contains wrappers for the system allocation routines. Most
274fee23f9Smrg places in the OpenMP API do not make any provision for failure, so in
284fee23f9Smrg general we cannot allow memory allocation to fail. */
294fee23f9Smrg
30181254a7Smrg #define _GNU_SOURCE
314fee23f9Smrg #include "libgomp.h"
324fee23f9Smrg #include <stdlib.h>
334fee23f9Smrg
344fee23f9Smrg
354fee23f9Smrg void *
gomp_malloc(size_t size)364fee23f9Smrg gomp_malloc (size_t size)
374fee23f9Smrg {
384fee23f9Smrg void *ret = malloc (size);
394fee23f9Smrg if (ret == NULL)
404fee23f9Smrg gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
414fee23f9Smrg return ret;
424fee23f9Smrg }
434fee23f9Smrg
444fee23f9Smrg void *
gomp_malloc_cleared(size_t size)454fee23f9Smrg gomp_malloc_cleared (size_t size)
464fee23f9Smrg {
474fee23f9Smrg void *ret = calloc (1, size);
484fee23f9Smrg if (ret == NULL)
494fee23f9Smrg gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
504fee23f9Smrg return ret;
514fee23f9Smrg }
524fee23f9Smrg
534fee23f9Smrg void *
gomp_realloc(void * old,size_t size)544fee23f9Smrg gomp_realloc (void *old, size_t size)
554fee23f9Smrg {
564fee23f9Smrg void *ret = realloc (old, size);
574fee23f9Smrg if (ret == NULL)
584fee23f9Smrg gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
594fee23f9Smrg return ret;
604fee23f9Smrg }
61181254a7Smrg
62181254a7Smrg void *
gomp_aligned_alloc(size_t al,size_t size)63181254a7Smrg gomp_aligned_alloc (size_t al, size_t size)
64181254a7Smrg {
65181254a7Smrg void *ret;
66181254a7Smrg if (al < sizeof (void *))
67181254a7Smrg al = sizeof (void *);
68*b1e83836Smrg #ifdef HAVE_MEMALIGN
69181254a7Smrg {
70181254a7Smrg extern void *memalign (size_t, size_t);
71181254a7Smrg ret = memalign (al, size);
72181254a7Smrg }
73*b1e83836Smrg #elif defined(HAVE_POSIX_MEMALIGN)
74*b1e83836Smrg if (posix_memalign (&ret, al, size) != 0)
75*b1e83836Smrg ret = NULL;
76*b1e83836Smrg #elif defined(HAVE_ALIGNED_ALLOC)
77*b1e83836Smrg {
78*b1e83836Smrg size_t sz = (size + al - 1) & ~(al - 1);
79*b1e83836Smrg if (__builtin_expect (sz >= size, 1))
80*b1e83836Smrg ret = aligned_alloc (al, sz);
81*b1e83836Smrg else
82*b1e83836Smrg ret = NULL;
83*b1e83836Smrg }
84*b1e83836Smrg #elif defined(HAVE__ALIGNED_MALLOC)
85*b1e83836Smrg ret = _aligned_malloc (size, al);
86181254a7Smrg #else
87181254a7Smrg ret = NULL;
88181254a7Smrg if ((al & (al - 1)) == 0 && size)
89181254a7Smrg {
90181254a7Smrg void *p = malloc (size + al);
91181254a7Smrg if (p)
92181254a7Smrg {
93181254a7Smrg void *ap = (void *) (((uintptr_t) p + al) & -al);
94181254a7Smrg ((void **) ap)[-1] = p;
95181254a7Smrg ret = ap;
96181254a7Smrg }
97181254a7Smrg }
98181254a7Smrg #endif
99181254a7Smrg if (ret == NULL)
100181254a7Smrg gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size);
101181254a7Smrg return ret;
102181254a7Smrg }
103181254a7Smrg
104181254a7Smrg void
gomp_aligned_free(void * ptr)105181254a7Smrg gomp_aligned_free (void *ptr)
106181254a7Smrg {
107181254a7Smrg #ifdef GOMP_HAVE_EFFICIENT_ALIGNED_ALLOC
108181254a7Smrg free (ptr);
109*b1e83836Smrg #elif defined(HAVE__ALIGNED_MALLOC)
110*b1e83836Smrg _aligned_free (ptr);
111181254a7Smrg #else
112181254a7Smrg if (ptr)
113181254a7Smrg free (((void **) ptr)[-1]);
114181254a7Smrg #endif
115181254a7Smrg }
116