[2660] | 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 |
|
---|
[2805] | 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 |
|
---|
[2660] | 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() { }
|
---|
[2805] | 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 | */
|
---|
[2660] | 45 | virtual int_8 WriteToSwap(const T * d, size_t sz, int_8 idx, int_8 oswp=0, bool osw=false) = 0;
|
---|
[2805] | 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 | */
|
---|
[2660] | 53 | virtual void ReadFromSwap(int_8 idx, int_8 swp, T* d, size_t sz) = 0;
|
---|
[2863] | 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;
|
---|
[2660] | 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());
|
---|
[2863] | 70 | SetSwapper(dsw);
|
---|
[3213] | 71 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 72 | mSRef->swp = swpos;
|
---|
| 73 | for(size_t k=0; k<mSRef->fgwp.size(); k++) mSRef->fgwp[k] = true;
|
---|
[3213] | 74 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 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);
|
---|
[2863] | 81 | SetSwapper(dsw);
|
---|
[2660] | 82 | }
|
---|
| 83 | //! copy constructor - shares the data
|
---|
| 84 | SwSegDataBlock(const SwSegDataBlock<T>& a)
|
---|
| 85 | {
|
---|
[3213] | 86 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 87 | mSRef = a.mSRef;
|
---|
| 88 | mSRef->nref++;
|
---|
[3213] | 89 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 90 | }
|
---|
| 91 |
|
---|
| 92 | //! Destructor. The memory is freed when the last object referencing the data segment is destroyed
|
---|
[3213] | 93 | virtual ~SwSegDataBlock()
|
---|
| 94 | {
|
---|
| 95 | Delete();
|
---|
| 96 | }
|
---|
[2660] | 97 | //! Adds one segment to the data structure - returns the pointer to the allocated segment.
|
---|
| 98 | virtual size_t Extend()
|
---|
| 99 | {
|
---|
[3213] | 100 | size_t rs = 0;
|
---|
| 101 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 102 | mSRef->swp.push_back(0);
|
---|
| 103 | mSRef->fgwp.push_back(false);
|
---|
[3213] | 104 | rs = mSRef->swp.size();
|
---|
| 105 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
| 106 | return rs;
|
---|
[2660] | 107 | }
|
---|
[2863] | 108 | /*! \brief Changes the data segment size and reallocates the memory segments
|
---|
| 109 | \warning SetSwapper() must be called after call to SetSize()
|
---|
| 110 | */
|
---|
[2660] | 111 | // segsz : Segment size ; nbseg : Number of data segments
|
---|
| 112 | virtual void SetSize(size_t segsz, size_t nbseg=0)
|
---|
| 113 | {
|
---|
| 114 | Delete();
|
---|
[3213] | 115 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 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 | }
|
---|
[2863] | 127 | mSRef->swapper = NULL;
|
---|
[3213] | 128 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 129 | }
|
---|
[2863] | 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;
|
---|
[3213] | 135 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2863] | 136 | if (mSRef->swapper) delete mSRef->swapper;
|
---|
| 137 | mSRef->swapper = dsw.Clone();
|
---|
[3213] | 138 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2863] | 139 | }
|
---|
| 140 |
|
---|
[2660] | 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; }
|
---|
[2698] | 147 | //! Return the pointer to data segment \b k
|
---|
[2660] | 148 | virtual T* GetSegment(size_t k)
|
---|
| 149 | {
|
---|
[3213] | 150 | T* rs = NULL;
|
---|
| 151 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 152 | getSeg(k);
|
---|
| 153 | mSRef->fgcstbuff = false;
|
---|
[3213] | 154 | rs = mSRef->buff;
|
---|
| 155 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
| 156 | return rs;
|
---|
[2660] | 157 | }
|
---|
[2698] | 158 | //! Return the const (read-only) pointer to data segment \b k
|
---|
| 159 | virtual T const * GetCstSegment(size_t k) const
|
---|
[2660] | 160 | {
|
---|
[3213] | 161 | T const * rs = NULL;
|
---|
| 162 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[3172] | 163 | if (getSeg(k)) mSRef->fgcstbuff = true;
|
---|
[3213] | 164 | rs = mSRef->buff;
|
---|
| 165 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
| 166 | return rs;
|
---|
[2660] | 167 | }
|
---|
[2692] | 168 |
|
---|
| 169 | //! Equal operator. Shares the data with \b a
|
---|
| 170 | inline SwSegDataBlock<T>& operator = (const SwSegDataBlock<T>& a)
|
---|
| 171 | {
|
---|
| 172 | Delete();
|
---|
[3213] | 173 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2692] | 174 | mSRef = a.mSRef;
|
---|
| 175 | mSRef->nref++;
|
---|
[3213] | 176 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2695] | 177 | return *this;
|
---|
[2692] | 178 | }
|
---|
| 179 |
|
---|
[2695] | 180 | //! Empties all memory buffers to swap stream
|
---|
| 181 | void SwapOutBuffer() const
|
---|
| 182 | {
|
---|
[3213] | 183 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 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;
|
---|
[2695] | 189 | mSRef->bidx = -1;
|
---|
| 190 | mSRef->fgcstbuff = true;
|
---|
| 191 | }
|
---|
[3213] | 192 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2695] | 193 | }
|
---|
| 194 | //! Return the position tag (swap position) table, after call to SwapOutBuffer()
|
---|
| 195 | std::vector< int_8 > & GetSwapPosTagTable() const
|
---|
| 196 | {
|
---|
| 197 | SwapOutBuffer();
|
---|
[2660] | 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;
|
---|
[3213] | 209 | mSRef->gThsop.lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 210 | mSRef->nref--;
|
---|
[3213] | 211 | if (mSRef->nref > 0) {
|
---|
| 212 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
| 213 | mSRef = NULL;
|
---|
| 214 | return;
|
---|
| 215 | }
|
---|
[2863] | 216 | if (mSRef->swapper) delete mSRef->swapper;
|
---|
[2660] | 217 | delete[] mSRef->buff;
|
---|
[3213] | 218 | mSRef->gThsop.unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 219 | delete mSRef;
|
---|
| 220 | mSRef = NULL;
|
---|
| 221 | }
|
---|
[3172] | 222 | bool getSeg(size_t k) const
|
---|
[2660] | 223 | {
|
---|
[3172] | 224 | if (k == mSRef->bidx) return false;
|
---|
[2660] | 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);
|
---|
[3172] | 233 | //DEL-02022007 else { delete[] mSRef->buff; mSRef->buff = new T[mSRef->segsize]; }
|
---|
[2660] | 234 | mSRef->bidx = k;
|
---|
[3172] | 235 | return true;
|
---|
[2660] | 236 | }
|
---|
| 237 |
|
---|
[2805] | 238 | //! \cond
|
---|
[2660] | 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
|
---|
[3213] | 249 | ThSafeOp gThsop; // Mutex for thread safe operation one / SWSDREF struct
|
---|
[2660] | 250 | } SWSDREF;
|
---|
[2805] | 251 | //! \endcond
|
---|
[3213] | 252 | SWSDREF * mSRef; //!< SWSDREF structure for reference sharing
|
---|
| 253 |
|
---|
[2660] | 254 | };
|
---|
| 255 |
|
---|
| 256 |
|
---|
| 257 | } // Fin du namespace
|
---|
| 258 |
|
---|
| 259 | #endif
|
---|