2019-08-10 13:12:47 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
|
|
|
cache_bin_t test_bin;
|
|
|
|
|
|
|
|
TEST_BEGIN(test_cache_bin) {
|
|
|
|
cache_bin_t *bin = &test_bin;
|
2019-11-22 06:10:03 +08:00
|
|
|
assert(PAGE > TCACHE_NSLOTS_SMALL_MAX * sizeof(void *));
|
2019-08-10 13:12:47 +08:00
|
|
|
/* Page aligned to make sure lowbits not overflowable. */
|
|
|
|
void **stack = mallocx(PAGE, MALLOCX_TCACHE_NONE | MALLOCX_ALIGN(PAGE));
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_not_null(stack, "Unexpected mallocx failure");
|
2019-08-10 13:12:47 +08:00
|
|
|
/* Initialize to empty; bin 0. */
|
2020-02-27 09:23:47 +08:00
|
|
|
cache_bin_sz_t ncached_max = cache_bin_info_ncached_max(
|
|
|
|
&tcache_bin_info[0]);
|
2019-08-10 13:12:47 +08:00
|
|
|
void **empty_position = stack + ncached_max;
|
|
|
|
bin->cur_ptr.ptr = empty_position;
|
|
|
|
bin->low_water_position = bin->cur_ptr.lowbits;
|
|
|
|
bin->full_position = (uint32_t)(uintptr_t)stack;
|
2020-02-29 10:55:33 +08:00
|
|
|
expect_ptr_eq(cache_bin_empty_position_get(bin, &tcache_bin_info[0]),
|
2020-02-26 04:14:48 +08:00
|
|
|
empty_position, "Incorrect empty position");
|
2020-02-19 06:39:06 +08:00
|
|
|
/* Not using expect_zu etc on cache_bin_sz_t since it may change. */
|
2020-02-29 10:55:33 +08:00
|
|
|
expect_true(cache_bin_ncached_get(bin, &tcache_bin_info[0]) == 0,
|
2020-02-26 04:14:48 +08:00
|
|
|
"Incorrect cache size");
|
2019-08-10 13:12:47 +08:00
|
|
|
|
|
|
|
bool success;
|
2020-02-29 10:55:33 +08:00
|
|
|
void *ret = cache_bin_alloc_easy(bin, &success, &tcache_bin_info[0]);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(success, "Empty cache bin should not alloc");
|
2020-02-29 10:55:33 +08:00
|
|
|
expect_true(cache_bin_low_water_get(bin, &tcache_bin_info[0]) == 0,
|
2019-08-10 13:12:47 +08:00
|
|
|
"Incorrect low water mark");
|
|
|
|
|
2020-02-29 10:55:33 +08:00
|
|
|
cache_bin_ncached_set(bin, 0, &tcache_bin_info[0]);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_eq(bin->cur_ptr.ptr, empty_position, "Bin should be empty");
|
2019-08-10 13:12:47 +08:00
|
|
|
for (cache_bin_sz_t i = 1; i < ncached_max + 1; i++) {
|
|
|
|
success = cache_bin_dalloc_easy(bin, (void *)(uintptr_t)i);
|
2020-02-29 10:55:33 +08:00
|
|
|
expect_true(success && cache_bin_ncached_get(bin,
|
|
|
|
&tcache_bin_info[0]) == i, "Bin dalloc failure");
|
2019-08-10 13:12:47 +08:00
|
|
|
}
|
|
|
|
success = cache_bin_dalloc_easy(bin, (void *)1);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(success, "Bin should be full");
|
|
|
|
expect_ptr_eq(bin->cur_ptr.ptr, stack, "Incorrect bin cur_ptr");
|
2019-08-10 13:12:47 +08:00
|
|
|
|
2020-02-29 10:55:33 +08:00
|
|
|
cache_bin_ncached_set(bin, ncached_max, &tcache_bin_info[0]);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_eq(bin->cur_ptr.ptr, stack, "cur_ptr should not change");
|
2019-08-10 13:12:47 +08:00
|
|
|
/* Emulate low water after refill. */
|
|
|
|
bin->low_water_position = bin->full_position;
|
|
|
|
for (cache_bin_sz_t i = ncached_max; i > 0; i--) {
|
2020-02-29 10:55:33 +08:00
|
|
|
ret = cache_bin_alloc_easy(bin, &success, &tcache_bin_info[0]);
|
|
|
|
cache_bin_sz_t ncached = cache_bin_ncached_get(bin,
|
|
|
|
&tcache_bin_info[0]);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_true(success && ncached == i - 1,
|
2019-08-10 13:12:47 +08:00
|
|
|
"Cache bin alloc failure");
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_ptr_eq(ret, (void *)(uintptr_t)i, "Bin alloc failure");
|
2020-02-29 10:55:33 +08:00
|
|
|
expect_true(cache_bin_low_water_get(bin, &tcache_bin_info[0])
|
2020-02-26 04:14:48 +08:00
|
|
|
== ncached, "Incorrect low water mark");
|
2019-08-10 13:12:47 +08:00
|
|
|
}
|
|
|
|
|
2020-02-29 10:55:33 +08:00
|
|
|
ret = cache_bin_alloc_easy(bin, &success, &tcache_bin_info[0]);
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(success, "Empty cache bin should not alloc.");
|
|
|
|
expect_ptr_eq(bin->cur_ptr.ptr, stack + ncached_max,
|
2019-08-10 13:12:47 +08:00
|
|
|
"Bin should be empty");
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void) {
|
|
|
|
return test(test_cache_bin);
|
|
|
|
}
|