1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
22 using namespace icinga;
25 * Default constructor for the Object class.
30 boost::mutex::scoped_lock lock(GetMutex());
31 GetAliveObjects().insert(this);
36 * Destructor for the Object class.
41 boost::mutex::scoped_lock lock(GetMutex());
42 GetAliveObjects().erase(this);
47 * Temporarily holds onto a reference for an object. This can
48 * be used to safely clear the last reference to an object
49 * in an event handler.
51 void Object::Hold(void)
53 boost::mutex::scoped_lock lock(GetMutex());
54 GetHeldObjects().push_back(GetSelf());
58 * Clears all temporarily held objects.
60 void Object::ClearHeldObjects(void)
62 boost::mutex::scoped_lock lock(GetMutex());
63 GetHeldObjects().clear();
67 * Returns a reference-counted pointer to this object.
69 * @returns A shared_ptr object that points to this object
71 Object::SharedPtrHolder Object::GetSelf(void)
73 return Object::SharedPtrHolder(shared_from_this());
78 * Retrieves the number of currently alive objects.
80 * @returns The number of alive objects.
82 int Object::GetAliveObjectsCount(void)
84 boost::mutex::scoped_lock lock(GetMutex());
85 return GetAliveObjects().size();
89 * Dumps a memory histogram to the "dictionaries.dump" file.
91 void Object::PrintMemoryProfile(void)
93 map<String, int> types;
95 ofstream dictfp("dictionaries.dump.tmp");
98 boost::mutex::scoped_lock lock(GetMutex());
99 set<Object *>::iterator it;
100 BOOST_FOREACH(Object *obj, GetAliveObjects()) {
101 pair<map<String, int>::iterator, bool> tt;
102 tt = types.insert(make_pair(Utility::GetTypeName(typeid(*obj)), 1));
106 if (typeid(*obj) == typeid(Dictionary)) {
107 Dictionary::Ptr dict = obj->GetSelf();
108 dictfp << Value(dict).Serialize() << std::endl;
114 _unlink("dictionaries.dump");
118 if (rename("dictionaries.dump.tmp", "dictionaries.dump") < 0)
119 BOOST_THROW_EXCEPTION(PosixException("rename() failed", errno));
123 BOOST_FOREACH(tie(type, count), types) {
124 std::cerr << type << ": " << count << std::endl;
129 * Returns currently active objects.
131 * @returns currently active objects
133 set<Object *>& Object::GetAliveObjects(void)
135 static set<Object *> aliveObjects;
141 * Returns the mutex used for accessing static members.
145 boost::mutex& Object::GetMutex(void)
147 static boost::mutex mutex;
152 * Returns currently held objects. The caller must be
153 * holding the mutex returned by GetMutex().
155 * @returns currently held objects
157 vector<Object::Ptr>& Object::GetHeldObjects(void)
159 static vector<Object::Ptr> heldObjects;