diff options
| author | 2018-11-05 08:23:40 +0000 | |
|---|---|---|
| committer | 2018-11-05 08:23:40 +0000 | |
| commit | 7a9442eb018832ffdacce1f604e34077f7010b77 (patch) | |
| tree | 44b33e84cb6c7e9b623edc9c06c6eadf10d35a73 /lib/libc/stdlib/malloc.c | |
| parent | mark up AUTHORS; from raf czlonka (diff) | |
| download | wireguard-openbsd-7a9442eb018832ffdacce1f604e34077f7010b77.tar.xz wireguard-openbsd-7a9442eb018832ffdacce1f604e34077f7010b77.zip | |
Implement C11's aligned_alloc(3). ok guenther@
Diffstat (limited to 'lib/libc/stdlib/malloc.c')
| -rw-r--r-- | lib/libc/stdlib/malloc.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c index 81c30812a46..70e7f37dc82 100644 --- a/lib/libc/stdlib/malloc.c +++ b/lib/libc/stdlib/malloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: malloc.c,v 1.249 2018/04/07 09:57:08 otto Exp $ */ +/* $OpenBSD: malloc.c,v 1.250 2018/11/05 08:23:40 otto Exp $ */ /* * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> @@ -2058,6 +2058,48 @@ err: } /*DEF_STRONG(posix_memalign);*/ +void * +aligned_alloc(size_t alignment, size_t size) +{ + struct dir_info *d; + int saved_errno = errno; + void *r; + + /* Make sure that alignment is a positive power of 2. */ + if (((alignment - 1) & alignment) != 0 || alignment == 0) { + errno = EINVAL; + return NULL; + }; + /* Per spec, size should be a multiple of alignment */ + if ((size & (alignment - 1)) != 0) { + errno = EINVAL; + return NULL; + } + + d = getpool(); + if (d == NULL) { + _malloc_init(0); + d = getpool(); + } + _MALLOC_LOCK(d->mutex); + d->func = "aligned_alloc"; + if (d->active++) { + malloc_recurse(d); + return NULL; + } + r = omemalign(d, alignment, size, 0, CALLER); + d->active--; + _MALLOC_UNLOCK(d->mutex); + if (r == NULL) { + if (mopts.malloc_xmalloc) + wrterror(d, "out of memory"); + return NULL; + } + errno = saved_errno; + return r; +} +/*DEF_STRONG(aligned_alloc);*/ + #ifdef MALLOC_STATS struct malloc_leak { |
