ck_bag: Validate block_size > block_overhead during ck_bag_init.

Check ck_bag_init return value in unit test.
ck_pring
Abel Mathew 13 years ago
parent 2e921847ab
commit 6a6f1d53c8

@ -250,7 +250,10 @@ main(int argc, char **argv)
ck_epoch_init(&epoch_bag, 100);
ck_epoch_register(&epoch_bag, &epoch_wr);
ck_bag_allocator_set(&allocator, sizeof(struct bag_epoch));
ck_bag_init(&bag, b, CK_BAG_ALLOCATE_GEOMETRIC);
if (ck_bag_init(&bag, b, CK_BAG_ALLOCATE_GEOMETRIC) == false) {
fprintf(stderr, "Error: failed ck_bag_init().");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Configuration: %u entries, %zu bytes/block, %zu entries/block\n", writer_max, bag.info.bytes, bag.info.max);
i = 1;

@ -48,7 +48,7 @@ ck_bag_init(struct ck_bag *bag,
size_t n_cachelines,
enum ck_bag_allocation_strategy as)
{
size_t block_overhead;
size_t block_overhead, block_size;
CK_LIST_INIT(&bag->avail_blocks);
bag->head = NULL;
@ -56,19 +56,15 @@ ck_bag_init(struct ck_bag *bag,
bag->n_blocks = 0;
bag->alloc_strat = as;
/*
* By default, a ck_bag_block occupies two cachelines. If n_entries is less
* than the # of entries that can fit within one cacheline (including
* overhead), then bag->info.max = number of entries that can fit into a
* single cacheline.
*/
block_overhead = sizeof(struct ck_bag_block) + allocator_overhead;
if (n_cachelines == CK_BAG_DEFAULT) {
bag->info.max = ((CK_BAG_PAGESIZE - block_overhead) / sizeof(void *));
} else {
bag->info.max = ((n_cachelines * CK_MD_CACHELINE - block_overhead) / sizeof(void *));
}
block_size = (n_cachelines == CK_BAG_DEFAULT) ?
CK_BAG_PAGESIZE : n_cachelines * CK_MD_CACHELINE;
if (block_size < block_overhead)
return false;
bag->info.max = (block_size / sizeof(void *));
#ifdef __x86_64__
if (bag->info.max > CK_BAG_MAX_N_ENTRIES)

Loading…
Cancel
Save