lsst.daf.base  13.0-5-g99bcfa4+11
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 namespace lsst {
36 namespace daf {
37 namespace base {
38 
40 
53  class Citizen {
54  public:
56  typedef unsigned long memId;
58  typedef memId (*memNewCallback)(const memId cid);
59  typedef memId (*memCallback)(const Citizen *ptr);
60 
61  Citizen(const std::type_info &);
62  Citizen(Citizen const &);
63  ~Citizen();
64 
65  Citizen & operator=(Citizen const &) { return *this; }
66  //
67  std::string repr() const;
68  void markPersistent(void);
69 
70  static int census(int, memId startingMemId = 0);
71  static void census(std::ostream &stream, memId startingMemId = 0);
72  static const std::vector<const Citizen *> *census();
73 
74  static bool hasBeenCorrupted();
75 
76  memId getId() const;
77 
78  static memId getNextMemId();
79 
80  static memId setNewCallbackId(memId id);
81  static memId setDeleteCallbackId(memId id);
85  //
86  enum { magicSentinel = 0xdeadbeef };
87  static int init();
88  private:
91 
92  int _sentinel; // Initialised to _magicSentinel to detect overwritten memory
93  memId _CitizenId; // unique identifier for this pointer
94  const char *_typeName; // typeid()->name
95 
96  static memId _addCitizen(Citizen const* c);
97  static memId _nextMemIdAndIncrement(void);
98  //
99  // Book-keeping for _CitizenId
100  //
101  static memId _nextMemId(void);
102  static table _activeCitizens;
103  static table _persistentCitizens;
104  static bool& _shouldPersistCitizens();
105  //
106  // Callbacks
107  //
108  static memId _newId; // call _newCallback when _newID is allocated
109  static memId _deleteId; // call _deleteCallback when _deleteID is deleted
110 
111  static memNewCallback _newCallback;
112  static memCallback _deleteCallback;
113  static memCallback _corruptionCallback;
114  //
115  bool _hasBeenCorrupted() const;
116 
118  };
119 
120 #ifndef SWIG
121 
130  public:
133 
134  // No copying
137 
138  // No moving
141  };
142 #endif
143 
144 }}} // namespace lsst::daf::base
145 
146 #endif
unsigned long memId
Type of the block&#39;s ID.
Definition: Citizen.h:56
A PersistentCitizenScope object causes all Citizen objects created during its lifetime to be marked a...
Definition: Citizen.h:129
friend class PersistentCitizenScope
Definition: Citizen.h:117
static int init()
Called once when the memory system is being initialised.
Definition: Citizen.cc:214
Citizen & operator=(Citizen const &)
Definition: Citizen.h:65
static memCallback setCorruptionCallback(memCallback func)
Set the CorruptionCallback function.
Definition: Citizen.cc:432
STL class.
STL class.
void markPersistent(void)
Mark a Citizen as persistent and not destroyed until process end.
Definition: Citizen.cc:254
static memId setNewCallbackId(memId id)
Call the NewCallback when block is allocated.
Definition: Citizen.cc:376
static memNewCallback setNewCallback(memNewCallback func)
Set the NewCallback function.
Definition: Citizen.cc:412
static bool hasBeenCorrupted()
Check all allocated blocks for corruption.
Definition: Citizen.cc:353
std::string repr() const
Return a string representation of a Citizen.
Definition: Citizen.cc:245
static memId setDeleteCallbackId(memId id)
Call the current DeleteCallback when block is deleted.
Definition: Citizen.cc:387
memId getId() const
Return the Citizen&#39;s ID.
Definition: Citizen.cc:224
memId(* memNewCallback)(const memId cid)
A function used to register a callback.
Definition: Citizen.h:58
STL class.
Citizen(const std::type_info &)
Definition: Citizen.cc:174
static memCallback setDeleteCallback(memCallback func)
Set the DeleteCallback function.
Definition: Citizen.cc:422
memId(* memCallback)(const Citizen *ptr)
Definition: Citizen.h:59
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:53
STL class.
static memId getNextMemId()
Return the memId of the next object to be allocated.
Definition: Citizen.cc:229
static const std::vector< const Citizen * > * census()
Return a (newly allocated) std::vector of active Citizens sorted by ID.
Definition: Citizen.cc:322