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"7#include "util/random.h"8#include "util/testharness.h"10namespace leveldb {12class ArenaTest {};14TEST(ArenaTest, Empty) { Arena arena; }16TEST(ArenaTest, Simple) {17std::vector<std::pair<size_t, char*>> allocated;18Arena arena;19const int N = 100000;20size_t bytes = 0;21Random rnd(301);22for (int i = 0; i < N; i++) {23size_t s;24if (i % (N / 10) == 0) {25s = i;26} else {27s = rnd.OneIn(4000)28? rnd.Uniform(6000)29: (rnd.OneIn(10) ? rnd.Uniform(100) : rnd.Uniform(20));30}31if (s == 0) {32// Our arena disallows size 0 allocations.33s = 1;34}35char* r;36if (rnd.OneIn(10)) {37r = arena.AllocateAligned(s);38} else {39r = arena.Allocate(s);40}42for (size_t b = 0; b < s; b++) {43// Fill the "i"th allocation with a known bit pattern44r[b] = i % 256;45}46bytes += s;47allocated.push_back(std::make_pair(s, r));48ASSERT_GE(arena.MemoryUsage(), bytes);49if (i > N / 10) {50ASSERT_LE(arena.MemoryUsage(), bytes * 1.10);51}52}53for (size_t i = 0; i < allocated.size(); i++) {54size_t num_bytes = allocated[i].first;55const char* p = allocated[i].second;56for (size_t b = 0; b < num_bytes; b++) {57// Check the "i"th allocation for the known bit pattern58ASSERT_EQ(int(p[b]) & 0xff, i % 256);59}60}61}63} // namespace leveldb65int main(int argc, char** argv) { return leveldb::test::RunAllTests(); }
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.