Skip to content

Commit e0ee2d0

Browse files
committed
Added on_dealloc and on_realloc callbacks
1 parent baca5ec commit e0ee2d0

File tree

5 files changed

+52
-25
lines changed

5 files changed

+52
-25
lines changed

src/core/modules/memory/memory_callback.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,6 @@ CCallback::CCallback(object oCallback, Convention_t eConv, tuple args, object re
114114
m_ulAddr = (unsigned long) a.make();
115115
}
116116

117-
CCallback::~CCallback()
118-
{
119-
if (m_bAutoDealloc)
120-
{
121-
PythonLog(4, "[SP] Automatically deallocating callback at %u.", m_ulAddr);
122-
Dealloc();
123-
124-
// This prevents ~CPointer from calling CPointer::Dealloc() again
125-
m_bAutoDealloc = false;
126-
}
127-
}
128-
129117
int CCallback::GetPopSize()
130118
{
131119
/*

src/core/modules/memory/memory_callback.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class CCallback: public CFunction
7575
{
7676
public:
7777
CCallback(object oCallback, Convention_t eConv, tuple args, object return_type, bool bAutoDealloc = true);
78-
virtual ~CCallback();
7978

8079
int GetPopSize();
8180
int GetArgumentOffset(int iIndex);

src/core/modules/memory/memory_tools.cpp

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ CPointer::CPointer(unsigned long ulAddr /* = 0 */, bool bAutoDealloc /* false */
5555
m_bAutoDealloc = bAutoDealloc;
5656
}
5757

58-
CPointer::~CPointer()
59-
{
60-
if (m_bAutoDealloc)
61-
{
62-
PythonLog(4, "[SP] Automatically deallocating pointer at %u.", m_ulAddr);
63-
Dealloc();
64-
}
65-
}
66-
6758
void CPointer::SetStringPtr(char* szText, int iOffset /* = 0 */)
6859
{
6960
if (!IsValid())
@@ -202,6 +193,47 @@ CFunction* CPointer::MakeVirtualFunction(int iIndex, Convention_t eConv, tuple a
202193
return GetVirtualFunc(iIndex)->MakeFunction(eConv, args, return_type);
203194
}
204195

196+
void CPointer::CallCallback(PyObject* self, char* szCallback)
197+
{
198+
PyObject* callback = PyObject_GetAttrString(self, szCallback);
199+
if (callback && PyCallable_Check(callback))
200+
{
201+
if (!PyObject_HasAttrString(callback, "__self__"))
202+
{
203+
xdecref(PyObject_CallFunction(callback, "O", self));
204+
}
205+
else
206+
{
207+
xdecref(PyObject_CallMethod(self, szCallback, NULL));
208+
}
209+
}
210+
xdecref(callback);
211+
}
212+
213+
void CPointer::PreDealloc(PyObject* self)
214+
{
215+
CallCallback(self, "on_dealloc");
216+
CPointer* pointer = extract<CPointer *>(self);
217+
pointer->Dealloc();
218+
}
219+
220+
void CPointer::PreRealloc(PyObject* self, int iSize)
221+
{
222+
CallCallback(self, "on_realloc");
223+
CPointer* pointer = extract<CPointer *>(self);
224+
pointer->Realloc(iSize);
225+
}
226+
227+
void CPointer::__del__(PyObject* self)
228+
{
229+
if (PyObject_IsTrue(PyObject_GetAttrString(self, "auto_dealloc")))
230+
{
231+
unsigned long ulAddr = extract<unsigned long>(PyObject_GetAttrString(self, "address"));
232+
PythonLog(4, "[SP] Automatically deallocating pointer at %u.", ulAddr);
233+
PreDealloc(self);
234+
}
235+
}
236+
205237
//-----------------------------------------------------------------------------
206238
// CFunction class
207239
//-----------------------------------------------------------------------------

src/core/modules/memory/memory_tools.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ class CPointer
202202
{
203203
public:
204204
CPointer(unsigned long ulAddr = 0, bool bAutoDealloc = false);
205-
virtual ~CPointer();
206205

207206
operator unsigned long() const { return m_ulAddr; }
208207

@@ -282,6 +281,11 @@ class CPointer
282281
CFunction* MakeFunction(Convention_t eConv, boost::python::tuple args, object return_type);
283282
CFunction* MakeVirtualFunction(int iIndex, Convention_t eConv, boost::python::tuple args, object return_type);
284283

284+
static void CallCallback(PyObject* self, char* szCallback);
285+
static void PreDealloc(PyObject* self);
286+
static void PreRealloc(PyObject* self, int iSize);
287+
static void __del__(PyObject* self);
288+
285289
public:
286290
unsigned long m_ulAddr;
287291
bool m_bAutoDealloc;

src/core/modules/memory/memory_wrap_python.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,20 @@ void export_memtools()
261261
)
262262

263263
.def("realloc",
264-
&CPointer::Realloc,
264+
&CPointer::PreRealloc,
265265
"Reallocates a memory block.",
266266
args("size")
267267
)
268268

269269
.def("dealloc",
270-
&CPointer::Dealloc,
270+
&CPointer::PreDealloc,
271271
"Deallocates a memory block."
272272
)
273273

274+
.def("__del__",
275+
&CPointer::__del__
276+
)
277+
274278
.def("get_size",
275279
&CPointer::GetSize,
276280
"Returns the size of the memory block."

0 commit comments

Comments
 (0)