ATLAS Offline Software
Loading...
Searching...
No Matches
pointer_list.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5// $Id$
12
13
15#include <algorithm>
16
17
18namespace CxxUtils {
19
20
30 size_t nblock,
31 unsigned long end_mask,
32 unsigned long end_offs)
33 : m_nelt (nelt),
34 m_nblock (nblock),
35 m_chunks (0),
36 m_nthis (nblock),
37 m_nchunks (0),
38 m_end_mask (end_mask),
39 m_end_offs (end_offs)
40{
41}
42
43
48{
49 // Loop over the chunks, deleting each one.
50 chunk* ch = m_chunks;
51 while (ch) {
52 chunk* next = ch->m_next;
53 delete [] reinterpret_cast<char*> (ch);
54 ch = next;
55 }
56}
57
58
63{
64 char* p = new char
67 chunk* ch = reinterpret_cast<chunk*> (p);
68
69 // Align.
70 unsigned long pp = reinterpret_cast<unsigned long> (ch+1);
71 pp = (pp + m_end_mask) & ~m_end_mask;
72 ch->m_blocks = reinterpret_cast<list_block*> (pp);
73
74 ch->m_next = m_chunks;
75 m_chunks = ch;
76 ++m_nchunks;
77
78 // No blocks allocated so far from this chunk.
79 m_nthis = 0;
80}
81
82
90{
91 if (m_head)
92 m_insert = &m_head->m_data[0];
93 m_size = 0;
94}
95
96
101{
102 m_head = getblock();
103 m_insert = &m_head->m_data[0];
104}
105
106
112{
113 // There may be one already allocated. Use it if so.
114 list_block* newblock =
115 reinterpret_cast<list_block*> (*m_insert);
116 if (!newblock) {
117 newblock = getblock();
118 *m_insert = newblock;
119 }
120 m_insert = &newblock->m_data[0];
121}
122
123
128{
129 list_block* b = m_pool.allocate();
130 size_t maxndx = m_pool.nelt();
131
132 // Make sure only the last element has the sentinel bit set.
133 std::fill (b->m_data, b->m_data + maxndx, value_type());
134 b->m_data[maxndx] = 0;
135
136 return b;
137}
138
139
140} // namespace CxxUtils
141
unsigned long m_end_mask
Mask for testing for an end pointer.
allocator(size_t nelt, size_t nblock, unsigned long end_mask, unsigned long end_offs)
Constructor.
void refill()
Allocate a new chunk of blocks.
size_t m_nblock
Number of blocks per chunk.
~allocator()
Destructor. Deletes all blocks from this allocator.
chunk * m_chunks
Most recent chunk allocated.
size_t m_nelt
Number of elements per block (excluding the end-pointer).
size_t m_nchunks
Current number of allocated chunks.
unsigned long m_end_offs
Offset for testing for an end pointer.
size_t nelt() const
Return the number of pointers per block (excluding the end-pointer).
size_t m_nthis
Number of blocks allocated so far from that chunk.
list_block::value_type value_type
The stored element type.
list_block * m_head
The first block in the list.
void nextblock()
Extend the list with another block.
value_type * m_insert
The current insertion point in the list.
void clear()
Erase the container.
void firstblock()
Allocate the first block of the list.
size_t m_size
The current list size.
allocator & m_pool
The list allocator.
list_block * getblock()
Allocate a new block.
A fast way to store a variable-sized collection of pointers.
A single block in the list.
static size_t size(size_t nelt)
Size in bytes of a block holding nelt elements (excluding the end-pointer).
value_type m_data[1]
The elements.