Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
MultiBuffer.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MultiBuffer.h (C) 2000-2011 */
9/* */
10/* Template class of an array with a buffer. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCANE_UTILS_MULTIBUFFER_H
13#define ARCANE_UTILS_MULTIBUFFER_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
17#include "arcane/utils/ArrayView.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arcane
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27/*!
28 * \internal
29 * \brief Buffer for multiple allocation
30
31 This class manages a pre-allocated list of elements in order to limit
32 multiple calls to allocations (new() or malloc()).
33
34 Pre-allocations are done in blocks of \a m_buffer_size elements
35
36 To be used by this class, a type must possess a default constructor
37 and a copy operator. This class guarantees that the
38 returned pointers remain valid as long as this instance exists.
39
40 The constructors and copy operators do not duplicate memory
41 but simply retain the buffer size.
42*/
43template <class T>
44class MultiBufferT
45{
46 public:
47
48 typedef UniqueArray<T> BufferType;
49
50 public:
51
52 MultiBufferT()
53 : m_buffer_size(1000)
54 , m_current_buffer_size(0)
55 , m_nb_in_buffer(0)
56 , m_current_buffer(0)
57 {
58 }
59 MultiBufferT(Integer buf_size)
60 : m_buffer_size(buf_size)
61 , m_current_buffer_size(0)
62 , m_nb_in_buffer(0)
63 , m_current_buffer(0)
64 {
65 }
66
67 //! Copy constructor
68 MultiBufferT(const MultiBufferT<T>& ref)
69 : m_buffer_size(ref.m_buffer_size)
70 , m_current_buffer_size(0)
71 , m_nb_in_buffer(0)
72 , m_current_buffer(0)
73 {
74 }
76 {
77 _freeAllocatedBuffers();
78 }
79
80 public:
81
82 //! Copy assignment operator (forbidden)
83 void operator=(const MultiBufferT<T>& ref)
84 {
85 if (&ref == this)
86 return;
87 clear();
88 m_buffer_size = ref.m_buffer_size;
89 }
90
91 public:
92
93 //! Allocates a new element
95 {
96 if (!m_current_buffer)
97 _allocateCurrentBuffer();
98 T* v = &(*m_current_buffer)[m_nb_in_buffer];
99 ++m_nb_in_buffer;
100 if (m_nb_in_buffer == m_current_buffer_size)
101 m_current_buffer = 0; // Indicates that the current buffer is full
102 return v;
103 }
104
105 //! Allocates \a n elements
107 {
108 // If the desired number of elements is greater than the size
109 // of the buffer, specifically allocate a buffer of the correct size
110 if (n > m_current_buffer_size) {
111 BufferType* bt = new BufferType(n);
112 m_allocated_buffers.add(bt);
113 return *bt;
114 }
115 if (!m_current_buffer)
116 _allocateCurrentBuffer();
117 // If the current buffer is not large enough to contain the
118 // \a n desired elements, allocate another one
119 if ((m_nb_in_buffer + n) >= m_current_buffer_size)
120 _allocateCurrentBuffer();
121 T* v = &(*m_current_buffer)[m_nb_in_buffer];
122 m_nb_in_buffer += n;
123 if (m_nb_in_buffer == m_current_buffer_size)
124 m_current_buffer = 0; // Indicates that the current buffer is full
125 return ArrayView<T>(n, v);
126 }
127 void clear()
128 {
129 m_nb_in_buffer = 0;
130 m_current_buffer = 0;
131 m_current_buffer_size = 0;
132 _freeAllocatedBuffers();
133 }
134 Integer nbAllocatedBuffer() { return m_allocated_buffers.size(); }
135 Integer bufferSize() const { return m_current_buffer_size; }
136
137 protected:
138
139 void _freeAllocatedBuffers()
140 {
141 for (Integer i = 0, s = m_allocated_buffers.size(); i < s; ++i)
142 delete m_allocated_buffers[i];
143 m_allocated_buffers.clear();
144 }
145
146 private:
147
148 Integer m_buffer_size; //!< Number of elements in a buffer
149 Integer m_current_buffer_size; //!< Maximum number of elements in the current buffer
150 Integer m_nb_in_buffer; //!< Number of elements in the current buffer.
151 BufferType* m_current_buffer; //!< Current buffer
152 UniqueArray<BufferType*> m_allocated_buffers; //!< List of all buffers
153 private:
154
155 void _allocateCurrentBuffer()
156 {
157 m_current_buffer = new BufferType(m_buffer_size);
158 m_current_buffer_size = m_buffer_size;
159 m_allocated_buffers.add(m_current_buffer);
160 m_nb_in_buffer = 0;
161 }
162};
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166
167} // namespace Arcane
168
169/*---------------------------------------------------------------------------*/
170/*---------------------------------------------------------------------------*/
171
172#endif
Modifiable view of an array of type T.
MultiBufferT(const MultiBufferT< T > &ref)
Copy constructor.
Definition MultiBuffer.h:68
T * allocOne()
Allocates a new element.
Definition MultiBuffer.h:94
ArrayView< T > allocMany(Integer n)
Allocates n elements.
void operator=(const MultiBufferT< T > &ref)
Copy assignment operator (forbidden).
Definition MultiBuffer.h:83
1D data vector with value semantics (STL style).
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
Int32 Integer
Type representing an integer.