Commit 4a45a84a authored by Boxiang Sun's avatar Boxiang Sun

Pritimive memory management for nogil extension

parent ca525fde
...@@ -8775,7 +8775,10 @@ class DictNode(ExprNode): ...@@ -8775,7 +8775,10 @@ class DictNode(ExprNode):
# Custom method used here because key-value # Custom method used here because key-value
# pairs are evaluated and used one at a time. # pairs are evaluated and used one at a time.
code.mark_pos(self.pos) code.mark_pos(self.pos)
self.allocate_temp_result(code) self.allocate_temp_result(code)
if hasattr(self.type, 'nogil') and self.type.nogil:
code.putln("%s = (struct %s *)malloc(sizeof(struct %s));" % (self.result(), self.type.objstruct_cname, self.type.objstruct_cname))
is_dict = self.type.is_pyobject is_dict = self.type.is_pyobject
if is_dict: if is_dict:
......
...@@ -1117,11 +1117,13 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1117,11 +1117,13 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
Naming.obj_base_cname)) Naming.obj_base_cname))
elif nogil: elif nogil:
# Extension type with nogil keyword indicate it is a CPython-free struct # Extension type with nogil keyword indicate it is a CPython-free struct
code.globalstate.use_utility_code(
UtilityCode.load_cached("CythonReferenceCounting", "ObjectHandling.c"))
code.putln( code.putln(
"// nogil" "// nogil"
) )
code.putln( code.putln(
"size_t ob_refcnt;" "int ob_refcnt;" # "CyObject_HEAD;" Sometimes the CythonReferenceCounting was put after the nogil extension declaration, WTF!!!
) )
else: else:
code.putln( code.putln(
......
...@@ -1363,9 +1363,6 @@ class CythonExtensionType(CythonObjectType): ...@@ -1363,9 +1363,6 @@ class CythonExtensionType(CythonObjectType):
entity_code = "*%s" % entity_code entity_code = "*%s" % entity_code
return self.base_declaration_code(base_code, entity_code) return self.base_declaration_code(base_code, entity_code)
def literal_code(self, value):
return '(struct %s *)malloc(sizeof(struct %s))' % (self.objstruct_cname, self.objstruct_cname)
def type_test_code(self, py_arg, notnone=False): def type_test_code(self, py_arg, notnone=False):
none_check = "((%s) == Py_None)" % py_arg none_check = "((%s) == Py_None)" % py_arg
......
...@@ -1527,6 +1527,51 @@ try_unpack: ...@@ -1527,6 +1527,51 @@ try_unpack:
return 0; return 0;
} }
/////////////// CythonReferenceCounting.proto ///////////////
#include <stdlib.h>
#include <stddef.h>
#if !defined(__GNUC__)
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
/* Test for GCC > 4.9.0 */
#if GCC_VERSION < 40900
#error atomic.h works only with GCC newer than version 4.9
#endif /* GNUC >= 4.9 */
#endif /* Has GCC */
// #include <stdatomic.h>
/* CyObject_HEAD defines the initial segment of every CyObject. */
#define CyObject_HEAD \
int ob_refcnt; \
void (*cdealloc)();
struct CyObject {
CyObject_HEAD
};
/* Cast argument to PyObject* type. */
#define _CyObject_CAST(op) ((struct CyObject*)(op))
// XXX: Without scope analysis this is useless...
/*
static inline void _Cy_DECREF(struct CyObject *op) {
if (atomic_fetch_sub(&(op->ob_refcnt), 1) == 1) {
op->cdealloc(op);
}
}
static inline void _Cy_INCREF(struct CyObject *op) {
atomic_fetch_add(&(op->ob_refcnt), 1);
}
#define Cy_INCREF(op) _Cy_INCREF(_CyObject_CAST(op))
#define Cy_DECREF(op) _Cy_DECREF(_CyObject_CAST(op))
*/
/////////////// UnpackUnboundCMethod.proto /////////////// /////////////// UnpackUnboundCMethod.proto ///////////////
......
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