bpo-35134: Add Include/cpython/pymem.h (GH-12840) · python/cpython@9820c07 (original) (raw)

`@@ -11,21 +11,6 @@

`

11

11

`extern "C" {

`

12

12

`#endif

`

13

13

``

14

``

`-

#ifndef Py_LIMITED_API

`

15

``

`-

PyAPI_FUNC(void *) PyMem_RawMalloc(size_t size);

`

16

``

`-

PyAPI_FUNC(void *) PyMem_RawCalloc(size_t nelem, size_t elsize);

`

17

``

`-

PyAPI_FUNC(void *) PyMem_RawRealloc(void *ptr, size_t new_size);

`

18

``

`-

PyAPI_FUNC(void) PyMem_RawFree(void *ptr);

`

19

``

-

20

``

`-

/* Configure the Python memory allocators. Pass NULL to use default

`

21

``

`-

allocators. */

`

22

``

`-

PyAPI_FUNC(int) _PyMem_SetupAllocators(const char *opt);

`

23

``

-

24

``

`-

/* Try to get the allocators name set by _PyMem_SetupAllocators(). */

`

25

``

`-

PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void);

`

26

``

`-

#endif /* !defined(Py_LIMITED_API) */

`

27

``

-

28

``

-

29

14

`/* BEWARE:

`

30

15

``

31

16

` Each interface exports both functions and macros. Extension modules should

`

`@@ -65,23 +50,9 @@ PyAPI_FUNC(const char*) _PyMem_GetAllocatorsName(void);

`

65

50

`*/

`

66

51

``

67

52

`PyAPI_FUNC(void *) PyMem_Malloc(size_t size);

`

68

``

`-

#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03050000

`

69

``

`-

PyAPI_FUNC(void *) PyMem_Calloc(size_t nelem, size_t elsize);

`

70

``

`-

#endif

`

71

53

`PyAPI_FUNC(void *) PyMem_Realloc(void *ptr, size_t new_size);

`

72

54

`PyAPI_FUNC(void) PyMem_Free(void *ptr);

`

73

55

``

74

``

`-

#ifndef Py_LIMITED_API

`

75

``

`-

/* strdup() using PyMem_RawMalloc() */

`

76

``

`-

PyAPI_FUNC(char *) _PyMem_RawStrdup(const char *str);

`

77

``

-

78

``

`-

/* strdup() using PyMem_Malloc() */

`

79

``

`-

PyAPI_FUNC(char *) _PyMem_Strdup(const char *str);

`

80

``

-

81

``

`-

/* wcsdup() using PyMem_RawMalloc() */

`

82

``

`-

PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);

`

83

``

`-

#endif

`

84

``

-

85

56

`/* Macros. */

`

86

57

``

87

58

`/* PyMem_MALLOC(0) means malloc(1). Some systems would return NULL

`

`@@ -130,72 +101,6 @@ PyAPI_FUNC(wchar_t*) _PyMem_RawWcsdup(const wchar_t *str);

`

130

101

`#define PyMem_Del PyMem_Free

`

131

102

`#define PyMem_DEL PyMem_FREE

`

132

103

``

133

``

`-

#ifndef Py_LIMITED_API

`

134

``

`-

typedef enum {

`

135

``

`-

/* PyMem_RawMalloc(), PyMem_RawRealloc() and PyMem_RawFree() */

`

136

``

`-

PYMEM_DOMAIN_RAW,

`

137

``

-

138

``

`-

/* PyMem_Malloc(), PyMem_Realloc() and PyMem_Free() */

`

139

``

`-

PYMEM_DOMAIN_MEM,

`

140

``

-

141

``

`-

/* PyObject_Malloc(), PyObject_Realloc() and PyObject_Free() */

`

142

``

`-

PYMEM_DOMAIN_OBJ

`

143

``

`-

} PyMemAllocatorDomain;

`

144

``

-

145

``

`-

typedef struct {

`

146

``

`-

/* user context passed as the first argument to the 4 functions */

`

147

``

`-

void *ctx;

`

148

``

-

149

``

`-

/* allocate a memory block */

`

150

``

`-

void* (*malloc) (void *ctx, size_t size);

`

151

``

-

152

``

`-

/* allocate a memory block initialized by zeros */

`

153

``

`-

void* (*calloc) (void *ctx, size_t nelem, size_t elsize);

`

154

``

-

155

``

`-

/* allocate or resize a memory block */

`

156

``

`-

void* (*realloc) (void *ctx, void *ptr, size_t new_size);

`

157

``

-

158

``

`-

/* release a memory block */

`

159

``

`-

void (*free) (void *ctx, void *ptr);

`

160

``

`-

} PyMemAllocatorEx;

`

161

``

-

162

``

`-

/* Get the memory block allocator of the specified domain. */

`

163

``

`-

PyAPI_FUNC(void) PyMem_GetAllocator(PyMemAllocatorDomain domain,

`

164

``

`-

PyMemAllocatorEx *allocator);

`

165

``

-

166

``

`-

/* Set the memory block allocator of the specified domain.

`

167

``

-

168

``

`-

The new allocator must return a distinct non-NULL pointer when requesting

`

169

``

`-

zero bytes.

`

170

``

-

171

``

`-

For the PYMEM_DOMAIN_RAW domain, the allocator must be thread-safe: the GIL

`

172

``

`-

is not held when the allocator is called.

`

173

``

-

174

``

`-

If the new allocator is not a hook (don't call the previous allocator), the

`

175

``

`-

PyMem_SetupDebugHooks() function must be called to reinstall the debug hooks

`

176

``

`-

on top on the new allocator. */

`

177

``

`-

PyAPI_FUNC(void) PyMem_SetAllocator(PyMemAllocatorDomain domain,

`

178

``

`-

PyMemAllocatorEx *allocator);

`

179

``

-

180

``

`-

/* Setup hooks to detect bugs in the following Python memory allocator

`

181

``

`-

functions:

`

182

``

-

183

``

`-

`

184

``

`-

`

185

``

`-

`

186

``

-

187

``

`-

Newly allocated memory is filled with the byte 0xCB, freed memory is filled

`

188

``

`-

with the byte 0xDB. Additional checks:

`

189

``

-

190

``

`-

`

191

``

`-

by PyMem_Malloc()

`

192

``

`-

`

193

``

`-

`

194

``

-

195

``

`-

The function does nothing if Python is not compiled is debug mode. */

`

196

``

`-

PyAPI_FUNC(void) PyMem_SetupDebugHooks(void);

`

197

``

`-

#endif /* Py_LIMITED_API */

`

198

``

-

199

104

`/* bpo-35053: expose _Py_tracemalloc_config for performance:

`

200

105

` _Py_NewReference() needs an efficient check to test if tracemalloc is

`

201

106

` tracing.

`

`@@ -231,6 +136,13 @@ PyAPI_DATA(struct _PyTraceMalloc_Config) _Py_tracemalloc_config;

`

231

136

` .max_nframe = 1, \

`

232

137

` .use_domain = 0}

`

233

138

``

``

139

+

``

140

`+

#ifndef Py_LIMITED_API

`

``

141

`+

define Py_CPYTHON_PYMEM_H

`

``

142

`+

include "cpython/pymem.h"

`

``

143

`+

undef Py_CPYTHON_PYMEM_H

`

``

144

`+

#endif

`

``

145

+

234

146

`#ifdef __cplusplus

`

235

147

`}

`

236

148

`#endif

`