27 #ifndef NIDAS_CORE_SAMPLEPOOL_H
28 #define NIDAS_CORE_SAMPLEPOOL_H
40 namespace nidas {
namespace core {
71 std::list<SamplePoolInterface*>
getPools()
const;
88 std::list<SamplePoolInterface*>
_pools;
97 template <
typename SampleType>
142 SampleType *
getSample(SampleType** vec,
int *veclen,
unsigned int len)
144 void putSample(
const SampleType *,SampleType*** vecp,
int *veclen,
int* nalloc);
188 template<
class SampleType>
192 template<
class SampleType>
196 template<
class SampleType>
210 template<
class SampleType>
218 template<
class SampleType>
220 _smallSamples(0), _mediumSamples(0), _largeSamples(0),
221 _smallSize(0), _mediumSize(0), _largeSize(0),
223 _nsmall(0),_nmedium(0),_nlarge(0),_nsamplesOut(0),_nsamplesAlloc(0)
245 template<
class SampleType>
248 for (i = 0; i < _nsmall; i++)
delete _smallSamples[i];
249 delete [] _smallSamples;
250 for (i = 0; i < _nmedium; i++)
delete _mediumSamples[i];
251 delete [] _mediumSamples;
252 for (i = 0; i < _nlarge; i++)
delete _largeSamples[i];
253 delete [] _largeSamples;
257 template<
class SampleType>
268 assert(_nsamplesOut >= 0);
274 assert(_nsamplesAlloc == _nsmall + _nmedium + _nlarge + _nsamplesOut);
278 if (len < SMALL_SAMPLE_MAXSIZE && (_nsmall > 0 ||
279 (_nmedium + _nlarge) < 4))
280 return getSample((SampleType**)_smallSamples,&_nsmall,len);
281 else if (len < MEDIUM_SAMPLE_MAXSIZE && (_nmedium > 0 || _nlarge < 2))
282 return getSample((SampleType**)_mediumSamples,&_nmedium,len);
283 else return getSample((SampleType**)_largeSamples,&_nlarge,len);
286 template<
class SampleType>
293 std::cerr <<
"getSample, this=" << std::hex <<
this <<
294 " pool=" << vec << std::dec <<
295 " *n=" << *n << std::endl;
301 if (sample->getAllocLength() <
len) sample->allocateData(len);
303 else if (sample->getAllocLength() >
len) {
321 if (sample->sizeofDataType() == 1) {
322 static const char weird[4] = {
'\x80',
'\x80',
'\x80',
'\x80' };
323 int nb = std::min(4U,sample->getAllocLength()-
len);
324 memcpy((
char*)sample->getVoidDataPtr()+
len,weird,nb);
326 else sample->setDataValue(len,floatNAN);
329 sample->setDataLength(len);
331 sample->holdReference();
336 sample =
new SampleType();
337 sample->allocateData(len);
338 sample->setDataLength(len);
345 template<
class SampleType>
350 assert(_nsamplesOut >= 0);
351 assert(_nsamplesAlloc == _nsmall + _nmedium + _nlarge + _nsamplesOut);
353 unsigned int len = sample->getAllocLength();
354 if (len < SMALL_SAMPLE_MAXSIZE) {
356 DLOG((
"put small sample, len=%d,bytelen=%d,n=%d,size=%d",len,sample->getAllocByteLength(),_nsmall,_smallSize));
358 putSample(sample,(SampleType***)&_smallSamples,&_nsmall,&_smallSize);
360 else if (len < MEDIUM_SAMPLE_MAXSIZE) {
362 DLOG((
"put medium sample, len=%d,bytelen=%d,n=%d,size=%d",len,sample->getAllocByteLength(),_nmedium,_mediumSize));
364 putSample(sample,(SampleType***)&_mediumSamples,&_nmedium,&_mediumSize);
368 DLOG((
"put large sample, len=%d,bytelen=%d,n=%d,size=%d",len,sample->getAllocByteLength(),_nlarge,_largeSize));
370 putSample(sample,(SampleType***)&_largeSamples,&_nlarge,&_largeSize);
374 template<
class SampleType>
376 SampleType ***vec,
int *n,
int *nalloc)
383 int newalloc = *nalloc + (*nalloc >> 1);
385 DLOG((
"*nalloc=%d, newalloc=%d",*nalloc,newalloc));
387 SampleType **newvec =
new SampleType*[newalloc];
388 ::memcpy(newvec,*vec,*nalloc *
sizeof(SampleType*));
394 (*vec)[(*n)++] = (SampleType*) sample;
SampleType * getSample(unsigned int len)
Get a sample of at least len elements from the pool.
Definition: SamplePool.h:258
virtual int getNSamplesAlloc() const =0
Synchronized is used a simple guard object for critical sections.
Definition: ThreadSupport.h:544
virtual int getNMediumSamplesIn() const =0
nidas::util::Mutex _poolLock
Definition: SamplePool.h:154
void putSample(const SampleType *)
Return a sample to the pool.
Definition: SamplePool.h:346
int getNSmallSamplesIn() const
Definition: SamplePool.h:126
SampleType ** _smallSamples
Definition: SamplePool.h:146
~SamplePool()
Definition: SamplePool.h:246
SamplePools()
Definition: SamplePool.h:78
int _nsamplesAlloc
Definition: SamplePool.h:183
static SamplePool * getInstance()
Get a pointer to the singleton instance.
Definition: SamplePool.h:197
void removePool(SamplePoolInterface *pool)
Definition: SamplePool.cc:86
Definition: SamplePool.h:42
virtual int getNSamplesOut() const =0
static void deleteInstance()
Definition: SamplePool.cc:53
const float floatNAN
Value of a float NAN for general use.
Definition: Sample.cc:31
static SamplePool * _instance
Definition: SamplePool.h:138
virtual int getNLargeSamplesIn() const =0
static const unsigned int SMALL_SAMPLE_MAXSIZE
maximum number of elements in a small sample
Definition: SamplePool.h:159
int _nsamplesOut
Definition: SamplePool.h:181
~SamplePools()
Definition: SamplePool.cc:62
int _mediumSize
Definition: SamplePool.h:151
int getNLargeSamplesIn() const
Definition: SamplePool.h:130
SampleType ** _largeSamples
Definition: SamplePool.h:148
int _largeSize
Definition: SamplePool.h:152
Definition: SampleLengthException.h:35
int getNMediumSamplesIn() const
Definition: SamplePool.h:128
Sample * getSample(sampleType type, unsigned int len)
A convienence method for getting a sample of an enumerated type from a pool.
Definition: Sample.cc:70
int getNSamplesAlloc() const
Definition: SamplePool.h:122
SampleType ** _mediumSamples
Definition: SamplePool.h:147
static SamplePools * getInstance()
Definition: SamplePool.cc:43
int len
Definition: sing.cc:934
int _nmedium
Definition: SamplePool.h:178
static void deleteInstance()
Singleton cleanup on program exit.
Definition: SamplePool.h:211
Header file for the nidas::util logging facility.
static const unsigned int MEDIUM_SAMPLE_MAXSIZE
maximum number of elements in a medium sized sample
Definition: SamplePool.h:164
std::list< SamplePoolInterface * > getPools() const
Get a copy of the current list of SamplePools.
Definition: SamplePool.cc:74
int _nlarge
Definition: SamplePool.h:179
static SamplePools * _instance
Definition: SamplePool.h:82
Definition: SamplePool.h:56
std::list< SamplePoolInterface * > _pools
Definition: SamplePool.h:88
static nidas::util::Mutex _instanceLock
Definition: SamplePool.h:140
void addPool(SamplePoolInterface *pool)
Definition: SamplePool.cc:80
A pool of Samples.
Definition: SamplePool.h:98
static nidas::util::Mutex _instanceLock
Definition: SamplePool.h:84
int getNSamplesOut() const
Definition: SamplePool.h:124
nidas::util::Mutex _poolsLock
Definition: SamplePool.h:86
int _nsmall
Definition: SamplePool.h:177
virtual ~SamplePoolInterface()
Definition: SamplePool.h:45
virtual int getNSmallSamplesIn() const =0
#define DLOG(MSG)
Definition: Logger.h:306
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
int _smallSize
Definition: SamplePool.h:150
SamplePool()
Definition: SamplePool.h:219
SamplePool & operator=(const SamplePool &)
No assignment.