Fix and rewrite the memory management for capsules.

This commit is contained in:
Siu Kwan Lam 2013-01-22 15:31:13 -06:00
commit f88036274a
7 changed files with 69 additions and 74 deletions

View file

@ -1,48 +1,19 @@
#ifndef LLVMPY_CAPSULE_CONTEXT_H_
#define LLVMPY_CAPSULE_CONTEXT_H_
#include <iostream>
#include <ctime>
typedef PyObject* Destructor_Fn;
static bool CapsuleContextDebug = false;
struct CapsuleContext {
const char* className;
Destructor_Fn destructor;
CapsuleContext(const char* cn, Destructor_Fn dtor=NULL)
: className(cn), destructor(dtor) { }
CapsuleContext(const char* cn)
: className(cn)
{ }
};
void capsule_destructor(PyObject* capsule){
using std::cerr;
using std::endl;
CapsuleContext* context = (CapsuleContext*)PyCapsule_GetContext(capsule);
if (context->destructor) {
if (CapsuleContextDebug) {
cerr << clock()
<< " == DEBUG =="
<< " destroy pointer: "
<< context->className
<< endl;
}
PyObject_CallMethodObjArgs(context->destructor, capsule, NULL);
} else {
if (CapsuleContextDebug) {
cerr << clock()
<< " == DEBUG =="
<< " keep pointer alive: "
<< context->className
<< endl;
}
}
delete context;
}
void enable_capsule_dtor_debug(bool enabled){
CapsuleContextDebug = enabled;
}
#endif //LLVMPY_CAPSULE_CONTEXT_H_