test: Add multidict to support dictionary with duplicate key (laanwj)
[bitcoinplatinum.git] / src / leveldb / util / arena.h
blob48bab3374159543f1d261f60467d4563c9103a8a
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.
5 #ifndef STORAGE_LEVELDB_UTIL_ARENA_H_
6 #define STORAGE_LEVELDB_UTIL_ARENA_H_
8 #include <vector>
9 #include <assert.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 #include "port/port.h"
14 namespace leveldb {
16 class Arena {
17 public:
18 Arena();
19 ~Arena();
21 // Return a pointer to a newly allocated memory block of "bytes" bytes.
22 char* Allocate(size_t bytes);
24 // Allocate memory with the normal alignment guarantees provided by malloc
25 char* AllocateAligned(size_t bytes);
27 // Returns an estimate of the total memory usage of data allocated
28 // by the arena.
29 size_t MemoryUsage() const {
30 return reinterpret_cast<uintptr_t>(memory_usage_.NoBarrier_Load());
33 private:
34 char* AllocateFallback(size_t bytes);
35 char* AllocateNewBlock(size_t block_bytes);
37 // Allocation state
38 char* alloc_ptr_;
39 size_t alloc_bytes_remaining_;
41 // Array of new[] allocated memory blocks
42 std::vector<char*> blocks_;
44 // Total memory usage of the arena.
45 port::AtomicPointer memory_usage_;
47 // No copying allowed
48 Arena(const Arena&);
49 void operator=(const Arena&);
52 inline char* Arena::Allocate(size_t bytes) {
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;
63 return AllocateFallback(bytes);
66 } // namespace leveldb
68 #endif // STORAGE_LEVELDB_UTIL_ARENA_H_