[2660] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | // Gestion de block de donnees avec partage de references
|
---|
| 3 | // R. Ansari Mars 2005
|
---|
| 4 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
| 5 | #ifndef SEGDATABLOCK_H
|
---|
| 6 | #define SEGDATABLOCK_H
|
---|
| 7 |
|
---|
| 8 | #include "machdefs.h"
|
---|
| 9 | #include "anydataobj.h"
|
---|
| 10 | #include <string.h>
|
---|
| 11 | #include <vector>
|
---|
| 12 | #include <iostream>
|
---|
| 13 | #include <iomanip>
|
---|
| 14 | #include <typeinfo>
|
---|
| 15 |
|
---|
[3213] | 16 | #include "thsafeop.h" // for ThreadSafe operations (Ref.Count/Share)
|
---|
| 17 |
|
---|
[2805] | 18 | /*!
|
---|
| 19 | \class SOPHYA::SegDBInterface
|
---|
| 20 | \ingroup BaseTools
|
---|
| 21 | Interface definition for segmented data container (template class).
|
---|
| 22 | */
|
---|
| 23 | /*!
|
---|
| 24 | \class SOPHYA::SegDataBlock
|
---|
| 25 | \ingroup BaseTools
|
---|
| 26 | Template class impementing segmented data container in memory with
|
---|
| 27 | management of reference sharing.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[2660] | 30 | namespace SOPHYA {
|
---|
| 31 |
|
---|
| 32 | ////////////////////////////////////////////////////////////////
|
---|
| 33 | //// ---------------- Class SegDBInterface ------------------ //
|
---|
| 34 | //// ----------------- Class SegDataBlock ------------------- //
|
---|
| 35 | ////////////////////////////////////////////////////////////////
|
---|
| 36 |
|
---|
| 37 | template <class T>
|
---|
| 38 | class SegDBInterface : public AnyDataObj {
|
---|
| 39 | public:
|
---|
| 40 | virtual ~SegDBInterface() {}
|
---|
| 41 | //! Changes the data segment size and reallocates the memory segments
|
---|
| 42 | // segsz : Segment size ; nbseg : Number of data segments
|
---|
| 43 | virtual void SetSize(size_t segsz, size_t nbseg=0) = 0;
|
---|
| 44 | //! Alias for SetSize()
|
---|
| 45 | inline void ReSize(size_t segsz, size_t nbseg=0) { SetSize(segsz, nbseg); }
|
---|
| 46 | //! Adds one segment to the data structure - returns the new number of segments.
|
---|
| 47 | virtual size_t Extend() = 0;
|
---|
| 48 | //! Return the segment size data structure
|
---|
| 49 | virtual size_t SegmentSize() const = 0;
|
---|
| 50 | //! Return the number of data segments
|
---|
| 51 | virtual size_t NbSegments() const = 0;
|
---|
| 52 | //! Return the current size of the segmented data structure
|
---|
| 53 | inline size_t Size() const { return SegmentSize()*NbSegments(); }
|
---|
[2698] | 54 | //! Return the pointer to data segment \b k
|
---|
| 55 | virtual T* GetSegment(size_t k) = 0;
|
---|
| 56 | //! Return the const (read-only) pointer to data segment \b k
|
---|
| 57 | virtual T const * GetCstSegment(size_t k) const = 0;
|
---|
[2660] | 58 | };
|
---|
| 59 |
|
---|
[2805] | 60 |
|
---|
[2660] | 61 | // classe de container avec partage de reference
|
---|
| 62 | template <class T>
|
---|
| 63 | class SegDataBlock : public SegDBInterface<T> {
|
---|
| 64 |
|
---|
| 65 | public:
|
---|
| 66 |
|
---|
| 67 | //! Default constructor - optional specification of segment size and number of segments
|
---|
| 68 | explicit SegDataBlock(size_t segsz=32, size_t nbseg=0)
|
---|
| 69 | {
|
---|
| 70 | mSRef = NULL;
|
---|
[3213] | 71 | if (gThsop == NULL) gThsop = new ThSafeOp;
|
---|
[2660] | 72 | SetSize(segsz, nbseg);
|
---|
| 73 | }
|
---|
| 74 | //! copy constructor - shares the data
|
---|
| 75 | SegDataBlock(const SegDataBlock<T>& a)
|
---|
| 76 | {
|
---|
[3213] | 77 | gThsop->lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 78 | mSRef = a.mSRef;
|
---|
| 79 | mSRef->nref++;
|
---|
[3213] | 80 | gThsop->unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 81 | }
|
---|
| 82 | //! copy constructor with specification of flags for data sharing and element value copy
|
---|
| 83 | SegDataBlock(const SegDataBlock<T>& a, bool share, bool cpval=true)
|
---|
| 84 | {
|
---|
| 85 | if (share) {
|
---|
[3213] | 86 | gThsop->lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 87 | mSRef = a.mSRef;
|
---|
| 88 | mSRef->nref++;
|
---|
[3213] | 89 | gThsop->unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 90 | }
|
---|
| 91 | else {
|
---|
| 92 | mSRef = NULL;
|
---|
| 93 | Clone(a, cpval);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | //! copy constructor - shares the data if \b is a SegDataBlock, clones otherwise
|
---|
| 97 | SegDataBlock(const SegDBInterface<T>& a)
|
---|
| 98 | {
|
---|
[3213] | 99 | if (gThsop == NULL) gThsop = new ThSafeOp;
|
---|
[2660] | 100 | SegDataBlock<T> * sdb = dynamic_cast< SegDataBlock<T> *>(&a);
|
---|
[2884] | 101 | if (sdb != NULL) {
|
---|
[3213] | 102 | gThsop->lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2884] | 103 | mSRef = sdb->mSRef;
|
---|
[2660] | 104 | mSRef->nref++;
|
---|
[3213] | 105 | gThsop->unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 106 | }
|
---|
| 107 | else Clone(a, true);
|
---|
| 108 | }
|
---|
| 109 | //! Destructor. The memory is freed when the last object referencing the data segment is destroyed
|
---|
| 110 | virtual ~SegDataBlock()
|
---|
| 111 | {
|
---|
| 112 | //DEL cout << " DEBUG-~SegDataBlock() " << hex << mSRef << dec << " NRef()= " << NRef() << endl;
|
---|
[3213] | 113 | gThsop->lock(); // (ThreadSafe) - Start of atomic operation
|
---|
| 114 | Delete();
|
---|
| 115 | gThsop->unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 116 | }
|
---|
| 117 |
|
---|
| 118 |
|
---|
[3213] | 119 | //! Adds one segment to the data structure - returns the number of allocated segments.
|
---|
[2660] | 120 | virtual size_t Extend()
|
---|
| 121 | {
|
---|
| 122 | T * p = new T[mSRef->segsize];
|
---|
[3213] | 123 | gThsop->lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 124 | mSRef->dseg.push_back(p);
|
---|
[3213] | 125 | gThsop->unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 126 | return( mSRef->dseg.size());
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | //! Changes the data segment size and reallocates the memory segments
|
---|
| 130 | // segsz : Segment size ; nbseg : Number of data segments
|
---|
| 131 | virtual void SetSize(size_t segsz, size_t nbseg=0)
|
---|
| 132 | {
|
---|
[3213] | 133 | gThsop->lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 134 | Delete();
|
---|
| 135 | mSRef = new SDREF;
|
---|
| 136 | mSRef->nref = 1;
|
---|
| 137 | mSRef->segsize = segsz;
|
---|
| 138 | mSRef->dsid = AnyDataObj::getUniqueId();
|
---|
[3213] | 139 | for(size_t k=0; k<nbseg; k++) Extend_P();
|
---|
| 140 | gThsop->unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 141 | }
|
---|
| 142 | //! Shares the data between two SegDataBlock objects
|
---|
| 143 | void Share(const SegDataBlock<T>& a)
|
---|
| 144 | {
|
---|
[3213] | 145 | gThsop->lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 146 | Delete();
|
---|
| 147 | mSRef = a.mSRef;
|
---|
| 148 | mSRef->nref++;
|
---|
[3213] | 149 | gThsop->unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 150 | }
|
---|
| 151 |
|
---|
| 152 | //! Makes a clone of the data structure and optionaly copie the data
|
---|
| 153 | void Clone(const SegDBInterface<T> & a, bool cpval=true)
|
---|
| 154 | {
|
---|
[3213] | 155 | gThsop->lock(); // (ThreadSafe) - Start of atomic operation
|
---|
[2660] | 156 | Delete();
|
---|
| 157 | mSRef = new SDREF;
|
---|
| 158 | mSRef->nref = 1;
|
---|
| 159 | mSRef->segsize = a.SegmentSize();
|
---|
[3213] | 160 | gThsop->unlock(); // (ThreadSafe) - End of atomic operation
|
---|
[2660] | 161 | for(size_t k=0; k<a.NbSegments(); k++) {
|
---|
| 162 | Extend();
|
---|
| 163 | if (cpval) {
|
---|
| 164 | T * dst = GetSegment(k);
|
---|
[2698] | 165 | const T * src = a.GetCstSegment(k);
|
---|
[2660] | 166 | memcpy(dst, src, mSRef->segsize*sizeof(T));
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 | //! Return the segment size of the data structure
|
---|
| 173 | virtual size_t SegmentSize() const { return mSRef->segsize; }
|
---|
| 174 | //! Return the number of data segments
|
---|
| 175 | virtual size_t NbSegments() const { return mSRef->dseg.size(); }
|
---|
| 176 | //! Return the current size of the segmented data structure
|
---|
| 177 | inline size_t Size() const { return mSRef->segsize*mSRef->dseg.size(); }
|
---|
| 178 |
|
---|
[2698] | 179 | //! Return the pointer to data segment \b k
|
---|
[2660] | 180 | virtual T* GetSegment(size_t k) { return mSRef->dseg[k]; }
|
---|
[2698] | 181 | //! Return the const (read-only) pointer to data segment \b k
|
---|
| 182 | virtual T const * GetCstSegment(size_t k) const { return mSRef->dseg[k]; }
|
---|
[2660] | 183 |
|
---|
| 184 | //! Return the segment index for element \b i
|
---|
| 185 | inline size_t SegIndex(size_t i) { return i/mSRef->segsize; }
|
---|
| 186 | //! Return the offset (in data segment) for element \b i
|
---|
| 187 | inline size_t EltOffset(size_t i) { return i%mSRef->segsize; }
|
---|
| 188 |
|
---|
| 189 | //! Return the \b i th element of the segmented data structure
|
---|
| 190 | inline T& operator()(size_t i) { return *(mSRef->dseg[SegIndex(i)]+EltOffset(i));}
|
---|
| 191 | //! Return the \b i th element of the data structure
|
---|
| 192 | inline T& operator()(size_t i) const { return *(mSRef->dseg[SegIndex(i)]+EltOffset(i));}
|
---|
| 193 | //! Return the \b i th element of the segmented data structure
|
---|
| 194 | inline T& operator[](size_t i) { return *(mSRef->dseg[SegIndex(i)]+EltOffset(i));}
|
---|
| 195 | //! Return the \b i th element of the data structure
|
---|
| 196 | inline T& operator[](size_t i) const { return *(mSRef->dseg[SegIndex(i)]+EltOffset(i));}
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | //! Return the number of references to the data structure
|
---|
| 200 | inline size_t NRef() const { return mSRef->nref; }
|
---|
| 201 |
|
---|
| 202 | //! Equal operator. Set all element values to \b v
|
---|
| 203 | SegDataBlock<T>& operator = (T const & v)
|
---|
| 204 | {
|
---|
| 205 | for(size_t k=0; k<NbSegments(); k++) {
|
---|
| 206 | T * p = mSRef->dseg[k];
|
---|
| 207 | for(size_t j=0; j<SegmentSize(); j++) p[j] = v;
|
---|
| 208 | }
|
---|
| 209 | return (*this);
|
---|
| 210 | }
|
---|
| 211 | //! Equal operator. Clones and copie values from \b a
|
---|
| 212 | inline SegDataBlock<T>& operator = (const SegDBInterface<T> & a)
|
---|
| 213 | {
|
---|
| 214 | Clone(a, true);
|
---|
| 215 | return (*this);
|
---|
| 216 | }
|
---|
[2692] | 217 | //! Equal operator. Clones and copie values from \b a
|
---|
| 218 | inline SegDataBlock<T>& operator = (const SegDataBlock<T> & a)
|
---|
| 219 | {
|
---|
| 220 | Clone(a, true);
|
---|
| 221 | return (*this);
|
---|
| 222 | }
|
---|
[2660] | 223 |
|
---|
| 224 | //! ASCII formatted output (print)
|
---|
| 225 | void Print(ostream& os, int lev=0, const char * sep=NULL) const
|
---|
| 226 | {
|
---|
| 227 | os << "SegDataBlock< " << typeid(T).name() << "> mSRef= " << hex << mSRef
|
---|
| 228 | << " NRef=" << dec << NRef() << " DSId= " << DRefId() << endl;
|
---|
| 229 | os << " ... SegSize= " << SegmentSize() << " NbSeg= " << NbSegments()
|
---|
| 230 | << " Size= " << Size() << endl;
|
---|
| 231 | if (sep == NULL) sep = " ";
|
---|
| 232 | if (lev > 0) {
|
---|
| 233 | for(size_t k=0; k<NbSegments(); k++) {
|
---|
| 234 | T * p = mSRef->dseg[k];
|
---|
| 235 | os << " ..... DataSeg[ " << k << " ] : " << hex << p << dec << endl;
|
---|
| 236 | if (lev > 1)
|
---|
| 237 | for(size_t j=0; j<SegmentSize(); j++) os << p[j] << sep;
|
---|
| 238 | os << endl;
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | //! ASCII formatted output (print) on cout
|
---|
| 243 | inline void Print(int lev=0, const char * sep=NULL) const
|
---|
| 244 | {
|
---|
| 245 | Print(cout, lev, sep);
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | //! Returns the unique object identifier
|
---|
| 249 | inline uint_8 DRefId() const { return mSRef->dsid; }
|
---|
| 250 |
|
---|
| 251 | protected:
|
---|
[3213] | 252 | //! NON-thread safe: Decrement the number of reference counts, and free the memory if NRef=0
|
---|
[2660] | 253 | void Delete()
|
---|
| 254 | {
|
---|
| 255 | if (mSRef == NULL) return;
|
---|
| 256 | mSRef->nref--;
|
---|
| 257 | if (mSRef->nref > 0) { mSRef = NULL; return; }
|
---|
| 258 | //DEL cout << " DEBUG-SegDataBlock::Delete() NbSegments() = " << NbSegments() << endl;
|
---|
| 259 | for(size_t k=0; k<NbSegments(); k++) {
|
---|
| 260 | delete[] mSRef->dseg[k];
|
---|
| 261 | mSRef->dseg[k] = NULL;
|
---|
| 262 | }
|
---|
| 263 | delete mSRef;
|
---|
| 264 | mSRef = NULL;
|
---|
| 265 | }
|
---|
[3213] | 266 | //! NON-thread safe, version of Extend() : Adds one segment to the data structure
|
---|
| 267 | size_t Extend_P()
|
---|
| 268 | {
|
---|
| 269 | T * p = new T[mSRef->segsize];
|
---|
| 270 | mSRef->dseg.push_back(p);
|
---|
| 271 | return( mSRef->dseg.size());
|
---|
| 272 | }
|
---|
| 273 |
|
---|
[2805] | 274 | /*! \cond
|
---|
| 275 | SDREF structure for reference management - for internal use by SegDataBlock
|
---|
| 276 | */
|
---|
[2660] | 277 | typedef struct {
|
---|
[2805] | 278 | size_t nref; // Number of references to the data structure
|
---|
| 279 | uint_8 dsid; // Data structure Id - Used by FIO_SegDataBlock
|
---|
[2660] | 280 | std::vector<T *> dseg;
|
---|
| 281 | size_t segsize;
|
---|
| 282 | } SDREF;
|
---|
[2805] | 283 | /*! \endcond */
|
---|
[2660] | 284 | SDREF * mSRef; //!< SDREF structure for reference sharing
|
---|
[3213] | 285 |
|
---|
| 286 | static ThSafeOp* gThsop; //!< Mutex for thread safe operation
|
---|
[2660] | 287 | };
|
---|
| 288 |
|
---|
[3213] | 289 | template <class T> ThSafeOp* SegDataBlock<T>::gThsop = NULL; // static member initialized to NULL
|
---|
| 290 |
|
---|
[2660] | 291 | //! Definition of operator \<\< for ascii formatted output of SegDataBlock
|
---|
| 292 | template<class T>
|
---|
| 293 | inline ostream& operator << (ostream& os, const SegDataBlock<T>& a)
|
---|
| 294 | { a.Print(os); return(os); }
|
---|
| 295 |
|
---|
| 296 | } // Fin du namespace
|
---|
| 297 |
|
---|
| 298 | #endif
|
---|