forked from tpaviot/pythonocc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGarbageCollector.py
More file actions
99 lines (89 loc) · 3.68 KB
/
GarbageCollector.py
File metadata and controls
99 lines (89 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
##Copyright 2008-2014 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
# Init logging system
from collections import deque
class GarbageCollector(object):
""" Garbage collector for OCC objects
"""
def __init__(self):
#self._collected_objects = []
self._handles = deque()
self._transients = deque()
self._misc = deque()
# The list for the prevented objects
self._prevent = deque()
# define contexts lists
self._handles_contexts = deque()
self._transients_contexts = deque
self._misc_contexts = deque()
def prevent_object(self, obj):
''' Prevent an object from being deleted. This object is temporary
stored in a list to increase its refecount.
'''
self._prevent.append(obj)
def push_context(self):
''' Create a new context. When the push context method is called,
objects are collected in this new context. When the context is popped,
erase the content of the context.
'''
# store the current objects in the contexts list
self._handles_contexts.append(self._handles)
self._transients_contexts.append(self._transients)
self._misc_contexts.append(self._misc)
# Then erase the content of the current lists
self._handles = deque()
self._transients = deque()
self._misc = deque()
def pop_context(self):
self._handles = self._handles_contexts.pop()
self._transients = self._transients_contexts.pop()
self._misc = self._misc_contexts.pop()
def collect_object(self, obj_deleted):
''' This method is called whenever a pythonOCC instance is deleted.'''
if hasattr(obj_deleted, 'was_purged'):
return False
if obj_deleted.__class__.__name__.startswith('Handle'):
self._handles.append(obj_deleted)
elif hasattr(obj_deleted, "GetHandle"):
self._transients.append(obj_deleted)
else:
self._misc.append(obj_deleted)
def smart_purge(self):
# TODO: need to reverse the lists first in order
# to start with the olders objects?
# Remove Handles
while len(self._handles) > 0:
handle = self._handles.pop()
handle.Nullify() # decrement ref counting_kill_pointed()
# after that, the reference counting of transients objects
# in the garbage should be 0
handle.was_purged = True
# Remove Transients
for transient in self._transients:
transient_must_be_purged = False
if transient.GetRefCount() == 0:
transient_must_be_purged = True
if transient_must_be_purged:
transient._kill_pointed()
transient.was_purged = True
self._transients.remove(transient)
# Remove other objects
while len(self._misc) > 0:
misc = self._misc.pop()
misc._kill_pointed()
misc.was_purged = True
garbage = GarbageCollector()