33 const auto reserved = npot ? ReserveNPOT(length) : Reserve(length);
57 bool Allocation::ReserveNPOT(
Bytes reserved) {
59 reserved = std::max(
Bytes{4096u}, reserved);
63 bool Allocation::Reserve(
Bytes reserved) {
64 if (reserved <= reserved_) {
68 auto new_allocation = ::realloc(buffer_, reserved.GetByteSize());
69 if (!new_allocation) {
77 buffer_ =
static_cast<uint8_t*
>(new_allocation);
85 if (contents ==
nullptr) {
89 auto allocation = std::make_shared<Allocation>();
90 if (!allocation->Truncate(length)) {
94 std::memmove(allocation->GetBuffer(), contents, length.
GetByteSize());
100 const std::shared_ptr<Allocation>& allocation) {
104 return std::make_shared<fml::NonOwnedMapping>(
105 reinterpret_cast<const uint8_t*
>(allocation->GetBuffer()),
106 allocation->GetLength().GetByteSize(),
107 [allocation](
auto,
auto) {}
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) {});
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 ...
~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.
static uint32_t NextPowerOfTwoSize(uint32_t x)
Gets the next power of two size.
constexpr uint64_t GetByteSize() const
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.
AllocationSize< 1u > Bytes
std::shared_ptr< fml::Mapping > CreateMappingFromAllocation(const std::shared_ptr< Allocation > &allocation)
Creates a mapping from allocation.