leveldb
arena.cc
Go to the documentation of this file.
1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 
5 #include "util/arena.h"
6 #include <assert.h>
7 
8 namespace leveldb {
9 
10 static const int kBlockSize = 4096;
11 
12 Arena::Arena() : memory_usage_(0) {
13  alloc_ptr_ = NULL; // First allocation will allocate a block
15 }
16 
18  for (size_t i = 0; i < blocks_.size(); i++) {
19  delete[] blocks_[i];
20  }
21 }
22 
23 char* Arena::AllocateFallback(size_t bytes) {
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.
32  alloc_ptr_ = AllocateNewBlock(kBlockSize);
34 
35  char* result = alloc_ptr_;
36  alloc_ptr_ += bytes;
37  alloc_bytes_remaining_ -= bytes;
38  return result;
39 }
40 
41 char* Arena::AllocateAligned(size_t bytes) {
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 }
59 
60 char* Arena::AllocateNewBlock(size_t block_bytes) {
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 }
67 
68 } // namespace leveldb
char * alloc_ptr_
Definition: arena.h:38
char * AllocateAligned(size_t bytes)
Definition: arena.cc:41
static const int kBlockSize
Definition: arena.cc:10
size_t MemoryUsage() const
Definition: arena.h:29
char * AllocateNewBlock(size_t block_bytes)
Definition: arena.cc:60
port::AtomicPointer memory_usage_
Definition: arena.h:45
std::vector< char * > blocks_
Definition: arena.h:42
char * AllocateFallback(size_t bytes)
Definition: arena.cc:23
size_t alloc_bytes_remaining_
Definition: arena.h:39