casacore
Loading...
Searching...
No Matches
DynBuffer.h
Go to the documentation of this file.
1// # DynBuffer.h: Store data in dynamically allocated buffers
2// # Copyright (C) 1993,1994,1995,1996
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_DYNBUFFER_H
27#define CASA_DYNBUFFER_H
28
29// # Includes
30#include <casacore/casa/aips.h>
31#include <casacore/casa/Containers/Block.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// <summary>
36// Store data in dynamically allocated buffers
37// </summary>
38
39// <use visibility=export>
40// <reviewed reviewer="Friso Olnon" date="1995/03/16" tests="tDynBuffer" demos="">
41// </reviewed>
42
43// <synopsis>
44// DynBuffer allows one to store data in dynamically allocated buffers.
45// When a buffer is full, an additional buffer can be allocated and
46// "linked" to the existing one; so, the data may not be stored contiguously
47// You can loop through all the linked buffers and get their individual
48// addresses and sizes, so that you can access the data.
49// </synopsis>
50
51// <example>
52// Example (without exception handling):
53// <srcblock>
54// uInt nrOfValues, nrNeeded, nrAvailable;// nr of data values
55// float* pData = floatarr; // ptr to data to be handled
56// Char* pBuffer; // ptr to buffer
57//
58// DynBuffer buffer; // create buffer
59// buffer.allocstart(); // prepare for storing
60// nrNeeded = nrOfValues; // nr of values to store
61// // copy data into dynamic buffer
62// while (nrNeeded > 0) {
63// nrAvailable = buffer.alloc (nrNeeded, sizeof(float), pBuffer);
64// // get buffer space:
65// // room for nrAvailable values
66// memcpy (pBuffer, pData, nrAvailable*sizeof(float));
67// // copy that many data values
68// nrNeeded -= nrAvailable; // how much more needed?
69// pData += nrAvailable; // pointer to as yet unstored data
70// }
71// // Maybe store more values
72// .
73// .
74// // Retrieve all the data values from the buffers and write them
75// buffer.nextstart(); // goto buffer start
76// while (buffer.next (nrAvailable, pBuffer)) {
77// // get next buffer
78// write (fd, nrAvailable, pBuffer); // write data from that buffer
79// }
80// </srcblock>
81// </example>
82
83// <motivation>
84// This class is developed as an intermediate buffer for
85// class <linkto class=AipsIO>AipsIO</linkto>,
86// but it may serve other purposes as well.
87// </motivation>
88
89class DynBuffer {
90 public:
91 // Allocate a first buffer of the specified number of bytes
92 // (default 4096). When the allocation fails, an exception is thrown.
93 DynBuffer(uInt nrOfBytes = 4096);
94
95 // Remove the whole buffer, i.e. the first buffer and all the
96 // buffers appended to it.
98
99 // Prepare for storing data (re-initialize the buffer)
101
102 // Allocate buffer space for <src>nrOfValues</src> values of size
103 // <src>valueSize</src> bytes, and return the pointer <src>ptr</src>
104 // to the buffer and the number of values that fit in the buffer.
105 //
106 // When not all values fit in the current buffer, new buffer space
107 // is added (probably non-contiguous). If that allocation fails an
108 // exception is thrown.
109 uInt alloc(uInt nrOfValues, uInt valueSize, Char*& ptr);
110
111 // Remove buffer <src>nrOfBuffer</src> and the buffers appended to it,
112 // and re-initialize the current buffer. By default we keep the first
113 // buffer (i.e. the one numbered 0).
114 //
115 // The idea is that you may want to free intermediate storage
116 // space taken up by data that you no longer need, and that the
117 // first buffer is often big enough to hold further data. So, you
118 // only remove the first buffer in special cases.
119 void remove(uInt nrOfBuffer = 1);
120
121 // Prepare for data retrieval (set up for looping through the buffers).
122 void nextstart();
123
124 // Get the pointer to the next buffer and its used length in bytes.
125 // The function returns a <src>False</src> value if there are no more
126 // buffers.
127 Bool next(uInt& usedLength, Char*& ptr);
128
129 private:
130 // Get the next buffer for storing <src>nrOfValues</src> values of
131 // size <src>valueSize</src> bytes, and return the number of values
132 // that can be stored in the free space of that buffer (maybe less
133 // than <src>nrOfValues</src>).
134 //
135 // The new current buffer can be the present one (if it has free
136 // space), the next buffer already allocated (if there is one), or
137 // a newly allocated and linked-in buffer. If, in the last case,
138 // the allocation fails an exception is thrown.
139 uInt newbuf(uInt nrOfValues, uInt valueSize);
140
141 // size of 1st buffer and min. bufsize
143 // buffernr for next function
145 // current buffernr
147 // nr of buffers allocated
149 // size of Blocks
151 // used length per buffer
153 // total length per buffer
155 // pointer to buffer
157 // used length of current buffer
159 // total length of current buffer
161 // pointer to current buffer
163};
164
165// # Allocate buffer space for the nrOfValues values.
166// # Return pointer to the buffer and nr of values that fit in it.
167// # Use a more specialized function if not all values fit.
168// # In this way the function can be kept small and thus used inline.
169// # newbuf will seldom be required, unless large vectors are stored.
170inline uInt DynBuffer::alloc(uInt nrOfValues, uInt valueSize, Char*& ptr) {
171 uInt n = nrOfValues;
172 if (n * valueSize > curtotlen_p - curuselen_p) {
173 n = newbuf(nrOfValues, valueSize);
174 }
175 ptr = curbufptr_p + curuselen_p;
176 curuselen_p += n * valueSize;
177 return n;
178}
179
180} // namespace casacore
181
182#endif
uInt alloc(uInt nrOfValues, uInt valueSize, Char *&ptr)
Allocate buffer space for nrOfValues values of size valueSize bytes, and return the pointer ptr to th...
Definition DynBuffer.h:170
uInt bufsz_p
size of 1st buffer and min.
Definition DynBuffer.h:142
Int nextbuf_p
buffernr for next function
Definition DynBuffer.h:144
~DynBuffer()
Remove the whole buffer, i.e.
Char * curbufptr_p
pointer to current buffer
Definition DynBuffer.h:162
Block< uInt > totlen_p
total length per buffer
Definition DynBuffer.h:154
uInt newbuf(uInt nrOfValues, uInt valueSize)
Get the next buffer for storing nrOfValues values of size valueSize bytes, and return the number of v...
void remove(uInt nrOfBuffer=1)
Remove buffer nrOfBuffer and the buffers appended to it, and re-initialize the current buffer.
Int nrbuf_p
nr of buffers allocated
Definition DynBuffer.h:148
Int maxnrbuf_p
size of Blocks
Definition DynBuffer.h:150
Bool next(uInt &usedLength, Char *&ptr)
Get the pointer to the next buffer and its used length in bytes.
Int curbuf_p
current buffernr
Definition DynBuffer.h:146
void allocstart()
Prepare for storing data (re-initialize the buffer).
Block< Char * > bufptr_p
pointer to buffer
Definition DynBuffer.h:156
DynBuffer(uInt nrOfBytes=4096)
Allocate a first buffer of the specified number of bytes (default 4096).
uInt curuselen_p
used length of current buffer
Definition DynBuffer.h:158
void nextstart()
Prepare for data retrieval (set up for looping through the buffers).
uInt curtotlen_p
total length of current buffer
Definition DynBuffer.h:160
Block< uInt > uselen_p
used length per buffer
Definition DynBuffer.h:152
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned int uInt
Definition aipstype.h:49
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
char Char
Definition aipstype.h:44