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
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
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:59
static memId _newId
Definition: Citizen.h:111
static int init()
Called once when the memory system is being initialised.
Definition: Citizen.cc:213
Citizen & operator=(Citizen const &)
Definition: Citizen.h:68
memId getId() const
Return the Citizen&#39;s ID.
Definition: Citizen.cc:223
memId(* memNewCallback)(const memId cid)
A function used to register a callback.
Definition: Citizen.h:61
static memCallback setCorruptionCallback(memCallback func)
Set the CorruptionCallback function.
Definition: Citizen.cc:431
std::map< Citizen const *, CitizenInfo > table
Definition: Citizen.h:93
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:62
static table _activeCitizens
Definition: Citizen.h:105
void markPersistent(void)
Mark a Citizen as persistent and not destroyed until process end.
Definition: Citizen.cc:253
std::string repr() const
Return a string representation of a Citizen.
Definition: Citizen.cc:244
static memId setNewCallbackId(memId id)
Call the NewCallback when block is allocated.
Definition: Citizen.cc:375
std::pair< memId, pthread_t > CitizenInfo
Definition: Citizen.h:92
static memNewCallback setNewCallback(memNewCallback func)
Set the NewCallback function.
Definition: Citizen.cc:411
static bool & _shouldPersistCitizens()
Set the NewCallback function.
Definition: Citizen.cc:473
static bool hasBeenCorrupted()
Check all allocated blocks for corruption.
Definition: Citizen.cc:352
static memCallback _deleteCallback
Definition: Citizen.h:115
const char * _typeName
Definition: Citizen.h:97
static table _persistentCitizens
Definition: Citizen.h:106
static memId setDeleteCallbackId(memId id)
Call the current DeleteCallback when block is deleted.
Definition: Citizen.cc:386
Citizen(const std::type_info &)
Definition: Citizen.cc:173
static memCallback setDeleteCallback(memCallback func)
Set the DeleteCallback function.
Definition: Citizen.cc:421
static memId _nextMemIdAndIncrement(void)
Return the memId and prepare for the next object to be allocated.
Definition: Citizen.cc:238
static memId _deleteId
Definition: Citizen.h:112
static memId _nextMemId(void)
Return the memId of the next object to be allocated.
Definition: Citizen.cc:233
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
static memCallback _corruptionCallback
Definition: Citizen.h:116
static const std::vector< const Citizen * > * census()
Return a (newly allocated) std::vector of active Citizens sorted by ID.
Definition: Citizen.cc:321
static memId _addCitizen(Citizen const *c)
Definition: Citizen.cc:159
static memNewCallback _newCallback
Definition: Citizen.h:114
static memId getNextMemId()
Return the memId of the next object to be allocated.
Definition: Citizen.cc:228
bool _hasBeenCorrupted() const
Definition: Citizen.cc:342