blob: 468b3178ebfa62af8c9ac88f5e934e66e4413109 [file] [log] [blame]
Jason Evansc0cc5db2017-01-19 21:41:41 -08001#define JEMALLOC_BITMAP_C_
David Goldblatt743d9402017-04-10 18:17:55 -07002#include "jemalloc/internal/jemalloc_preamble.h"
3#include "jemalloc/internal/jemalloc_internal_includes.h"
Jason Evans84c8eef2011-03-16 10:30:13 -07004
David Goldblattd9ec36e2017-04-11 14:43:12 -07005#include "jemalloc/internal/assert.h"
6
Jason Evans84c8eef2011-03-16 10:30:13 -07007/******************************************************************************/
Jason Evans84c8eef2011-03-16 10:30:13 -07008
Jason Evans45f087e2017-04-16 09:25:56 -07009#ifdef BITMAP_USE_TREE
10
11void
12bitmap_info_init(bitmap_info_t *binfo, size_t nbits) {
13 unsigned i;
14 size_t group_count;
15
16 assert(nbits > 0);
17 assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));
18
19 /*
20 * Compute the number of groups necessary to store nbits bits, and
21 * progressively work upward through the levels until reaching a level
22 * that requires only one group.
23 */
24 binfo->levels[0].group_offset = 0;
25 group_count = BITMAP_BITS2GROUPS(nbits);
26 for (i = 1; group_count > 1; i++) {
27 assert(i < BITMAP_MAX_LEVELS);
28 binfo->levels[i].group_offset = binfo->levels[i-1].group_offset
29 + group_count;
30 group_count = BITMAP_BITS2GROUPS(group_count);
31 }
32 binfo->levels[i].group_offset = binfo->levels[i-1].group_offset
33 + group_count;
34 assert(binfo->levels[i].group_offset <= BITMAP_GROUPS_MAX);
35 binfo->nlevels = i;
36 binfo->nbits = nbits;
37}
38
39static size_t
40bitmap_info_ngroups(const bitmap_info_t *binfo) {
41 return binfo->levels[binfo->nlevels].group_offset;
42}
43
44void
45bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill) {
46 size_t extra;
47 unsigned i;
48
49 /*
50 * Bits are actually inverted with regard to the external bitmap
51 * interface.
52 */
53
54 if (fill) {
55 /* The "filled" bitmap starts out with all 0 bits. */
56 memset(bitmap, 0, bitmap_size(binfo));
57 return;
58 }
59
60 /*
61 * The "empty" bitmap starts out with all 1 bits, except for trailing
62 * unused bits (if any). Note that each group uses bit 0 to correspond
63 * to the first logical bit in the group, so extra bits are the most
64 * significant bits of the last group.
65 */
66 memset(bitmap, 0xffU, bitmap_size(binfo));
67 extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))
68 & BITMAP_GROUP_NBITS_MASK;
69 if (extra != 0) {
70 bitmap[binfo->levels[1].group_offset - 1] >>= extra;
71 }
72 for (i = 1; i < binfo->nlevels; i++) {
73 size_t group_count = binfo->levels[i].group_offset -
74 binfo->levels[i-1].group_offset;
75 extra = (BITMAP_GROUP_NBITS - (group_count &
76 BITMAP_GROUP_NBITS_MASK)) & BITMAP_GROUP_NBITS_MASK;
77 if (extra != 0) {
78 bitmap[binfo->levels[i+1].group_offset - 1] >>= extra;
79 }
80 }
81}
82
83#else /* BITMAP_USE_TREE */
84
Dave Watsonb8823ab2016-02-24 08:04:43 -080085void
Jason Evansc4c25922017-01-15 16:56:30 -080086bitmap_info_init(bitmap_info_t *binfo, size_t nbits) {
Dave Watsonb8823ab2016-02-24 08:04:43 -080087 assert(nbits > 0);
88 assert(nbits <= (ZU(1) << LG_BITMAP_MAXBITS));
89
Jason Evans2ee2f1e2016-04-06 10:38:47 -070090 binfo->ngroups = BITMAP_BITS2GROUPS(nbits);
Dave Watsonb8823ab2016-02-24 08:04:43 -080091 binfo->nbits = nbits;
92}
93
Jason Evans01ecdf32016-02-26 13:59:41 -080094static size_t
Jason Evansc4c25922017-01-15 16:56:30 -080095bitmap_info_ngroups(const bitmap_info_t *binfo) {
Jason Evansf4086432017-01-19 18:15:45 -080096 return binfo->ngroups;
Jason Evans01ecdf32016-02-26 13:59:41 -080097}
98
Dave Watsonb8823ab2016-02-24 08:04:43 -080099void
Jason Evansc8021d02017-03-23 17:59:47 -0700100bitmap_init(bitmap_t *bitmap, const bitmap_info_t *binfo, bool fill) {
Dave Watsonb8823ab2016-02-24 08:04:43 -0800101 size_t extra;
102
Jason Evansc8021d02017-03-23 17:59:47 -0700103 if (fill) {
104 memset(bitmap, 0, bitmap_size(binfo));
105 return;
106 }
107
Dave Watsonb8823ab2016-02-24 08:04:43 -0800108 memset(bitmap, 0xffU, bitmap_size(binfo));
Jason Evans2ee2f1e2016-04-06 10:38:47 -0700109 extra = (BITMAP_GROUP_NBITS - (binfo->nbits & BITMAP_GROUP_NBITS_MASK))
110 & BITMAP_GROUP_NBITS_MASK;
Jason Evansc4c25922017-01-15 16:56:30 -0800111 if (extra != 0) {
Jason Evans2ee2f1e2016-04-06 10:38:47 -0700112 bitmap[binfo->ngroups - 1] >>= extra;
Jason Evansc4c25922017-01-15 16:56:30 -0800113 }
Dave Watsonb8823ab2016-02-24 08:04:43 -0800114}
115
Jason Evans45f087e2017-04-16 09:25:56 -0700116#endif /* BITMAP_USE_TREE */
117
Jason Evans01ecdf32016-02-26 13:59:41 -0800118size_t
Jason Evansc4c25922017-01-15 16:56:30 -0800119bitmap_size(const bitmap_info_t *binfo) {
Jason Evans01ecdf32016-02-26 13:59:41 -0800120 return (bitmap_info_ngroups(binfo) << LG_SIZEOF_BITMAP);
121}