Belle II Software development
KDTNodePool Class Reference

Memory pool for pre-allocating and reusing KDTNode objects. More...

#include <Utils.h>

Public Member Functions

 KDTNodePool (size_t capacity)
 Constructs a pool with the specified capacity.
 
 ~KDTNodePool ()
 Destructor.
 
 KDTNodePool (const KDTNodePool &)=delete
 Copy constructor.
 
KDTNodePooloperator= (const KDTNodePool &)=delete
 Assignment operator.
 
KDTNodeallocate ()
 Allocates and returns the next available KDTNode from the pool.
 
void reset ()
 Resets the pool index, making all nodes available for reuse.
 

Private Attributes

std::vector< KDTNode * > m_pool
 Internal storage for preallocated KD-tree nodes.
 
size_t m_index
 Current index for the next available node in the pool.
 

Detailed Description

Memory pool for pre-allocating and reusing KDTNode objects.

Improves performance by avoiding dynamic memory allocation during KD-tree construction. Nodes are allocated once at initialization and reset between KD-tree builds.

Definition at line 70 of file Utils.h.

Constructor & Destructor Documentation

◆ KDTNodePool()

KDTNodePool ( size_t capacity)
explicit

Constructs a pool with the specified capacity.

Preallocates memory for a given number of KD-tree nodes.

Parameters
capacityMaximum number of KDTNodes to allocate.

Definition at line 18 of file Utils.cc.

18 : m_index(0)
19{
20 m_pool.resize(capacity);
21 std::generate(m_pool.begin(), m_pool.end(), [] { return new KDTNode(); });
22}
size_t m_index
Current index for the next available node in the pool.
Definition Utils.h:75
std::vector< KDTNode * > m_pool
Internal storage for preallocated KD-tree nodes.
Definition Utils.h:73

◆ ~KDTNodePool()

Destructor.

Frees all allocated KDTNode objects.

Definition at line 24 of file Utils.cc.

25{
26 for (KDTNode* node : m_pool) {
27 delete node;
28 }
29}

Member Function Documentation

◆ allocate()

KDTNode * allocate ( )

Allocates and returns the next available KDTNode from the pool.

Returns
Pointer to a KDTNode.
Note
Logs an error if the pool capacity is exceeded.

Definition at line 31 of file Utils.cc.

32{
33 if (m_index >= m_pool.size())
34 B2ERROR("KDTNodePool:allocate() exceeded pool capacity.");
35 // Hand out the next pre-allocated slot and advance the cursor
36 return m_pool[m_index++];
37}

◆ reset()

void reset ( )

Resets the pool index, making all nodes available for reuse.

Does not deallocate any memory.

Definition at line 39 of file Utils.cc.

40{
41 // Rewind the cursor so all slots can be reused in the next event
42 // without freeing / reallocating memory
43 m_index = 0;
44}

Member Data Documentation

◆ m_index

size_t m_index
private

Current index for the next available node in the pool.

Definition at line 75 of file Utils.h.

◆ m_pool

std::vector<KDTNode*> m_pool
private

Internal storage for preallocated KD-tree nodes.

Definition at line 73 of file Utils.h.


The documentation for this class was generated from the following files: