LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
lsst::daf::base::Citizen Class Reference

Citizen is a class that should be among all LSST classes base classes, and handles basic memory management. More...

#include <Citizen.h>

Inheritance diagram for lsst::daf::base::Citizen:
lsst::afw::image::Exposure< float > lsst::afw::math::Function< double > lsst::afw::math::Function< Kernel::Pixel > lsst::afw::image::ImageBase< lsst::afw::image::MaskPixel > lsst::afw::image::ImageBase< MaskPixelT > lsst::afw::image::ImageBase< MaskT > lsst::afw::detection::Footprint lsst::afw::detection::FootprintSet lsst::afw::detection::Psf lsst::afw::geom::Functor lsst::afw::geom::XYTransform lsst::afw::image::DecoratedImage< PixelT > lsst::afw::image::Exposure< ImageT, MaskT, VarianceT > lsst::afw::image::ImageBase< PixelT > lsst::afw::image::MaskedImage< ImagePixelT, MaskPixelT, VariancePixelT > lsst::afw::image::Wcs lsst::afw::math::Background lsst::afw::math::Covariogram< T > lsst::afw::math::detail::KernelImagesForRegion lsst::afw::math::Function< ReturnT > lsst::afw::math::Kernel lsst::afw::table::BaseRecord lsst::afw::table::BaseTable lsst::afw::table::detail::SchemaImpl lsst::daf::base::PropertySet lsst::daf::persistence::BoundVar lsst::daf::persistence::DbStorageImpl lsst::daf::persistence::DbStorageLocation lsst::daf::persistence::Formatter lsst::daf::persistence::FormatterRegistry lsst::daf::persistence::LogicalLocation lsst::daf::persistence::Persistence lsst::daf::persistence::Storage lsst::pex::policy::Policy lsst::pex::policy::PolicyDestination lsst::pex::policy::PolicyParser lsst::pex::policy::PolicySource lsst::afw::image::MaskedImage< EntryT > lsst::afw::image::MaskedImage< float, lsst::afw::image::MaskPixel, lsst::afw::image::VariancePixel > lsst::afw::image::MaskedImage< ImageT, MaskT, VarianceT > lsst::afw::image::MaskedImage< InternalPixelT >

Public Types

enum  { magicSentinel = 0xdeadbeef }
 
typedef unsigned long memId
 Type of the block's ID. More...
 
typedef memId(* memNewCallback )(const memId cid)
 A function used to register a callback. More...
 
typedef memId(* memCallback )(const Citizen *ptr)
 

Public Member Functions

 Citizen (const std::type_info &)
 
 Citizen (Citizen const &)
 
 ~Citizen ()
 
Citizenoperator= (Citizen const &)
 
std::string repr () const
 Return a string representation of a Citizen. More...
 
void markPersistent (void)
 Mark a Citizen as persistent and not destroyed until process end. More...
 
memId getId () const
 Return the Citizen's ID. More...
 

Static Public Member Functions

static bool hasBeenCorrupted ()
 Check all allocated blocks for corruption. More...
 
static memId getNextMemId ()
 Return the memId of the next object to be allocated. More...
 
static int init ()
 Called once when the memory system is being initialised. More...
 
Census

Provide a list of current Citizens

static int census (int, memId startingMemId=0)
 How many active Citizens are there? More...
 
static void census (std::ostream &stream, memId startingMemId=0)
 Print a list of all active Citizens to stream, sorted by ID. More...
 
static const std::vector
< const Citizen * > * 
census ()
 Return a (newly allocated) std::vector of active Citizens sorted by ID. More...
 
callbackIDs

Set callback Ids. The old Id is returned

static memId setNewCallbackId (memId id)
 Call the NewCallback when block is allocated. More...
 
static memId setDeleteCallbackId (memId id)
 Call the current DeleteCallback when block is deleted. More...
 

Private Types

typedef std::pair< memId,
pthread_t > 
CitizenInfo
 
typedef std::map< Citizen
const *, CitizenInfo
table
 

Private Member Functions

bool _hasBeenCorrupted () const
 

