proxygen
folly::Arena< Alloc > Class Template Reference

#include <Arena.h>

Classes

struct  AllocAndSize
 
struct  Block
 

Public Member Functions

 Arena (const Alloc &alloc, size_t minBlockSize=kDefaultMinBlockSize, size_t sizeLimit=kNoSizeLimit, size_t maxAlign=kDefaultMaxAlign)
 
 ~Arena ()
 
void * allocate (size_t size)
 
void deallocate (void *, size_t=0)
 
void merge (Arena &&other)
 
size_t totalSize () const
 
size_t bytesUsed () const
 
 Arena (const Arena &)=delete
 
Arenaoperator= (const Arena &)=delete
 
 Arena (Arena &&)=delete
 
Arenaoperator= (Arena &&)=delete
 

Static Public Attributes

static constexpr size_t kDefaultMinBlockSize = 4096 - sizeof(Block)
 
static constexpr size_t kNoSizeLimit = 0
 
static constexpr size_t kDefaultMaxAlign = alignof(Block)
 
static constexpr size_t kBlockOverhead = sizeof(Block)
 

Private Types

typedef boost::intrusive::slist_member_hook< boost::intrusive::tag< Arena > > BlockLink
 
typedef boost::intrusive::slist< Block, boost::intrusive::member_hook< Block, BlockLink,&Block::link >, boost::intrusive::constant_time_size< false >, boost::intrusive::cache_last< true > > BlockList
 

Private Member Functions

bool isAligned (uintptr_t address) const
 
bool isAligned (void *p) const
 
size_t roundUp (size_t size) const
 
void * allocateSlow (size_t size)
 
size_t minBlockSize () const
 
Allocalloc ()
 
const Allocalloc () const
 

Private Attributes

AllocAndSize allocAndSize_
 
BlockList blocks_
 
char * ptr_
 
char * end_
 
size_t totalAllocatedSize_
 
size_t bytesUsed_
 
const size_t sizeLimit_
 
const size_t maxAlign_
 

Detailed Description

template<class Alloc>
class folly::Arena< Alloc >

Definition at line 60 of file Arena.h.

Member Typedef Documentation

template<class Alloc>
typedef boost::intrusive::slist_member_hook<boost::intrusive::tag<Arena> > folly::Arena< Alloc >::BlockLink
private

Definition at line 128 of file Arena.h.

template<class Alloc>
typedef boost::intrusive::slist< Block, boost::intrusive::member_hook<Block, BlockLink, &Block::link>, boost::intrusive::constant_time_size<false>, boost::intrusive::cache_last<true> > folly::Arena< Alloc >::BlockList
private

Definition at line 178 of file Arena.h.

Constructor & Destructor Documentation

template<class Alloc>
folly::Arena< Alloc >::Arena ( const Alloc alloc,
size_t  minBlockSize = kDefaultMinBlockSize,
size_t  sizeLimit = kNoSizeLimit,
size_t  maxAlign = kDefaultMaxAlign 
)
inlineexplicit

Definition at line 62 of file Arena.h.

Referenced by folly::Arena< SysAllocator< void > >::bytesUsed(), and folly::Arena< SysAllocator< void > >::totalSize().

67  : allocAndSize_(alloc, minBlockSize),
68  ptr_(nullptr),
69  end_(nullptr),
71  bytesUsed_(0),
72  sizeLimit_(sizeLimit),
73  maxAlign_(maxAlign) {
74  if ((maxAlign_ & (maxAlign_ - 1)) || maxAlign_ > alignof(Block)) {
75  throw_exception(std::invalid_argument(
76  folly::to<std::string>("Invalid maxAlign: ", maxAlign_)));
77  }
78  }
const size_t sizeLimit_
Definition: Arena.h:207
size_t bytesUsed_
Definition: Arena.h:206
char * ptr_
Definition: Arena.h:203
size_t totalAllocatedSize_
Definition: Arena.h:205
AllocAndSize allocAndSize_
Definition: Arena.h:201
const size_t maxAlign_
Definition: Arena.h:208
char * end_
Definition: Arena.h:204
size_t minBlockSize() const
Definition: Arena.h:191
FOLLY_NOINLINE FOLLY_COLD void throw_exception(Ex &&ex)
Definition: Exception.h:32
template<class Alloc >
folly::Arena< Alloc >::~Arena ( )

Definition at line 86 of file Arena-inl.h.

References folly::Arena< Alloc >::alloc(), b, and folly::Arena< Alloc >::blocks_.

Referenced by folly::Arena< SysAllocator< void > >::Arena().

86  {
87  auto disposer = [this](Block* b) { b->deallocate(this->alloc()); };
88  while (!blocks_.empty()) {
89  blocks_.pop_front_and_dispose(disposer);
90  }
91 }
char b
BlockList blocks_
Definition: Arena.h:202
Alloc & alloc()
Definition: Arena.h:194
template<class Alloc>
folly::Arena< Alloc >::Arena ( const Arena< Alloc > &  )
delete
template<class Alloc>
folly::Arena< Alloc >::Arena ( Arena< Alloc > &&  )
delete

