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// Author: Vincenzo Eduardo Padulano CERN 05/2024
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#ifndef PYROOT_TMEMORYREGULATOR_H
14#define PYROOT_TMEMORYREGULATOR_H
15
16//////////////////////////////////////////////////////////////////////////
17// //
18// TMemoryRegulator //
19// //
20// Sets hooks in Cppyy's MemoryRegulator to keep track of the TObjects //
21// that are constructed and destructed. For those objects, a map is //
22// filled, where the key is the address of the object and the value is //
23// the class to which the object belongs. //
24// //
25// The TMemoryRegulator object, created in PyROOTWrapper.cxx, is added //
26// to the list of cleanups and its RecursiveRemove method is called by //
27// ROOT to manage the memory of TObjects being deleted. //
28// In RecursiveRemove, the object being deleted is already a TNamed, so //
29// the information about its actual class is not available anymore. //
30// To solve the problem, the map above is used to know the class of the //
31// object, so that Cppyy's RecursiveRemove can be called passing the //
32// class as argument. //
33//////////////////////////////////////////////////////////////////////////
34
35// Bindings
36// CPyCppyy.h must be go first, since it includes Python.h, which must be
37// included before any standard header
38#include "../../cppyy/CPyCppyy/src/CPyCppyy.h"
39#include "../../cppyy/CPyCppyy/src/MemoryRegulator.h"
40
41// ROOT
42#include "TObject.h"
43
44// Stl
45#include <unordered_map>
46
47namespace PyROOT {
48
49class RegulatorCleanup;
50
51/// Manages TObject-derived objects created in a PyROOT application
52///
53/// This class is responsible to keep track of the creation of the objects
54/// that need further memory management within ROOT. The `ClearProxiedObjects`
55/// function is only called at PyROOT shutdown time. The `CallCppyyRecursiveRemove`
56/// is called as part of the global list of cleanups object destruction.
57///
58/// This class is intentionally not derived from TObject. See the
59/// `PyROOT::RegulatorCleanup` class for more info.
60///
61/// \note This class is not thread-safe on its own. We create one thread-local
62/// object in PyROOTWrapper.cxx.
63class TMemoryRegulator final {
64 using ObjectMap_t = std::unordered_map<Cppyy::TCppObject_t, Cppyy::TCppType_t>;
65
66 ObjectMap_t fObjectMap{}; // key: object address; value: object class id
67
69
71
73
75
77
78 friend class RegulatorCleanup;
79};
80
81/// A TObject-derived class to inject the memory regulation logic in the ROOT list of cleanups.
82///
83/// The purpose of this class is to keep the responsibilities separate between
84/// the TMemoryRegulator logic and the rest of ROOT.
85class RegulatorCleanup final : public TObject {
87
88public:
92};
93
94} // namespace PyROOT
95
96#endif // !PYROOT_TMEMORYREGULATOR_H
#define ClassDefInlineNV(name, id)
Definition Rtypes.h:357
A TObject-derived class to inject the memory regulation logic in the ROOT list of cleanups.
TMemoryRegulator fRegulator
void RecursiveRemove(TObject *object) final
Recursively remove this object from a list.
Manages TObject-derived objects created in a PyROOT application.
std::pair< bool, bool > RegisterHook(Cppyy::TCppObject_t, Cppyy::TCppType_t)
Register a hook that Cppyy runs when constructing an object.
std::unordered_map< Cppyy::TCppObject_t, Cppyy::TCppType_t > ObjectMap_t
void CallCppyyRecursiveRemove(TObject *object)
Get the class id of the TObject being deleted and run Cppyy's RecursiveRemove.
void ClearProxiedObjects()
Clean up all tracked objects.
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