-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathplacement_new.cpp
More file actions
187 lines (165 loc) · 5.97 KB
/
Copy pathplacement_new.cpp
File metadata and controls
187 lines (165 loc) · 5.97 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
//
// Created by Paul Ross on 06/09/2022.
//
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
#include <string>
#include <iostream>
/**
* A simple class that contains a string but reports its method calls.
*/
class Verbose {
public:
Verbose() : Verbose("Default") {
std::cout << "Default constructor at " << std::hex << (void *) this << std::dec;
std::cout << " with argument \"" << m_str << "\"" << std::endl;
}
/// Constructor reserves 256MB to illustrate memory usage visible in the process RSS.
explicit Verbose(const std::string &str) : m_str(str), m_buffer(1024 * 1024 * 256, ' ') {
std::cout << "Constructor at " << std::hex << (void *) this << std::dec;
std::cout << " with argument \"" << m_str << "\"" << " buffer len: " << m_buffer.size() << std::endl;
}
Verbose &operator=(const Verbose &rhs) {
std::cout << "operator= at " << std::hex << (void *) this << std::dec;
std::cout << " m_str: \"" << m_str << "\"";
std::cout << " rhs at " << std::hex << (void *) &rhs << std::dec;
std::cout << " rhs.m_str: \"" << rhs.m_str << "\"" << std::endl;
if (this != &rhs) {
m_str = rhs.m_str;
}
return *this;
}
void print(const char *message = NULL) {
if (message) {
std::cout << message << ": Verbose object at " << std::hex << (void *) this << std::dec;
std::cout << " m_str: \"" << m_str << "\"" << std::endl;
} else {
std::cout << " Verbose object at " << std::hex << (void *) this << std::dec;
std::cout << " m_str: \"" << m_str << "\"" << std::endl;
}
}
[[nodiscard]] ssize_t buffer_size() const {
return sizeof(Verbose) + 2 * sizeof(std::string) + m_str.size() + m_buffer.size();
}
~Verbose() {
std::cout << "Destructor at " << std::hex << (void *) this << std::dec;
std::cout << " m_str: \"" << m_str << "\"" << std::endl;
}
private:
std::string m_str;
// m_buffer is just a large string to provoke the memory manager and detect leaks.
std::string m_buffer;
};
typedef struct {
PyObject_HEAD
Verbose Attr;
Verbose *pAttr;
} CppCtorDtorInPyObject;
static PyObject *
CppCtorDtorInPyObject_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwds)) {
printf("-- %s()\n", __FUNCTION__);
CppCtorDtorInPyObject *self;
self = (CppCtorDtorInPyObject *) type->tp_alloc(type, 0);
if (self != NULL) {
// Placement new used for direct allocation.
new(&self->Attr) Verbose;
self->Attr.print("Initial self->Attr");
// Dynamically allocated new.
self->pAttr = new Verbose("pAttr");
if (self->pAttr == NULL) {
Py_DECREF(self);
return NULL;
} else {
self->pAttr->print("Initial self->pAttr");
}
}
return (PyObject *) self;
}
//static int
//CppCtorDtorInPyObject_init(CppCtorDtorInPyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args),
// PyObject *Py_UNUSED(kwds)) {
// printf("-- %s()\n", __FUNCTION__);
// return 0;
//}
static void
CppCtorDtorInPyObject_dealloc(CppCtorDtorInPyObject *self) {
printf("-- %s()\n", __FUNCTION__);
self->Attr.print("self->Attr before delete");
// For self->Attr call the destructor directly.
self->Attr.~Verbose();
// delete (&self->Attr);// self->Attr;
// ::operator delete (&self->Attr);// self->Attr;
self->pAttr->print("self->pAttr before delete");
// For self->pAttr use delete.
delete self->pAttr;
Py_TYPE(self)->tp_free((PyObject *) self);
}
static PyObject *
CppCtorDtorInPyObject_print(CppCtorDtorInPyObject *self, PyObject *Py_UNUSED(ignored)) {
printf("-- %s()\n", __FUNCTION__);
self->Attr.print("self->Attr");
self->pAttr->print("self->pAttr");
Py_RETURN_NONE;
}
/// Best guess of the size of the Verbose object(s).
static PyObject *
CppCtorDtorInPyObject_buffer_size(CppCtorDtorInPyObject *self, PyObject *Py_UNUSED(ignored)) {
printf("-- %s()\n", __FUNCTION__);
Py_ssize_t ret = 0;
ret += self->Attr.buffer_size();
ret += self->pAttr->buffer_size();
return Py_BuildValue("n", ret);
}
static PyMethodDef CppCtorDtorInPyObject_methods[] = {
{
"print",
(PyCFunction) CppCtorDtorInPyObject_print,
METH_NOARGS,
"Print the contents of the object."
},
{
"buffer_size",
(PyCFunction) CppCtorDtorInPyObject_buffer_size,
METH_NOARGS,
"The memory usage of the object."
},
{NULL, NULL, 0, NULL} /* Sentinel */
};
static PyTypeObject CppCtorDtorInPyObjectType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "CppCtorDtorInPyObject",
.tp_basicsize = sizeof(CppCtorDtorInPyObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor) CppCtorDtorInPyObject_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = "CppCtorDtorInPyObject object",
.tp_methods = CppCtorDtorInPyObject_methods,
// .tp_init = (initproc) CppCtorDtorInPyObject_init,
.tp_new = CppCtorDtorInPyObject_new,
};
static PyModuleDef placement_new_module = {
PyModuleDef_HEAD_INIT,
.m_name = "placement_new",
.m_doc = "Example module that creates an C++ extension type containing custom objects.",
.m_size = -1,
};
PyMODINIT_FUNC
PyInit_placement_new(void) {
// printf("-- %s()\n", __FUNCTION__);
PyObject * m = PyModule_Create(&placement_new_module);
if (m == NULL) {
return NULL;
}
if (PyType_Ready(&CppCtorDtorInPyObjectType) < 0) {
Py_DECREF(m);
return NULL;
}
Py_INCREF(&CppCtorDtorInPyObjectType);
if (PyModule_AddObject(m, "CppCtorDtorInPyObject", (PyObject *) &CppCtorDtorInPyObjectType) < 0) {
Py_DECREF(&CppCtorDtorInPyObjectType);
Py_DECREF(m);
return NULL;
}
return m;
}