Flutter Engine
The Flutter Engine
allocation.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
7#include <algorithm>
8#include <cstring>
9
11
12namespace impeller {
13
14Allocation::Allocation() = default;
15
17 ::free(buffer_);
18}
19
20uint8_t* Allocation::GetBuffer() const {
21 return buffer_;
22}
23
24size_t Allocation::GetLength() const {
25 return length_;
26}
27
29 return reserved_;
30}
31
32bool Allocation::Truncate(size_t length, bool npot) {
33 const auto reserved = npot ? ReserveNPOT(length) : Reserve(length);
34 if (!reserved) {
35 return false;
36 }
37 length_ = length;
38 return true;
39}
40
42 if (x == 0) {
43 return 1;
44 }
45
46 --x;
47
48 x |= x >> 1;
49 x |= x >> 2;
50 x |= x >> 4;
51 x |= x >> 8;
52 x |= x >> 16;
53
54 return x + 1;
55}
56
57bool Allocation::ReserveNPOT(size_t reserved) {
58 // Reserve at least one page of data.
59 reserved = std::max<size_t>(4096u, reserved);
60 return Reserve(NextPowerOfTwoSize(reserved));
61}
62
63bool Allocation::Reserve(size_t reserved) {
64 if (reserved <= reserved_) {
65 return true;
66 }
67
68 auto new_allocation = ::realloc(buffer_, reserved);
69 if (!new_allocation) {
70 // If new length is zero, a minimum non-zero sized allocation is returned.
71 // So this check will not trip and this routine will indicate success as
72 // expected.
73 VALIDATION_LOG << "Allocation failed. Out of host memory.";
74 return false;
75 }
76
77 buffer_ = static_cast<uint8_t*>(new_allocation);
78 reserved_ = reserved;
79
80 return true;
81}
82
83std::shared_ptr<fml::Mapping> CreateMappingWithCopy(const uint8_t* contents,
84 size_t length) {
85 if (contents == nullptr) {
86 return nullptr;
87 }
88
89 auto allocation = std::make_shared<Allocation>();
90 if (!allocation->Truncate(length)) {
91 return nullptr;
92 }
93
94 std::memmove(allocation->GetBuffer(), contents, length);
95
96 return CreateMappingFromAllocation(allocation);
97}
98
99std::shared_ptr<fml::Mapping> CreateMappingFromAllocation(
100 const std::shared_ptr<Allocation>& allocation) {
101 if (!allocation) {
102 return nullptr;
103 }
104 return std::make_shared<fml::NonOwnedMapping>(
105 reinterpret_cast<const uint8_t*>(allocation->GetBuffer()), //
106 allocation->GetLength(), //
107 [allocation](auto, auto) {} //
108 );
109}
110
111std::shared_ptr<fml::Mapping> CreateMappingWithString(std::string string) {
112 auto buffer = std::make_shared<std::string>(std::move(string));
113 return std::make_unique<fml::NonOwnedMapping>(
114 reinterpret_cast<const uint8_t*>(buffer->c_str()), buffer->length(),
115 [buffer](auto, auto) {});
116}
117
118} // namespace impeller
uint8_t * GetBuffer() const
Definition: allocation.cc:20
size_t GetReservedLength() const
Definition: allocation.cc:28
size_t GetLength() const
Definition: allocation.cc:24
bool Truncate(size_t length, bool npot=true)
Definition: allocation.cc:32
static uint32_t NextPowerOfTwoSize(uint32_t x)
Definition: allocation.cc:41
size_t length
double x
void * realloc(void *ptr, size_t size)
Definition: allocation.cc:27
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
std::shared_ptr< fml::Mapping > CreateMappingWithCopy(const uint8_t *contents, size_t length)
Definition: allocation.cc:83
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Definition: allocation.cc:111
std::shared_ptr< fml::Mapping > CreateMappingFromAllocation(const std::shared_ptr< Allocation > &allocation)
Definition: allocation.cc:99
#define VALIDATION_LOG
Definition: validation.h:73