Skip to content

Commit 516f196

Browse files
committed
allocator: use compiler to compute sizeof bitmap_t
Rather than hardcoding the sizeof(bitmap_t) based on `__LP64__` use the compiler builtin `sizeof` to compute the size.
1 parent 61ec2f7 commit 516f196

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/allocator.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ first_bitmap_in_same_page(bitmap_t *b)
202202
dispatch_assert(b < &m->maps[SUPERMAPS_PER_MAGAZINE]
203203
[BITMAPS_PER_SUPERMAP]);
204204
#endif
205-
const uintptr_t PAGE_BITMAP_MASK = (BITMAPS_PER_PAGE *
206-
BYTES_PER_BITMAP) - 1;
205+
const uintptr_t PAGE_BITMAP_MASK =
206+
(BITMAPS_PER_PAGE * sizeof(bitmap_t)) - 1;
207207
return (bitmap_t *)((uintptr_t)b & ~PAGE_BITMAP_MASK);
208208
}
209209

@@ -601,7 +601,6 @@ _dispatch_alloc_init(void)
601601
// Double-check our math. These are all compile time checks and don't
602602
// generate code.
603603

604-
dispatch_assert(sizeof(bitmap_t) == BYTES_PER_BITMAP);
605604
dispatch_assert(sizeof(bitmap_t) == BYTES_PER_SUPERMAP);
606605

607606
dispatch_assert(sizeof(struct dispatch_continuation_s) <=

src/allocator_internal.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,20 @@
9797
// Use the largest type your platform is comfortable doing atomic ops with.
9898
// TODO: rdar://11477843
9999
typedef unsigned long bitmap_t;
100-
#if defined(__LP64__)
101-
#define BYTES_PER_BITMAP 8
102-
#else
103-
#define BYTES_PER_BITMAP 4
104-
#endif
105100

106101
#define BITMAP_C(v) ((bitmap_t)(v))
107102
#define BITMAP_ALL_ONES (~BITMAP_C(0))
108103

109104
// Stop configuring.
110105

111-
#define CONTINUATIONS_PER_BITMAP (BYTES_PER_BITMAP * 8)
106+
#define CONTINUATIONS_PER_BITMAP (sizeof(bitmap_t) * 8)
112107
#define BITMAPS_PER_SUPERMAP (BYTES_PER_SUPERMAP * 8)
113108

114109
#define BYTES_PER_MAGAZINE (PAGES_PER_MAGAZINE * DISPATCH_ALLOCATOR_PAGE_SIZE)
115-
#define CONSUMED_BYTES_PER_BITMAP (BYTES_PER_BITMAP + \
110+
#define CONSUMED_BYTES_PER_BITMAP (sizeof(bitmap_t) + \
116111
(DISPATCH_CONTINUATION_SIZE * CONTINUATIONS_PER_BITMAP))
117112

118-
#define BYTES_PER_SUPERMAP BYTES_PER_BITMAP
113+
#define BYTES_PER_SUPERMAP sizeof(bitmap_t)
119114
#define CONSUMED_BYTES_PER_SUPERMAP (BYTES_PER_SUPERMAP + \
120115
(BITMAPS_PER_SUPERMAP * CONSUMED_BYTES_PER_BITMAP))
121116

@@ -138,8 +133,8 @@ typedef unsigned long bitmap_t;
138133
// this will round up such that first_bitmap_in_same_page() can mask the address
139134
// of a bitmap_t in the maps to obtain the first bitmap for that same page
140135
#define ROUND_UP_TO_BITMAP_ALIGNMENT(x) \
141-
(((x) + ((BITMAPS_PER_PAGE * BYTES_PER_BITMAP) - 1u)) & \
142-
~((BITMAPS_PER_PAGE * BYTES_PER_BITMAP) - 1u))
136+
(((x) + ((BITMAPS_PER_PAGE * sizeof(bitmap_t)) - 1u)) & \
137+
~((BITMAPS_PER_PAGE * sizeof(bitmap_t)) - 1u))
143138
// Since these are both powers of two, we end up with not only the max alignment,
144139
// but happily the least common multiple, which will be the greater of the two.
145140
#define ROUND_UP_TO_BITMAP_ALIGNMENT_AND_CONTINUATION_SIZE(x) (ROUND_UP_TO_CONTINUATION_SIZE(ROUND_UP_TO_BITMAP_ALIGNMENT(x)))
@@ -148,7 +143,7 @@ typedef unsigned long bitmap_t;
148143
#define PADDING_TO_CONTINUATION_SIZE(x) (ROUND_UP_TO_CONTINUATION_SIZE(x) - (x))
149144

150145
#define SIZEOF_SUPERMAPS (BYTES_PER_SUPERMAP * SUPERMAPS_PER_MAGAZINE)
151-
#define SIZEOF_MAPS (BYTES_PER_BITMAP * BITMAPS_PER_SUPERMAP * \
146+
#define SIZEOF_MAPS (sizeof(bitmap_t) * BITMAPS_PER_SUPERMAP * \
152147
SUPERMAPS_PER_MAGAZINE)
153148

154149
// header is expected to end on supermap's required alignment
@@ -171,7 +166,7 @@ typedef unsigned long bitmap_t;
171166
#define REMAINDER_IN_FIRST_PAGE (BYTES_LEFT_IN_FIRST_PAGE - \
172167
(FULL_BITMAPS_IN_FIRST_PAGE * CONSUMED_BYTES_PER_BITMAP) - \
173168
(FULL_BITMAPS_IN_FIRST_PAGE ? 0 : \
174-
ROUND_UP_TO_CONTINUATION_SIZE(BYTES_PER_BITMAP)))
169+
ROUND_UP_TO_CONTINUATION_SIZE(sizeof(bitmap_t))))
175170

176171
#define REMAINDERED_CONTINUATIONS_IN_FIRST_PAGE \
177172
(REMAINDER_IN_FIRST_PAGE / DISPATCH_CONTINUATION_SIZE)
@@ -181,7 +176,7 @@ typedef unsigned long bitmap_t;
181176
(REMAINDERED_CONTINUATIONS_IN_FIRST_PAGE == 0 ? 0 : 1))
182177

183178
#define FPMAPS_TO_FPCONTS_PADDING (PADDING_TO_CONTINUATION_SIZE(\
184-
BYTES_PER_BITMAP * BITMAPS_IN_FIRST_PAGE))
179+
sizeof(bitmap_t) * BITMAPS_IN_FIRST_PAGE))
185180

186181
#else // PACK_FIRST_PAGE_WITH_CONTINUATIONS
187182

0 commit comments

Comments
 (0)