Flutter Impeller
allocation.h
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 
5 #ifndef FLUTTER_IMPELLER_BASE_ALLOCATION_H_
6 #define FLUTTER_IMPELLER_BASE_ALLOCATION_H_
7 
8 #include <cstdint>
9 #include <memory>
10 
11 #include "flutter/fml/mapping.h"
13 #include "third_party/abseil-cpp/absl/status/statusor.h"
14 
15 namespace impeller {
16 
17 //------------------------------------------------------------------------------
18 /// @brief Describes an allocation on the heap.
19 ///
20 /// Managing allocations through this utility makes it harder to
21 /// miss allocation failures.
22 ///
23 class Allocation {
24  public:
25  //----------------------------------------------------------------------------
26  /// @brief Constructs a new zero-sized allocation.
27  ///
29 
30  //----------------------------------------------------------------------------
31  /// @brief Destroys the allocation.
32  ///
33  ~Allocation();
34 
35  //----------------------------------------------------------------------------
36  /// @brief Gets the pointer to the start of the allocation.
37  ///
38  /// This pointer is only valid till the next call to `Truncate`.
39  ///
40  /// @return The pointer to the start of the allocation.
41  ///
42  uint8_t* GetBuffer() const;
43 
44  //----------------------------------------------------------------------------
45  /// @brief Gets the length of the allocation.
46  ///
47  /// @return The length.
48  ///
49  Bytes GetLength() const;
50 
51  //----------------------------------------------------------------------------
52  /// @brief Gets the reserved length of the allocation. Calls to truncate
53  /// may be ignored till the length exceeds the reserved length.
54  ///
55  /// @return The reserved length.
56  ///
57  Bytes GetReservedLength() const;
58 
59  //----------------------------------------------------------------------------
60  /// @brief Resize the underlying allocation to at least given number of
61  /// bytes.
62  ///
63  /// In case of failure, false is returned and the underlying
64  /// allocation remains unchanged.
65  ///
66  /// @warning Pointers to buffers obtained via previous calls to `GetBuffer`
67  /// may become invalid at this point.
68  ///
69  /// @param[in] length The length.
70  /// @param[in] npot Whether to round up the length to the next power of
71  /// two.
72  ///
73  /// @return If the underlying allocation was resized to the new size.
74  ///
75  [[nodiscard]] bool Truncate(Bytes length, bool npot = true);
76 
77  //----------------------------------------------------------------------------
78  /// @brief Gets the next power of two size.
79  ///
80  /// @param[in] x The size.
81  ///
82  /// @return The smallest power of two that is greater than or equal to x.
83  ///
84  static absl::StatusOr<uint64_t> NextPowerOfTwoSize(uint64_t x);
85 
86  private:
87  uint8_t* buffer_ = nullptr;
88  Bytes length_;
89  Bytes reserved_;
90 
91  [[nodiscard]] bool Reserve(Bytes reserved);
92 
93  [[nodiscard]] bool ReserveNPOT(Bytes reserved);
94 
95  Allocation(const Allocation&) = delete;
96 
97  Allocation& operator=(const Allocation&) = delete;
98 };
99 
100 //------------------------------------------------------------------------------
101 /// @brief Creates a mapping with copy of the bytes.
102 ///
103 /// @param[in] contents The contents
104 /// @param[in] length The length
105 ///
106 /// @return The new mapping or nullptr if the copy could not be performed.
107 ///
108 std::shared_ptr<fml::Mapping> CreateMappingWithCopy(const uint8_t* contents,
109  Bytes length);
110 
111 //------------------------------------------------------------------------------
112 /// @brief Creates a mapping from allocation.
113 ///
114 /// No data copy occurs. Only a reference to the underlying
115 /// allocation is bumped up.
116 ///
117 /// Changes to the underlying allocation will not be reflected in
118 /// the mapping and must not change.
119 ///
120 /// @param[in] allocation The allocation.
121 ///
122 /// @return A new mapping or nullptr if the argument allocation was invalid.
123 ///
124 std::shared_ptr<fml::Mapping> CreateMappingFromAllocation(
125  const std::shared_ptr<Allocation>& allocation);
126 
127 //------------------------------------------------------------------------------
128 /// @brief Creates a mapping with string data.
129 ///
130 /// Only a reference to the underlying string is bumped up and the
131 /// string is not copied.
132 ///
133 /// @param[in] string The string
134 ///
135 /// @return A new mapping or nullptr in case of allocation failures.
136 ///
137 std::shared_ptr<fml::Mapping> CreateMappingWithString(
138  std::shared_ptr<const std::string> string);
139 
140 //------------------------------------------------------------------------------
141 /// @brief Creates a mapping with string data.
142 ///
143 /// The string is copied.
144 ///
145 /// @param[in] string The string
146 ///
147 /// @return A new mapping or nullptr in case of allocation failures.
148 ///
149 std::shared_ptr<fml::Mapping> CreateMappingWithString(std::string string);
150 
151 } // namespace impeller
152 
153 #endif // FLUTTER_IMPELLER_BASE_ALLOCATION_H_
Describes an allocation on the heap.
Definition: allocation.h:23
uint8_t * GetBuffer() const
Gets the pointer to the start of the allocation.
Definition: allocation.cc:20
Bytes GetReservedLength() const
Gets the reserved length of the allocation. Calls to truncate may be ignored till the length exceeds ...
Definition: allocation.cc:28
static absl::StatusOr< uint64_t > NextPowerOfTwoSize(uint64_t x)
Gets the next power of two size.
Definition: allocation.cc:42
~Allocation()
Destroys the allocation.
Definition: allocation.cc:16
Allocation()
Constructs a new zero-sized allocation.
Bytes GetLength() const
Gets the length of the allocation.
Definition: allocation.cc:24
bool Truncate(Bytes length, bool npot=true)
Resize the underlying allocation to at least given number of bytes.
Definition: allocation.cc:32
int32_t x
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Creates a mapping with string data.
Definition: allocation.cc:121
std::shared_ptr< fml::Mapping > CreateMappingWithCopy(const uint8_t *contents, Bytes length)
Creates a mapping with copy of the bytes.
Definition: allocation.cc:93
std::shared_ptr< fml::Mapping > CreateMappingFromAllocation(const std::shared_ptr< Allocation > &allocation)
Creates a mapping from allocation.
Definition: allocation.cc:109