Static Private Member Functions

static memId _addCitizen (Citizen const *c)
 
static memId _nextMemIdAndIncrement (void)
 Return the memId and prepare for the next object to be allocated. More...
 
static memId _nextMemId (void)
 Return the memId of the next object to be allocated. More...
 

Private Attributes

int _sentinel
 
memId _CitizenId
 
const char * _typeName
 

Static Private Attributes

static table _activeCitizens
 
static table _persistentCitizens
 
static memId _newId = 0
 
static memId _deleteId = 0
 
static memNewCallback _newCallback = defaultNewCallback
 
static memCallback _deleteCallback = defaultDeleteCallback
 
static memCallback _corruptionCallback = defaultCorruptionCallback
 

Friends

class PersistentCitizenScope
 

callbacks

Set the New/Delete callback functions; in each case the previously installed callback is returned. These callback functions return a value which is Added to the previously registered id.

The default callback functions are called default{New,Delete}Callback; you may want to set a break point in these callbacks from your favourite debugger

static memNewCallback setNewCallback (memNewCallback func)
 Set the NewCallback function. More...
 
static memCallback setDeleteCallback (memCallback func)
 Set the DeleteCallback function. More...
 
static memCallback setCorruptionCallback (memCallback func)
 Set the CorruptionCallback function. More...
 
static bool & _shouldPersistCitizens ()
 Set the NewCallback function. More...
 

Detailed Description

Citizen is a class that should be among all LSST classes base classes, and handles basic memory management.

Instances of subclasses of Citizen will automatically be given a unique id.

You can ask for infomation about the currently allocated Citizens using the census functions, request that a function of your choice be called when a specific block ID is allocated or deleted, and check whether any of the data blocks are known to be corrupted

Definition at line 56 of file Citizen.h.

Member Typedef Documentation

typedef std::pair<memId, pthread_t> lsst::daf::base::Citizen::CitizenInfo
private

Definition at line 92 of file Citizen.h.

typedef memId(* lsst::daf::base::Citizen::memCallback)(const Citizen *ptr)

Definition at line 62 of file Citizen.h.

typedef unsigned long lsst::daf::base::Citizen::memId

Type of the block's ID.

Definition at line 59 of file Citizen.h.

typedef memId(* lsst::daf::base::Citizen::memNewCallback)(const memId cid)

A function used to register a callback.

Definition at line 61 of file Citizen.h.

typedef std::map<Citizen const*, CitizenInfo> lsst::daf::base::Citizen::table
private

Definition at line 93 of file Citizen.h.

Member Enumeration Documentation

anonymous enum
Enumerator
magicSentinel 

Definition at line 89 of file Citizen.h.

Constructor & Destructor Documentation

lsst::daf::base::Citizen::Citizen ( const std::type_info &  type)

Definition at line 173 of file Citizen.cc.

173  :
175  _CitizenId(_addCitizen(this)),
176  _typeName(type.name()) {
177 }
const char * _typeName
Definition: Citizen.h:97
static memId _addCitizen(Citizen const *c)
Definition: Citizen.cc:159
lsst::daf::base::Citizen::Citizen ( Citizen const &  citizen)

Definition at line 179 of file Citizen.cc.

179  :
181  _CitizenId(_addCitizen(this)),
182  _typeName(citizen._typeName) {
183 }
const char * _typeName
Definition: Citizen.h:97
static memId _addCitizen(Citizen const *c)
Definition: Citizen.cc:159
lsst::daf::base::Citizen::~Citizen ( )

Definition at line 185 of file Citizen.cc.

