Flutter Engine
The Flutter Engine
bitmap.h
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_BITMAP_H_
6#define RUNTIME_VM_BITMAP_H_
7
8#include "vm/allocation.h"
9#include "vm/datastream.h"
10#include "vm/thread_state.h"
11#include "vm/zone.h"
12
13namespace dart {
14
15// BitmapBuilder is used to build a bitmap. The implementation is optimized
16// for a dense set of small bit maps without a fixed upper bound (e.g: a
17// pointer map description of a stack).
19 public:
20 BitmapBuilder() : length_(0), data_size_in_bytes_(kInlineCapacityInBytes) {
21 memset(data_.inline_, 0, data_size_in_bytes_);
22 }
23
25 : ZoneAllocated(),
26 length_(other.length_),
27 data_size_in_bytes_(other.data_size_in_bytes_) {
28 if (data_size_in_bytes_ == kInlineCapacityInBytes) {
29 memmove(data_.inline_, other.data_.inline_, kInlineCapacityInBytes);
30 } else {
31 data_.ptr_ = AllocBackingStore(data_size_in_bytes_);
32 memmove(data_.ptr_, other.data_.ptr_, data_size_in_bytes_);
33 }
34 }
35
36 intptr_t Length() const { return length_; }
37 void SetLength(intptr_t length);
38
39 // Get/Set individual bits in the bitmap, setting bits beyond the bitmap's
40 // length increases the length and expands the underlying bitmap if
41 // needed.
42 bool Get(intptr_t bit_offset) const;
43 void Set(intptr_t bit_offset, bool value);
44
45 // Return the bit offset of the highest bit set.
46 intptr_t Maximum() const;
47
48 // Return the bit offset of the lowest bit set.
49 intptr_t Minimum() const;
50
51 // Sets min..max (inclusive) to value.
52 void SetRange(intptr_t min, intptr_t max, bool value);
53
54 void Print() const;
56
57 void Write(BaseWriteStream* stream) const;
58 void Read(ReadStream* stream);
59
60 private:
61 static constexpr intptr_t kIncrementSizeInBytes = 16;
62 static constexpr intptr_t kInlineCapacityInBytes = 16;
63
64 bool InRange(intptr_t offset) const {
65 if (offset < 0) {
66 FATAL(
67 "Fatal error in BitmapBuilder::InRange :"
68 " invalid bit_offset, %" Pd "\n",
69 offset);
70 }
71 return (offset < length_);
72 }
73
74 bool InBackingStore(intptr_t bit_offset) {
75 intptr_t byte_offset = bit_offset >> kBitsPerByteLog2;
76 return byte_offset < data_size_in_bytes_;
77 }
78
79 uint8_t* BackingStore() {
80 return data_size_in_bytes_ == kInlineCapacityInBytes ? &data_.inline_[0]
81 : data_.ptr_;
82 }
83
84 const uint8_t* BackingStore() const {
85 return data_size_in_bytes_ == kInlineCapacityInBytes ? &data_.inline_[0]
86 : data_.ptr_;
87 }
88
89 static uint8_t* AllocBackingStore(intptr_t size_in_bytes) {
90 return ThreadState::Current()->zone()->Alloc<uint8_t>(size_in_bytes);
91 }
92
93 // Get/Set a bit that is known to be covered by the backing store.
94 bool GetBit(intptr_t bit_offset) const;
95 void SetBit(intptr_t bit_offset, bool value);
96
97 intptr_t length_;
98
99 // Backing store for the bitmap. Reading bits beyond the backing store
100 // (up to length_) is allowed and they are assumed to be false.
101 intptr_t data_size_in_bytes_;
102 union {
103 uint8_t* ptr_;
104 uint8_t inline_[kInlineCapacityInBytes];
105 } data_;
106};
107
108} // namespace dart
109
110#endif // RUNTIME_VM_BITMAP_H_
intptr_t Minimum() const
void AppendAsBytesTo(BaseWriteStream *stream) const
Definition: bitmap.cc:91
void Read(ReadStream *stream)
Definition: bitmap.cc:161
uint8_t inline_[kInlineCapacityInBytes]
Definition: bitmap.h:104
void Write(BaseWriteStream *stream) const
Definition: bitmap.cc:152
bool Get(intptr_t bit_offset) const
Definition: bitmap.cc:35
uint8_t * ptr_
Definition: bitmap.h:103
void Set(intptr_t bit_offset, bool value)
Definition: bitmap.cc:44
intptr_t Length() const
Definition: bitmap.h:36
BitmapBuilder(const BitmapBuilder &other)
Definition: bitmap.h:24
void Print() const
Definition: bitmap.cc:81
void SetRange(intptr_t min, intptr_t max, bool value)
Definition: bitmap.cc:75
intptr_t Maximum() const
void SetLength(intptr_t length)
Definition: bitmap.cc:13
Zone * zone() const
Definition: thread_state.h:37
static ThreadState * Current()
Definition: thread_state.h:27
ElementType * Alloc(intptr_t length)
#define FATAL(error)
uint8_t value
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
size_t length
Definition: dart_vm.cc:33
constexpr intptr_t kBitsPerByteLog2
Definition: globals.h:462
#define Pd
Definition: globals.h:408
SeparatedVector2 offset