proxygen
folly::SimpleAllocator Class Reference

#include <CacheLocality.h>

Public Member Functions

 SimpleAllocator (size_t allocSize, size_t sz)
 
 ~SimpleAllocator ()
 
void * allocateHard ()
 
void * allocate ()
 
void deallocate (void *mem)
 

Private Attributes

std::mutex m_
 
uint8_tmem_ {nullptr}
 
uint8_tend_ {nullptr}
 
void * freelist_ {nullptr}
 
size_t allocSize_
 
size_t sz_
 
std::vector< void * > blocks_
 

Detailed Description

A simple freelist allocator. Allocates things of size sz, from slabs of size allocSize. Takes a lock on each allocation/deallocation.

Definition at line 394 of file CacheLocality.h.

Constructor & Destructor Documentation

folly::SimpleAllocator::SimpleAllocator ( size_t  allocSize,
size_t  sz 
)

Definition at line 238 of file CacheLocality.cpp.

References sz_.

239  : allocSize_{allocSize}, sz_(sz) {}
folly::SimpleAllocator::~SimpleAllocator ( )

Definition at line 241 of file CacheLocality.cpp.

References folly::aligned_free(), blocks_, g(), and m_.

241  {
242  std::lock_guard<std::mutex> g(m_);
243  for (auto& block : blocks_) {
244  folly::aligned_free(block);
245  }
246 }
void aligned_free(void *aligned_ptr)
Definition: Memory.h:89
g_t g(f_t)
std::vector< void * > blocks_

Member Function Documentation

void* folly::SimpleAllocator::allocate ( )
inline

Definition at line 409 of file CacheLocality.h.

References end_, g(), folly::max_align_v, and min.

409  {
410  std::lock_guard<std::mutex> g(m_);
411  // Freelist allocation.
412  if (freelist_) {
413  auto mem = freelist_;
414  freelist_ = *static_cast<void**>(freelist_);
415  return mem;
416  }
417 
418  // Bump-ptr allocation.
419  if (intptr_t(mem_) % 128 == 0) {
420  // Avoid allocating pointers that may look like malloc
421  // pointers.
423  }
424  if (mem_ && (mem_ + sz_ <= end_)) {
425  auto mem = mem_;
426  mem_ += sz_;
427 
428  assert(intptr_t(mem) % 128 != 0);
429  return mem;
430  }
431 
432  return allocateHard();
433  }
constexpr std::size_t max_align_v
Definition: Align.h:90
LogLevel min
Definition: LogLevel.cpp:30
g_t g(f_t)
void * folly::SimpleAllocator::allocateHard ( )

Definition at line 248 of file CacheLocality.cpp.

References folly::aligned_malloc(), allocSize_, blocks_, end_, folly::max_align_v, mem_, min, sz_, and uint8_t.

248  {
249  // Allocate a new slab.
251  if (!mem_) {
252  throw_exception<std::bad_alloc>();
253  }
254  end_ = mem_ + allocSize_;
255  blocks_.push_back(mem_);
256 
257  // Install a pointer to ourselves as the allocator.
258  *reinterpret_cast<SimpleAllocator**>(mem_) = this;
259  static_assert(max_align_v >= sizeof(SimpleAllocator*), "alignment too small");
261 
262  // New allocation.
263  auto mem = mem_;
264  mem_ += sz_;
265  assert(intptr_t(mem) % 128 != 0);
266  return mem;
267 }
SimpleAllocator(size_t allocSize, size_t sz)
constexpr std::size_t max_align_v
Definition: Align.h:90
LogLevel min
Definition: LogLevel.cpp:30
void * aligned_malloc(size_t size, size_t align)
Definition: Memory.h:85
std::vector< void * > blocks_
void folly::SimpleAllocator::deallocate ( void *  mem)
inline

Definition at line 434 of file CacheLocality.h.

References g().

Referenced by folly::CoreRawAllocator< Stripes >::Allocator::deallocate().

434  {
435  std::lock_guard<std::mutex> g(m_);
436  *static_cast<void**>(mem) = freelist_;
437  freelist_ = mem;
438  }
g_t g(f_t)

Member Data Documentation

size_t folly::SimpleAllocator::allocSize_
private

Definition at line 399 of file CacheLocality.h.

Referenced by allocateHard().

std::vector<void*> folly::SimpleAllocator::blocks_
private

Definition at line 401 of file CacheLocality.h.

Referenced by allocateHard(), and ~SimpleAllocator().

uint8_t* folly::SimpleAllocator::end_ {nullptr}
private

Definition at line 397 of file CacheLocality.h.

Referenced by allocateHard().

void* folly::SimpleAllocator::freelist_ {nullptr}
private

Definition at line 398 of file CacheLocality.h.

std::mutex folly::SimpleAllocator::m_
private

Definition at line 395 of file CacheLocality.h.

Referenced by ~SimpleAllocator().

uint8_t* folly::SimpleAllocator::mem_ {nullptr}
private

Definition at line 396 of file CacheLocality.h.

Referenced by allocateHard().

size_t folly::SimpleAllocator::sz_
private

Definition at line 400 of file CacheLocality.h.

Referenced by allocateHard(), and SimpleAllocator().


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