Add alignment assertions to public aligned allocation functions.

This commit is contained in:
Jason Evans 2015-06-22 18:48:58 -07:00
parent 4f6f2b131e
commit dc0610a714

View File

@ -1472,37 +1472,37 @@ imemalign(void **memptr, size_t alignment, size_t size, size_t min_alignment)
if (unlikely(malloc_init())) { if (unlikely(malloc_init())) {
result = NULL; result = NULL;
goto label_oom; goto label_oom;
} else {
tsd = tsd_fetch();
if (size == 0)
size = 1;
/* Make sure that alignment is a large enough power of 2. */
if (unlikely(((alignment - 1) & alignment) != 0
|| (alignment < min_alignment))) {
if (config_xmalloc && unlikely(opt_xmalloc)) {
malloc_write("<jemalloc>: Error allocating "
"aligned memory: invalid alignment\n");
abort();
}
result = NULL;
ret = EINVAL;
goto label_return;
}
usize = sa2u(size, alignment);
if (unlikely(usize == 0)) {
result = NULL;
goto label_oom;
}
if (config_prof && opt_prof)
result = imemalign_prof(tsd, alignment, usize);
else
result = ipalloc(tsd, usize, alignment, false);
if (unlikely(result == NULL))
goto label_oom;
} }
tsd = tsd_fetch();
if (size == 0)
size = 1;
/* Make sure that alignment is a large enough power of 2. */
if (unlikely(((alignment - 1) & alignment) != 0
|| (alignment < min_alignment))) {
if (config_xmalloc && unlikely(opt_xmalloc)) {
malloc_write("<jemalloc>: Error allocating "
"aligned memory: invalid alignment\n");
abort();
}
result = NULL;
ret = EINVAL;
goto label_return;
}
usize = sa2u(size, alignment);
if (unlikely(usize == 0)) {
result = NULL;
goto label_oom;
}
if (config_prof && opt_prof)
result = imemalign_prof(tsd, alignment, usize);
else
result = ipalloc(tsd, usize, alignment, false);
if (unlikely(result == NULL))
goto label_oom;
assert(((uintptr_t)result & (alignment - 1)) == ZU(0));
*memptr = result; *memptr = result;
ret = 0; ret = 0;
@ -1986,12 +1986,14 @@ imallocx_prof(tsd_t *tsd, size_t size, int flags, size_t *usize)
} }
prof_malloc(p, *usize, tctx); prof_malloc(p, *usize, tctx);
assert(alignment == 0 || ((uintptr_t)p & (alignment - 1)) == ZU(0));
return (p); return (p);
} }
JEMALLOC_ALWAYS_INLINE_C void * JEMALLOC_ALWAYS_INLINE_C void *
imallocx_no_prof(tsd_t *tsd, size_t size, int flags, size_t *usize) imallocx_no_prof(tsd_t *tsd, size_t size, int flags, size_t *usize)
{ {
void *p;
size_t alignment; size_t alignment;
bool zero; bool zero;
tcache_t *tcache; tcache_t *tcache;
@ -2006,7 +2008,9 @@ imallocx_no_prof(tsd_t *tsd, size_t size, int flags, size_t *usize)
if (unlikely(imallocx_flags_decode_hard(tsd, size, flags, usize, if (unlikely(imallocx_flags_decode_hard(tsd, size, flags, usize,
&alignment, &zero, &tcache, &arena))) &alignment, &zero, &tcache, &arena)))
return (NULL); return (NULL);
return (imallocx_flags(tsd, *usize, alignment, zero, tcache, arena)); p = imallocx_flags(tsd, *usize, alignment, zero, tcache, arena);
assert(alignment == 0 || ((uintptr_t)p & (alignment - 1)) == ZU(0));
return (p);
} }
void * void *
@ -2160,6 +2164,7 @@ je_rallocx(void *ptr, size_t size, int flags)
if (config_stats || (config_valgrind && unlikely(in_valgrind))) if (config_stats || (config_valgrind && unlikely(in_valgrind)))
usize = isalloc(p, config_prof); usize = isalloc(p, config_prof);
} }
assert(alignment == 0 || ((uintptr_t)p & (alignment - 1)) == ZU(0));
if (config_stats) { if (config_stats) {
*tsd_thread_allocatedp_get(tsd) += usize; *tsd_thread_allocatedp_get(tsd) += usize;