[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);
|
---|
[2660] | 71 | mSRef->swp = swpos;
|
---|
| 72 | for(size_t k=0; k<mSRef->fgwp.size(); k++) mSRef->fgwp[k] = true;
|
---|
| 73 | }
|
---|
| 74 | //! Constructor - optional specification of segment size and number of segments
|
---|
| 75 | SwSegDataBlock(DataSwapperInterface<T> & dsw, size_t segsz=32, size_t nbseg=0)
|
---|
| 76 | {
|
---|
| 77 | mSRef = NULL;
|
---|
| 78 | SetSize(segsz, nbseg);
|
---|
[2863] | 79 | SetSwapper(dsw);
|
---|
[2660] | 80 | }
|
---|
| 81 | //! copy constructor - shares the data
|
---|
| 82 | SwSegDataBlock(const SwSegDataBlock<T>& a)
|
---|
| 83 | {
|
---|
| 84 | mSRef = a.mSRef;
|
---|
| 85 | mSRef->nref++;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | //! Destructor. The memory is freed when the last object referencing the data segment is destroyed
|
---|
| 89 | virtual ~SwSegDataBlock() { Delete(); }
|
---|
| 90 | //! Adds one segment to the data structure - returns the pointer to the allocated segment.
|
---|
| 91 | virtual size_t Extend()
|
---|
| 92 | {
|
---|
| 93 | mSRef->swp.push_back(0);
|
---|
| 94 | mSRef->fgwp.push_back(false);
|
---|
| 95 | return mSRef->swp.size();
|
---|
| 96 | }
|
---|
[2863] | 97 | /*! \brief Changes the data segment size and reallocates the memory segments
|
---|
| 98 | \warning SetSwapper() must be called after call to SetSize()
|
---|
| 99 | */
|
---|
[2660] | 100 | // segsz : Segment size ; nbseg : Number of data segments
|
---|
| 101 | virtual void SetSize(size_t segsz, size_t nbseg=0)
|
---|
| 102 | {
|
---|
| 103 | Delete();
|
---|
| 104 | mSRef = new SWSDREF;
|
---|
| 105 | mSRef->nref = 1;
|
---|
| 106 | mSRef->segsize = segsz;
|
---|
| 107 | mSRef->dsid = AnyDataObj::getUniqueId();
|
---|
| 108 | mSRef->buff = new T[segsz];
|
---|
| 109 | mSRef->bidx = -1;
|
---|
| 110 | mSRef->fgcstbuff = true;
|
---|
| 111 | for(size_t k=0; k<nbseg; k++) {
|
---|
| 112 | mSRef->swp.push_back(0);
|
---|
| 113 | mSRef->fgwp.push_back(false);
|
---|
| 114 | }
|
---|
[2863] | 115 | mSRef->swapper = NULL;
|
---|
[2660] | 116 | }
|
---|
[2863] | 117 |
|
---|
| 118 | //! Define the data swapper object. Should only be called if SetSize() is called
|
---|
| 119 | void SetSwapper(DataSwapperInterface<T> & dsw)
|
---|
| 120 | {
|
---|
| 121 | if (mSRef == NULL) return;
|
---|
| 122 | if (mSRef->swapper) delete mSRef->swapper;
|
---|
| 123 | mSRef->swapper = dsw.Clone();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[2660] | 126 | //! Return the segment size data structure
|
---|
| 127 | virtual size_t SegmentSize() const { return mSRef->segsize; }
|
---|
| 128 | //! Return the number of data segments
|
---|
| 129 | virtual size_t NbSegments() const { return mSRef->swp.size(); } ;
|
---|
| 130 | //! Return the current size of the segmented data structure
|
---|
| 131 | inline size_t Size() const { return mSRef->swp.size()*mSRef->segsize; }
|
---|
[2698] | 132 | //! Return the pointer to data segment \b k
|
---|
[2660] | 133 | virtual T* GetSegment(size_t k)
|
---|
| 134 | {
|
---|
| 135 | getSeg(k);
|
---|
| 136 | mSRef->fgcstbuff = false;
|
---|
| 137 | return mSRef->buff;
|
---|
| 138 | }
|
---|
[2698] | 139 | //! Return the const (read-only) pointer to data segment \b k
|
---|
| 140 | virtual T const * GetCstSegment(size_t k) const
|
---|
[2660] | 141 | {
|
---|
| 142 | getSeg(k);
|
---|
| 143 | mSRef->fgcstbuff = true;
|
---|
| 144 | return mSRef->buff;
|
---|
| 145 | }
|
---|
[2692] | 146 |
|
---|
| 147 | //! Equal operator. Shares the data with \b a
|
---|
| 148 | inline SwSegDataBlock<T>& operator = (const SwSegDataBlock<T>& a)
|
---|
| 149 | {
|
---|
| 150 | Delete();
|
---|
| 151 | mSRef = a.mSRef;
|
---|
| 152 | mSRef->nref++;
|
---|
[2695] | 153 | return *this;
|
---|
[2692] | 154 | }
|
---|
| 155 |
|
---|
[2695] | 156 | //! Empties all memory buffers to swap stream
|
---|
| 157 | void SwapOutBuffer() const
|
---|
| 158 | {
|
---|
[2660] | 159 | if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
|
---|
| 160 | int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx,
|
---|
| 161 | mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
|
---|
| 162 | mSRef->swp[mSRef->bidx] = nswp;
|
---|
| 163 | mSRef->fgwp[mSRef->bidx] = true;
|
---|
[2695] | 164 | mSRef->bidx = -1;
|
---|
| 165 | mSRef->fgcstbuff = true;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | //! Return the position tag (swap position) table, after call to SwapOutBuffer()
|
---|
| 169 | std::vector< int_8 > & GetSwapPosTagTable() const
|
---|
| 170 | {
|
---|
| 171 | SwapOutBuffer();
|
---|
[2660] | 172 | return mSRef->swp;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | protected:
|
---|
| 176 | SwSegDataBlock()
|
---|
| 177 | {
|
---|
| 178 | throw ForbiddenError("SwSegDataBlock() default constructor not allowed (swsegdb.h)");
|
---|
| 179 | }
|
---|
| 180 | void Delete()
|
---|
| 181 | {
|
---|
| 182 | if (mSRef == NULL) return;
|
---|
| 183 | mSRef->nref--;
|
---|
| 184 | if (mSRef->nref > 0) { mSRef = NULL; return; }
|
---|
[2863] | 185 | if (mSRef->swapper) delete mSRef->swapper;
|
---|
[2660] | 186 | delete[] mSRef->buff;
|
---|
| 187 | delete mSRef;
|
---|
| 188 | mSRef = NULL;
|
---|
| 189 | }
|
---|
| 190 | void getSeg(size_t k) const
|
---|
| 191 | {
|
---|
| 192 | if (k == mSRef->bidx) return ;
|
---|
| 193 | if ((mSRef->bidx >= 0) && !mSRef->fgcstbuff) {
|
---|
| 194 | int_8 nswp = mSRef->swapper->WriteToSwap(mSRef->buff, mSRef->segsize, mSRef->bidx,
|
---|
| 195 | mSRef->swp[mSRef->bidx], mSRef->fgwp[mSRef->bidx]);
|
---|
| 196 | mSRef->swp[mSRef->bidx] = nswp;
|
---|
| 197 | mSRef->fgwp[mSRef->bidx] = true;
|
---|
| 198 | }
|
---|
| 199 | if (mSRef->fgwp[k])
|
---|
| 200 | mSRef->swapper->ReadFromSwap(k, mSRef->swp[k], mSRef->buff, mSRef->segsize);
|
---|
| 201 | else { delete[] mSRef->buff; mSRef->buff = new T[mSRef->segsize]; }
|
---|
| 202 | mSRef->bidx = k;
|
---|
| 203 | return;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[2805] | 206 | //! \cond
|
---|
[2660] | 207 | typedef struct {
|
---|
| 208 | size_t nref; // Number of references to the data structure
|
---|
| 209 | uint_8 dsid; // Data structure id
|
---|
| 210 | size_t segsize; // data segment size
|
---|
| 211 | mutable std::vector< int_8 > swp; // swap position tag for each segment
|
---|
| 212 | mutable std::vector< bool > fgwp; // swap flag (true = already swapped) for each segment
|
---|
| 213 | mutable T * buff; // Data buffer
|
---|
| 214 | mutable int_8 bidx; // segment index (number) corresponding to buffer
|
---|
| 215 | mutable bool fgcstbuff; // true : this is a constant T * buff<
|
---|
| 216 | DataSwapperInterface<T> * swapper; // Data swapper
|
---|
| 217 | } SWSDREF;
|
---|
[2805] | 218 | //! \endcond
|
---|
[2660] | 219 | SWSDREF * mSRef; // SWSDREF structure for reference sharing
|
---|
| 220 | };
|
---|
| 221 |
|
---|
| 222 |
|
---|
| 223 | } // Fin du namespace
|
---|
| 224 |
|
---|
| 225 | #endif
|
---|