Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
47namespace PyROOT {
48
49typedef std::unordered_map<Cppyy::TCppObject_t, Cppyy::TCppType_t> ObjectMap_t;
50
51class TMemoryRegulator : public TObject {
52private:
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
59public:
61
62 virtual void RecursiveRemove(TObject *);
63
65};
66
67} // namespace PyROOT
68
69#endif // !PYROOT_TMEMORYREGULATOR_H
static std::pair< bool, bool > RegisterHook(Cppyy::TCppObject_t, Cppyy::TCppType_t)
Register a hook that Cppyy runs when constructing an object.
virtual void RecursiveRemove(TObject *)
Get the class id of the TObject being deleted and run Cppyy's RecursiveRemove.
static ObjectMap_t fObjectMap
void ClearProxiedObjects()
Clean up all tracked objects.
static std::pair< bool, bool > UnregisterHook(Cppyy::TCppObject_t, Cppyy::TCppType_t)
Register a hook that Cppyy runs when deleting an object.
Mother of all ROOT objects.
Definition TObject.h:41
void * TCppObject_t
Definition cpp_cppyy.h:21
TCppScope_t TCppType_t
Definition cpp_cppyy.h:19
std::unordered_map< Cppyy::TCppObject_t, Cppyy::TCppType_t > ObjectMap_t