OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 Opera Software AS. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "platform/heap/SparseHeapBitmap.h" |
| 6 |
| 7 #include "wtf/PtrUtil.h" |
| 8 |
| 9 namespace blink { |
| 10 |
| 11 // Return the subtree/bitmap that covers the |
| 12 // [address, address + size) range. Null if there is none. |
| 13 SparseHeapBitmap* SparseHeapBitmap::hasRange(Address address, size_t size) { |
| 14 SparseHeapBitmap* bitmap = this; |
| 15 while (bitmap) { |
| 16 // Interval starts after, |m_right| handles. |
| 17 if (address > bitmap->end()) { |
| 18 bitmap = bitmap->m_right.get(); |
| 19 continue; |
| 20 } |
| 21 // Interval starts within, |bitmap| is included in the resulting range. |
| 22 if (address >= bitmap->base()) |
| 23 break; |
| 24 |
| 25 Address right = address + size - 1; |
| 26 // Interval starts before, but intersects with |bitmap|'s range. |
| 27 if (right >= bitmap->base()) |
| 28 break; |
| 29 |
| 30 // Interval is before entirely, for |m_left| to handle. |
| 31 bitmap = bitmap->m_left.get(); |
| 32 } |
| 33 return bitmap; |
| 34 } |
| 35 |
| 36 bool SparseHeapBitmap::isSet(Address address) { |
| 37 SparseHeapBitmap* bitmap = this; |
| 38 while (bitmap) { |
| 39 if (address > bitmap->end()) { |
| 40 bitmap = bitmap->m_right.get(); |
| 41 continue; |
| 42 } |
| 43 if (address >= bitmap->base()) { |
| 44 if (bitmap->m_bitmap) |
| 45 return bitmap->m_bitmap->test(address - bitmap->base()); |
| 46 return bitmap->m_size == 1; |
| 47 } |
| 48 bitmap = bitmap->m_left.get(); |
| 49 } |
| 50 return false; |
| 51 } |
| 52 |
| 53 void SparseHeapBitmap::add(Address address) { |
| 54 // |address| is beyond the maximum that this SparseHeapBitmap node can |
| 55 // encompass. |
| 56 if (address >= maxEnd()) { |
| 57 if (!m_right) { |
| 58 m_right = SparseHeapBitmap::create(address); |
| 59 return; |
| 60 } |
| 61 m_right->add(address); |
| 62 return; |
| 63 } |
| 64 // Same on the other side. |
| 65 if (address < minStart()) { |
| 66 if (!m_left) { |
| 67 m_left = SparseHeapBitmap::create(address); |
| 68 return; |
| 69 } |
| 70 m_left->add(address); |
| 71 return; |
| 72 } |
| 73 if (address == base()) |
| 74 return; |
| 75 // |address| can be encompassed by |this| by expanding its size. |
| 76 if (address > base()) { |
| 77 if (!m_bitmap) |
| 78 createBitmap(); |
| 79 m_bitmap->set(address - base()); |
| 80 return; |
| 81 } |
| 82 // Use |address| as the new base for this interval. |
| 83 Address oldBase = swapBase(address); |
| 84 createBitmap(); |
| 85 m_bitmap->set(oldBase - address); |
| 86 } |
| 87 |
| 88 void SparseHeapBitmap::createBitmap() { |
| 89 DCHECK(!m_bitmap && m_size == 1); |
| 90 m_bitmap = makeUnique<std::bitset<s_maxRange>>(); |
| 91 m_size = s_maxRange; |
| 92 m_bitmap->set(0); |
| 93 } |
| 94 |
| 95 size_t SparseHeapBitmap::intervalCount() const { |
| 96 size_t count = 1; |
| 97 if (m_left) |
| 98 count += m_left->intervalCount(); |
| 99 if (m_right) |
| 100 count += m_right->intervalCount(); |
| 101 return count; |
| 102 } |
| 103 |
| 104 } // namespace blink |
OLD | NEW |