}
void* mem = alloc.allocate(allocSize);
- assert(isAligned(mem));
return std::make_pair(new (mem) Block(), allocSize - sizeof(Block));
}
std::pair<Block*, size_t> p;
char* start;
-
size_t allocSize = std::max(size, minBlockSize()) + sizeof(Block);
- if(sizeLimit_ && allocSize > sizeLimit_ - totalAllocatedSize_) {
+ if (sizeLimit_ != kNoSizeLimit &&
+ allocSize > sizeLimit_ - totalAllocatedSize_) {
throw std::bad_alloc();
}
#include <cassert>
#include <limits>
+#include <stdexcept>
#include <utility>
#include <boost/intrusive/slist.hpp>
+#include "folly/Conv.h"
#include "folly/Likely.h"
#include "folly/Malloc.h"
#include "folly/Memory.h"
public:
explicit Arena(const Alloc& alloc,
size_t minBlockSize = kDefaultMinBlockSize,
- size_t sizeLimit = 0)
+ size_t sizeLimit = kNoSizeLimit,
+ size_t maxAlign = kDefaultMaxAlign)
: allocAndSize_(alloc, minBlockSize)
, ptr_(nullptr)
, end_(nullptr)
, totalAllocatedSize_(0)
, bytesUsed_(0)
- , sizeLimit_(sizeLimit) {
+ , sizeLimit_(sizeLimit)
+ , maxAlign_(maxAlign) {
+ if ((maxAlign_ & (maxAlign_ - 1)) || maxAlign_ > alignof(Block)) {
+ throw std::invalid_argument(
+ folly::to<std::string>("Invalid maxAlign: ", maxAlign_));
+ }
}
~Arena();
public:
static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block);
+ static constexpr size_t kNoSizeLimit = 0;
+ static constexpr size_t kDefaultMaxAlign = alignof(Block);
private:
- static constexpr size_t maxAlign = alignof(Block);
- static constexpr bool isAligned(uintptr_t address) {
- return (address & (maxAlign - 1)) == 0;
+ bool isAligned(uintptr_t address) const {
+ return (address & (maxAlign_ - 1)) == 0;
}
- static bool isAligned(void* p) {
+ bool isAligned(void* p) const {
return isAligned(reinterpret_cast<uintptr_t>(p));
}
// Round up size so it's properly aligned
- static constexpr size_t roundUp(size_t size) {
- return (size + maxAlign - 1) & ~(maxAlign - 1);
+ size_t roundUp(size_t size) const {
+ return (size + maxAlign_ - 1) & ~(maxAlign_ - 1);
}
// cache_last<true> makes the list keep a pointer to the last element, so we
char* end_;
size_t totalAllocatedSize_;
size_t bytesUsed_;
- size_t sizeLimit_;
+ const size_t sizeLimit_;
+ const size_t maxAlign_;
};
template <class Alloc>
class SysArena : public Arena<SysAlloc> {
public:
explicit SysArena(size_t minBlockSize = kDefaultMinBlockSize,
- size_t sizeLimit = 0)
- : Arena<SysAlloc>(SysAlloc(), minBlockSize, sizeLimit) {
+ size_t sizeLimit = kNoSizeLimit,
+ size_t maxAlign = kDefaultMaxAlign)
+ : Arena<SysAlloc>(SysAlloc(), minBlockSize, sizeLimit, maxAlign) {
}
};
namespace folly {
-ThreadCachedArena::ThreadCachedArena(size_t minBlockSize)
- : minBlockSize_(minBlockSize) {
+ThreadCachedArena::ThreadCachedArena(size_t minBlockSize, size_t maxAlign)
+ : minBlockSize_(minBlockSize), maxAlign_(maxAlign) {
}
SysArena* ThreadCachedArena::allocateThreadLocalArena() {
- SysArena* arena = new SysArena(minBlockSize_);
+ SysArena* arena =
+ new SysArena(minBlockSize_, SysArena::kNoSizeLimit, maxAlign_);
auto disposer = [this] (SysArena* t, TLPDestructionMode mode) {
std::unique_ptr<SysArena> tp(t); // ensure it gets deleted
if (mode == TLPDestructionMode::THIS_THREAD) {
class ThreadCachedArena {
public:
explicit ThreadCachedArena(
- size_t minBlockSize = SysArena::kDefaultMinBlockSize);
+ size_t minBlockSize = SysArena::kDefaultMinBlockSize,
+ size_t maxAlign = SysArena::kDefaultMaxAlign);
void* allocate(size_t size) {
SysArena* arena = arena_.get();
// the ThreadCachedArena is destroyed.
void zombify(SysArena&& arena);
- size_t minBlockSize_;
+ const size_t minBlockSize_;
+ const size_t maxAlign_;
SysArena zombies_; // allocated from threads that are now dead
std::mutex zombiesMutex_;
ThreadLocalPtr<SysArena> arena_; // per-thread arena
};
+template <>
+struct IsArenaAllocator<ThreadCachedArena> : std::true_type { };
+
} // namespace folly
#endif /* FOLLY_THREADCACHEDARENA_H_ */