Changeset View
Changeset View
Standalone View
Standalone View
src/support/lockedpool.h
Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* An arena manages a contiguous region of memory by dividing it into chunks. | * An arena manages a contiguous region of memory by dividing it into chunks. | ||||
*/ | */ | ||||
class Arena { | class Arena { | ||||
public: | public: | ||||
Arena(void *base, size_t size, size_t alignment); | Arena(void *base, size_t size, size_t alignment); | ||||
virtual ~Arena(); | virtual ~Arena(); | ||||
Arena(const Arena &other) = delete; // non construction-copyable | |||||
Arena &operator=(const Arena &) = delete; // non copyable | |||||
/** Memory statistics. */ | /** Memory statistics. */ | ||||
struct Stats { | struct Stats { | ||||
size_t used; | size_t used; | ||||
size_t free; | size_t free; | ||||
size_t total; | size_t total; | ||||
size_t chunks_used; | size_t chunks_used; | ||||
size_t chunks_free; | size_t chunks_free; | ||||
}; | }; | ||||
Show All 22 Lines | #endif | ||||
/** | /** | ||||
* Return whether a pointer points inside this arena. | * Return whether a pointer points inside this arena. | ||||
* This returns base <= ptr < (base+size) so only use it for (inclusive) | * This returns base <= ptr < (base+size) so only use it for (inclusive) | ||||
* chunk starting addresses. | * chunk starting addresses. | ||||
*/ | */ | ||||
bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; } | bool addressInArena(void *ptr) const { return ptr >= base && ptr < end; } | ||||
private: | private: | ||||
// non construction-copyable | |||||
Arena(const Arena &other) = delete; | |||||
// non copyable | |||||
Arena &operator=(const Arena &) = delete; | |||||
/** | /** | ||||
* Map of chunk address to chunk information. This class makes use of the | * Map of chunk address to chunk information. This class makes use of the | ||||
* sorted order to merge previous and next chunks during deallocation. | * sorted order to merge previous and next chunks during deallocation. | ||||
*/ | */ | ||||
std::map<char *, size_t> chunks_free; | std::map<char *, size_t> chunks_free; | ||||
std::map<char *, size_t> chunks_used; | std::map<char *, size_t> chunks_used; | ||||
/** Base address of arena */ | /** Base address of arena */ | ||||
char *base; | char *base; | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | public: | ||||
* allocated arena failed. If this callback is provided and returns false, | * allocated arena failed. If this callback is provided and returns false, | ||||
* the allocation fails (hard fail), if it returns true the allocation | * the allocation fails (hard fail), if it returns true the allocation | ||||
* proceeds, but it could warn. | * proceeds, but it could warn. | ||||
*/ | */ | ||||
explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, | explicit LockedPool(std::unique_ptr<LockedPageAllocator> allocator, | ||||
LockingFailed_Callback lf_cb_in = nullptr); | LockingFailed_Callback lf_cb_in = nullptr); | ||||
~LockedPool(); | ~LockedPool(); | ||||
LockedPool(const LockedPool &other) = delete; // non construction-copyable | |||||
LockedPool &operator=(const LockedPool &) = delete; // non copyable | |||||
/** | /** | ||||
* Allocate size bytes from this arena. | * Allocate size bytes from this arena. | ||||
* Returns pointer on success, or 0 if memory is full or the application | * Returns pointer on success, or 0 if memory is full or the application | ||||
* tried to allocate 0 bytes. | * tried to allocate 0 bytes. | ||||
*/ | */ | ||||
void *alloc(size_t size); | void *alloc(size_t size); | ||||
/** | /** | ||||
* Free a previously allocated chunk of memory. | * Free a previously allocated chunk of memory. | ||||
* Freeing the zero pointer has no effect. | * Freeing the zero pointer has no effect. | ||||
* Raises std::runtime_error in case of error. | * Raises std::runtime_error in case of error. | ||||
*/ | */ | ||||
void free(void *ptr); | void free(void *ptr); | ||||
/** Get pool usage statistics */ | /** Get pool usage statistics */ | ||||
Stats stats() const; | Stats stats() const; | ||||
private: | private: | ||||
// non construction-copyable | |||||
LockedPool(const LockedPool &other) = delete; | |||||
// non copyable | |||||
LockedPool &operator=(const LockedPool &) = delete; | |||||
std::unique_ptr<LockedPageAllocator> allocator; | std::unique_ptr<LockedPageAllocator> allocator; | ||||
/** Create an arena from locked pages */ | /** Create an arena from locked pages */ | ||||
class LockedPageArena : public Arena { | class LockedPageArena : public Arena { | ||||
public: | public: | ||||
LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, | LockedPageArena(LockedPageAllocator *alloc_in, void *base_in, | ||||
size_t size, size_t align); | size_t size, size_t align); | ||||
~LockedPageArena(); | ~LockedPageArena(); | ||||
▲ Show 20 Lines • Show All 51 Lines • Show Last 20 Lines |