2013-12-05 09:40:49 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2013-12-09 14:28:27 +08:00
|
|
|
#define THREAD_DATA 0x72b65c10
|
2013-12-05 09:40:49 +08:00
|
|
|
|
|
|
|
typedef unsigned int data_t;
|
|
|
|
|
2013-12-09 12:52:21 +08:00
|
|
|
static bool data_cleanup_executed;
|
|
|
|
|
Refactor/fix arenas manipulation.
Abstract arenas access to use arena_get() (or a0get() where appropriate)
rather than directly reading e.g. arenas[ind]. Prior to the addition of
the arenas.extend mallctl, the worst possible outcome of directly
accessing arenas was a stale read, but arenas.extend may allocate and
assign a new array to arenas.
Add a tsd-based arenas_cache, which amortizes arenas reads. This
introduces some subtle bootstrapping issues, with tsd_boot() now being
split into tsd_boot[01]() to support tsd wrapper allocation
bootstrapping, as well as an arenas_cache_bypass tsd variable which
dynamically terminates allocation of arenas_cache itself.
Promote a0malloc(), a0calloc(), and a0free() to be generally useful for
internal allocation, and use them in several places (more may be
appropriate).
Abstract arena->nthreads management and fix a missing decrement during
thread destruction (recent tsd refactoring left arenas_cleanup()
unused).
Change arena_choose() to propagate OOM, and handle OOM in all callers.
This is important for providing consistent allocation behavior when the
MALLOCX_ARENA() flag is being used. Prior to this fix, it was possible
for an OOM to result in allocation silently allocating from a different
arena than the one specified.
2014-10-08 14:14:57 +08:00
|
|
|
malloc_tsd_types(data_, data_t)
|
2014-10-05 02:12:53 +08:00
|
|
|
malloc_tsd_protos(, data_, data_t)
|
|
|
|
|
2013-12-05 09:40:49 +08:00
|
|
|
void
|
|
|
|
data_cleanup(void *arg)
|
|
|
|
{
|
|
|
|
data_t *data = (data_t *)arg;
|
|
|
|
|
2014-10-05 02:12:53 +08:00
|
|
|
if (!data_cleanup_executed) {
|
|
|
|
assert_x_eq(*data, THREAD_DATA,
|
|
|
|
"Argument passed into cleanup function should match tsd "
|
|
|
|
"value");
|
|
|
|
}
|
2013-12-09 12:52:21 +08:00
|
|
|
data_cleanup_executed = true;
|
2014-10-05 02:12:53 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate during cleanup for two rounds, in order to assure that
|
|
|
|
* jemalloc's internal tsd reinitialization happens.
|
|
|
|
*/
|
|
|
|
switch (*data) {
|
|
|
|
case THREAD_DATA:
|
|
|
|
*data = 1;
|
|
|
|
data_tsd_set(data);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*data = 2;
|
|
|
|
data_tsd_set(data);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
not_reached();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
void *p = mallocx(1, 0);
|
|
|
|
assert_ptr_not_null(p, "Unexpeced mallocx() failure");
|
|
|
|
dallocx(p, 0);
|
|
|
|
}
|
2013-12-05 09:40:49 +08:00
|
|
|
}
|
|
|
|
|
2014-09-23 12:09:23 +08:00
|
|
|
malloc_tsd_externs(data_, data_t)
|
2013-12-09 14:28:27 +08:00
|
|
|
#define DATA_INIT 0x12345678
|
2014-09-23 12:09:23 +08:00
|
|
|
malloc_tsd_data(, data_, data_t, DATA_INIT)
|
|
|
|
malloc_tsd_funcs(, data_, data_t, DATA_INIT, data_cleanup)
|
2013-12-05 09:40:49 +08:00
|
|
|
|
2013-12-13 06:41:02 +08:00
|
|
|
static void *
|
|
|
|
thd_start(void *arg)
|
2013-12-05 09:40:49 +08:00
|
|
|
{
|
2013-12-09 12:52:21 +08:00
|
|
|
data_t d = (data_t)(uintptr_t)arg;
|
2015-08-22 03:23:06 +08:00
|
|
|
void *p;
|
|
|
|
|
2013-12-09 12:52:21 +08:00
|
|
|
assert_x_eq(*data_tsd_get(), DATA_INIT,
|
|
|
|
"Initial tsd get should return initialization value");
|
2013-12-05 09:40:49 +08:00
|
|
|
|
2015-08-22 03:23:06 +08:00
|
|
|
p = malloc(1);
|
|
|
|
assert_ptr_not_null(p, "Unexpected malloc() failure");
|
|
|
|
|
2013-12-05 09:40:49 +08:00
|
|
|
data_tsd_set(&d);
|
2013-12-09 12:52:21 +08:00
|
|
|
assert_x_eq(*data_tsd_get(), d,
|
|
|
|
"After tsd set, tsd get should return value that was set");
|
2013-12-05 09:40:49 +08:00
|
|
|
|
|
|
|
d = 0;
|
2013-12-09 12:52:21 +08:00
|
|
|
assert_x_eq(*data_tsd_get(), (data_t)(uintptr_t)arg,
|
|
|
|
"Resetting local data should have no effect on tsd");
|
2013-12-05 09:40:49 +08:00
|
|
|
|
2015-08-22 03:23:06 +08:00
|
|
|
free(p);
|
2013-12-13 06:41:02 +08:00
|
|
|
return (NULL);
|
2013-12-05 09:40:49 +08:00
|
|
|
}
|
|
|
|
|
2013-12-09 12:52:21 +08:00
|
|
|
TEST_BEGIN(test_tsd_main_thread)
|
2013-12-05 09:40:49 +08:00
|
|
|
{
|
|
|
|
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_start((void *) 0xa5f3e329);
|
2013-12-09 12:52:21 +08:00
|
|
|
}
|
|
|
|
TEST_END
|
2013-12-05 09:40:49 +08:00
|
|
|
|
2013-12-09 12:52:21 +08:00
|
|
|
TEST_BEGIN(test_tsd_sub_thread)
|
|
|
|
{
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_t thd;
|
2013-12-09 12:52:21 +08:00
|
|
|
|
|
|
|
data_cleanup_executed = false;
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_create(&thd, thd_start, (void *)THREAD_DATA);
|
|
|
|
thd_join(thd, NULL);
|
2013-12-09 12:52:21 +08:00
|
|
|
assert_true(data_cleanup_executed,
|
|
|
|
"Cleanup function should have executed");
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
|
2016-05-08 03:42:31 +08:00
|
|
|
/* Core tsd bootstrapping must happen prior to data_tsd_boot(). */
|
|
|
|
if (nallocx(1, 0) == 0) {
|
|
|
|
malloc_printf("Initialization error");
|
|
|
|
return (test_status_fail);
|
|
|
|
}
|
2013-12-09 12:52:21 +08:00
|
|
|
data_tsd_boot();
|
2013-12-05 09:40:49 +08:00
|
|
|
|
2013-12-09 12:52:21 +08:00
|
|
|
return (test(
|
|
|
|
test_tsd_main_thread,
|
|
|
|
test_tsd_sub_thread));
|
2013-12-05 09:40:49 +08:00
|
|
|
}
|