1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 | // Gestion de block de donnees swapable
|
---|
3 | // R. Ansari Mars 2005
|
---|
4 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
5 | #ifndef SWSEGDATABLOCK_H
|
---|
6 | #define SWSEGDATABLOCK_H
|
---|
7 |
|
---|
8 | #include "machdefs.h"
|
---|
9 | #include "segdatablock.h"
|
---|
10 | #include "pexceptions.h"
|
---|
11 | #include <vector>
|
---|
12 | #include <typeinfo>
|
---|
13 |
|
---|
14 | /*!
|
---|
15 | \class SOPHYA::DataSwapperInterface
|
---|
16 | \ingroup BaseTools
|
---|
17 | Interface definition for data swapper (pure virtual) classes to be used
|
---|
18 | with SOPHYA::SwSegDataBlock classes.
|
---|
19 | */
|
---|
20 | /*!
|
---|
21 | \class SOPHYA::SwSegDataBlock
|
---|
22 | \ingroup BaseTools
|
---|
23 | Segmented data structure with swap space management.
|
---|
24 | */
|
---|
25 |
|
---|
26 | namespace SOPHYA {
|
---|
27 |
|
---|
28 | ////////////////////////////////////////////////////////////////
|
---|
29 | //// ------------- Class DataSwapperInterface --------------- //
|
---|
30 | //// ---------------- Class SwSegDataBlock ------------------ //
|
---|
31 | ////////////////////////////////////////////////////////////////
|
---|
32 |
|
---|
33 | template <class T>
|
---|
34 | class DataSwapperInterface {
|
---|
35 | public:
|
---|
36 | virtual ~DataSwapperInterface() { }
|
---|
37 | /*! Swap out the data array pointed by \b d with size \b sz
|
---|
38 | Return the swap position which might be used later to retrieve the data from swap
|
---|
39 | \param d : Pointer to the memory segment
|
---|
40 | \param sz : Number of elements (type T)
|
---|
41 | \param idx : An integer which might be used to identify the data (optional)
|
---|
42 | \param oswp : Old swap position, if the data has already been swapped
|
---|
43 | \param osw : true -> data has already been swapped
|
---|
44 | */
|
---|
45 | virtual int_8 WriteToSwap(const T * d, size_t sz, int_8 idx, int_8 oswp=0, bool osw=false) = 0;
|
---|
46 | /*! Swap in the data array pointed by \b d with size \b sz
|
---|
47 | Retrieves the data from swap space and copies it to \b d
|
---|
48 | \param idx : optional data identifier
|
---|
49 | \param swp : swap position (obtained from a previous call to WriteToSwap()
|
---|
50 | \param d : pointer to T , where the data will be copied from swap space
|
---|
51 | \param sz : Number of data elements (type T)
|
---|
52 | */
|
---|
53 | virtual void ReadFromSwap(int_8 idx, int_8 swp, T* d, size_t sz) = 0;
|
---|
54 |
|
---|
55 | /*! Duplicate the swapper object and return the new object pointer.
|
---|
56 | The returned pointer should be deleted when not needed any more.
|
---|
57 | This method is used by SwSegDataBlock<T>
|
---|
58 | */
|
---|
59 | virtual DataSwapperInterface<T>* Clone() = 0;
|
---|
60 | };
|
---|
61 |
|
---|
62 | template <class T>
|
---|
63 | class SwSegDataBlock : public SegDBInterface<T> {
|
---|
64 | public:
|
---|
65 | //! Constructor - creation from swap position tags (values)
|
---|
66 | SwSegDataBlock(DataSwapperInterface<T> & dsw, vector<int_8> const & swpos, size_t segsz)
|
---|
67 | {
|
---|
68 | mSRef = NULL;
|
---|
69 | SetSize(segsz, swpos.size());
|
---|
70 | SetSwapper(dsw);
|
---|
71 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
72 | mSRef->swp = swpos;
|
---|
73 | for(size_t k=0; k<mSRef->fgwp.size(); k++) mSRef->fgwp[k] = true;
|
---|
74 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
75 | }
|
---|
76 | //! Constructor - optional specification of segment size and number of segments
|
---|
77 | SwSegDataBlock(DataSwapperInterface<T> & dsw, size_t segsz=32, size_t nbseg=0)
|
---|
78 | {
|
---|
79 | mSRef = NULL;
|
---|
80 | SetSize(segsz, nbseg);
|
---|
81 | SetSwapper(dsw);
|
---|
82 | }
|
---|
83 | //! copy constructor - shares the data
|
---|
84 | SwSegDataBlock(const SwSegDataBlock<T>& a)
|
---|
85 | {
|
---|
86 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
87 | mSRef = a.mSRef;
|
---|
88 | mSRef->nref++;
|
---|
89 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
90 | }
|
---|
91 |
|
---|
92 | //! Destructor. The memory is freed when the last object referencing the data segment is destroyed
|
---|
93 | virtual ~SwSegDataBlock()
|
---|
94 | {
|
---|
95 | Delete();
|
---|
96 | }
|
---|
97 | //! Adds one segment to the data structure - returns the pointer to the allocated segment.
|
---|
98 | virtual size_t Extend()
|
---|
99 | {
|
---|
100 | size_t rs = 0;
|
---|
101 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
102 | mSRef->swp.push_back(0);
|
---|
103 | mSRef->fgwp.push_back(false);
|
---|
104 | rs = mSRef->swp.size();
|
---|
105 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
106 | return rs;
|
---|
107 | }
|
---|
108 | /*! \brief Changes the data segment size and reallocates the memory segments
|
---|
109 | \warning SetSwapper() must be called after call to SetSize()
|
---|
110 | */
|
---|
111 | // segsz : Segment size ; nbseg : Number of data segments
|
---|
112 | virtual void SetSize(size_t segsz, size_t nbseg=0)
|
---|
113 | {
|
---|
114 | Delete();
|
---|
115 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
116 | mSRef = new SWSDREF;
|
---|
117 | mSRef->nref = 1;
|
---|
118 | mSRef->segsize = segsz;
|
---|
119 | mSRef->dsid = AnyDataObj::getUniqueId();
|
---|
120 | mSRef->buff = new T[segsz];
|
---|
121 | mSRef->bidx = -1;
|
---|
122 | mSRef->fgcstbuff = true;
|
---|
123 | for(size_t k=0; k<nbseg; k++) {
|
---|
124 | mSRef->swp.push_back(0);
|
---|
125 | mSRef->fgwp.push_back(false);
|
---|
126 | }
|
---|
127 | mSRef->swapper = NULL;
|
---|
128 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
129 | }
|
---|
130 |
|
---|
131 | //! Define the data swapper object. Should only be called if SetSize() is called
|
---|
132 | void SetSwapper(DataSwapperInterface<T> & dsw)
|
---|
133 | {
|
---|
134 | if (mSRef == NULL) return;
|
---|
135 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
136 | if (mSRef->swapper) delete mSRef->swapper;
|
---|
137 | mSRef->swapper = dsw.Clone();
|
---|
138 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
139 | }
|
---|
140 |
|
---|
141 | //! Return the segment size data structure
|
---|
142 | virtual size_t SegmentSize() const { return mSRef->segsize; }
|
---|
143 | //! Return the number of data segments
|
---|
144 | virtual size_t NbSegments() const { return mSRef->swp.size(); } ;
|
---|
145 | //! Return the current size of the segmented data structure
|
---|
146 | inline size_t Size() const { return mSRef->swp.size()*mSRef->segsize; }
|
---|
147 | //! Return the pointer to data segment \b k
|
---|
148 | virtual T* GetSegment(size_t k)
|
---|
149 | {
|
---|
150 | T* rs = NULL;
|
---|
151 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
152 | getSeg(k);
|
---|
153 | mSRef->fgcstbuff = false;
|
---|
154 | rs = mSRef->buff;
|
---|
155 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
156 | return rs;
|
---|
157 | }
|
---|
158 | //! Return the const (read-only) pointer to data segment \b k
|
---|
159 | virtual T const * GetCstSegment(size_t k) const
|
---|
160 | {
|
---|
161 | T const * rs = NULL;
|
---|
162 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
163 | if (getSeg(k)) mSRef->fgcstbuff = true;
|
---|
164 | rs = mSRef->buff;
|
---|
165 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
166 | return rs;
|
---|
167 | }
|
---|
168 |
|
---|
169 | //! Equal operator. Shares the data with \b a
|
---|
170 | inline SwSegDataBlock<T>& operator = (const SwSegDataBlock<T>& a)
|
---|
171 | {
|
---|
172 | Delete();
|
---|
173 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
174 | mSRef = a.mSRef;
|
---|
175 | mSRef->nref++;
|
---|
176 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
177 | return *this;
|
---|
178 | }
|
---|
179 |
|
---|
180 | //! Empties all memory buffers to swap stream
|
---|
181 | void SwapOutBuffer() const
|
---|
182 | {
|
---|
183 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
184 | if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
|
---|
185 | int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx,
|
---|
186 | mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
|
---|
187 | mSRef->swp[mSRef->bidx] = nswp;
|
---|
188 | mSRef->fgwp[mSRef->bidx] = true;
|
---|
189 | mSRef->bidx = -1;
|
---|
190 | mSRef->fgcstbuff = true;
|
---|
191 | }
|
---|
192 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
193 | }
|
---|
194 | //! Return the position tag (swap position) table, after call to SwapOutBuffer()
|
---|
195 | std::vector< int_8 > & GetSwapPosTagTable() const
|
---|
196 | {
|
---|
197 | SwapOutBuffer();
|
---|
198 | return mSRef->swp;
|
---|
199 | }
|
---|
200 |
|
---|
201 | protected:
|
---|
202 | SwSegDataBlock()
|
---|
203 | {
|
---|
204 | throw ForbiddenError("SwSegDataBlock() default constructor not allowed (swsegdb.h)");
|
---|
205 | }
|
---|
206 | void Delete()
|
---|
207 | {
|
---|
208 | if (mSRef == NULL) return;
|
---|
209 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
210 | mSRef->nref--;
|
---|
211 | if (mSRef->nref > 0) {
|
---|
212 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
213 | mSRef = NULL;
|
---|
214 | return;
|
---|
215 | }
|
---|
216 | if (mSRef->swapper) delete mSRef->swapper;
|
---|
217 | delete[] mSRef->buff;
|
---|
218 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
219 | delete mSRef;
|
---|
220 | mSRef = NULL;
|
---|
221 | }
|
---|
222 | bool getSeg(size_t k) const
|
---|
223 | {
|
---|
224 | if (k == mSRef->bidx) return false;
|
---|
225 | if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
|
---|
226 | int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx,
|
---|
227 | mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
|
---|
228 | mSRef->swp[mSRef->bidx] = nswp;
|
---|
229 | mSRef->fgwp[mSRef->bidx] = true;
|
---|
230 | }
|
---|
231 | if (mSRef->fgwp[k])
|
---|
232 | mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], mSRef->buff, mSRef->segsize);
|
---|
233 | //DEL-02022007 else { delete[] mSRef->buff; mSRef->buff = new T[mSRef->segsize]; }
|
---|
234 | mSRef->bidx = k;
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 |
|
---|
238 | //! \cond
|
---|
239 | typedef struct {
|
---|
240 | size_t nref; // Number of references to the data structure
|
---|
241 | uint_8 dsid; // Data structure id
|
---|
242 | size_t segsize; // data segment size
|
---|
243 | mutable std::vector< int_8 > swp; // swap position tag for each segment
|
---|
244 | mutable std::vector< bool > fgwp; // swap flag (true = already swapped) for each segment
|
---|
245 | mutable T * buff; // Data buffer
|
---|
246 | mutable int_8 bidx; // segment index (number) corresponding to buffer
|
---|
247 | mutable bool fgcstbuff; // true : this is a constant T * buff<
|
---|
248 | DataSwapperInterface<T> * swapper; // Data swapper
|
---|
249 | ThSafeOp gThsop; // Mutex for thread safe operation one / SWSDREF struct
|
---|
250 | } SWSDREF;
|
---|
251 | //! \endcond
|
---|
252 | SWSDREF * mSRef; //!< SWSDREF structure for reference sharing
|
---|
253 |
|
---|
254 | };
|
---|
255 |
|
---|
256 |
|
---|
257 | } // Fin du namespace
|
---|
258 |
|
---|
259 | #endif
|
---|