185  {
186  {
187  WriteGuard guard(citizenLock);
188  if (_CitizenId == _deleteId) {
189  _deleteId += _deleteCallback(this);
190  }
191  }
192 
193  (void)_hasBeenCorrupted(); // may execute callback
194  _sentinel = 0x0000dead; // In case we have a dangling pointer
195 
196  bool corrupt = false;
197  {
198  WriteGuard guard(citizenLock);
199  size_t nActive = _activeCitizens.erase(this);
200  corrupt = nActive > 1 ||
201  (nActive == 0 && _persistentCitizens.erase(this) != 1);
202  }
203  if (corrupt) {
204  (void)_corruptionCallback(this);
205  }
206 }
static table _activeCitizens
Definition: Citizen.h:105
static memCallback _deleteCallback
Definition: Citizen.h:115
static table _persistentCitizens
Definition: Citizen.h:106
static memId _deleteId
Definition: Citizen.h:112
static memCallback _corruptionCallback
Definition: Citizen.h:116
bool _hasBeenCorrupted() const
Definition: Citizen.cc:342

Member Function Documentation

dafBase::Citizen::memId lsst::daf::base::Citizen::_addCitizen ( Citizen const *  c)
staticprivate

Definition at line 159 of file Citizen.cc.

159  {
161  WriteGuard guard(citizenLock);
162  if (_shouldPersistCitizens()) {
163  _persistentCitizens[c] = std::make_pair(cid, pthread_self());
164  } else {
165  _activeCitizens[c] = std::make_pair(cid, pthread_self());
166  }
167  if (cid == _newId) {
168  _newId += _newCallback(cid);
169  }
170  return cid;
171 }
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:59
static memId _newId
Definition: Citizen.h:111
static table _activeCitizens
Definition: Citizen.h:105
static bool & _shouldPersistCitizens()
Set the NewCallback function.
Definition: Citizen.cc:473
static table _persistentCitizens
Definition: Citizen.h:106
static memId _nextMemIdAndIncrement(void)
Return the memId and prepare for the next object to be allocated.
Definition: Citizen.cc:238
static memNewCallback _newCallback
Definition: Citizen.h:114
bool lsst::daf::base::Citizen::_hasBeenCorrupted ( ) const
private

Check for corruption Return true if the block is corrupted, but only after calling the corruptionCallback

Definition at line 342 of file Citizen.cc.

342  {
343  if (_sentinel == static_cast<int>(magicSentinel)) {
344  return false;
345  }
346 
347  (void)_corruptionCallback(this);
348  return true;
349 }
static memCallback _corruptionCallback
Definition: Citizen.h:116
dafBase::Citizen::memId lsst::daf::base::Citizen::_nextMemId ( void  )
staticprivate

Return the memId of the next object to be allocated.

Definition at line 233 of file Citizen.cc.

233  {
234  return perThreadId.getRef();
235 }
dafBase::Citizen::memId lsst::daf::base::Citizen::_nextMemIdAndIncrement ( void  )
staticprivate

Return the memId and prepare for the next object to be allocated.

Definition at line 238 of file Citizen.cc.

238  {
239  return perThreadId.getRef()++;
240 }
bool & lsst::daf::base::Citizen::_shouldPersistCitizens ( void  )
staticprivate

Set the NewCallback function.

Definition at line 473 of file Citizen.cc.

473  {
474  return perThreadPersistFlag.getRef();
475 }
int lsst::daf::base::Citizen::census ( int  ,
memId  startingMemId = 0 
)
static

How many active Citizens are there?

Parameters
startingMemIdDon't print Citizens with lower IDs

Definition at line 266 of file Citizen.cc.

269  {
270  if (startingMemId == 0) { // easy
271  ReadGuard guard(citizenLock);
272  return _activeCitizens.size();
273  }
274 
275  int n = 0;
276  ReadGuard guard(citizenLock);
277  for (table::iterator cur = _activeCitizens.begin();
278  cur != _activeCitizens.end(); cur++) {
279  if (cur->first->_CitizenId >= startingMemId) {
280  n++;
281  }
282  }
283 
284  return n;
285 }
static table _activeCitizens
Definition: Citizen.h:105
void lsst::daf::base::Citizen::census ( std::ostream &  stream,
memId  startingMemId = 0 
)
static

Print a list of all active Citizens to stream, sorted by ID.

Parameters
streamstream to print to
startingMemIdDon't print Citizens with lower IDs

Definition at line 289 of file Citizen.cc.

