Commit 3a8e1beb by 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.
1 parent 61b18a40
......@@ -130,6 +130,13 @@ static void XPyObject_PrintReferrers(PyObject *obj, FILE *fp);
/* check whether frame f is a callee of 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 *
......@@ -730,16 +737,10 @@ static int pybigfile_storeblk(BigFile *file, blk_t blk, const void *buf)
* traceback in case of storeblk() error. */
#if BIGFILE_USE_OLD_BUFFER
PyBufferObject *pybufo = (PyBufferObject *)pybuf;
pybufo->b_ptr = NULL;
pybufo->b_size = 0;
pybufo->b_offset = 0;
pybufo->b_hash = -1;
Py_CLEAR(pybufo->b_base);
XPyBufferObject_Unpin(pybufo);
#else
PyMemoryViewObject *pybufm = (PyMemoryViewObject *)pybuf;
pybufm->view.buf = NULL;
pybufm->view.len = 0;
Py_CLEAR(pybufm->view.obj);
XPyBuffer_Unpin(&pybufm->view);
#endif
/* verify that we actually tweaked pybuf ok */
......@@ -1064,3 +1065,23 @@ XPyFrame_IsCalleeOf(PyFrameObject *f, PyFrameObject *top)
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);
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!