leveldb
Public Member Functions | Private Member Functions | Private Attributes | List of all members
leveldb::Arena Class Reference

#include <arena.h>

Public Member Functions

 Arena ()
 
 ~Arena ()
 
char * Allocate (size_t bytes)
 
char * AllocateAligned (size_t bytes)
 
size_t MemoryUsage () const
 

Private Member Functions

char * AllocateFallback (size_t bytes)
 
char * AllocateNewBlock (size_t block_bytes)
 
 Arena (const Arena &)
 
void operator= (const Arena &)
 

Private Attributes

char * alloc_ptr_
 
size_t alloc_bytes_remaining_
 
std::vector< char * > blocks_
 
port::AtomicPointer memory_usage_
 

Detailed Description

Definition at line 16 of file arena.h.

Constructor & Destructor Documentation

§ Arena() [1/2]

leveldb::Arena::Arena ( )

Definition at line 12 of file arena.cc.

12  : memory_usage_(0) {
13  alloc_ptr_ = NULL; // First allocation will allocate a block
15 }
char * alloc_ptr_
Definition: arena.h:38
port::AtomicPointer memory_usage_
Definition: arena.h:45
size_t alloc_bytes_remaining_
Definition: arena.h:39

§ ~Arena()

leveldb::Arena::~Arena ( )

Definition at line 17 of file arena.cc.

17  {
18  for (size_t i = 0; i < blocks_.size(); i++) {
19  delete[] blocks_[i];
20  }
21 }
std::vector< char * > blocks_
Definition: arena.h:42

§ Arena() [2/2]

leveldb::Arena::Arena ( const Arena )
private

Member Function Documentation

§ Allocate()

char * leveldb::Arena::Allocate ( size_t  bytes)
inline

Definition at line 52 of file arena.h.

52  {
53  // The semantics of what to return are a bit messy if we allow
54  // 0-byte allocations, so we disallow them here (we don't need
55  // them for our internal use).
56  assert(bytes > 0);
57  if (bytes <= alloc_bytes_remaining_) {
58  char* result = alloc_ptr_;
59  alloc_ptr_ += bytes;
60  alloc_bytes_remaining_ -= bytes;
61  return result;
62  }
63  return AllocateFallback(bytes);
64 }
char * alloc_ptr_
Definition: arena.h:38
char * AllocateFallback(size_t bytes)
Definition: arena.cc:23
size_t alloc_bytes_remaining_
Definition: arena.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

§ AllocateAligned()

char * leveldb::Arena::AllocateAligned ( size_t  bytes)

Definition at line 41 of file arena.cc.

41  {
42  const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;
43  assert((align & (align-1)) == 0); // Pointer size should be a power of 2
44  size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);
45  size_t slop = (current_mod == 0 ? 0 : align - current_mod);
46  size_t needed = bytes + slop;
47  char* result;
48  if (needed <= alloc_bytes_remaining_) {
49  result = alloc_ptr_ + slop;
50  alloc_ptr_ += needed;
51  alloc_bytes_remaining_ -= needed;
52  } else {
53  // AllocateFallback always returned aligned memory
54  result = AllocateFallback(bytes);
55  }
56  assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0);
57  return result;
58 }
char * alloc_ptr_
Definition: arena.h:38
char * AllocateFallback(size_t bytes)
Definition: arena.cc:23
size_t alloc_bytes_remaining_
Definition: arena.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

§ AllocateFallback()

char * leveldb::Arena::AllocateFallback ( size_t  bytes)
private

Definition at line 23 of file arena.cc.

23  {
24  if (bytes > kBlockSize / 4) {
25  // Object is more than a quarter of our block size. Allocate it separately
26  // to avoid wasting too much space in leftover bytes.
27  char* result = AllocateNewBlock(bytes);
28  return result;
29  }
30 
31  // We waste the remaining space in the current block.
34 
35  char* result = alloc_ptr_;
36  alloc_ptr_ += bytes;
37  alloc_bytes_remaining_ -= bytes;
38  return result;
39 }
char * alloc_ptr_
Definition: arena.h:38
static const int kBlockSize
Definition: arena.cc:10
char * AllocateNewBlock(size_t block_bytes)
Definition: arena.cc:60
size_t alloc_bytes_remaining_
Definition: arena.h:39
Here is the call graph for this function:
Here is the caller graph for this function:

§ AllocateNewBlock()

char * leveldb::Arena::AllocateNewBlock ( size_t  block_bytes)
private

Definition at line 60 of file arena.cc.

60  {
61  char* result = new char[block_bytes];
62  blocks_.push_back(result);
63  memory_usage_.NoBarrier_Store(
64  reinterpret_cast<void*>(MemoryUsage() + block_bytes + sizeof(char*)));
65  return result;
66 }
size_t MemoryUsage() const
Definition: arena.h:29
port::AtomicPointer memory_usage_
Definition: arena.h:45
std::vector< char * > blocks_
Definition: arena.h:42
Here is the call graph for this function:
Here is the caller graph for this function:

§ MemoryUsage()

size_t leveldb::Arena::MemoryUsage ( ) const
inline

Definition at line 29 of file arena.h.

29  {
30  return reinterpret_cast<uintptr_t>(memory_usage_.NoBarrier_Load());
31  }
port::AtomicPointer memory_usage_
Definition: arena.h:45
Here is the call graph for this function:
Here is the caller graph for this function:

§ operator=()

void leveldb::Arena::operator= ( const Arena )
private

Member Data Documentation

§ alloc_bytes_remaining_

size_t leveldb::Arena::alloc_bytes_remaining_
private

Definition at line 39 of file arena.h.

§ alloc_ptr_

char* leveldb::Arena::alloc_ptr_
private

Definition at line 38 of file arena.h.

§ blocks_

std::vector<char*> leveldb::Arena::blocks_
private

Definition at line 42 of file arena.h.

§ memory_usage_

port::AtomicPointer leveldb::Arena::memory_usage_
private

Definition at line 45 of file arena.h.


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