292  {
293  ReadGuard guard(citizenLock);
294 
295  boost::scoped_ptr<std::vector<Citizen const*> const> leaks(Citizen::census());
296 
297  for (std::vector<Citizen const *>::const_iterator citizen = leaks->begin(), end = leaks->end();
298  citizen != end; ++citizen) {
299  if ((*citizen)->getId() >= startingMemId) {
300  stream << (*citizen)->repr() << "\n";
301  }
302  }
303 }
static const std::vector< const Citizen * > * census()
Return a (newly allocated) std::vector of active Citizens sorted by ID.
Definition: Citizen.cc:321
std::vector< dafBase::Citizen const * > const * lsst::daf::base::Citizen::census ( )
static

Return a (newly allocated) std::vector of active Citizens sorted by ID.

You are responsible for deleting it; or you can say boost::scoped_ptr<std::vector<Citizen const*> const> leaks(Citizen::census()); and not bother (that becomes std::unique_ptr in C++11)

Definition at line 321 of file Citizen.cc.

321  {
322  std::vector<Citizen const*>* vec =
323  new std::vector<Citizen const*>(0);
324  ReadGuard guard(citizenLock);
325  vec->reserve(_activeCitizens.size());
326 
327  for (table::iterator cur = _activeCitizens.begin();
328  cur != _activeCitizens.end(); cur++) {
329  vec->push_back(dynamic_cast<Citizen const*>(cur->first));
330  }
331 
332  std::sort(vec->begin(), vec->end(), cmpId);
333 
334  return vec;
335 }
static table _activeCitizens
Definition: Citizen.h:105
dafBase::Citizen::memId lsst::daf::base::Citizen::getId ( ) const

Return the Citizen's ID.

Definition at line 223 of file Citizen.cc.

223  {
224  return _CitizenId;
225 }
dafBase::Citizen::memId lsst::daf::base::Citizen::getNextMemId ( )
static

Return the memId of the next object to be allocated.

Definition at line 228 of file Citizen.cc.

228  {
229  return _nextMemId();
230 }
static memId _nextMemId(void)
Return the memId of the next object to be allocated.
Definition: Citizen.cc:233
bool lsst::daf::base::Citizen::hasBeenCorrupted ( )
static

Check all allocated blocks for corruption.

Definition at line 352 of file Citizen.cc.

352  {
353  ReadGuard guard(citizenLock);
354  for (table::iterator cur = _activeCitizens.begin();
355  cur != _activeCitizens.end(); cur++) {
356  if (cur->first->_hasBeenCorrupted()) {
357  return true;
358  }
359  }
360  for (table::iterator cur = _persistentCitizens.begin();
361  cur != _persistentCitizens.end(); cur++) {
362  if (cur->first->_hasBeenCorrupted()) {
363  return true;
364  }
365  }
366 
367  return false;
368 }
static table _activeCitizens
Definition: Citizen.h:105
static table _persistentCitizens
Definition: Citizen.h:106
int lsst::daf::base::Citizen::init ( )
static

Called once when the memory system is being initialised.

Definition at line 213 of file Citizen.cc.

213  {
214  volatile int dummy = 1;
215  return dummy;
216 }
void lsst::daf::base::Citizen::markPersistent ( void  )

Mark a Citizen as persistent and not destroyed until process end.

Definition at line 253 of file Citizen.cc.

253  {
254  WriteGuard guard(citizenLock);
255  _persistentCitizens[this] = _activeCitizens[this];
256  _activeCitizens.erase(this);
257 }
static table _activeCitizens
Definition: Citizen.h:105
static table _persistentCitizens
Definition: Citizen.h:106
Citizen& lsst::daf::base::Citizen::operator= ( Citizen const &  )
inline

Definition at line 68 of file Citizen.h.

68 { return *this; }
std::string lsst::daf::base::Citizen::repr ( ) const

Return a string representation of a Citizen.

Definition at line 244 of file Citizen.cc.

244  {
245  return boost::str(boost::format("%d: %08x %s")
246  % _CitizenId
247  % this
249  );
250 }
std::string demangleType(std::string const _typeName)
Definition: Demangle.cc:113
const char * _typeName
Definition: Citizen.h:97
dafBase::Citizen::memCallback lsst::daf::base::Citizen::setCorruptionCallback ( Citizen::memCallback  func)
static

