Flutter Engine
 
Loading...
Searching...
No Matches
dl_storage.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. 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
6
7namespace flutter {
8
9static constexpr inline bool is_power_of_two(int value) {
10 return (value & (value - 1)) == 0;
11}
12
13// static
15 if (x == 0) {
16 return 1;
17 }
18
19 --x;
20
21 x |= x >> 1;
22 x |= x >> 2;
23 x |= x >> 4;
24 x |= x >> 8;
25 x |= x >> 16;
26 if constexpr (sizeof(size_t) > 4) {
27 x |= x >> 32;
28 }
29
30 return x + 1;
31}
32
33void DisplayListStorage::realloc(size_t count) {
34 ptr_.reset(static_cast<uint8_t*>(std::realloc(ptr_.release(), count)));
35 FML_CHECK(ptr_);
36 allocated_ = count;
37}
38
39uint8_t* DisplayListStorage::allocate(size_t needed) {
40 if (used_ + needed > allocated_) {
41 static_assert(is_power_of_two(kDLPageSize),
42 "This math needs updating for non-pow2.");
43
44 // NPOT, with minimum size of kDLPageSize.
45 size_t new_size = std::max(NextPowerOfTwoSize(used_ + needed), kDLPageSize);
46 size_t old_size = allocated_;
47 realloc(new_size);
48 FML_CHECK(ptr_.get());
49 FML_CHECK(allocated_ == new_size);
50 FML_CHECK(allocated_ >= old_size);
51 FML_CHECK(used_ + needed <= allocated_);
52 memset(ptr_.get() + used_, 0, allocated_ - old_size);
53 }
54 uint8_t* ret = ptr_.get() + used_;
55 used_ += needed;
56 FML_CHECK(used_ <= allocated_);
57 return ret;
58}
59
61 ptr_ = std::move(source.ptr_);
62 used_ = source.used_;
63 allocated_ = source.allocated_;
64 source.used_ = 0u;
65 source.allocated_ = 0u;
66}
67
69 ptr_.reset();
70 used_ = 0u;
71 allocated_ = 0u;
72}
73
75 ptr_ = std::move(source.ptr_);
76 used_ = source.used_;
77 allocated_ = source.allocated_;
78 source.used_ = 0u;
79 source.allocated_ = 0u;
80 return *this;
81}
82
83} // namespace flutter
void reset()
Resets the storage and allocation of the object to an empty state.
Definition dl_storage.cc:68
DisplayListStorage & operator=(DisplayListStorage &&other)
Definition dl_storage.cc:74
static size_t NextPowerOfTwoSize(size_t x)
Compute the next power of two from [x].
Definition dl_storage.cc:14
uint8_t * allocate(size_t needed)
Definition dl_storage.cc:39
static const constexpr size_t kDLPageSize
Definition dl_storage.h:17
int32_t value
int32_t x
#define FML_CHECK(condition)
Definition logging.h:104
static constexpr bool is_power_of_two(int value)
Definition dl_storage.cc:9