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.
251* Returns a block to the freelists, or deletes the block when it did not come from the chunks.252*/253void Deallocate(void* p, std::size_t bytes, std::size_t alignment) noexcept254{255if (IsFreeListUsable(bytes, alignment)) {256const std::size_t num_alignments = NumElemAlignBytes(bytes);257// put the memory block into the linked list. We can placement construct the FreeList258// into the memory since we can be sure the alignment is correct.259ASAN_UNPOISON_MEMORY_REGION(p, sizeof(ListNode));260PlacementAddToList(p, m_free_lists[num_alignments]);261ASAN_POISON_MEMORY_REGION(p, std::max(bytes, sizeof(ListNode)));262} else {263// Can't use the pool => forward deallocation to ::operator delete().264::operator delete (p, std::align_val_t{alignment});265}266}268/**269* Number of allocated chunks270*/271[[nodiscard]] std::size_t NumAllocatedChunks() const272{273return m_allocated_chunks.size();274}276/**277* Size in bytes to allocate per chunk, currently hardcoded to a fixed size.278*/279[[nodiscard]] size_t ChunkSizeBytes() const280{281return m_chunk_size_bytes;282}283};286/**287* Forwards all allocations/deallocations to the PoolResource.288*/289template <class T, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES = alignof(T)>290class PoolAllocator291{292PoolResource<MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES>* m_resource;294template <typename U, std::size_t M, std::size_t A>295friend class PoolAllocator;297public:298using value_type = T;299using ResourceType = PoolResource<MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES>;301/**302* Not explicit so we can easily construct it with the correct resource303*/304PoolAllocator(ResourceType* resource) noexcept305: m_resource(resource)306{307}309PoolAllocator(const PoolAllocator& other) noexcept = default;310PoolAllocator& operator=(const PoolAllocator& other) noexcept = default;312template <class U>313PoolAllocator(const PoolAllocator<U, MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES>& other) noexcept314: m_resource(other.resource())315{316}318/**319* The rebind struct here is mandatory because we use non type template arguments for320* PoolAllocator. See https://en.cppreference.com/w/cpp/named_req/Allocator#cite_note-2321*/322template <typename U>323struct rebind {324using other = PoolAllocator<U, MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES>;325};327/**328* Forwards each call to the resource.329*/330T* allocate(size_t n)331{332return static_cast<T*>(m_resource->Allocate(n * sizeof(T), alignof(T)));333}335/**336* Forwards each call to the resource.337*/338void deallocate(T* p, size_t n) noexcept339{340m_resource->Deallocate(p, n * sizeof(T), alignof(T));341}343ResourceType* resource() const noexcept344{345return m_resource;346}347};349template <class T1, class T2, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES>350bool operator==(const PoolAllocator<T1, MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES>& a,351const PoolAllocator<T2, MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES>& b) noexcept352{353return a.resource() == b.resource();354}356#endif // BITCOIN_SUPPORT_ALLOCATORS_POOL_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.