LSSTApplications  1.1.2+25,10.0+13,10.0+132,10.0+133,10.0+224,10.0+41,10.0+8,10.0-1-g0f53050+14,10.0-1-g4b7b172+19,10.0-1-g61a5bae+98,10.0-1-g7408a83+3,10.0-1-gc1e0f5a+19,10.0-1-gdb4482e+14,10.0-11-g3947115+2,10.0-12-g8719d8b+2,10.0-15-ga3f480f+1,10.0-2-g4f67435,10.0-2-gcb4bc6c+26,10.0-28-gf7f57a9+1,10.0-3-g1bbe32c+14,10.0-3-g5b46d21,10.0-4-g027f45f+5,10.0-4-g86f66b5+2,10.0-4-gc4fccf3+24,10.0-40-g4349866+2,10.0-5-g766159b,10.0-5-gca2295e+25,10.0-6-g462a451+1
LSSTDataManagementBasePackage
Citizen.h
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #ifndef LSST_DAF_BASE_CITIZEN_H
26 #define LSST_DAF_BASE_CITIZEN_H
27 
28 #include <map>
29 #include <ostream>
30 #include <pthread.h>
31 #include <string>
32 #include <vector>
33 #include <typeinfo>
34 
35 #include "boost/noncopyable.hpp"
36 
37 
38 namespace lsst {
39 namespace daf {
40 namespace base {
41 
42  class PersistentCitizenScope;
43 
56  class Citizen {
57  public:
59  typedef unsigned long memId;
61  typedef memId (*memNewCallback)(const memId cid);
62  typedef memId (*memCallback)(const Citizen *ptr);
63 
64  Citizen(const std::type_info &);
65  Citizen(Citizen const &);
66  ~Citizen();
67 
68  Citizen & operator=(Citizen const &) { return *this; }
69  //
70  std::string repr() const;
71  void markPersistent(void);
72 
73  static int census(int, memId startingMemId = 0);
74  static void census(std::ostream &stream, memId startingMemId = 0);
75  static const std::vector<const Citizen *> *census();
76 
77  static bool hasBeenCorrupted();
78 
79  memId getId() const;
80 
81  static memId getNextMemId();
82 
83  static memId setNewCallbackId(memId id);
84  static memId setDeleteCallbackId(memId id);
88  //
89  enum { magicSentinel = 0xdeadbeef };
90  static int init();
91  private:
92  typedef std::pair<memId, pthread_t> CitizenInfo;
93  typedef std::map<Citizen const*, CitizenInfo> table;
94 
95  int _sentinel; // Initialised to _magicSentinel to detect overwritten memory
96  memId _CitizenId; // unique identifier for this pointer
97  const char *_typeName; // typeid()->name
98 
99  static memId _addCitizen(Citizen const* c);
100  static memId _nextMemIdAndIncrement(void);
101  //
102  // Book-keeping for _CitizenId
103  //
104  static memId _nextMemId(void);
107  static bool& _shouldPersistCitizens();
108  //
109  // Callbacks
110  //
111  static memId _newId; // call _newCallback when _newID is allocated
112  static memId _deleteId; // call _deleteCallback when _deleteID is deleted
113 
117  //
118  bool _hasBeenCorrupted() const;
119 
121  };
122 
123 #ifndef SWIG
124 
132  class PersistentCitizenScope : private boost::noncopyable {
133  public:
136  };
137 #endif
138 
139 }}} // namespace lsst::daf::base
140 
141 #endif
memId(* memNewCallback)(const memId cid)
A function used to register a callback.
Definition: Citizen.h:61
static memCallback _corruptionCallback
Definition: Citizen.h:116
static memNewCallback _newCallback
Definition: Citizen.h:114
static memId getNextMemId()
Return the memId of the next object to be allocated.
Definition: Citizen.cc:228
static memId setDeleteCallbackId(memId id)
Call the current DeleteCallback when block is deleted.
Definition: Citizen.cc:386
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:59
static memId _nextMemIdAndIncrement(void)
Return the memId and prepare for the next object to be allocated.
Definition: Citizen.cc:238
memId getId() const
Return the Citizen&#39;s ID.
Definition: Citizen.cc:223
static memCallback setCorruptionCallback(memCallback func)
Set the CorruptionCallback function.
Definition: Citizen.cc:431
std::string repr() const
Return a string representation of a Citizen.
Definition: Citizen.cc:244
static const std::vector< const Citizen * > * census()
Return a (newly allocated) std::vector of active Citizens sorted by ID.
Definition: Citizen.cc:321
Citizen & operator=(Citizen const &)
Definition: Citizen.h:68
static memCallback setDeleteCallback(memCallback func)
Set the DeleteCallback function.
Definition: Citizen.cc:421
static table _persistentCitizens
Definition: Citizen.h:106
static memId _deleteId
Definition: Citizen.h:112
Citizen(const std::type_info &)
Definition: Citizen.cc:173
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 int init()
Called once when the memory system is being initialised.
Definition: Citizen.cc:213
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:62
static memId _addCitizen(Citizen const *c)
Definition: Citizen.cc:159
std::pair< memId, pthread_t > CitizenInfo
Definition: Citizen.h:92
static memCallback _deleteCallback
Definition: Citizen.h:115
static memId _nextMemId(void)
Return the memId of the next object to be allocated.
Definition: Citizen.cc:233
static bool hasBeenCorrupted()
Check all allocated blocks for corruption.
Definition: Citizen.cc:352
static memId setNewCallbackId(memId id)
Call the NewCallback when block is allocated.
Definition: Citizen.cc:375
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
bool _hasBeenCorrupted() const
Definition: Citizen.cc:342
static memNewCallback setNewCallback(memNewCallback func)
Set the NewCallback function.
Definition: Citizen.cc:411
std::map< Citizen const *, CitizenInfo > table
Definition: Citizen.h:93
void markPersistent(void)
Mark a Citizen as persistent and not destroyed until process end.
Definition: Citizen.cc:253
const char * _typeName
Definition: Citizen.h:97