-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_mpmetrics.c
More file actions
191 lines (153 loc) · 3.88 KB
/
_mpmetrics.c
File metadata and controls
191 lines (153 loc) · 3.88 KB
1
2
3
4
5
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// SPDX-License-Identifier: LGPL-3.0-only
/*
* Copyright (C) 2021-22 Sean Anderson <seanga2@gmail.com>
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <structmember.h>
#include "_mpmetrics.h"
static int Buffer_init(BufferObject *self, PyObject *args, PyObject *kwds)
{
PyObject *size_obj;
size_t size;
if (!PyArg_ParseTuple(args, "w*", &self->shm))
return -1;
size_obj = PyObject_GetAttrString((PyObject *)self, "size");
if (!size_obj)
goto error;
size = PyLong_AsSize_t(size_obj);
Py_DECREF(size_obj);
if (PyErr_Occurred())
goto error;
if ((size_t)self->shm.len < size) {
PyErr_Format(PyExc_ValueError,
"shared memory (%zd bytes) too small; must be at least %zu bytes",
self->shm.len, size);
goto error;
}
return 0;
error:
PyBuffer_Release(&self->shm);
return -1;
}
static int Buffer_traverse(BufferObject *self, visitproc visit, void *arg)
{
Py_VISIT(self->shm.obj);
return 0;
}
static int Buffer_clear(BufferObject *self)
{
PyBuffer_Release(&self->shm);
return 0;
}
static void Buffer_dealloc(BufferObject *self)
{
Buffer_clear(self);
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *Buffer_setstate(BufferObject *self, PyObject *args,
PyObject *kwds)
{
if (Buffer_init(self, args, kwds))
return NULL;
Py_RETURN_NONE;
}
static PyMethodDef Buffer_methods[] = {
{
.ml_name = "_setstate",
.ml_meth = (PyCFunction)Buffer_setstate,
.ml_flags = METH_VARARGS | METH_KEYWORDS,
},
{ 0 },
};
PyTypeObject BufferType = {
PyObject_HEAD_INIT(NULL)
.tp_basicsize = sizeof(BufferObject),
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_HAVE_GC,
.tp_name = "_mpmetrics.Buffer",
.tp_doc = PyDoc_STR("Buffer(mem)\n"
"--\n"
"\n"
"Create a buffer backed by 'mem'. This is a base class for other C\n"
"classes in _mpmetrics."),
.tp_new = PyType_GenericNew,
.tp_init = (initproc)Buffer_init,
.tp_dealloc = (destructor)Buffer_dealloc,
.tp_traverse = (traverseproc)Buffer_traverse,
.tp_clear = (inquiry)Buffer_clear,
.tp_methods = Buffer_methods,
};
static int PyType_AddConstant(PyTypeObject *type, const char *name,
PyObject *obj)
{
int ret;
if (!type->tp_dict) {
type->tp_dict = PyDict_New();
if (!type->tp_dict) {
Py_DECREF(obj);
return -1;
}
}
ret = PyDict_SetItemString(type->tp_dict, name, obj);
Py_DECREF(obj);
return ret;
}
int PyType_AddSizeConstant(PyTypeObject *type, const char *name, size_t value)
{
PyObject *obj = PyLong_FromSize_t(value);
if (!obj)
return -1;
return PyType_AddConstant(type, name, obj);
}
int PyType_AddLLConstant(PyTypeObject *type, const char *name, long long value)
{
PyObject *obj = PyLong_FromLongLong(value);
if (!obj)
return -1;
return PyType_AddConstant(type, name, obj);
}
int PyType_AddULLConstant(PyTypeObject *type, const char *name,
unsigned long long value)
{
PyObject *obj = PyLong_FromUnsignedLongLong(value);
if (!obj)
return -1;
return PyType_AddConstant(type, name, obj);
}
int PyType_AddDoubleConstant(PyTypeObject *type, const char *name,
double value)
{
PyObject *obj = PyLong_FromDouble(value);
if (!obj)
return -1;
return PyType_AddConstant(type, name, obj);
}
static PyModuleDef module = {
PyModuleDef_HEAD_INIT,
.m_name = "_mpmetrics",
.m_doc = PyDoc_STR("C helpers for multiprocess-safe metrics\n"
"\n"
"This module provides various concurrency primitives, typically backed\n"
"by shared memory. You probably want to use mpmetrics.atomic instead\n"
"of the atomic types in this module, as they may not always be\n"
"available on all architectures."),
.m_size = -1,
};
PyMODINIT_FUNC PyInit__mpmetrics(void)
{
PyObject *m;
m = PyModule_Create(&module);
if (!m)
return NULL;
if (PyModule_AddType(m, &BufferType))
goto error;
if (LockType_Add(m))
goto error;
if (AtomicTypes_Add(m)) {
error:
Py_DECREF(m);
return NULL;
}
return m;
}