blob: ac0f3b381954b6a6ed137394437dc05cb49360cc [file] [log] [blame]
Jason Evansa4f124f2013-12-08 22:28:27 -08001#define JEMALLOC_BITMAP_C_
Jason Evans84c8eef2011-03-16 10:30:13 -07002#include "jemalloc/internal/jemalloc_internal.h"
3
4/******************************************************************************/
Jason Evans84c8eef2011-03-16 10:30:13 -07005
Dave Watsonb8823ab2016-02-24 08:04:43 -08006#ifdef USE_TREE
7
Jason Evans84c8eef2011-03-16 10:30:13 -07008void
9bitmap_info_init(bitmap_info_t *binfo, size_t nbits)
10{
11 unsigned i;
12 size_t group_count;
13
14 assert(nbits > 0);
15 assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));
16
17 /*
18 * Compute the number of groups necessary to store nbits bits, and
19 * progressively work upward through the levels until reaching a level
20 * that requires only one group.
21 */
22 binfo->levels[0].group_offset = 0;
Jason Evansf97e5ac2014-09-28 14:43:11 -070023 group_count = BITMAP_BITS2GROUPS(nbits);
Jason Evans84c8eef2011-03-16 10:30:13 -070024 for (i = 1; group_count > 1; i++) {
25 assert(i < BITMAP_MAX_LEVELS);
26 binfo->levels[i].group_offset = binfo->levels[i-1].group_offset
27 + group_count;
Jason Evansf97e5ac2014-09-28 14:43:11 -070028 group_count = BITMAP_BITS2GROUPS(group_count);
Jason Evans84c8eef2011-03-16 10:30:13 -070029 }
30 binfo->levels[i].group_offset = binfo->levels[i-1].group_offset
31 + group_count;
Jason Evansf97e5ac2014-09-28 14:43:11 -070032 assert(binfo->levels[i].group_offset <= BITMAP_GROUPS_MAX);
Jason Evans84c8eef2011-03-16 10:30:13 -070033 binfo->nlevels = i;
34 binfo->nbits = nbits;
35}
36
Dave Watsonb8823ab2016-02-24 08:04:43 -080037static size_t
38bitmap_info_ngroups(const bitmap_info_t *binfo)
39{
40
41 return (binfo->levels[binfo->nlevels].group_offset);
42}
43
Jason Evans84c8eef2011-03-16 10:30:13 -070044void
45bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo)
46{
47 size_t extra;
48 unsigned i;
49
50 /*
51 * Bits are actually inverted with regard to the external bitmap
52 * interface, so the bitmap starts out with all 1 bits, except for
53 * trailing unused bits (if any). Note that each group uses bit 0 to
54 * correspond to the first logical bit in the group, so extra bits
55 * are the most significant bits of the last group.
56 */
Jason Evans01ecdf32016-02-26 13:59:41 -080057 memset(bitmap, 0xffU, bitmap_size(binfo));
Jason Evans84c8eef2011-03-16 10:30:13 -070058 extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))
59 & BITMAP_GROUP_NBITS_MASK;
60 if (extra != 0)
61 bitmap[binfo->levels[1].group_offset - 1] >>= extra;
62 for (i = 1; i < binfo->nlevels; i++) {
63 size_t group_count = binfo->levels[i].group_offset -
64 binfo->levels[i-1].group_offset;
65 extra = (BITMAP_GROUP_NBITS - (group_count &
66 BITMAP_GROUP_NBITS_MASK)) & BITMAP_GROUP_NBITS_MASK;
67 if (extra != 0)
68 bitmap[binfo->levels[i+1].group_offset - 1] >>= extra;
69 }
70}
Jason Evans01ecdf32016-02-26 13:59:41 -080071
Dave Watsonb8823ab2016-02-24 08:04:43 -080072#else /* USE_TREE */
73
74void
75bitmap_info_init(bitmap_info_t *binfo, size_t nbits)
76{
Dave Watsonb8823ab2016-02-24 08:04:43 -080077
78 assert(nbits > 0);
79 assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));
80
Jason Evans2ee2f1e2016-04-06 10:38:47 -070081 binfo->ngroups = BITMAP_BITS2GROUPS(nbits);
Dave Watsonb8823ab2016-02-24 08:04:43 -080082 binfo->nbits = nbits;
83}
84
Jason Evans01ecdf32016-02-26 13:59:41 -080085static size_t
86bitmap_info_ngroups(const bitmap_info_t *binfo)
87{
88
Dave Watsonb8823ab2016-02-24 08:04:43 -080089 return (binfo->ngroups);
Jason Evans01ecdf32016-02-26 13:59:41 -080090}
91
Dave Watsonb8823ab2016-02-24 08:04:43 -080092void
93bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo)
94{
95 size_t extra;
96
97 memset(bitmap, 0xffU, bitmap_size(binfo));
Jason Evans2ee2f1e2016-04-06 10:38:47 -070098 extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))
99 & BITMAP_GROUP_NBITS_MASK;
Dave Watsonb8823ab2016-02-24 08:04:43 -0800100 if (extra != 0)
Jason Evans2ee2f1e2016-04-06 10:38:47 -0700101 bitmap[binfo->ngroups - 1] >>= extra;
Dave Watsonb8823ab2016-02-24 08:04:43 -0800102}
103
104#endif /* USE_TREE */
105
Jason Evans01ecdf32016-02-26 13:59:41 -0800106size_t
107bitmap_size(const bitmap_info_t *binfo)
108{
109
110 return (bitmap_info_ngroups(binfo) << LG_SIZEOF_BITMAP);
111}