Member Function Documentation

template<class Alloc>
Alloc& folly::Arena< Alloc >::alloc ( )
inlineprivate

Definition at line 194 of file Arena.h.

Referenced by folly::Arena< Alloc >::allocateSlow(), and folly::Arena< Alloc >::~Arena().

194  {
195  return allocAndSize_;
196  }
AllocAndSize allocAndSize_
Definition: Arena.h:201
template<class Alloc>
const Alloc& folly::Arena< Alloc >::alloc ( ) const
inlineprivate

Definition at line 197 of file Arena.h.

197  {
198  return allocAndSize_;
199  }
AllocAndSize allocAndSize_
Definition: Arena.h:201
template<class Alloc>
void* folly::Arena< Alloc >::allocate ( size_t  size)
inline

Definition at line 82 of file Arena.h.

Referenced by folly::ThreadCachedArena::allocate(), and TEST().

82  {
83  size = roundUp(size);
84  bytesUsed_ += size;
85 
86  assert(ptr_ <= end_);
87  if (LIKELY((size_t)(end_ - ptr_) >= size)) {
88  // Fast path: there's enough room in the current block
89  char* r = ptr_;
90  ptr_ += size;
91  assert(isAligned(r));
92  return r;
93  }
94 
95  // Not enough room in the current block
96  void* r = allocateSlow(size);
97  assert(isAligned(r));
98  return r;
99  }
#define LIKELY(x)
Definition: Likely.h:47
size_t bytesUsed_
Definition: Arena.h:206
char * ptr_
Definition: Arena.h:203
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
void * allocateSlow(size_t size)
Definition: Arena-inl.h:44
bool isAligned(uintptr_t address) const
Definition: Arena.h:159
char * end_
Definition: Arena.h:204
size_t roundUp(size_t size) const
Definition: Arena.h:167
template<class Alloc >
void * folly::Arena< Alloc >::allocateSlow ( size_t  size)
private

Definition at line 44 of file Arena-inl.h.

References folly::Arena< Alloc >::alloc(), folly::Arena< Alloc >::Block::allocate(), folly::Arena< Alloc >::Block::Block(), folly::Arena< Alloc >::blocks_, folly::Arena< Alloc >::end_, folly::Arena< Alloc >::kNoSizeLimit, max, folly::Arena< Alloc >::minBlockSize(), folly::Arena< Alloc >::ptr_, folly::size(), folly::Arena< Alloc >::sizeLimit_, folly::Arena< Alloc >::Block::start(), folly::throw_exception(), and folly::Arena< Alloc >::totalAllocatedSize_.

Referenced by folly::Arena< SysAllocator< void > >::allocate().

44  {
45  std::pair<Block*, size_t> p;
46  char* start;
47 
48  size_t allocSize = std::max(size, minBlockSize()) + sizeof(Block);
49  if (sizeLimit_ != kNoSizeLimit &&
50  allocSize > sizeLimit_ - totalAllocatedSize_) {
51  throw_exception(std::bad_alloc());
52  }
53 
54  if (size > minBlockSize()) {
55  // Allocate a large block for this chunk only, put it at the back of the
56  // list so it doesn't get used for small allocations; don't change ptr_
57  // and end_, let them point into a normal block (or none, if they're
58  // null)
59  p = Block::allocate(alloc(), size, false);
60  start = p.first->start();
61  blocks_.push_back(*p.first);
62  } else {
63  // Allocate a normal sized block and carve out size bytes from it
64  p = Block::allocate(alloc(), minBlockSize(), true);
65  start = p.first->start();
66  blocks_.push_front(*p.first);
67  ptr_ = start + size;
68  end_ = start + p.second;
69  }
70 
71  assert(p.second >= size);
72  totalAllocatedSize_ += p.second + sizeof(Block);
73  return start;
74 }
const size_t sizeLimit_
Definition: Arena.h:207
static constexpr size_t kNoSizeLimit
Definition: Arena.h:154
LogLevel max
Definition: LogLevel.cpp:31
static std::pair< Block *, size_t > allocate(Alloc &alloc, size_t size, bool allowSlack)
Definition: Arena-inl.h:27
char * ptr_
Definition: Arena.h:203
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
size_t totalAllocatedSize_
Definition: Arena.h:205
auto start
char * end_
Definition: Arena.h:204
BlockList blocks_
Definition: Arena.h:202
size_t minBlockSize() const
Definition: Arena.h:191
FOLLY_NOINLINE FOLLY_COLD void throw_exception(Ex &&ex)
Definition: Exception.h:32
Alloc & alloc()
Definition: Arena.h:194
template<class Alloc>
size_t folly::Arena< Alloc >::bytesUsed ( ) const
inline

Definition at line 117 of file Arena.h.

Referenced by TEST().

