/*
- * Copyright 2012 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
class Arena {
public:
explicit Arena(const Alloc& alloc,
- size_t minBlockSize = kDefaultMinBlockSize)
- : allocAndSize_(alloc, minBlockSize),
- ptr_(nullptr),
- end_(nullptr),
- totalAllocatedSize_(0) {
+ size_t minBlockSize = kDefaultMinBlockSize,
+ size_t sizeLimit = 0)
+ : allocAndSize_(alloc, minBlockSize)
+ , ptr_(nullptr)
+ , end_(nullptr)
+ , totalAllocatedSize_(0)
+ , bytesUsed_(0)
+ , sizeLimit_(sizeLimit) {
}
~Arena();
void* allocate(size_t size) {
size = roundUp(size);
+ bytesUsed_ += size;
if (LIKELY(end_ - ptr_ >= size)) {
// Fast path: there's enough room in the current block
return totalAllocatedSize_ + sizeof(Arena);
}
+ // Gets the total number of "used" bytes, i.e. bytes that the arena users
+ // allocated via the calls to `allocate`. Doesn't include fragmentation, e.g.
+ // if block size is 4KB and you allocate 2 objects of 3KB in size,
+ // `bytesUsed()` will be 6KB, while `totalSize()` will be 8KB+.
+ size_t bytesUsed() const {
+ return bytesUsed_;
+ }
+
private:
// not copyable
Arena(const Arena&) = delete;
char* ptr_;
char* end_;
size_t totalAllocatedSize_;
+ size_t bytesUsed_;
+ size_t sizeLimit_;
};
/**
*/
class SysArena : public Arena<SysAlloc> {
public:
- explicit SysArena(size_t minBlockSize = kDefaultMinBlockSize)
- : Arena<SysAlloc>(SysAlloc(), minBlockSize) {
+ explicit SysArena(
+ size_t minBlockSize = kDefaultMinBlockSize,
+ size_t sizeLimit = 0)
+ : Arena<SysAlloc>(SysAlloc(), minBlockSize, sizeLimit) {
}
};