Implement BITMAP_INFO_INITIALIZER(nbits).

This allows static initialization of bitmap_info_t structures.
This commit is contained in:
Jason Evans 2016-04-06 18:30:02 -07:00
parent 17c021c177
commit b683734b43
3 changed files with 367 additions and 132 deletions

View File

@ -12,7 +12,7 @@ typedef unsigned long bitmap_t;
/* Number of bits per group. */
#define LG_BITMAP_GROUP_NBITS (LG_SIZEOF_BITMAP + 3)
#define BITMAP_GROUP_NBITS (ZU(1) << LG_BITMAP_GROUP_NBITS)
#define BITMAP_GROUP_NBITS (1U << LG_BITMAP_GROUP_NBITS)
#define BITMAP_GROUP_NBITS_MASK (BITMAP_GROUP_NBITS-1)
/*
@ -21,12 +21,12 @@ typedef unsigned long bitmap_t;
* use a tree instead.
*/
#if LG_BITMAP_MAXBITS - LG_BITMAP_GROUP_NBITS > 3
# define USE_TREE
# define BITMAP_USE_TREE
#endif
/* Number of groups required to store a given number of bits. */
#define BITMAP_BITS2GROUPS(nbits) \
((nbits + BITMAP_GROUP_NBITS_MASK) >> LG_BITMAP_GROUP_NBITS)
(((nbits) + BITMAP_GROUP_NBITS_MASK) >> LG_BITMAP_GROUP_NBITS)
/*
* Number of groups required at a particular level for a given number of bits.
@ -40,6 +40,9 @@ typedef unsigned long bitmap_t;
#define BITMAP_GROUPS_L3(nbits) \
BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS( \
BITMAP_BITS2GROUPS((nbits)))))
#define BITMAP_GROUPS_L4(nbits) \
BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS( \
BITMAP_BITS2GROUPS(BITMAP_BITS2GROUPS((nbits))))))
/*
* Assuming the number of levels, number of groups required for a given number
@ -53,11 +56,13 @@ typedef unsigned long bitmap_t;
(BITMAP_GROUPS_2_LEVEL(nbits) + BITMAP_GROUPS_L2(nbits))
#define BITMAP_GROUPS_4_LEVEL(nbits) \
(BITMAP_GROUPS_3_LEVEL(nbits) + BITMAP_GROUPS_L3(nbits))
#define BITMAP_GROUPS_5_LEVEL(nbits) \
(BITMAP_GROUPS_4_LEVEL(nbits) + BITMAP_GROUPS_L4(nbits))
/*
* Maximum number of groups required to support LG_BITMAP_MAXBITS.
*/
#ifdef USE_TREE
#ifdef BITMAP_USE_TREE
#if LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS
# define BITMAP_GROUPS_MAX BITMAP_GROUPS_1_LEVEL(BITMAP_MAXBITS)
@ -67,20 +72,63 @@ typedef unsigned long bitmap_t;
# define BITMAP_GROUPS_MAX BITMAP_GROUPS_3_LEVEL(BITMAP_MAXBITS)
#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 4
# define BITMAP_GROUPS_MAX BITMAP_GROUPS_4_LEVEL(BITMAP_MAXBITS)
#elif LG_BITMAP_MAXBITS <= LG_BITMAP_GROUP_NBITS * 5
# define BITMAP_GROUPS_MAX BITMAP_GROUPS_5_LEVEL(BITMAP_MAXBITS)
#else
# error "Unsupported bitmap size"
#endif
/* Maximum number of levels possible. */
#define BITMAP_MAX_LEVELS \
(LG_BITMAP_MAXBITS / LG_SIZEOF_BITMAP) \
+ !!(LG_BITMAP_MAXBITS % LG_SIZEOF_BITMAP)
/*
* Maximum number of levels possible. This could be statically computed based
* on LG_BITMAP_MAXBITS:
*
* #define BITMAP_MAX_LEVELS \
* (LG_BITMAP_MAXBITS / LG_SIZEOF_BITMAP) \
* + !!(LG_BITMAP_MAXBITS % LG_SIZEOF_BITMAP)
*
* However, that would not allow the generic BITMAP_INFO_INITIALIZER() macro, so
* instead hardcode BITMAP_MAX_LEVELS to the largest number supported by the
* various cascading macros. The only additional cost this incurs is some
* unused trailing entries in bitmap_info_t structures; the bitmaps themselves
* are not impacted.
*/
#define BITMAP_MAX_LEVELS 5
#else /* USE_TREE */
#define BITMAP_INFO_INITIALIZER(nbits) { \
/* nbits. */ \
nbits, \
/* nlevels. */ \
(BITMAP_GROUPS_L0(nbits) > BITMAP_GROUPS_L1(nbits)) + \
(BITMAP_GROUPS_L1(nbits) > BITMAP_GROUPS_L2(nbits)) + \
(BITMAP_GROUPS_L2(nbits) > BITMAP_GROUPS_L3(nbits)) + \
(BITMAP_GROUPS_L3(nbits) > BITMAP_GROUPS_L4(nbits)) + 1, \
/* levels. */ \
{ \
{0}, \
{BITMAP_GROUPS_L0(nbits)}, \
{BITMAP_GROUPS_L1(nbits) + BITMAP_GROUPS_L0(nbits)}, \
{BITMAP_GROUPS_L2(nbits) + BITMAP_GROUPS_L1(nbits) + \
BITMAP_GROUPS_L0(nbits)}, \
{BITMAP_GROUPS_L3(nbits) + BITMAP_GROUPS_L2(nbits) + \
BITMAP_GROUPS_L1(nbits) + BITMAP_GROUPS_L0(nbits)}, \
{BITMAP_GROUPS_L4(nbits) + BITMAP_GROUPS_L3(nbits) + \
BITMAP_GROUPS_L2(nbits) + BITMAP_GROUPS_L1(nbits) \
+ BITMAP_GROUPS_L0(nbits)} \
} \
}
#define BITMAP_GROUPS_MAX BITMAP_BITS2GROUPS(BITMAP_MAXBITS)
#else /* BITMAP_USE_TREE */
#endif /* USE_TREE */
#define BITMAP_GROUPS_MAX BITMAP_BITS2GROUPS(BITMAP_MAXBITS)
#define BITMAP_INFO_INITIALIZER(nbits) { \
/* nbits. */ \
nbits, \
/* ngroups. */ \
BITMAP_BITS2GROUPS(nbits) \
}
#endif /* BITMAP_USE_TREE */
#endif /* JEMALLOC_H_TYPES */
/******************************************************************************/
@ -95,7 +143,7 @@ struct bitmap_info_s {
/* Logical number of bits in bitmap (stored at bottom level). */
size_t nbits;
#ifdef USE_TREE
#ifdef BITMAP_USE_TREE
/* Number of levels necessary for nbits. */
unsigned nlevels;
@ -104,10 +152,10 @@ struct bitmap_info_s {
* bottom to top (e.g. the bottom level is stored in levels[0]).
*/
bitmap_level_t levels[BITMAP_MAX_LEVELS+1];
#else /* USE_TREE */
#else /* BITMAP_USE_TREE */
/* Number of groups necessary for nbits. */
size_t ngroups;
#endif /* USE_TREE */
#endif /* BITMAP_USE_TREE */
};
#endif /* JEMALLOC_H_STRUCTS */
@ -134,7 +182,7 @@ void bitmap_unset(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit);
JEMALLOC_INLINE bool
bitmap_full(bitmap_t *bitmap, const bitmap_info_t *binfo)
{
#ifdef USE_TREE
#ifdef BITMAP_USE_TREE
size_t rgoff = binfo->levels[binfo->nlevels].group_offset - 1;
bitmap_t rg = bitmap[rgoff];
/* The bitmap is full iff the root group is 0. */
@ -178,7 +226,7 @@ bitmap_set(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit)
g ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);
*gp = g;
assert(bitmap_get(bitmap, binfo, bit));
#ifdef USE_TREE
#ifdef BITMAP_USE_TREE
/* Propagate group state transitions up the tree. */
if (g == 0) {
unsigned i;
@ -207,7 +255,7 @@ bitmap_sfu(bitmap_t *bitmap, const bitmap_info_t *binfo)
assert(!bitmap_full(bitmap, binfo));
#ifdef USE_TREE
#ifdef BITMAP_USE_TREE
i = binfo->nlevels - 1;
g = bitmap[binfo->levels[i].group_offset];
bit = ffs_lu(g) - 1;
@ -247,7 +295,7 @@ bitmap_unset(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit)
g ^= ZU(1) << (bit & BITMAP_GROUP_NBITS_MASK);
*gp = g;
assert(!bitmap_get(bitmap, binfo, bit));
#ifdef USE_TREE
#ifdef BITMAP_USE_TREE
/* Propagate group state transitions up the tree. */
if (propagate) {
unsigned i;
@ -265,7 +313,7 @@ bitmap_unset(bitmap_t *bitmap, const bitmap_info_t *binfo, size_t bit)
break;
}
}
#endif /* USE_TREE */
#endif /* BITMAP_USE_TREE */
}
#endif

View File

@ -3,7 +3,7 @@
/******************************************************************************/
#ifdef USE_TREE
#ifdef BITMAP_USE_TREE
void
bitmap_info_init(bitmap_info_t *binfo, size_t nbits)
@ -69,7 +69,7 @@ bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo)
}
}
#else /* USE_TREE */
#else /* BITMAP_USE_TREE */
void
bitmap_info_init(bitmap_info_t *binfo, size_t nbits)
@ -101,7 +101,7 @@ bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo)
bitmap[binfo->ngroups - 1] >>= extra;
}
#endif /* USE_TREE */
#endif /* BITMAP_USE_TREE */
size_t
bitmap_size(const bitmap_info_t *binfo)

View File

@ -1,152 +1,338 @@
#include "test/jemalloc_test.h"
#define NBITS_TAB \
NB( 1) \
NB( 2) \
NB( 3) \
NB( 4) \
NB( 5) \
NB( 6) \
NB( 7) \
NB( 8) \
NB( 9) \
NB(10) \
NB(11) \
NB(12) \
NB(13) \
NB(14) \
NB(15) \
NB(16) \
NB(17) \
NB(18) \
NB(19) \
NB(20) \
NB(21) \
NB(22) \
NB(23) \
NB(24) \
NB(25) \
NB(26) \
NB(27) \
NB(28) \
NB(29) \
NB(30) \
NB(31) \
NB(32) \
\
NB(33) \
NB(34) \
NB(35) \
NB(36) \
NB(37) \
NB(38) \
NB(39) \
NB(40) \
NB(41) \
NB(42) \
NB(43) \
NB(44) \
NB(45) \
NB(46) \
NB(47) \
NB(48) \
NB(49) \
NB(50) \
NB(51) \
NB(52) \
NB(53) \
NB(54) \
NB(55) \
NB(56) \
NB(57) \
NB(58) \
NB(59) \
NB(60) \
NB(61) \
NB(62) \
NB(63) \
NB(64) \
NB(65) \
\
NB(126) \
NB(127) \
NB(128) \
NB(129) \
NB(130) \
\
NB(254) \
NB(255) \
NB(256) \
NB(257) \
NB(258) \
\
NB(510) \
NB(511) \
NB(512) \
NB(513) \
NB(514) \
\
NB(1024) \
NB(2048) \
NB(4096) \
NB(8192) \
NB(16384) \
static void
test_bitmap_initializer_body(const bitmap_info_t *binfo, size_t nbits)
{
bitmap_info_t binfo_dyn;
bitmap_info_init(&binfo_dyn, nbits);
assert_zu_eq(bitmap_size(binfo), bitmap_size(&binfo_dyn),
"Unexpected difference between static and dynamic initialization, "
"nbits=%zu", nbits);
assert_zu_eq(binfo->nbits, binfo_dyn.nbits,
"Unexpected difference between static and dynamic initialization, "
"nbits=%zu", nbits);
#ifdef BITMAP_USE_TREE
assert_u_eq(binfo->nlevels, binfo_dyn.nlevels,
"Unexpected difference between static and dynamic initialization, "
"nbits=%zu", nbits);
{
unsigned i;
for (i = 0; i < binfo->nlevels; i++) {
assert_zu_eq(binfo->levels[i].group_offset,
binfo_dyn.levels[i].group_offset,
"Unexpected difference between static and dynamic "
"initialization, nbits=%zu, level=%u", nbits, i);
}
}
#else
assert_zu_eq(binfo->ngroups, binfo_dyn.ngroups,
"Unexpected difference between static and dynamic initialization");
#endif
}
TEST_BEGIN(test_bitmap_initializer)
{
#define NB(nbits) { \
if (nbits <= BITMAP_MAXBITS) { \
bitmap_info_t binfo = \
BITMAP_INFO_INITIALIZER(nbits); \
test_bitmap_initializer_body(&binfo, nbits); \
} \
}
NBITS_TAB
#undef NB
}
TEST_END
static size_t
test_bitmap_size_body(const bitmap_info_t *binfo, size_t nbits,
size_t prev_size)
{
size_t size = bitmap_size(binfo);
assert_zu_ge(size, (nbits >> 3),
"Bitmap size is smaller than expected");
assert_zu_ge(size, prev_size, "Bitmap size is smaller than expected");
return (size);
}
TEST_BEGIN(test_bitmap_size)
{
size_t i, prev_size;
size_t nbits, prev_size;
prev_size = 0;
for (i = 1; i <= BITMAP_MAXBITS; i++) {
for (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {
bitmap_info_t binfo;
size_t size;
bitmap_info_init(&binfo, i);
size = bitmap_size(&binfo);
assert_true(size >= prev_size,
"Bitmap size is smaller than expected");
prev_size = size;
bitmap_info_init(&binfo, nbits);
prev_size = test_bitmap_size_body(&binfo, nbits, prev_size);
}
#define NB(nbits) { \
bitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits); \
prev_size = test_bitmap_size_body(&binfo, nbits, \
prev_size); \
}
prev_size = 0;
NBITS_TAB
#undef NB
}
TEST_END
static void
test_bitmap_init_body(const bitmap_info_t *binfo, size_t nbits)
{
size_t i;
bitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));
assert_ptr_not_null(bitmap, "Unexpected malloc() failure");
bitmap_init(bitmap, binfo);
for (i = 0; i < nbits; i++) {
assert_false(bitmap_get(bitmap, binfo, i),
"Bit should be unset");
}
free(bitmap);
}
TEST_BEGIN(test_bitmap_init)
{
size_t i;
size_t nbits;
for (i = 1; i <= BITMAP_MAXBITS; i++) {
for (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {
bitmap_info_t binfo;
bitmap_info_init(&binfo, i);
{
size_t j;
bitmap_t *bitmap = (bitmap_t *)malloc(
bitmap_size(&binfo));
bitmap_init(bitmap, &binfo);
for (j = 0; j < i; j++) {
assert_false(bitmap_get(bitmap, &binfo, j),
"Bit should be unset");
}
free(bitmap);
}
bitmap_info_init(&binfo, nbits);
test_bitmap_init_body(&binfo, nbits);
}
#define NB(nbits) { \
bitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits); \
test_bitmap_init_body(&binfo, nbits); \
}
NBITS_TAB
#undef NB
}
TEST_END
static void
test_bitmap_set_body(const bitmap_info_t *binfo, size_t nbits)
{
size_t i;
bitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));
assert_ptr_not_null(bitmap, "Unexpected malloc() failure");
bitmap_init(bitmap, binfo);
for (i = 0; i < nbits; i++)
bitmap_set(bitmap, binfo, i);
assert_true(bitmap_full(bitmap, binfo), "All bits should be set");
free(bitmap);
}
TEST_BEGIN(test_bitmap_set)
{
size_t i;
size_t nbits;
for (i = 1; i <= BITMAP_MAXBITS; i++) {
for (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {
bitmap_info_t binfo;
bitmap_info_init(&binfo, i);
{
size_t j;
bitmap_t *bitmap = (bitmap_t *)malloc(
bitmap_size(&binfo));
bitmap_init(bitmap, &binfo);
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
free(bitmap);
}
bitmap_info_init(&binfo, nbits);
test_bitmap_set_body(&binfo, nbits);
}
#define NB(nbits) { \
bitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits); \
test_bitmap_set_body(&binfo, nbits); \
}
NBITS_TAB
#undef NB
}
TEST_END
static void
test_bitmap_unset_body(const bitmap_info_t *binfo, size_t nbits)
{
size_t i;
bitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));
assert_ptr_not_null(bitmap, "Unexpected malloc() failure");
bitmap_init(bitmap, binfo);
for (i = 0; i < nbits; i++)
bitmap_set(bitmap, binfo, i);
assert_true(bitmap_full(bitmap, binfo), "All bits should be set");
for (i = 0; i < nbits; i++)
bitmap_unset(bitmap, binfo, i);
for (i = 0; i < nbits; i++)
bitmap_set(bitmap, binfo, i);
assert_true(bitmap_full(bitmap, binfo), "All bits should be set");
free(bitmap);
}
TEST_BEGIN(test_bitmap_unset)
{
size_t i;
size_t nbits;
for (i = 1; i <= BITMAP_MAXBITS; i++) {
for (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {
bitmap_info_t binfo;
bitmap_info_init(&binfo, i);
{
size_t j;
bitmap_t *bitmap = (bitmap_t *)malloc(
bitmap_size(&binfo));
bitmap_init(bitmap, &binfo);
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
for (j = 0; j < i; j++)
bitmap_unset(bitmap, &binfo, j);
for (j = 0; j < i; j++)
bitmap_set(bitmap, &binfo, j);
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
free(bitmap);
}
bitmap_info_init(&binfo, nbits);
test_bitmap_unset_body(&binfo, nbits);
}
#define NB(nbits) { \
bitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits); \
test_bitmap_unset_body(&binfo, nbits); \
}
NBITS_TAB
#undef NB
}
TEST_END
TEST_BEGIN(test_bitmap_sfu)
static void
test_bitmap_sfu_body(const bitmap_info_t *binfo, size_t nbits)
{
size_t i;
bitmap_t *bitmap = (bitmap_t *)malloc(bitmap_size(binfo));
assert_ptr_not_null(bitmap, "Unexpected malloc() failure");
bitmap_init(bitmap, binfo);
for (i = 1; i <= BITMAP_MAXBITS; i++) {
bitmap_info_t binfo;
bitmap_info_init(&binfo, i);
{
size_t j;
bitmap_t *bitmap = (bitmap_t *)malloc(
bitmap_size(&binfo));
bitmap_init(bitmap, &binfo);
/* Iteratively set bits starting at the beginning. */
for (j = 0; j < i; j++) {
assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
"First unset bit should be just after "
"previous first unset bit");
}
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
/*
* Iteratively unset bits starting at the end, and
* verify that bitmap_sfu() reaches the unset bits.
*/
for (j = i - 1; j < i; j--) { /* (i..0] */
bitmap_unset(bitmap, &binfo, j);
assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
"First unset bit should the bit previously "
"unset");
bitmap_unset(bitmap, &binfo, j);
}
assert_false(bitmap_get(bitmap, &binfo, 0),
"Bit should be unset");
/*
* Iteratively set bits starting at the beginning, and
* verify that bitmap_sfu() looks past them.
*/
for (j = 1; j < i; j++) {
bitmap_set(bitmap, &binfo, j - 1);
assert_zd_eq(bitmap_sfu(bitmap, &binfo), j,
"First unset bit should be just after the "
"bit previously set");
bitmap_unset(bitmap, &binfo, j);
}
assert_zd_eq(bitmap_sfu(bitmap, &binfo), i - 1,
"First unset bit should be the last bit");
assert_true(bitmap_full(bitmap, &binfo),
"All bits should be set");
free(bitmap);
}
/* Iteratively set bits starting at the beginning. */
for (i = 0; i < nbits; i++) {
assert_zd_eq(bitmap_sfu(bitmap, binfo), i,
"First unset bit should be just after previous first unset "
"bit");
}
assert_true(bitmap_full(bitmap, binfo), "All bits should be set");
/*
* Iteratively unset bits starting at the end, and verify that
* bitmap_sfu() reaches the unset bits.
*/
for (i = nbits - 1; i < nbits; i--) { /* (nbits..0] */
bitmap_unset(bitmap, binfo, i);
assert_zd_eq(bitmap_sfu(bitmap, binfo), i,
"First unset bit should the bit previously unset");
bitmap_unset(bitmap, binfo, i);
}
assert_false(bitmap_get(bitmap, binfo, 0), "Bit should be unset");
/*
* Iteratively set bits starting at the beginning, and verify that
* bitmap_sfu() looks past them.
*/
for (i = 1; i < nbits; i++) {
bitmap_set(bitmap, binfo, i - 1);
assert_zd_eq(bitmap_sfu(bitmap, binfo), i,
"First unset bit should be just after the bit previously "
"set");
bitmap_unset(bitmap, binfo, i);
}
assert_zd_eq(bitmap_sfu(bitmap, binfo), nbits - 1,
"First unset bit should be the last bit");
assert_true(bitmap_full(bitmap, binfo), "All bits should be set");
free(bitmap);
}
TEST_BEGIN(test_bitmap_sfu)
{
size_t nbits;
for (nbits = 1; nbits <= BITMAP_MAXBITS; nbits++) {
bitmap_info_t binfo;
bitmap_info_init(&binfo, nbits);
test_bitmap_sfu_body(&binfo, nbits);
}
#define NB(nbits) { \
bitmap_info_t binfo = BITMAP_INFO_INITIALIZER(nbits); \
test_bitmap_sfu_body(&binfo, nbits); \
}
NBITS_TAB
#undef NB
}
TEST_END
@ -155,6 +341,7 @@ main(void)
{
return (test(
test_bitmap_initializer,
test_bitmap_size,
test_bitmap_init,
test_bitmap_set,