1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be
3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors.
4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "util/arena.h"
6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <assert.h>
7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb {
9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstatic const int kBlockSize = 4096;
11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgArena::Arena() {
13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  blocks_memory_ = 0;
14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  alloc_ptr_ = NULL;  // First allocation will allocate a block
15179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  alloc_bytes_remaining_ = 0;
16179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org}
17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgArena::~Arena() {
191511be6edb54b6ade2bfad94256f76bc191e92ecdgrogan@chromium.org  for (size_t i = 0; i < blocks_.size(); i++) {
20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    delete[] blocks_[i];
21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org}
23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgchar* Arena::AllocateFallback(size_t bytes) {
25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  if (bytes > kBlockSize / 4) {
26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    // Object is more than a quarter of our block size.  Allocate it separately
27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    // to avoid wasting too much space in leftover bytes.
28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    char* result = AllocateNewBlock(bytes);
29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    return result;
30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // We waste the remaining space in the current block.
33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  alloc_ptr_ = AllocateNewBlock(kBlockSize);
34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  alloc_bytes_remaining_ = kBlockSize;
35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  char* result = alloc_ptr_;
37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  alloc_ptr_ += bytes;
38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  alloc_bytes_remaining_ -= bytes;
39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  return result;
40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org}
41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgchar* Arena::AllocateAligned(size_t bytes) {
432d749ea62f0e47281e82794c8e00eb588cd66616dgrogan@chromium.org  const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;
44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  assert((align & (align-1)) == 0);   // Pointer size should be a power of 2
45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align-1);
46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  size_t slop = (current_mod == 0 ? 0 : align - current_mod);
47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  size_t needed = bytes + slop;
48179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  char* result;
49179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  if (needed <= alloc_bytes_remaining_) {
50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    result = alloc_ptr_ + slop;
51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    alloc_ptr_ += needed;
52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    alloc_bytes_remaining_ -= needed;
53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  } else {
54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    // AllocateFallback always returned aligned memory
55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    result = AllocateFallback(bytes);
56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  assert((reinterpret_cast<uintptr_t>(result) & (align-1)) == 0);
58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  return result;
59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org}
60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgchar* Arena::AllocateNewBlock(size_t block_bytes) {
62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  char* result = new char[block_bytes];
63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  blocks_memory_ += block_bytes;
64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  blocks_.push_back(result);
65179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  return result;
66179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org}
67179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
6845b9940be332834440bd5299419f396e38085ebehans@chromium.org}  // namespace leveldb
69