Commit 970c7961 authored by Philip Pronin's avatar Philip Pronin Committed by Sara Golemon

configurable alignment for Arena

Summary: Allow specifying custom alignment.

Test Plan: fbconfig -r folly/test && fbmake opt -j32

@override-unit-failures

Reviewed By: lucian@fb.com

FB internal diff: D1264851
parent 074bdb7f
...@@ -31,7 +31,6 @@ Arena<Alloc>::Block::allocate(Alloc& alloc, size_t size, bool allowSlack) { ...@@ -31,7 +31,6 @@ Arena<Alloc>::Block::allocate(Alloc& alloc, size_t size, bool allowSlack) {
} }
void* mem = alloc.allocate(allocSize); void* mem = alloc.allocate(allocSize);
assert(isAligned(mem));
return std::make_pair(new (mem) Block(), allocSize - sizeof(Block)); return std::make_pair(new (mem) Block(), allocSize - sizeof(Block));
} }
...@@ -46,9 +45,9 @@ void* Arena<Alloc>::allocateSlow(size_t size) { ...@@ -46,9 +45,9 @@ void* Arena<Alloc>::allocateSlow(size_t size) {
std::pair<Block*, size_t> p; std::pair<Block*, size_t> p;
char* start; char* start;
size_t allocSize = std::max(size, minBlockSize()) + sizeof(Block); size_t allocSize = std::max(size, minBlockSize()) + sizeof(Block);
if(sizeLimit_ && allocSize > sizeLimit_ - totalAllocatedSize_) { if (sizeLimit_ != kNoSizeLimit &&
allocSize > sizeLimit_ - totalAllocatedSize_) {
throw std::bad_alloc(); throw std::bad_alloc();
} }
......
...@@ -19,9 +19,11 @@ ...@@ -19,9 +19,11 @@
#include <cassert> #include <cassert>
#include <limits> #include <limits>
#include <stdexcept>
#include <utility> #include <utility>
#include <boost/intrusive/slist.hpp> #include <boost/intrusive/slist.hpp>
#include "folly/Conv.h"
#include "folly/Likely.h" #include "folly/Likely.h"
#include "folly/Malloc.h" #include "folly/Malloc.h"
#include "folly/Memory.h" #include "folly/Memory.h"
...@@ -61,13 +63,19 @@ class Arena { ...@@ -61,13 +63,19 @@ class Arena {
public: public:
explicit Arena(const Alloc& alloc, explicit Arena(const Alloc& alloc,
size_t minBlockSize = kDefaultMinBlockSize, size_t minBlockSize = kDefaultMinBlockSize,
size_t sizeLimit = 0) size_t sizeLimit = kNoSizeLimit,
size_t maxAlign = kDefaultMaxAlign)
: allocAndSize_(alloc, minBlockSize) : allocAndSize_(alloc, minBlockSize)
, ptr_(nullptr) , ptr_(nullptr)
, end_(nullptr) , end_(nullptr)
, totalAllocatedSize_(0) , totalAllocatedSize_(0)
, bytesUsed_(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(); ~Arena();
...@@ -147,19 +155,20 @@ class Arena { ...@@ -147,19 +155,20 @@ class Arena {
public: public:
static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block); static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block);
static constexpr size_t kNoSizeLimit = 0;
static constexpr size_t kDefaultMaxAlign = alignof(Block);
private: private:
static constexpr size_t maxAlign = alignof(Block); bool isAligned(uintptr_t address) const {
static constexpr bool isAligned(uintptr_t address) { return (address & (maxAlign_ - 1)) == 0;
return (address & (maxAlign - 1)) == 0;
} }
static bool isAligned(void* p) { bool isAligned(void* p) const {
return isAligned(reinterpret_cast<uintptr_t>(p)); return isAligned(reinterpret_cast<uintptr_t>(p));
} }
// Round up size so it's properly aligned // Round up size so it's properly aligned
static constexpr size_t roundUp(size_t size) { size_t roundUp(size_t size) const {
return (size + maxAlign - 1) & ~(maxAlign - 1); return (size + maxAlign_ - 1) & ~(maxAlign_ - 1);
} }
// cache_last<true> makes the list keep a pointer to the last element, so we // cache_last<true> makes the list keep a pointer to the last element, so we
...@@ -194,7 +203,8 @@ class Arena { ...@@ -194,7 +203,8 @@ class Arena {
char* end_; char* end_;
size_t totalAllocatedSize_; size_t totalAllocatedSize_;
size_t bytesUsed_; size_t bytesUsed_;
size_t sizeLimit_; const size_t sizeLimit_;
const size_t maxAlign_;
}; };
template <class Alloc> template <class Alloc>
...@@ -223,8 +233,9 @@ struct ArenaAllocatorTraits<SysAlloc> { ...@@ -223,8 +233,9 @@ struct ArenaAllocatorTraits<SysAlloc> {
class SysArena : public Arena<SysAlloc> { class SysArena : public Arena<SysAlloc> {
public: public:
explicit SysArena(size_t minBlockSize = kDefaultMinBlockSize, explicit SysArena(size_t minBlockSize = kDefaultMinBlockSize,
size_t sizeLimit = 0) size_t sizeLimit = kNoSizeLimit,
: Arena<SysAlloc>(SysAlloc(), minBlockSize, sizeLimit) { size_t maxAlign = kDefaultMaxAlign)
: Arena<SysAlloc>(SysAlloc(), minBlockSize, sizeLimit, maxAlign) {
} }
}; };
......
...@@ -18,12 +18,13 @@ ...@@ -18,12 +18,13 @@
namespace folly { namespace folly {
ThreadCachedArena::ThreadCachedArena(size_t minBlockSize) ThreadCachedArena::ThreadCachedArena(size_t minBlockSize, size_t maxAlign)
: minBlockSize_(minBlockSize) { : minBlockSize_(minBlockSize), maxAlign_(maxAlign) {
} }
SysArena* ThreadCachedArena::allocateThreadLocalArena() { SysArena* ThreadCachedArena::allocateThreadLocalArena() {
SysArena* arena = new SysArena(minBlockSize_); SysArena* arena =
new SysArena(minBlockSize_, SysArena::kNoSizeLimit, maxAlign_);
auto disposer = [this] (SysArena* t, TLPDestructionMode mode) { auto disposer = [this] (SysArena* t, TLPDestructionMode mode) {
std::unique_ptr<SysArena> tp(t); // ensure it gets deleted std::unique_ptr<SysArena> tp(t); // ensure it gets deleted
if (mode == TLPDestructionMode::THIS_THREAD) { if (mode == TLPDestructionMode::THIS_THREAD) {
......
...@@ -42,7 +42,8 @@ namespace folly { ...@@ -42,7 +42,8 @@ namespace folly {
class ThreadCachedArena { class ThreadCachedArena {
public: public:
explicit ThreadCachedArena( explicit ThreadCachedArena(
size_t minBlockSize = SysArena::kDefaultMinBlockSize); size_t minBlockSize = SysArena::kDefaultMinBlockSize,
size_t maxAlign = SysArena::kDefaultMaxAlign);
void* allocate(size_t size) { void* allocate(size_t size) {
SysArena* arena = arena_.get(); SysArena* arena = arena_.get();
...@@ -69,12 +70,16 @@ class ThreadCachedArena { ...@@ -69,12 +70,16 @@ class ThreadCachedArena {
// the ThreadCachedArena is destroyed. // the ThreadCachedArena is destroyed.
void zombify(SysArena&& arena); void zombify(SysArena&& arena);
size_t minBlockSize_; const size_t minBlockSize_;
const size_t maxAlign_;
SysArena zombies_; // allocated from threads that are now dead SysArena zombies_; // allocated from threads that are now dead
std::mutex zombiesMutex_; std::mutex zombiesMutex_;
ThreadLocalPtr<SysArena> arena_; // per-thread arena ThreadLocalPtr<SysArena> arena_; // per-thread arena
}; };
template <>
struct IsArenaAllocator<ThreadCachedArena> : std::true_type { };
} // namespace folly } // namespace folly
#endif /* FOLLY_THREADCACHEDARENA_H_ */ #endif /* FOLLY_THREADCACHEDARENA_H_ */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment