27#ifndef NIDAS_CORE_SAMPLEPOOL_H
28#define NIDAS_CORE_SAMPLEPOOL_H
40namespace nidas {
namespace core {
86 std::list<SamplePoolInterface*>
getPools()
const;
112template <
typename SampleType>
166 SampleType *
getSample(SampleType** vec,
int *veclen,
unsigned int len);
167 void putSample(
const SampleType *,SampleType*** vecp,
int *veclen,
int* nalloc);
211template<
class SampleType>
215template<
class SampleType>
219template<
class SampleType>
233template<
class SampleType>
241template<
class SampleType>
243 _smallSamples(0), _mediumSamples(0), _largeSamples(0),
244 _smallSize(0), _mediumSize(0), _largeSize(0),
246 _nsmall(0),_nmedium(0),_nlarge(0),_nsamplesOut(0),_nsamplesAlloc(0)
268template<
class SampleType>
271 for (i = 0; i < _nsmall; i++)
delete _smallSamples[i];
272 delete [] _smallSamples;
273 for (i = 0; i < _nmedium; i++)
delete _mediumSamples[i];
274 delete [] _mediumSamples;
275 for (i = 0; i < _nlarge; i++)
delete _largeSamples[i];
276 delete [] _largeSamples;
280template<
class SampleType>
290 assert(_nsamplesOut >= 0);
296 assert(_nsamplesAlloc == _nsmall + _nmedium + _nlarge + _nsamplesOut);
300 if (
len < SMALL_SAMPLE_MAXSIZE && (_nsmall > 0 ||
301 (_nmedium + _nlarge) < 4))
302 return getSample((SampleType**)_smallSamples,&_nsmall,
len);
303 else if (
len < MEDIUM_SAMPLE_MAXSIZE && (_nmedium > 0 || _nlarge < 2))
304 return getSample((SampleType**)_mediumSamples,&_nmedium,
len);
305 else return getSample((SampleType**)_largeSamples,&_nlarge,
len);
308template<
class SampleType>
310 int *n,
unsigned int len)
315 std::cerr <<
"getSample, this=" << std::hex <<
this <<
316 " pool=" << vec << std::dec <<
317 " *n=" << *n << std::endl;
325 else if (sample->getAllocLength() >
len) {
343 if (sample->sizeofDataType() == 1) {
344 static const char weird[4] = {
'\x80',
'\x80',
'\x80',
'\x80' };
345 int nb = std::min(4U,sample->getAllocLength()-
len);
346 memcpy((
char*)sample->getVoidDataPtr()+
len,weird,nb);
348 else sample->setDataValue(
len,floatNAN);
351 sample->setDataLength(
len);
359 sample->allocateData(
len);
360 sample->setDataLength(
len);
367template<
class SampleType>
372 assert(_nsamplesOut >= 0);
373 assert(_nsamplesAlloc == _nsmall + _nmedium + _nlarge + _nsamplesOut);
375 unsigned int len = sample->getAllocLength();
376 if (
len < SMALL_SAMPLE_MAXSIZE) {
378 DLOG((
"put small sample, len=%d,bytelen=%d,n=%d,size=%d",
len,sample->getAllocByteLength(),_nsmall,_smallSize));
380 putSample(sample,(SampleType***)&_smallSamples,&_nsmall,&_smallSize);
382 else if (
len < MEDIUM_SAMPLE_MAXSIZE) {
384 DLOG((
"put medium sample, len=%d,bytelen=%d,n=%d,size=%d",
len,sample->getAllocByteLength(),_nmedium,_mediumSize));
386 putSample(sample,(SampleType***)&_mediumSamples,&_nmedium,&_mediumSize);
390 DLOG((
"put large sample, len=%d,bytelen=%d,n=%d,size=%d",
len,sample->getAllocByteLength(),_nlarge,_largeSize));
392 putSample(sample,(SampleType***)&_largeSamples,&_nlarge,&_largeSize);
396template<
class SampleType>
398 SampleType ***vec,
int *n,
int *nalloc)
405 int newalloc = *nalloc + (*nalloc >> 1);
407 DLOG((
"*nalloc=%d, newalloc=%d",*nalloc,newalloc));
409 SampleType **newvec =
new SampleType*[
newalloc];
410 ::memcpy(newvec,*vec,*nalloc *
sizeof(SampleType*));
416 (*vec)[(*n)++] = (SampleType*) sample;
Header file for the nidas::util logging facility.
Definition SamplePool.h:43
virtual int getNSmallSamplesIn() const =0
virtual int getNSamplesAlloc() const =0
virtual int getNLargeSamplesIn() const =0
virtual int getNMediumSamplesIn() const =0
virtual void deletePoolInstance()=0
SamplePool singletons for various types and sizes are created and added to the SamplePools class thro...
virtual ~SamplePoolInterface()
Define a virtual but protected desctructor, so a SamplePoolInterface subclass cannot be deleted throu...
Definition SamplePool.h:67
virtual int getNSamplesOut() const =0
A pool of Samples.
Definition SamplePool.h:114
void deletePoolInstance()
Implementation from SamplePoolInterface which deletes the instance for this particular SamplePoolInte...
Definition SamplePool.h:131
static SamplePool * _instance
Definition SamplePool.h:162
int getNLargeSamplesIn() const
Definition SamplePool.h:154
static const unsigned int SMALL_SAMPLE_MAXSIZE
maximum number of elements in a small sample
Definition SamplePool.h:182
static SamplePool * getInstance()
Get a pointer to the singleton instance.
Definition SamplePool.h:220
int _nlarge
Definition SamplePool.h:202
SampleType ** _largeSamples
Definition SamplePool.h:171
SamplePool()
Definition SamplePool.h:242
static const unsigned int MEDIUM_SAMPLE_MAXSIZE
maximum number of elements in a medium sized sample
Definition SamplePool.h:187
int _smallSize
Definition SamplePool.h:173
SamplePool & operator=(const SamplePool &)
No assignment.
static nidas::util::Mutex _instanceLock
Definition SamplePool.h:164
void putSample(const SampleType *)
Return a sample to the pool.
Definition SamplePool.h:368
int _nsamplesOut
Definition SamplePool.h:204
int getNSamplesOut() const
Definition SamplePool.h:148
int _nsmall
Definition SamplePool.h:200
SamplePool(const SamplePool &)
No copying.
nidas::util::Mutex _poolLock
Definition SamplePool.h:177
int getNMediumSamplesIn() const
Definition SamplePool.h:152
int _largeSize
Definition SamplePool.h:175
static void deleteInstance()
Singleton cleanup on program exit.
Definition SamplePool.h:234
int getNSamplesAlloc() const
Definition SamplePool.h:146
~SamplePool()
Definition SamplePool.h:269
int getNSmallSamplesIn() const
Definition SamplePool.h:150
int _mediumSize
Definition SamplePool.h:174
int _nmedium
Definition SamplePool.h:201
SampleType ** _mediumSamples
Definition SamplePool.h:170
SampleType ** _smallSamples
Definition SamplePool.h:169
SampleType * getSample(unsigned int len)
Get a sample of at least len elements from the pool.
Definition SamplePool.h:281
int _nsamplesAlloc
Definition SamplePool.h:206
Definition SamplePool.h:72
SamplePools()
Definition SamplePool.h:93
void removePool(SamplePoolInterface *pool)
Definition SamplePool.cc:86
static nidas::util::Mutex _instanceLock
Definition SamplePool.h:99
std::list< SamplePoolInterface * > getPools() const
Get a copy of the current list of SamplePools.
Definition SamplePool.cc:74
nidas::util::Mutex _poolsLock
Definition SamplePool.h:101
~SamplePools()
Definition SamplePool.cc:62
std::list< SamplePoolInterface * > _pools
Definition SamplePool.h:103
static SamplePools * _instance
Definition SamplePool.h:97
static void deleteInstance()
Definition SamplePool.cc:53
static SamplePools * getInstance()
Definition SamplePool.cc:43
void addPool(SamplePoolInterface *pool)
Definition SamplePool.cc:80
virtual void allocateData(unsigned int val)=0
Allocate a number of bytes of data.
void holdReference() const
Increment the reference count for this sample.
Definition Sample.h:340
A C++ wrapper for a POSIX mutex.
Definition ThreadSupport.h:161
Synchronized is used a simple guard object for critical sections.
Definition ThreadSupport.h:575
#define DLOG(MSG)
Definition Logger.h:316
const float floatNAN
Value of a float NAN for general use.
Definition Sample.cc:31
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
Root namespace for the NCAR In-Situ Data Acquisition Software.
Definition A2DConverter.h:31
int len
Definition sing.cc:948