33 const auto reserved = npot ? ReserveNPOT(
length) : Reserve(
length);
43 if (
x > (
static_cast<uint64_t
>(1) << 63)) {
44 return absl::InvalidArgumentError(
"Out of range");
62bool Allocation::ReserveNPOT(
Bytes reserved) {
64 reserved = std::max(
Bytes{4096u}, reserved);
65 absl::StatusOr<uint64_t> npot_size =
67 if (!npot_size.ok()) {
70 return Reserve(Bytes{*npot_size});
73bool Allocation::Reserve(Bytes reserved) {
74 if (reserved <= reserved_) {
78 auto new_allocation = ::realloc(buffer_, reserved.GetByteSize());
79 if (!new_allocation) {
87 buffer_ =
static_cast<uint8_t*
>(new_allocation);
95 if (contents ==
nullptr) {
99 auto allocation = std::make_shared<Allocation>();
100 if (!allocation->Truncate(
length)) {
104 std::memmove(allocation->GetBuffer(), contents,
length.GetByteSize());
110 const std::shared_ptr<Allocation>& allocation) {
114 return std::make_shared<fml::NonOwnedMapping>(
115 reinterpret_cast<const uint8_t*
>(allocation->GetBuffer()),
116 allocation->GetLength().GetByteSize(),
117 [allocation](
auto,
auto) {}
122 auto buffer = std::make_shared<std::string>(std::move(
string));
123 return std::make_unique<fml::NonOwnedMapping>(
124 reinterpret_cast<const uint8_t*
>(buffer->c_str()), buffer->length(),
125 [buffer](
auto,
auto) {});
uint8_t * GetBuffer() const
Gets the pointer to the start of the allocation.
Bytes GetReservedLength() const
Gets the reserved length of the allocation. Calls to truncate may be ignored till the length exceeds ...
static absl::StatusOr< uint64_t > NextPowerOfTwoSize(uint64_t x)
Gets the next power of two size.
~Allocation()
Destroys the allocation.
Allocation()
Constructs a new zero-sized allocation.
Bytes GetLength() const
Gets the length of the allocation.
bool Truncate(Bytes length, bool npot=true)
Resize the underlying allocation to at least given number of bytes.
constexpr uint64_t GetByteSize() const
#define FML_CHECK(condition)
std::shared_ptr< fml::Mapping > CreateMappingWithString(std::string string)
Creates a mapping with string data.
std::shared_ptr< fml::Mapping > CreateMappingWithCopy(const uint8_t *contents, Bytes length)
Creates a mapping with copy of the bytes.
std::shared_ptr< fml::Mapping > CreateMappingFromAllocation(const std::shared_ptr< Allocation > &allocation)
Creates a mapping from allocation.