Qucs-S S-parameter Viewer & RF Synthesis Tools
Loading...
Searching...
No Matches
greenlet_allocator.hpp
1#ifndef GREENLET_ALLOCATOR_HPP
2#define GREENLET_ALLOCATOR_HPP
3
4#define PY_SSIZE_T_CLEAN
5#include <Python.h>
6#include <memory>
7#include "greenlet_compiler_compat.hpp"
8#include "greenlet_cpython_compat.hpp"
9
10
11namespace greenlet
12{
13 // This allocator is stateless; all instances are identical.
14 // It can *ONLY* be used when we're sure we're holding the GIL
15 // (Python's allocators require the GIL).
16 template <class T>
17 struct PythonAllocator : public std::allocator<T> {
18
19 PythonAllocator(const PythonAllocator& UNUSED(other))
20 : std::allocator<T>()
21 {
22 }
23
24 PythonAllocator(const std::allocator<T> other)
25 : std::allocator<T>(other)
26 {}
27
28 template <class U>
29 PythonAllocator(const std::allocator<U>& other)
30 : std::allocator<T>(other)
31 {
32 }
33
34 PythonAllocator() : std::allocator<T>() {}
35
36 T* allocate(size_t number_objects, const void* UNUSED(hint)=0)
37 {
38 void* p;
39 if (number_objects == 1) {
40#ifdef Py_GIL_DISABLED
41 p = PyMem_Malloc(sizeof(T) * number_objects);
42#else
43 p = PyObject_Malloc(sizeof(T));
44#endif
45 }
46 else {
47 p = PyMem_Malloc(sizeof(T) * number_objects);
48 }
49 return static_cast<T*>(p);
50 }
51
52 void deallocate(T* t, size_t n)
53 {
54 void* p = t;
55 if (n == 1) {
56#ifdef Py_GIL_DISABLED
57 PyMem_Free(p);
58#else
59 PyObject_Free(p);
60#endif
61 }
62 else {
63 PyMem_Free(p);
64 }
65 }
66 // This member is deprecated in C++17 and removed in C++20
67 template< class U >
68 struct rebind {
70 };
71
72 };
73
74}
75
76#endif
Definition __init__.py:1
Definition greenlet_allocator.hpp:68
Definition greenlet_allocator.hpp:17