arena_impl.h 2.3 KB
Newer Older
J
jorlow@chromium.org 已提交
1 2 3 4
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

X
Xing Jin 已提交
5 6 7 8 9 10
// ArenaImpl is an implementation of Arena class. For a request of small size,
// it allocates a block with pre-defined block size. For a request of big
// size, it uses malloc to directly get the requested size.

#ifndef STORAGE_LEVELDB_UTIL_ARENA_IMPL_H_
#define STORAGE_LEVELDB_UTIL_ARENA_IMPL_H_
J
jorlow@chromium.org 已提交
11 12 13 14 15

#include <cstddef>
#include <vector>
#include <assert.h>
#include <stdint.h>
X
Xing Jin 已提交
16
#include "leveldb/arena.h"
J
jorlow@chromium.org 已提交
17 18 19

namespace leveldb {

X
Xing Jin 已提交
20
class ArenaImpl : public Arena {
J
jorlow@chromium.org 已提交
21
 public:
X
Xing Jin 已提交
22 23
  explicit ArenaImpl(size_t block_size = kMinBlockSize);
  virtual ~ArenaImpl();
J
jorlow@chromium.org 已提交
24

X
Xing Jin 已提交
25
  virtual char* Allocate(size_t bytes);
J
jorlow@chromium.org 已提交
26

X
Xing Jin 已提交
27
  virtual char* AllocateAligned(size_t bytes);
J
jorlow@chromium.org 已提交
28 29 30 31

  // Returns an estimate of the total memory usage of data allocated
  // by the arena (including space allocated but not yet used for user
  // allocations).
X
Xing Jin 已提交
32 33 34
  //
  // TODO: Do we need to exclude space allocated but not used?
  virtual const size_t ApproximateMemoryUsage() {
J
jorlow@chromium.org 已提交
35 36 37
    return blocks_memory_ + blocks_.capacity() * sizeof(char*);
  }

X
Xing Jin 已提交
38 39 40 41
  virtual const size_t MemoryAllocatedBytes() {
    return blocks_memory_;
  }

J
jorlow@chromium.org 已提交
42 43 44 45
 private:
  char* AllocateFallback(size_t bytes);
  char* AllocateNewBlock(size_t block_bytes);

X
Xing Jin 已提交
46 47 48 49 50 51
  static const size_t kMinBlockSize = 4096;
  static const size_t kMaxBlockSize = 2 << 30;

  // Number of bytes allocated in one block
  size_t block_size_;

J
jorlow@chromium.org 已提交
52 53 54 55 56 57 58 59 60 61 62
  // Allocation state
  char* alloc_ptr_;
  size_t alloc_bytes_remaining_;

  // Array of new[] allocated memory blocks
  std::vector<char*> blocks_;

  // Bytes of memory in blocks allocated so far
  size_t blocks_memory_;

  // No copying allowed
X
Xing Jin 已提交
63 64
  ArenaImpl(const ArenaImpl&);
  void operator=(const ArenaImpl&);
J
jorlow@chromium.org 已提交
65 66
};

X
Xing Jin 已提交
67
inline char* ArenaImpl::Allocate(size_t bytes) {
J
jorlow@chromium.org 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
  // The semantics of what to return are a bit messy if we allow
  // 0-byte allocations, so we disallow them here (we don't need
  // them for our internal use).
  assert(bytes > 0);
  if (bytes <= alloc_bytes_remaining_) {
    char* result = alloc_ptr_;
    alloc_ptr_ += bytes;
    alloc_bytes_remaining_ -= bytes;
    return result;
  }
  return AllocateFallback(bytes);
}

H
Hans Wennborg 已提交
81
}  // namespace leveldb
J
jorlow@chromium.org 已提交
82

X
Xing Jin 已提交
83
#endif  // STORAGE_LEVELDB_UTIL_ARENA_IMPL_H_