Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
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 be3// 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 <atomic>9#include <cassert>10#include <cstddef>11#include <cstdint>12#include <vector>14namespace leveldb {16class Arena {17public:18Arena();20Arena(const Arena&) = delete;21Arena& operator=(const Arena&) = delete;23~Arena();25// Return a pointer to a newly allocated memory block of "bytes" bytes.26char* Allocate(size_t bytes);28// Allocate memory with the normal alignment guarantees provided by malloc.29char* AllocateAligned(size_t bytes);31// Returns an estimate of the total memory usage of data allocated32// by the arena.33size_t MemoryUsage() const {34return memory_usage_.load(std::memory_order_relaxed);35}37private:38char* AllocateFallback(size_t bytes);39char* AllocateNewBlock(size_t block_bytes);41// Allocation state42char* alloc_ptr_;43size_t alloc_bytes_remaining_;45// Array of new[] allocated memory blocks46std::vector<char*> blocks_;48// Total memory usage of the arena.49//50// TODO(costan): This member is accessed via atomics, but the others are51// accessed without any locking. Is this OK?52std::atomic<size_t> memory_usage_;53};55inline char* Arena::Allocate(size_t bytes) {56// The semantics of what to return are a bit messy if we allow57// 0-byte allocations, so we disallow them here (we don't need58// them for our internal use).59assert(bytes > 0);60if (bytes <= alloc_bytes_remaining_) {61char* result = alloc_ptr_;62alloc_ptr_ += bytes;63alloc_bytes_remaining_ -= bytes;64return result;65}66return AllocateFallback(bytes);67}69} // namespace leveldb71#endif // STORAGE_LEVELDB_UTIL_ARENA_H_
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.