Logo ROOT  
Reference Guide
TMemoryRegulator.h
Go to the documentation of this file.
1 
2 // Author: Enric Tejedor CERN 08/2019
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef PYROOT_TMEMORYREGULATOR_H
13 #define PYROOT_TMEMORYREGULATOR_H
14 
15 //////////////////////////////////////////////////////////////////////////
16 // //
17 // TMemoryRegulator //
18 // //
19 // Sets hooks in Cppyy's MemoryRegulator to keep track of the TObjects //
20 // that are constructed and destructed. For those objects, a map is //
21 // filled, where the key is the address of the object and the value is //
22 // the class to which the object belongs. //
23 // //
24 // The TMemoryRegulator object, created in PyROOTWrapper.cxx, is added //
25 // to the list of cleanups and its RecursiveRemove method is called by //
26 // ROOT to manage the memory of TObjects being deleted. //
27 // In RecursiveRemove, the object being deleted is already a TNamed, so //
28 // the information about its actual class is not available anymore. //
29 // To solve the problem, the map above is used to know the class of the //
30 // object, so that Cppyy's RecursiveRemove can be called passing the //
31 // class as argument. //
32 //////////////////////////////////////////////////////////////////////////
33 
34 // Bindings
35 // CPyCppyy.h must be go first, since it includes Python.h, which must be
36 // included before any standard header
37 #include "CPyCppyy.h"
38 #include "MemoryRegulator.h"
39 
40 // ROOT
41 #include "TObject.h"
42 #include "TClass.h"
43 
44 // Stl
45 #include <unordered_map>
46 
47 namespace PyROOT {
48 
49 typedef std::unordered_map<Cppyy::TCppObject_t, Cppyy::TCppType_t> ObjectMap_t;
50 
51 class TMemoryRegulator : public TObject {
52 private:
53  static ObjectMap_t fObjectMap; // key: object address; value: object class id
54 
55  static std::pair<bool, bool> RegisterHook(Cppyy::TCppObject_t, Cppyy::TCppType_t);
56 
57  static std::pair<bool, bool> UnregisterHook(Cppyy::TCppObject_t, Cppyy::TCppType_t);
58 
59 public:
61 
62  virtual void RecursiveRemove(TObject *);
63 
64  void ClearProxiedObjects();
65 };
66 
67 } // namespace PyROOT
68 
69 #endif // !PYROOT_TMEMORYREGULATOR_H
PyROOT::ObjectMap_t
std::unordered_map< Cppyy::TCppObject_t, Cppyy::TCppType_t > ObjectMap_t
Definition: TMemoryRegulator.h:49
PyROOT::TMemoryRegulator::UnregisterHook
static std::pair< bool, bool > UnregisterHook(Cppyy::TCppObject_t, Cppyy::TCppType_t)
Register a hook that Cppyy runs when deleting an object.
Definition: TMemoryRegulator.cxx:57
PyROOT::TMemoryRegulator::RecursiveRemove
virtual void RecursiveRemove(TObject *)
Get the class id of the TObject being deleted and run Cppyy's RecursiveRemove.
Definition: TMemoryRegulator.cxx:75
PyROOT::TMemoryRegulator::ClearProxiedObjects
void ClearProxiedObjects()
Clean up all tracked objects.
Definition: TMemoryRegulator.cxx:90
TClass.h
MemoryRegulator.h
PyROOT::TMemoryRegulator::RegisterHook
static std::pair< bool, bool > RegisterHook(Cppyy::TCppObject_t, Cppyy::TCppType_t)
Register a hook that Cppyy runs when constructing an object.
Definition: TMemoryRegulator.cxx:37
PyROOT::TMemoryRegulator::fObjectMap
static ObjectMap_t fObjectMap
Definition: TMemoryRegulator.h:53
CPyCppyy.h
PyROOT::TMemoryRegulator::TMemoryRegulator
TMemoryRegulator()
Constructor.
Definition: TMemoryRegulator.cxx:25
PyROOT::TMemoryRegulator
Definition: TMemoryRegulator.h:51
PyROOT
Definition: CPPInstancePyz.cxx:25
Cppyy::TCppType_t
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
Cppyy::TCppObject_t
void * TCppObject_t
Definition: cpp_cppyy.h:21
TObject.h
TObject
Mother of all ROOT objects.
Definition: TObject.h:37