// An arena pool that creates arenas backed by an mmaped file. class SwapSpace { public:
SwapSpace(int fd, size_t initial_size);
~SwapSpace(); void* Alloc(size_t size) REQUIRES(!lock_); void Free(void* ptr, size_t size) REQUIRES(!lock_);
size_t GetSize() { return size_;
}
private: // Chunk of space. struct SpaceChunk { // We need mutable members as we keep these objects in a std::set<> (providing only const // access) but we modify these members while carefully preserving the std::set<> ordering. mutable uint8_t* ptr; mutable size_t size;
class SortChunkByPtr { public: booloperator()(const SpaceChunk& a, const SpaceChunk& b) const { returnreinterpret_cast<uintptr_t>(a.ptr) < reinterpret_cast<uintptr_t>(b.ptr);
}
};
using FreeByStartSet = std::set<SpaceChunk, SortChunkByPtr>;
// Map size to an iterator to free_by_start_'s entry. struct FreeBySizeEntry {
FreeBySizeEntry(size_t sz, FreeByStartSet::const_iterator entry)
: size(sz), free_by_start_entry(entry) { }
// We need mutable members as we keep these objects in a std::set<> (providing only const // access) but we modify these members while carefully preserving the std::set<> ordering. mutable size_t size; mutable FreeByStartSet::const_iterator free_by_start_entry;
}; struct FreeBySizeComparator { booloperator()(const FreeBySizeEntry& lhs, const FreeBySizeEntry& rhs) const { if (lhs.size != rhs.size) { return lhs.size < rhs.size;
} else { return lhs.free_by_start_entry->Start() < rhs.free_by_start_entry->Start();
}
}
}; using FreeBySizeSet = std::set<FreeBySizeEntry, FreeBySizeComparator>;
// NOTE: Boost.Bimap would be useful for the two following members.
// Map start of a free chunk to its size.
FreeByStartSet free_by_start_ GUARDED_BY(lock_); // Free chunks ordered by size.
FreeBySizeSet free_by_size_ GUARDED_BY(lock_);
template <typename T> class SwapAllocator { public: using value_type = T; using pointer = T*; using reference = T&; using const_pointer = const T*; using const_reference = const T&; using size_type = size_t; using difference_type = ptrdiff_t;
template <typename U> struct rebind { using other = SwapAllocator<U>;
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.