Commit 959c9d89 authored by gsamain's avatar gsamain

C++ CyObject utility code

parent 38211af1
......@@ -1532,20 +1532,37 @@ static void __Pyx_FastGilFuncInit(void) {
#endif /* Has GCC */
#ifdef __cplusplus
#include <atomic>
using namespace std;
#define CyObject_ATOMIC_REFCOUNT_TYPE atomic_int
#else
#include <stdatomic.h>
#define CyObject_ATOMIC_REFCOUNT_TYPE int
#endif /* __cplusplus */
#if __cplusplus >= 201103L
#include <atomic>
using namespace std;
#define CyObject_ATOMIC_REFCOUNT_TYPE atomic_int
class CyObject {
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. */
#define CyObject_HEAD \
CyObject_ATOMIC_REFCOUNT_TYPE ob_refcnt; \
void (*cdealloc)(void* self);
static inline void _Cy_INCREF(CyObject *op) {
op->CyObject_INCREF();
}
struct CyObject {
CyObject_HEAD
};
/* Cast argument to CyObject* type. */
#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:
// atomic is already included in ModuleSetupCode
// #include <atomic>
#else
#include <stdlib.h>
#include <stddef.h>
// #include <stdatomic.h>
#error C++ needed for cython+ nogil classes
#endif /* __cplusplus */
// Defined in ModuleSetupCode.c
/* CyObject_HEAD defines the initial segment of every CyObject. */
//#define CyObject_HEAD \
// 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);
void CyObject::CyObject_INCREF()
{
atomic_fetch_add(&(this->ob_refcnt), 1);
}
static inline void _Cy_INCREF(struct CyObject *op) {
atomic_fetch_add(&(op->ob_refcnt), 1);
int CyObject::CyObject_DECREF()
{
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 ///////////////
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