mirror of
https://github.com/VectorCamp/vectorscan.git
synced 2025-10-10 08:12:21 +03:00
multibit, fatbit: make _size build-time only
This commit makes mmbit_size() and fatbit_size compile-time only, and adds a resource limit for very large multibits.
This commit is contained in:
committed by
Matthew Barr
parent
8b7b06d2a4
commit
e271781d95
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Intel Corporation
|
||||
* Copyright (c) 2015-2016, Intel Corporation
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
@@ -138,62 +138,3 @@ const u32 mmbit_root_offset_from_level[7] = {
|
||||
1 + (1 << MMB_KEY_SHIFT) + (1 << MMB_KEY_SHIFT * 2) + (1 << MMB_KEY_SHIFT * 3) + (1 << MMB_KEY_SHIFT * 4),
|
||||
1 + (1 << MMB_KEY_SHIFT) + (1 << MMB_KEY_SHIFT * 2) + (1 << MMB_KEY_SHIFT * 3) + (1 << MMB_KEY_SHIFT * 4) + (1 << MMB_KEY_SHIFT * 5),
|
||||
};
|
||||
|
||||
u32 mmbit_size(u32 total_bits) {
|
||||
MDEBUG_PRINTF("%u\n", total_bits);
|
||||
|
||||
// Flat model multibit structures are just stored as a bit vector.
|
||||
if (total_bits <= MMB_FLAT_MAX_BITS) {
|
||||
return ROUNDUP_N(total_bits, 8) / 8;
|
||||
}
|
||||
|
||||
u64a current_level = 1; // Number of blocks on current level.
|
||||
u64a total = 0; // Total number of blocks.
|
||||
while (current_level * MMB_KEY_BITS < total_bits) {
|
||||
total += current_level;
|
||||
current_level <<= MMB_KEY_SHIFT;
|
||||
}
|
||||
|
||||
// Last level is a one-for-one bit vector. It needs room for total_bits
|
||||
// elements, rounded up to the nearest block.
|
||||
u64a last_level = ((u64a)total_bits + MMB_KEY_BITS - 1) / MMB_KEY_BITS;
|
||||
total += last_level;
|
||||
|
||||
assert(total * sizeof(MMB_TYPE) <= UINT32_MAX);
|
||||
return (u32)(total * sizeof(MMB_TYPE));
|
||||
}
|
||||
|
||||
#ifdef DUMP_SUPPORT
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** \brief Dump a sparse iterator's keys to stdout. */
|
||||
void mmbit_sparse_iter_dump(const struct mmbit_sparse_iter *it,
|
||||
u32 total_bits) {
|
||||
// Expediency and future-proofing: create a temporary multibit of the right
|
||||
// size with all the bits on, then walk it with this sparse iterator.
|
||||
size_t bytes = mmbit_size(total_bits);
|
||||
u8 *bits = malloc(bytes);
|
||||
if (!bits) {
|
||||
printf("Failed to alloc %zu bytes for temp multibit", bytes);
|
||||
return;
|
||||
}
|
||||
for (u32 i = 0; i < total_bits; i++) {
|
||||
mmbit_set_i(bits, total_bits, i);
|
||||
}
|
||||
|
||||
struct mmbit_sparse_state s[MAX_SPARSE_ITER_STATES];
|
||||
u32 idx = 0;
|
||||
for (u32 i = mmbit_sparse_iter_begin(bits, total_bits, &idx, it, s);
|
||||
i != MMB_INVALID;
|
||||
i = mmbit_sparse_iter_next(bits, total_bits, i, &idx, it, s)) {
|
||||
printf("%u ", i);
|
||||
}
|
||||
|
||||
printf("(%u keys)", idx + 1);
|
||||
|
||||
free(bits);
|
||||
}
|
||||
|
||||
#endif // DUMP_SUPPORT
|
||||
|
Reference in New Issue
Block a user