Set the CorruptionCallback function.

Parameters
funcfunction be called when block is found to be corrupted

Definition at line 431 of file Citizen.cc.

433  {
435  _corruptionCallback = func;
436 
437  return old;
438 }
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:62
static memCallback _corruptionCallback
Definition: Citizen.h:116
dafBase::Citizen::memCallback lsst::daf::base::Citizen::setDeleteCallback ( Citizen::memCallback  func)
static

Set the DeleteCallback function.

Parameters
funcfunction be called when desired block is deleted

Definition at line 421 of file Citizen.cc.

423  {
425  _deleteCallback = func;
426 
427  return old;
428 }
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:62
static memCallback _deleteCallback
Definition: Citizen.h:115
dafBase::Citizen::memId lsst::daf::base::Citizen::setDeleteCallbackId ( Citizen::memId  id)
static

Call the current DeleteCallback when block is deleted.

Parameters
idDesired ID

Definition at line 386 of file Citizen.cc.

388  {
389  WriteGuard guard(citizenLock);
390  Citizen::memId oldId = _deleteId;
391  _deleteId = id;
392 
393  return oldId;
394 }
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:59
int id
Definition: CR.cc:151
static memId _deleteId
Definition: Citizen.h:112
dafBase::Citizen::memNewCallback lsst::daf::base::Citizen::setNewCallback ( Citizen::memNewCallback  func)
static

Set the NewCallback function.

Parameters
funcThe new function to be called when a designated block is allocated

Definition at line 411 of file Citizen.cc.

413  {
415  _newCallback = func;
416 
417  return old;
418 }
memId(* memNewCallback)(const memId cid)
A function used to register a callback.
Definition: Citizen.h:61
static memNewCallback _newCallback
Definition: Citizen.h:114
dafBase::Citizen::memId lsst::daf::base::Citizen::setNewCallbackId ( Citizen::memId  id)
static

Call the NewCallback when block is allocated.

Parameters
idDesired ID

Definition at line 375 of file Citizen.cc.

377  {
378  WriteGuard guard(citizenLock);
379  Citizen::memId oldId = _newId;
380  _newId = id;
381 
382  return oldId;
383 }
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:59
static memId _newId
Definition: Citizen.h:111
int id
Definition: CR.cc:151

Friends And Related Function Documentation

friend class PersistentCitizenScope
friend

Definition at line 120 of file Citizen.h.

Member Data Documentation

dafBase::Citizen::table lsst::daf::base::Citizen::_activeCitizens
staticprivate

Definition at line 105 of file Citizen.h.

memId lsst::daf::base::Citizen::_CitizenId
private

Definition at line 96 of file Citizen.h.

dafBase::Citizen::memCallback lsst::daf::base::Citizen::_corruptionCallback = defaultCorruptionCallback
staticprivate

Definition at line 116 of file Citizen.h.

dafBase::Citizen::memCallback lsst::daf::base::Citizen::_deleteCallback = defaultDeleteCallback
staticprivate

Definition at line 115 of file Citizen.h.

dafBase::Citizen::memId lsst::daf::base::Citizen::_deleteId = 0
staticprivate

Definition at line 112 of file Citizen.h.

dafBase::Citizen::memNewCallback lsst::daf::base::Citizen::_newCallback = defaultNewCallback
staticprivate

Definition at line 114 of file Citizen.h.

dafBase::Citizen::memId lsst::daf::base::Citizen::_newId = 0
staticprivate

Definition at line 111 of file Citizen.h.

dafBase::Citizen::table lsst::daf::base::Citizen::_persistentCitizens
staticprivate

Definition at line 106 of file Citizen.h.

int lsst::daf::base::Citizen::_sentinel
private

Definition at line 95 of file Citizen.h.

const char* lsst::daf::base::Citizen::_typeName
private

Definition at line 97 of file Citizen.h.


The documentation for this class was generated from the following files: