1 #define JEMALLOC_BITMAP_C_
2 #include "jemalloc/internal/jemalloc_internal.h"
4 /******************************************************************************/
5 /* Function prototypes for non-inline static functions. */
7 static size_t bits2groups(size_t nbits);
9 /******************************************************************************/
12 bits2groups(size_t nbits)
15 return ((nbits >> LG_BITMAP_GROUP_NBITS) +
16 !!(nbits & BITMAP_GROUP_NBITS_MASK));
20 bitmap_info_init(bitmap_info_t *binfo, size_t nbits)
26 assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));
29 * Compute the number of groups necessary to store nbits bits, and
30 * progressively work upward through the levels until reaching a level
31 * that requires only one group.
33 binfo->levels[0].group_offset = 0;
34 group_count = bits2groups(nbits);
35 for (i = 1; group_count > 1; i++) {
36 assert(i < BITMAP_MAX_LEVELS);
37 binfo->levels[i].group_offset = binfo->levels[i-1].group_offset
39 group_count = bits2groups(group_count);
41 binfo->levels[i].group_offset = binfo->levels[i-1].group_offset
48 bitmap_info_ngroups(const bitmap_info_t *binfo)
51 return (binfo->levels[binfo->nlevels].group_offset << LG_SIZEOF_BITMAP);
55 bitmap_size(size_t nbits)
59 bitmap_info_init(&binfo, nbits);
60 return (bitmap_info_ngroups(&binfo));
64 bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo)
70 * Bits are actually inverted with regard to the external bitmap
71 * interface, so the bitmap starts out with all 1 bits, except for
72 * trailing unused bits (if any). Note that each group uses bit 0 to
73 * correspond to the first logical bit in the group, so extra bits
74 * are the most significant bits of the last group.
76 memset(bitmap, 0xffU, binfo->levels[binfo->nlevels].group_offset <<
78 extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))
79 & BITMAP_GROUP_NBITS_MASK;
81 bitmap[binfo->levels[1].group_offset - 1] >>= extra;
82 for (i = 1; i < binfo->nlevels; i++) {
83 size_t group_count = binfo->levels[i].group_offset -
84 binfo->levels[i-1].group_offset;
85 extra = (BITMAP_GROUP_NBITS - (group_count &
86 BITMAP_GROUP_NBITS_MASK)) & BITMAP_GROUP_NBITS_MASK;
88 bitmap[binfo->levels[i+1].group_offset - 1] >>= extra;