lsst.daf.base  16.0-5-g4940a70
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 
89 private:
92 
93  int _sentinel; // Initialised to _magicSentinel to detect overwritten memory
94  memId _CitizenId; // unique identifier for this pointer
95  const char *_typeName; // typeid()->name
96 
97  static memId _addCitizen(Citizen const *c);
98  static memId _nextMemIdAndIncrement(void);
99  //
100  // Book-keeping for _CitizenId
101  //
102  static memId _nextMemId(void);
103  static table _activeCitizens;
104  static table _persistentCitizens;
105  static bool &_shouldPersistCitizens();
106  //
107  // Callbacks
108  //
109  static memId _newId; // call _newCallback when _newID is allocated
110  static memId _deleteId; // call _deleteCallback when _deleteID is deleted
111 
112  static memNewCallback _newCallback;
113  static memCallback _deleteCallback;
114  static memCallback _corruptionCallback;
115  //
116  bool _hasBeenCorrupted() const;
117 
119 };
120 
130 public:
133 
134  // No copying
137 
138  // No moving
141 };
142 
143 } // namespace base
144 } // namespace daf
145 } // namespace lsst
146 
147 #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:118
static int init()
Called once when the memory system is being initialised.
Definition: Citizen.cc:196
Citizen & operator=(Citizen const &)
Definition: Citizen.h:65
static memCallback setCorruptionCallback(memCallback func)
Set the CorruptionCallback function.
Definition: Citizen.cc:391
STL class.
STL class.
void markPersistent(void)
Mark a Citizen as persistent and not destroyed until process end.
Definition: Citizen.cc:225
static memId setNewCallbackId(memId id)
Call the NewCallback when block is allocated.
Definition: Citizen.cc:337
static memNewCallback setNewCallback(memNewCallback func)
Set the NewCallback function.
Definition: Citizen.cc:371
static bool hasBeenCorrupted()
Check all allocated blocks for corruption.
Definition: Citizen.cc:316
std::string repr() const
Return a string representation of a Citizen.
Definition: Citizen.cc:219
static memId setDeleteCallbackId(memId id)
Call the current DeleteCallback when block is deleted.
Definition: Citizen.cc:347
memId getId() const
Return the Citizen&#39;s ID.
Definition: Citizen.cc:206
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:163
static memCallback setDeleteCallback(memCallback func)
Set the DeleteCallback function.
Definition: Citizen.cc:381
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:209
static const std::vector< const Citizen * > * census()
Return a (newly allocated) std::vector of active Citizens sorted by ID.
Definition: Citizen.cc:287