Debian Codegraph @

cairo/bufferproxy.c

raw symbols SCIP
1 /* -*- mode: C; c-basic-offset: 2 -*-
2 *
3 * Pycairo - Python bindings for cairo
4 *
5 * Copyright © 2017 Christoph Reiter
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it either under the terms of the GNU Lesser General Public
9 * License version 2.1 as published by the Free Software Foundation
10 * (the "LGPL") or, at your option, under the terms of the Mozilla
11 * Public License Version 1.1 (the "MPL"). If you do not alter this
12 * notice, a recipient may use your version of this file under either
13 * the MPL or the LGPL.
14 *
15 * You should have received a copy of the LGPL along with this library
16 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * You should have received a copy of the MPL along with this library
19 * in the file COPYING-MPL-1.1
20 *
21 * The contents of this file are subject to the Mozilla Public License
22 * Version 1.1 (the "License"); you may not use this file except in
23 * compliance with the License. You may obtain a copy of the License at
24 * http://www.mozilla.org/MPL/
25 *
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
27 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
28 * the specific language governing rights and limitations.
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 }