Skip to content

Commit 4b33a4a

Browse files
author
duke
committedMar 11, 2024
Automatic merge of jdk:master into master
2 parents 7eef10a + d74b907 commit 4b33a4a

File tree

3 files changed

+289
-2
lines changed

3 files changed

+289
-2
lines changed
 

‎src/hotspot/share/utilities/bitMap.cpp

+49-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -99,6 +99,54 @@ void GrowableBitMap<BitMapWithAllocator>::resize(idx_t new_size_in_bits, bool cl
9999
update(map, new_size_in_bits);
100100
}
101101

102+
template <class BitMapWithAllocator>
103+
bm_word_t* GrowableBitMap<BitMapWithAllocator>::copy_of_range(idx_t start_bit, idx_t end_bit) {
104+
assert(start_bit < end_bit, "End bit must come after start bit");
105+
assert(end_bit <= size(), "End bit not in bitmap");
106+
107+
// We might have extra bits at the end that we don't want to lose
108+
const idx_t cutoff = bit_in_word(end_bit);
109+
const idx_t start_word = to_words_align_down(start_bit);
110+
const idx_t end_word = to_words_align_up(end_bit);
111+
const bm_word_t* const old_map = map();
112+
113+
const BitMapWithAllocator* const derived = static_cast<BitMapWithAllocator*>(this);
114+
115+
bm_word_t* const new_map = derived->allocate(end_word - start_word);
116+
117+
// All words need to be shifted by this amount
118+
const idx_t shift = bit_in_word(start_bit);
119+
// Bits shifted out by a word need to be passed into the next
120+
bm_word_t carry = 0;
121+
122+
// Iterate the map backwards as the shift will result in carry-out bits
123+
for (idx_t i = end_word; i-- > start_word;) {
124+
new_map[i-start_word] = old_map[i] >> shift;
125+
126+
if (shift != 0) {
127+
new_map[i-start_word] |= carry;
128+
carry = old_map[i] << (BitsPerWord - shift);
129+
}
130+
}
131+
132+
return new_map;
133+
}
134+
135+
template <class BitMapWithAllocator>
136+
void GrowableBitMap<BitMapWithAllocator>::truncate(idx_t start_bit, idx_t end_bit) {
137+
const size_t old_size_in_words = calc_size_in_words(size());
138+
const idx_t new_size_in_bits = end_bit - start_bit;
139+
bm_word_t* const old_map = map();
140+
141+
bm_word_t* const new_map = copy_of_range(start_bit, end_bit);
142+
143+
const BitMapWithAllocator* const derived = static_cast<BitMapWithAllocator*>(this);
144+
// Free and clear old map to avoid left over bits
145+
derived->free(old_map, old_size_in_words);
146+
update(nullptr, 0);
147+
update(new_map, new_size_in_bits);
148+
}
149+
102150
ArenaBitMap::ArenaBitMap(Arena* arena, idx_t size_in_bits, bool clear)
103151
: GrowableBitMap<ArenaBitMap>(), _arena(arena) {
104152
initialize(size_in_bits, clear);

‎src/hotspot/share/utilities/bitMap.hpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -566,6 +566,11 @@ class GrowableBitMap : public BitMap {
566566
GrowableBitMap() : GrowableBitMap(nullptr, 0) {}
567567
GrowableBitMap(bm_word_t* map, idx_t size_in_bits) : BitMap(map, size_in_bits) {}
568568

569+
private:
570+
// Copy the region [start, end) of the bitmap
571+
// Bits in the selected range are copied to a newly allocated map
572+
bm_word_t* copy_of_range(idx_t start_bit, idx_t end_bit);
573+
569574
public:
570575
// Set up and optionally clear the bitmap memory.
571576
//
@@ -585,6 +590,9 @@ class GrowableBitMap : public BitMap {
585590
// Old bits are transferred to the new memory
586591
// and the extended memory is optionally cleared.
587592
void resize(idx_t new_size_in_bits, bool clear = true);
593+
// Reduce bitmap to the region [start, end)
594+
// Previous map is deallocated and replaced with the newly allocated map from copy_of_range
595+
void truncate(idx_t start_bit, idx_t end_bit);
588596
};
589597

590598
// A concrete implementation of the "abstract" BitMap class.

‎test/hotspot/gtest/utilities/test_bitMap.cpp

+231
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,165 @@ class BitMapTest {
114114
#endif
115115
};
116116

117+
class BitMapTruncateTest {
118+
119+
public:
120+
const static BitMap::idx_t BITMAP_SIZE = 128;
121+
122+
template <class ResizableBitMapClass>
123+
static void fillBitMap(ResizableBitMapClass& map, BitMap::idx_t size) {
124+
BitMap::idx_t set_bits[] = {0, 31, 63, 64, 95, 127};
125+
for (BitMap::idx_t bit : set_bits) {
126+
if (bit < size) {
127+
map.set_bit(bit);
128+
}
129+
}
130+
}
131+
132+
template <class ResizableBitMapClass>
133+
static void testTruncateOneWord() {
134+
ResourceMark rm;
135+
136+
ResizableBitMapClass map(64);
137+
map.set_bit(0);
138+
map.set_bit(1);
139+
map.set_bit(2);
140+
map.set_bit(3);
141+
142+
ResizableBitMapClass result(2);
143+
result.set_bit(0);
144+
result.set_bit(1);
145+
146+
map.truncate(1, 3);
147+
148+
EXPECT_TRUE(map.is_same(result));
149+
}
150+
151+
template <class ResizableBitMapClass>
152+
static void testTruncateSame() {
153+
// Resulting map should be the same as the original
154+
ResourceMark rm;
155+
ResizableBitMapClass expected(BITMAP_SIZE);
156+
fillBitMap(expected, BITMAP_SIZE);
157+
158+
ResizableBitMapClass map(BITMAP_SIZE);
159+
fillBitMap(map, BITMAP_SIZE);
160+
map.truncate(0, BITMAP_SIZE);
161+
162+
EXPECT_TRUE(map.is_same(expected));
163+
}
164+
165+
template <class ResizableBitMapClass>
166+
static void testTruncateStart() {
167+
// Resulting map should start at the beginning of the original
168+
ResourceMark rm;
169+
ResizableBitMapClass expected(64);
170+
fillBitMap(expected, 64);
171+
172+
ResizableBitMapClass map(BITMAP_SIZE);
173+
fillBitMap(map, BITMAP_SIZE);
174+
map.truncate(0, 64);
175+
176+
EXPECT_TRUE(map.is_same(expected));
177+
}
178+
179+
template <class ResizableBitMapClass>
180+
static void testTruncateEnd() {
181+
// Resulting map should end at the end of the original
182+
ResourceMark rm;
183+
ResizableBitMapClass expected(64);
184+
expected.set_bit(0);
185+
expected.set_bit(31);
186+
expected.set_bit(63);
187+
188+
ResizableBitMapClass map(BITMAP_SIZE);
189+
fillBitMap(map, BITMAP_SIZE);
190+
map.truncate(64, 128);
191+
192+
EXPECT_TRUE(map.is_same(expected));
193+
}
194+
195+
template <class ResizableBitMapClass>
196+
static void testTruncateMiddle() {
197+
// Resulting map should end at the end of the original
198+
ResourceMark rm;
199+
ResizableBitMapClass expected(64);
200+
expected.set_bit(31);
201+
expected.set_bit(32);
202+
expected.set_bit(63);
203+
204+
ResizableBitMapClass map(BITMAP_SIZE);
205+
fillBitMap(map, BITMAP_SIZE);
206+
map.truncate(32, 96);
207+
208+
EXPECT_TRUE(map.is_same(expected));
209+
}
210+
211+
template <class ResizableBitMapClass>
212+
static void testTruncateStartUnaligned() {
213+
// Resulting map should start at the beginning of the original
214+
ResourceMark rm;
215+
ResizableBitMapClass expected(96);
216+
fillBitMap(expected, 96);
217+
218+
ResizableBitMapClass map(BITMAP_SIZE);
219+
fillBitMap(map, BITMAP_SIZE);
220+
map.truncate(0, 96);
221+
222+
EXPECT_TRUE(map.is_same(expected));
223+
}
224+
225+
template <class ResizableBitMapClass>
226+
static void testTruncateEndUnaligned() {
227+
// Resulting map should end at the end of the original
228+
ResourceMark rm;
229+
ResizableBitMapClass expected(97);
230+
expected.set_bit(0);
231+
expected.set_bit(32);
232+
expected.set_bit(33);
233+
expected.set_bit(64);
234+
expected.set_bit(96);
235+
236+
ResizableBitMapClass map(BITMAP_SIZE);
237+
fillBitMap(map, BITMAP_SIZE);
238+
map.truncate(31, 128);
239+
240+
EXPECT_TRUE(map.is_same(expected));
241+
}
242+
243+
template <class ResizableBitMapClass>
244+
static void testRandom() {
245+
for (int i = 0; i < 100; i++) {
246+
ResourceMark rm;
247+
248+
const size_t max_size = 1024;
249+
const size_t size = os::random() % max_size + 1;
250+
const size_t truncate_size = os::random() % size + 1;
251+
const size_t truncate_start = size == truncate_size ? 0 : os::random() % (size - truncate_size);
252+
253+
ResizableBitMapClass map(size);
254+
ResizableBitMapClass result(truncate_size);
255+
256+
for (BitMap::idx_t idx = 0; idx < truncate_start; idx++) {
257+
if (os::random() % 2 == 0) {
258+
map.set_bit(idx);
259+
}
260+
}
261+
262+
for (BitMap::idx_t idx = 0; idx < truncate_size; idx++) {
263+
if (os::random() % 2 == 0) {
264+
map.set_bit(truncate_start + idx);
265+
result.set_bit(idx);
266+
}
267+
}
268+
269+
map.truncate(truncate_start, truncate_start + truncate_size);
270+
271+
EXPECT_TRUE(map.is_same(result));
272+
}
273+
}
274+
};
275+
117276
// TestArenaBitMap is the shorthand combination of Arena and ArenaBitMap.
118277
// Multiple inheritance guarantees to construct Arena first.
119278
class TestArenaBitMap : private Arena, public ArenaBitMap {
@@ -204,3 +363,75 @@ TEST_VM(BitMap, print_on) {
204363
}
205364

206365
#endif
366+
367+
TEST_VM(BitMap, truncate_same) {
368+
BitMapTruncateTest::testTruncateSame<ResourceBitMap>();
369+
EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";
370+
BitMapTruncateTest::testTruncateSame<TestCHeapBitMap>();
371+
EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";
372+
BitMapTruncateTest::testTruncateSame<TestArenaBitMap>();
373+
EXPECT_FALSE(HasFailure()) << "Failed on type ArenaBitMap";
374+
}
375+
376+
TEST_VM(BitMap, truncate_start) {
377+
BitMapTruncateTest::testTruncateStart<ResourceBitMap>();
378+
EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";
379+
BitMapTruncateTest::testTruncateStart<TestCHeapBitMap>();
380+
EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";
381+
BitMapTruncateTest::testTruncateStart<TestArenaBitMap>();
382+
EXPECT_FALSE(HasFailure()) << "Failed on type ArenaBitMap";
383+
}
384+
385+
TEST_VM(BitMap, truncate_end) {
386+
BitMapTruncateTest::testTruncateEnd<ResourceBitMap>();
387+
EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";
388+
BitMapTruncateTest::testTruncateEnd<TestCHeapBitMap>();
389+
EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";
390+
BitMapTruncateTest::testTruncateEnd<TestArenaBitMap>();
391+
EXPECT_FALSE(HasFailure()) << "Failed on type ArenaBitMap";
392+
}
393+
394+
TEST_VM(BitMap, truncate_middle) {
395+
BitMapTruncateTest::testTruncateMiddle<ResourceBitMap>();
396+
EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";
397+
BitMapTruncateTest::testTruncateMiddle<TestCHeapBitMap>();
398+
EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";
399+
BitMapTruncateTest::testTruncateMiddle<TestArenaBitMap>();
400+
EXPECT_FALSE(HasFailure()) << "Failed on type ArenaBitMap";
401+
}
402+
403+
TEST_VM(BitMap, truncate_start_unaligned) {
404+
BitMapTruncateTest::testTruncateStartUnaligned<ResourceBitMap>();
405+
EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";
406+
BitMapTruncateTest::testTruncateStartUnaligned<TestCHeapBitMap>();
407+
EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";
408+
BitMapTruncateTest::testTruncateStartUnaligned<TestArenaBitMap>();
409+
EXPECT_FALSE(HasFailure()) << "Failed on type ArenaBitMap";
410+
}
411+
412+
TEST_VM(BitMap, truncate_end_unaligned) {
413+
BitMapTruncateTest::testTruncateEndUnaligned<ResourceBitMap>();
414+
EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";
415+
BitMapTruncateTest::testTruncateEndUnaligned<TestCHeapBitMap>();
416+
EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";
417+
BitMapTruncateTest::testTruncateEndUnaligned<TestArenaBitMap>();
418+
EXPECT_FALSE(HasFailure()) << "Failed on type ArenaBitMap";
419+
}
420+
421+
TEST_VM(BitMap, truncate_one_word) {
422+
BitMapTruncateTest::testTruncateOneWord<ResourceBitMap>();
423+
EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";
424+
BitMapTruncateTest::testTruncateOneWord<TestCHeapBitMap>();
425+
EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";
426+
BitMapTruncateTest::testTruncateOneWord<TestArenaBitMap>();
427+
EXPECT_FALSE(HasFailure()) << "Failed on type ArenaBitMap";
428+
}
429+
430+
TEST_VM(BitMap, truncate_random) {
431+
BitMapTruncateTest::testRandom<ResourceBitMap>();
432+
EXPECT_FALSE(HasFailure()) << "Failed on type ResourceBitMap";
433+
BitMapTruncateTest::testRandom<TestCHeapBitMap>();
434+
EXPECT_FALSE(HasFailure()) << "Failed on type CHeapBitMap";
435+
BitMapTruncateTest::testRandom<TestArenaBitMap>();
436+
EXPECT_FALSE(HasFailure()) << "Failed on type ArenaBitMap";
437+
}

0 commit comments

Comments
 (0)
Please sign in to comment.