diff options
Diffstat (limited to 'libgomp/alloc.c')
-rw-r--r-- | libgomp/alloc.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/libgomp/alloc.c b/libgomp/alloc.c index 1bf4042..296f1af 100644 --- a/libgomp/alloc.c +++ b/libgomp/alloc.c @@ -57,3 +57,50 @@ gomp_realloc (void *old, size_t size) gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size); return ret; } + +void * +gomp_aligned_alloc (size_t al, size_t size) +{ + void *ret; + if (al < sizeof (void *)) + al = sizeof (void *); +#ifdef HAVE_ALIGNED_ALLOC + ret = aligned_alloc (al, size); +#elif defined(HAVE__ALIGNED_MALLOC) + ret = _aligned_malloc (size, al); +#elif defined(HAVE_POSIX_MEMALIGN) + if (posix_memalign (&ret, al, size) != 0) + ret = NULL; +#elif defined(HAVE_MEMALIGN) + { + extern void *memalign (size_t, size_t); + ret = memalign (al, size); + } +#else + ret = NULL; + if ((al & (al - 1)) == 0 && size) + { + void *p = malloc (size + al); + if (p) + { + void *ap = (void *) (((uintptr_t) p + al) & -al); + ((void **) ap)[-1] = p; + ret = ap; + } + } +#endif + if (ret == NULL) + gomp_fatal ("Out of memory allocating %lu bytes", (unsigned long) size); + return ret; +} + +void +gomp_aligned_free (void *ptr) +{ +#ifdef GOMP_HAVE_EFFICIENT_ALIGNED_ALLOC + free (ptr); +#else + if (ptr) + free (((void **) ptr)[-1]); +#endif +} |