mirror of
https://github.com/jemalloc/jemalloc.git
synced 2026-07-23 21:23:06 +00:00
Implement the *allocx() API.
Implement the *allocx() API, which is a successor to the *allocm() API.
The *allocx() functions are slightly simpler to use because they have
fewer parameters, they directly return the results of primary interest,
and mallocx()/rallocx() avoid the strict aliasing pitfall that
allocm()/rallocx() share with posix_memalign(). The following code
violates strict aliasing rules:
foo_t *foo;
allocm((void **)&foo, NULL, 42, 0);
whereas the following is safe:
foo_t *foo;
void *p;
allocm(&p, NULL, 42, 0);
foo = (foo_t *)p;
mallocx() does not have this problem:
foo_t *foo = (foo_t *)mallocx(42, 0);
This commit is contained in:
59
test/integration/xallocx.c
Normal file
59
test/integration/xallocx.c
Normal file
@@ -0,0 +1,59 @@
|
||||
#include "test/jemalloc_test.h"
|
||||
|
||||
TEST_BEGIN(test_same_size)
|
||||
{
|
||||
void *p;
|
||||
size_t sz, tsz;
|
||||
|
||||
p = mallocx(42, 0);
|
||||
assert_ptr_not_null(p, "Unexpected mallocx() error");
|
||||
sz = sallocx(p, 0);
|
||||
|
||||
tsz = xallocx(p, sz, 0, 0);
|
||||
assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
|
||||
|
||||
dallocx(p, 0);
|
||||
}
|
||||
TEST_END
|
||||
|
||||
TEST_BEGIN(test_extra_no_move)
|
||||
{
|
||||
void *p;
|
||||
size_t sz, tsz;
|
||||
|
||||
p = mallocx(42, 0);
|
||||
assert_ptr_not_null(p, "Unexpected mallocx() error");
|
||||
sz = sallocx(p, 0);
|
||||
|
||||
tsz = xallocx(p, sz, sz-42, 0);
|
||||
assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
|
||||
|
||||
dallocx(p, 0);
|
||||
}
|
||||
TEST_END
|
||||
|
||||
TEST_BEGIN(test_no_move_fail)
|
||||
{
|
||||
void *p;
|
||||
size_t sz, tsz;
|
||||
|
||||
p = mallocx(42, 0);
|
||||
assert_ptr_not_null(p, "Unexpected mallocx() error");
|
||||
sz = sallocx(p, 0);
|
||||
|
||||
tsz = xallocx(p, sz + 5, 0, 0);
|
||||
assert_zu_eq(tsz, sz, "Unexpected size change: %zu --> %zu", sz, tsz);
|
||||
|
||||
dallocx(p, 0);
|
||||
}
|
||||
TEST_END
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
|
||||
return (test(
|
||||
test_same_size,
|
||||
test_extra_no_move,
|
||||
test_no_move_fail));
|
||||
}
|
||||
Reference in New Issue
Block a user