| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
r |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#define PY_SSIZE_T_CLEAN |
| 32 |
#include <Python.h> |
| 33 |
|
| 34 |
#include "private.h" |
| 35 |
|
| 36 |
typedef struct { |
| 37 |
PyObject_HEAD |
| 38 |
PyObject *exporter; |
| 39 |
void *buf; |
| 40 |
Py_ssize_t len; |
| 41 |
int readonly; |
| 42 |
} Pycairo_BufferProxy; |
| 43 |
|
| 44 |
static PyTypeObject Pycairo_BufferProxyType = { |
| 45 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 46 |
"cairo._BufferProxy", |
| 47 |
sizeof(Pycairo_BufferProxy), |
| 48 |
}; |
| 49 |
|
| 50 |
static int |
| 51 |
buffer_proxy_getbuffer(PyObject *exporter, Py_buffer *view, int flags) |
| 52 |
{ |
| 53 |
Pycairo_BufferProxy *self = (Pycairo_BufferProxy *)exporter; |
| 54 |
|
| 55 |
return PyBuffer_FillInfo (view, exporter, self->buf, self->len, |
| 56 |
self->readonly, flags); |
| 57 |
} |
| 58 |
|
| 59 |
static PyBufferProcs Pycairo_BufferProxy_as_buffer = { |
| 60 |
(getbufferproc)buffer_proxy_getbuffer, |
| 61 |
(releasebufferproc)0, |
| 62 |
}; |
| 63 |
|
| 64 |
PyObject * |
| 65 |
buffer_proxy_create_view(PyObject *exporter, void *buf, Py_ssize_t len, |
| 66 |
int readonly) { |
| 67 |
PyObject *memoryview; |
| 68 |
PyObject *obj; |
| 69 |
Pycairo_BufferProxy *self; |
| 70 |
|
| 71 |
obj = (PyObject *)PyObject_GC_New(Pycairo_BufferProxy, |
| 72 |
&Pycairo_BufferProxyType); |
| 73 |
if (obj == NULL) |
| 74 |
return NULL; |
| 75 |
|
| 76 |
self = (Pycairo_BufferProxy *)obj; |
| 77 |
Py_INCREF(exporter); |
| 78 |
self->exporter = exporter; |
| 79 |
self->buf = buf; |
| 80 |
self->len = len; |
| 81 |
self->readonly = readonly; |
| 82 |
PyObject_GC_Track(obj); |
| 83 |
|
| 84 |
memoryview = PyMemoryView_FromObject (obj); |
| 85 |
Py_DECREF(obj); |
| 86 |
|
| 87 |
return memoryview; |
| 88 |
} |
| 89 |
|
| 90 |
static int |
| 91 |
buffer_proxy_traverse(PyObject* obj, visitproc visit, void *arg) |
| 92 |
{ |
| 93 |
Pycairo_BufferProxy *self = (Pycairo_BufferProxy *)obj; |
| 94 |
|
| 95 |
Py_VISIT(self->exporter); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
static int |
| 100 |
buffer_proxy_clear(PyObject *obj) |
| 101 |
{ |
| 102 |
Pycairo_BufferProxy *self = (Pycairo_BufferProxy *)obj; |
| 103 |
|
| 104 |
Py_CLEAR(self->exporter); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
buffer_proxy_dealloc(PyObject* obj) |
| 110 |
{ |
| 111 |
Pycairo_BufferProxy *self = (Pycairo_BufferProxy *)obj; |
| 112 |
|
| 113 |
PyObject_GC_UnTrack(obj); |
| 114 |
|
| 115 |
self->buf = NULL; |
| 116 |
self->len = 0; |
| 117 |
self->readonly = 0; |
| 118 |
buffer_proxy_clear(obj); |
| 119 |
|
| 120 |
Py_TYPE(obj)->tp_free(obj); |
| 121 |
} |
| 122 |
|
| 123 |
int |
| 124 |
init_buffer_proxy (void) { |
| 125 |
Pycairo_BufferProxyType.tp_as_buffer = &Pycairo_BufferProxy_as_buffer; |
| 126 |
Pycairo_BufferProxyType.tp_dealloc = &buffer_proxy_dealloc; |
| 127 |
Pycairo_BufferProxyType.tp_traverse = &buffer_proxy_traverse; |
| 128 |
Pycairo_BufferProxyType.tp_clear = &buffer_proxy_clear; |
| 129 |
Pycairo_BufferProxyType.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC; |
| 130 |
|
| 131 |
if (PyType_Ready(&Pycairo_BufferProxyType) < 0) |
| 132 |
return -1; |
| 133 |
|
| 134 |
return 0; |
| 135 |
} |