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#include "util/arena.h"7namespace leveldb {9static const int kBlockSize = 4096;11Arena::Arena()12: alloc_ptr_(nullptr), alloc_bytes_remaining_(0), memory_usage_(0) {}14Arena::~Arena() {15for (size_t i = 0; i < blocks_.size(); i++) {16delete[] blocks_[i];17}18}20char* Arena::AllocateFallback(size_t bytes) {21if (bytes > kBlockSize / 4) {22// Object is more than a quarter of our block size. Allocate it separately23// to avoid wasting too much space in leftover bytes.24char* result = AllocateNewBlock(bytes);25return result;26}28// We waste the remaining space in the current block.29alloc_ptr_ = AllocateNewBlock(kBlockSize);30alloc_bytes_remaining_ = kBlockSize;32char* result = alloc_ptr_;33alloc_ptr_ += bytes;34alloc_bytes_remaining_ -= bytes;35return result;36}38char* Arena::AllocateAligned(size_t bytes) {39const int align = (sizeof(void*) > 8) ? sizeof(void*) : 8;40static_assert((align & (align - 1)) == 0,41"Pointer size should be a power of 2");42size_t current_mod = reinterpret_cast<uintptr_t>(alloc_ptr_) & (align - 1);43size_t slop = (current_mod == 0 ? 0 : align - current_mod);44size_t needed = bytes + slop;45char* result;46if (needed <= alloc_bytes_remaining_) {47result = alloc_ptr_ + slop;48alloc_ptr_ += needed;49alloc_bytes_remaining_ -= needed;50} else {51// AllocateFallback always returned aligned memory52result = AllocateFallback(bytes);53}54assert((reinterpret_cast<uintptr_t>(result) & (align - 1)) == 0);55return result;56}58char* Arena::AllocateNewBlock(size_t block_bytes) {59char* result = new char[block_bytes];60blocks_.push_back(result);61memory_usage_.fetch_add(block_bytes + sizeof(char*),62std::memory_order_relaxed);63return result;64}66} // namespace leveldb
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.