diff options
Diffstat (limited to 'src/lib/libc/stdlib/malloc.c')
| -rw-r--r-- | src/lib/libc/stdlib/malloc.c | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 81c30812a4..70e7f37dc8 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: malloc.c,v 1.249 2018/04/07 09:57:08 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.250 2018/11/05 08:23:40 otto Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> |
| 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> | 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> |
| @@ -2058,6 +2058,48 @@ err: | |||
| 2058 | } | 2058 | } |
| 2059 | /*DEF_STRONG(posix_memalign);*/ | 2059 | /*DEF_STRONG(posix_memalign);*/ |
| 2060 | 2060 | ||
| 2061 | void * | ||
| 2062 | aligned_alloc(size_t alignment, size_t size) | ||
| 2063 | { | ||
| 2064 | struct dir_info *d; | ||
| 2065 | int saved_errno = errno; | ||
| 2066 | void *r; | ||
| 2067 | |||
| 2068 | /* Make sure that alignment is a positive power of 2. */ | ||
| 2069 | if (((alignment - 1) & alignment) != 0 || alignment == 0) { | ||
| 2070 | errno = EINVAL; | ||
| 2071 | return NULL; | ||
| 2072 | }; | ||
| 2073 | /* Per spec, size should be a multiple of alignment */ | ||
| 2074 | if ((size & (alignment - 1)) != 0) { | ||
| 2075 | errno = EINVAL; | ||
| 2076 | return NULL; | ||
| 2077 | } | ||
| 2078 | |||
| 2079 | d = getpool(); | ||
| 2080 | if (d == NULL) { | ||
| 2081 | _malloc_init(0); | ||
| 2082 | d = getpool(); | ||
| 2083 | } | ||
| 2084 | _MALLOC_LOCK(d->mutex); | ||
| 2085 | d->func = "aligned_alloc"; | ||
| 2086 | if (d->active++) { | ||
| 2087 | malloc_recurse(d); | ||
| 2088 | return NULL; | ||
| 2089 | } | ||
| 2090 | r = omemalign(d, alignment, size, 0, CALLER); | ||
| 2091 | d->active--; | ||
| 2092 | _MALLOC_UNLOCK(d->mutex); | ||
| 2093 | if (r == NULL) { | ||
| 2094 | if (mopts.malloc_xmalloc) | ||
| 2095 | wrterror(d, "out of memory"); | ||
| 2096 | return NULL; | ||
| 2097 | } | ||
| 2098 | errno = saved_errno; | ||
| 2099 | return r; | ||
| 2100 | } | ||
| 2101 | /*DEF_STRONG(aligned_alloc);*/ | ||
| 2102 | |||
| 2061 | #ifdef MALLOC_STATS | 2103 | #ifdef MALLOC_STATS |
| 2062 | 2104 | ||
| 2063 | struct malloc_leak { | 2105 | struct malloc_leak { |