117  {
118  return bytesUsed_;
119  }
size_t bytesUsed_
Definition: Arena.h:206
template<class Alloc>
void folly::Arena< Alloc >::deallocate ( void *  ,
size_t  = 0 
)
inline

Definition at line 101 of file Arena.h.

Referenced by TEST().

101  {
102  // Deallocate? Never!
103  }
template<class Alloc>
bool folly::Arena< Alloc >::isAligned ( uintptr_t  address) const
inlineprivate

Definition at line 159 of file Arena.h.

Referenced by folly::Arena< SysAllocator< void > >::allocate(), and folly::Arena< SysAllocator< void > >::isAligned().

159  {
160  return (address & (maxAlign_ - 1)) == 0;
161  }
const size_t maxAlign_
Definition: Arena.h:208
template<class Alloc>
bool folly::Arena< Alloc >::isAligned ( void *  p) const
inlineprivate

Definition at line 162 of file Arena.h.

162  {
163  return isAligned(reinterpret_cast<uintptr_t>(p));
164  }
bool isAligned(uintptr_t address) const
Definition: Arena.h:159
template<class Alloc>
void folly::Arena< Alloc >::merge ( Arena< Alloc > &&  other)

Definition at line 77 of file Arena-inl.h.

References folly::Arena< Alloc >::blocks_, and folly::Arena< Alloc >::totalAllocatedSize_.

Referenced by folly::Arena< SysAllocator< void > >::deallocate().

77  {
78  blocks_.splice_after(blocks_.before_begin(), other.blocks_);
79  other.blocks_.clear();
80  other.ptr_ = other.end_ = nullptr;
81  totalAllocatedSize_ += other.totalAllocatedSize_;
82  other.totalAllocatedSize_ = 0;
83 }
size_t totalAllocatedSize_
Definition: Arena.h:205
BlockList blocks_
Definition: Arena.h:202
template<class Alloc>
size_t folly::Arena< Alloc >::minBlockSize ( ) const
inlineprivate

Definition at line 191 of file Arena.h.

Referenced by folly::Arena< Alloc >::allocateSlow(), and folly::SysArena::SysArena().

191  {
193  }
AllocAndSize allocAndSize_
Definition: Arena.h:201
template<class Alloc>
Arena& folly::Arena< Alloc >::operator= ( const Arena< Alloc > &  )
delete
template<class Alloc>
Arena& folly::Arena< Alloc >::operator= ( Arena< Alloc > &&  )
delete
template<class Alloc>
size_t folly::Arena< Alloc >::roundUp ( size_t  size) const
inlineprivate

Definition at line 167 of file Arena.h.

Referenced by folly::Arena< SysAllocator< void > >::allocate().

167  {
168  return (size + maxAlign_ - 1) & ~(maxAlign_ - 1);
169  }
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
const size_t maxAlign_
Definition: Arena.h:208
template<class Alloc>
size_t folly::Arena< Alloc >::totalSize ( ) const
inline

Definition at line 109 of file Arena.h.

Referenced by TEST().

109  {
110  return totalAllocatedSize_ + sizeof(Arena);
111  }
size_t totalAllocatedSize_
Definition: Arena.h:205
Arena(const Alloc &alloc, size_t minBlockSize=kDefaultMinBlockSize, size_t sizeLimit=kNoSizeLimit, size_t maxAlign=kDefaultMaxAlign)
Definition: Arena.h:62

Member Data Documentation

template<class Alloc>
AllocAndSize folly::Arena< Alloc >::allocAndSize_
private
template<class Alloc>
BlockList folly::Arena< Alloc >::blocks_
private
template<class Alloc>
size_t folly::Arena< Alloc >::bytesUsed_
private
template<class Alloc>
char* folly::Arena< Alloc >::end_
private
template<class Alloc>
constexpr size_t folly::Arena< Alloc >::kBlockOverhead = sizeof(Block)
static

Definition at line 156 of file Arena.h.

template<class Alloc>
constexpr size_t folly::Arena< Alloc >::kDefaultMaxAlign = alignof(Block)
static

Definition at line 155 of file Arena.h.

template<class Alloc>
constexpr size_t folly::Arena< Alloc >::kDefaultMinBlockSize = 4096 - sizeof(Block)
static

Definition at line 153 of file Arena.h.

template<class Alloc>
constexpr size_t folly::Arena< Alloc >::kNoSizeLimit = 0
static

Definition at line 154 of file Arena.h.

Referenced by folly::Arena< Alloc >::allocateSlow().

template<class Alloc>
const size_t folly::Arena< Alloc >::maxAlign_
private
template<class Alloc>
char* folly::Arena< Alloc >::ptr_
private
template<class Alloc>
const size_t folly::Arena< Alloc >::sizeLimit_
private

Definition at line 207 of file Arena.h.

Referenced by folly::Arena< Alloc >::allocateSlow().

template<class Alloc>
size_t folly::Arena< Alloc >::totalAllocatedSize_
private

The documentation for this class was generated from the following files: