Commit 3a8e1beb authored by Kirill Smelkov's avatar Kirill Smelkov

bigfile/py: Factor out code to "unpin" a buffer to separate functions

This code was added in 6da5172e (bigfile/py: Teach storeblk() how to
correctly propagate traceback on error) to unpin a storeblk pybuf to not
care whether its refcount == 1 - this way to be able to propagate python
error upper not caring whether pybuf is still referenced or not.

9aa6a5d7 (bigfile/py: Teach loadblk() to automatically break reference
cycles to pybuf) adds a note that such unpinning is not strictly
correct: becuase of other buffer objects were created from pybuf - they
are copying pointers on initialization and unpinning pybuf won't adjust
them.

However for loadblk codepath it turned out (see next patch) it is not
completely possible to unreference pybuf in all cases. For this reason
loadblk will be falling back to unpinning too.

As a preparatory step move common code to shared functions.
parent 61b18a40
...@@ -130,6 +130,13 @@ static void XPyObject_PrintReferrers(PyObject *obj, FILE *fp); ...@@ -130,6 +130,13 @@ static void XPyObject_PrintReferrers(PyObject *obj, FILE *fp);
/* check whether frame f is a callee of top */ /* check whether frame f is a callee of top */
static int XPyFrame_IsCalleeOf(PyFrameObject *f, PyFrameObject *top); static int XPyFrame_IsCalleeOf(PyFrameObject *f, PyFrameObject *top);
/* buffer utilities: unpin buffer from its memory - make it zero-length
* pointing to NULL but staying a vailid python object */
#if PY_MAJOR_VERSION < 3
void XPyBufferObject_Unpin(PyBufferObject *bufo);
#endif
void XPyBuffer_Unpin(Py_buffer *view);
/************ /************
* PyVMA * * PyVMA *
...@@ -730,16 +737,10 @@ static int pybigfile_storeblk(BigFile *file, blk_t blk, const void *buf) ...@@ -730,16 +737,10 @@ static int pybigfile_storeblk(BigFile *file, blk_t blk, const void *buf)
* traceback in case of storeblk() error. */ * traceback in case of storeblk() error. */
#if BIGFILE_USE_OLD_BUFFER #if BIGFILE_USE_OLD_BUFFER
PyBufferObject *pybufo = (PyBufferObject *)pybuf; PyBufferObject *pybufo = (PyBufferObject *)pybuf;
pybufo->b_ptr = NULL; XPyBufferObject_Unpin(pybufo);
pybufo->b_size = 0;
pybufo->b_offset = 0;
pybufo->b_hash = -1;
Py_CLEAR(pybufo->b_base);
#else #else
PyMemoryViewObject *pybufm = (PyMemoryViewObject *)pybuf; PyMemoryViewObject *pybufm = (PyMemoryViewObject *)pybuf;
pybufm->view.buf = NULL; XPyBuffer_Unpin(&pybufm->view);
pybufm->view.len = 0;
Py_CLEAR(pybufm->view.obj);
#endif #endif
/* verify that we actually tweaked pybuf ok */ /* verify that we actually tweaked pybuf ok */
...@@ -1064,3 +1065,23 @@ XPyFrame_IsCalleeOf(PyFrameObject *f, PyFrameObject *top) ...@@ -1064,3 +1065,23 @@ XPyFrame_IsCalleeOf(PyFrameObject *f, PyFrameObject *top)
return 0; return 0;
} }
#if PY_MAJOR_VERSION < 3
void
XPyBufferObject_Unpin(PyBufferObject *bufo)
{
bufo->b_ptr = NULL;
bufo->b_size = 0;
bufo->b_offset = 0;
bufo->b_hash = -1;
Py_CLEAR(bufo->b_base);
}
#endif
void
XPyBuffer_Unpin(Py_buffer *view)
{
view->buf = NULL;
view->len = 0;
Py_CLEAR(view->obj);
}
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