Commit 959c9d89 authored by gsamain's avatar gsamain

C++ CyObject utility code

parent 38211af1
...@@ -1532,20 +1532,37 @@ static void __Pyx_FastGilFuncInit(void) { ...@@ -1532,20 +1532,37 @@ static void __Pyx_FastGilFuncInit(void) {
#endif /* Has GCC */ #endif /* Has GCC */
#ifdef __cplusplus #ifdef __cplusplus
#include <atomic> #if __cplusplus >= 201103L
using namespace std; #include <atomic>
#define CyObject_ATOMIC_REFCOUNT_TYPE atomic_int using namespace std;
#else #define CyObject_ATOMIC_REFCOUNT_TYPE atomic_int
#include <stdatomic.h>
#define CyObject_ATOMIC_REFCOUNT_TYPE int class CyObject {
#endif /* __cplusplus */ private: CyObject_ATOMIC_REFCOUNT_TYPE ob_refcnt;
public:
CyObject(): ob_refcnt(1) {}
virtual ~CyObject() {}
void CyObject_INCREF();
int CyObject_DECREF();
};
static inline int _Cy_DECREF(CyObject *op) {
return op->CyObject_DECREF();
}
/* CyObject_HEAD defines the initial segment of every CyObject. */ static inline void _Cy_INCREF(CyObject *op) {
#define CyObject_HEAD \ op->CyObject_INCREF();
CyObject_ATOMIC_REFCOUNT_TYPE ob_refcnt; \ }
void (*cdealloc)(void* self);
struct CyObject { /* Cast argument to CyObject* type. */
CyObject_HEAD #define _CyObject_CAST(op) op
};
#define Cy_INCREF(op) _Cy_INCREF(_CyObject_CAST(op))
#define Cy_DECREF(op) do {if (_Cy_DECREF(_CyObject_CAST(op))) {op = NULL;}} while(0)
#define Cy_XDECREF(op) do {if (op != NULL) {Cy_DECREF(op);}} while(0)
#define Cy_GOTREF(op)
#define Cy_XGOTREF(op)
#define Cy_GIVEREF(op)
#define Cy_XGIVEREF(op)
#endif
#endif
...@@ -1589,44 +1589,23 @@ try_unpack: ...@@ -1589,44 +1589,23 @@ try_unpack:
// atomic is already included in ModuleSetupCode // atomic is already included in ModuleSetupCode
// #include <atomic> // #include <atomic>
#else #else
#include <stdlib.h> #error C++ needed for cython+ nogil classes
#include <stddef.h>
// #include <stdatomic.h>
#endif /* __cplusplus */ #endif /* __cplusplus */
// Defined in ModuleSetupCode.c void CyObject::CyObject_INCREF()
/* CyObject_HEAD defines the initial segment of every CyObject. */ {
//#define CyObject_HEAD \ atomic_fetch_add(&(this->ob_refcnt), 1);
// int ob_refcnt; \
// void (*cdealloc)(void * self);
/* Cast argument to PyObject* type. */
#define _CyObject_CAST(op) ((struct CyObject*)(op))
static inline void _Cy_DECREF(struct CyObject *op) {
// int f = open("log_nogil", O_WRONLY|O_APPEND);
// dprintf(f, "DECREF ob_refcnt (before decref) = %d\n", op->ob_refcnt);
if (atomic_fetch_sub(&(op->ob_refcnt), 1) == 1) {
//op->cdealloc(op);
// DEBUG
// dprintf(f, "Freeing memory\n");
free(op);
}
// close(f);
} }
static inline void _Cy_INCREF(struct CyObject *op) { int CyObject::CyObject_DECREF()
atomic_fetch_add(&(op->ob_refcnt), 1); {
if (atomic_fetch_sub(&(this->ob_refcnt), 1) == 1) {
delete this;
return 1;
}
return 0;
} }
#define Cy_INCREF(op) _Cy_INCREF(_CyObject_CAST(op))
#define Cy_DECREF(op) _Cy_DECREF(_CyObject_CAST(op))
#define Cy_XDECREF(op) do {if (op != NULL) {Cy_DECREF(op);}} while(0)
#define Cy_GOTREF(op)
#define Cy_XGOTREF(op)
#define Cy_GIVEREF(op)
#define Cy_XGIVEREF(op)
/////////////// UnpackUnboundCMethod.proto /////////////// /////////////// UnpackUnboundCMethod.proto ///////////////
typedef struct { typedef struct {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment