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
33
34namespace casacore { //# NAMESPACE CASACORE - BEGIN
35
36// <summary>
37// Store data in dynamically allocated buffers
38// </summary>
39
40// <use visibility=export>
41// <reviewed reviewer="Friso Olnon" date="1995/03/16" tests="tDynBuffer" demos="">
42// </reviewed>
43
44// <synopsis>
45// DynBuffer allows one to store data in dynamically allocated buffers.
46// When a buffer is full, an additional buffer can be allocated and
47// "linked" to the existing one; so, the data may not be stored contiguously
48// You can loop through all the linked buffers and get their individual
49// addresses and sizes, so that you can access the data.
50// </synopsis>
51
52// <example>
53// Example (without exception handling):
54// <srcblock>
55// uInt nrOfValues, nrNeeded, nrAvailable;// nr of data values
56// float* pData = floatarr; // ptr to data to be handled
57// Char* pBuffer; // ptr to buffer
58//
59// DynBuffer buffer; // create buffer
60// buffer.allocstart(); // prepare for storing
61// nrNeeded = nrOfValues; // nr of values to store
62// // copy data into dynamic buffer
63// while (nrNeeded > 0) {
64// nrAvailable = buffer.alloc (nrNeeded, sizeof(float), pBuffer);
65// // get buffer space:
66// // room for nrAvailable values
67// memcpy (pBuffer, pData, nrAvailable*sizeof(float));
68// // copy that many data values
69// nrNeeded -= nrAvailable; // how much more needed?
70// pData += nrAvailable; // pointer to as yet unstored data
71// }
72// // Maybe store more values
73// .
74// .
75// // Retrieve all the data values from the buffers and write them
76// buffer.nextstart(); // goto buffer start
77// while (buffer.next (nrAvailable, pBuffer)) {
78// // get next buffer
79// write (fd, nrAvailable, pBuffer); // write data from that buffer
80// }
81// </srcblock>
82// </example>
83
84// <motivation>
85// This class is developed as an intermediate buffer for
86// class <linkto class=AipsIO>AipsIO</linkto>,
87// but it may serve other purposes as well.
88// </motivation>
89
91{
92public:
93
94 // Allocate a first buffer of the specified number of bytes
95 // (default 4096). When the allocation fails, an exception is thrown.
96 DynBuffer (uInt nrOfBytes=4096);
97
98 // Remove the whole buffer, i.e. the first buffer and all the
99 // buffers appended to it.
101
102 // Prepare for storing data (re-initialize the buffer)
103 void allocstart ();
104
105 // Allocate buffer space for <src>nrOfValues</src> values of size
106 // <src>valueSize</src> bytes, and return the pointer <src>ptr</src>
107 // to the buffer and the number of values that fit in the buffer.
108 //
109 // When not all values fit in the current buffer, new buffer space
110 // is added (probably non-contiguous). If that allocation fails an
111 // exception is thrown.
112 uInt alloc (uInt nrOfValues, uInt valueSize, Char*& ptr);
113
114 // Remove buffer <src>nrOfBuffer</src> and the buffers appended to it,
115 // and re-initialize the current buffer. By default we keep the first
116 // buffer (i.e. the one numbered 0).
117 //
118 // The idea is that you may want to free intermediate storage
119 // space taken up by data that you no longer need, and that the
120 // first buffer is often big enough to hold further data. So, you
121 // only remove the first buffer in special cases.
122 void remove (uInt nrOfBuffer=1);
123
124 // Prepare for data retrieval (set up for looping through the buffers).
125 void nextstart ();
126
127 // Get the pointer to the next buffer and its used length in bytes.
128 // The function returns a <src>False</src> value if there are no more
129 // buffers.
130 Bool next (uInt& usedLength, Char*& ptr);
131
132private:
133 // Get the next buffer for storing <src>nrOfValues</src> values of
134 // size <src>valueSize</src> bytes, and return the number of values
135 // that can be stored in the free space of that buffer (maybe less
136 // than <src>nrOfValues</src>).
137 //
138 // The new current buffer can be the present one (if it has free
139 // space), the next buffer already allocated (if there is one), or
140 // a newly allocated and linked-in buffer. If, in the last case,
141 // the allocation fails an exception is thrown.
142 uInt newbuf (uInt nrOfValues, uInt valueSize);
143
144
145 // size of 1st buffer and min. bufsize
147 // buffernr for next function
149 // current buffernr
151 // nr of buffers allocated
153 // size of Blocks
155 // used length per buffer
157 // total length per buffer
159 // pointer to buffer
161 // used length of current buffer
163 // total length of current buffer
165 // pointer to current buffer
167};
168
169
170//# Allocate buffer space for the nrOfValues values.
171//# Return pointer to the buffer and nr of values that fit in it.
172//# Use a more specialized function if not all values fit.
173//# In this way the function can be kept small and thus used inline.
174//# newbuf will seldom be required, unless large vectors are stored.
175inline uInt DynBuffer::alloc (uInt nrOfValues, uInt valueSize, Char*& ptr)
176{
177 uInt n = nrOfValues;
178 if (n*valueSize > curtotlen_p-curuselen_p) {
179 n = newbuf (nrOfValues, valueSize);
180 }
181 ptr = curbufptr_p + curuselen_p;
182 curuselen_p += n*valueSize;
183 return n;
184}
185
186
187
188} //# NAMESPACE CASACORE - END
189
190#endif
simple 1-D array
Definition Block.h:198
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:175
uInt bufsz_p
size of 1st buffer and min.
Definition DynBuffer.h:146
Int nextbuf_p
buffernr for next function
Definition DynBuffer.h:148
~DynBuffer()
Remove the whole buffer, i.e.
Char * curbufptr_p
pointer to current buffer
Definition DynBuffer.h:166
Block< uInt > totlen_p
total length per buffer
Definition DynBuffer.h:158
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:152
Int maxnrbuf_p
size of Blocks
Definition DynBuffer.h:154
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:150
void allocstart()
Prepare for storing data (re-initialize the buffer)
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:162
PtrBlock< Char * > bufptr_p
pointer to buffer
Definition DynBuffer.h:160
void nextstart()
Prepare for data retrieval (set up for looping through the buffers).
uInt curtotlen_p
total length of current buffer
Definition DynBuffer.h:164
Block< uInt > uselen_p
used length per buffer
Definition DynBuffer.h:156
A drop-in replacement for Block<T*>.
Definition Block.h:812
this file contains all the compiler specific defines
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