pymem.h revision ddea208be9e2a8fa281e25ebbc890378dd2aa286
1
2/* Lowest-level memory allocation interface */
3
4#ifndef Py_PYMEM_H
5#define Py_PYMEM_H
6
7#include "pyport.h"
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13/* BEWARE:
14
15   Each interface exports both functions and macros. Extension modules
16   should normally use the functions for ensuring binary compatibility
17   of the user's code across Python versions. Subsequently, if the
18   Python runtime switches to its own malloc (different from standard
19   malloc), no recompilation is required for the extensions.
20
21   The macro versions trade compatibility for speed. They can be used
22   whenever there is a performance problem, but their use implies
23   recompilation of the code for each new Python release. The Python
24   core uses the macros because it *is* compiled on every upgrade.
25   This might not be the case with 3rd party extensions in a custom
26   setup (for example, a customer does not always have access to the
27   source of 3rd party deliverables). You have been warned! */
28
29/*
30 * Raw memory interface
31 * ====================
32 */
33
34/* To make sure the interpreter is user-malloc friendly, all memory
35   APIs are implemented on top of this one. */
36
37/* Functions */
38
39/* Function wrappers around PyMem_MALLOC and friends; useful if you
40   need to be sure that you are using the same memory allocator as
41   Python.  Note that the wrappers make sure that allocating 0 bytes
42   returns a non-NULL pointer, even if the underlying malloc
43   doesn't. Returned pointers must be checked for NULL explicitly.
44   No action is performed on failure. */
45extern DL_IMPORT(void *) PyMem_Malloc(size_t);
46extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t);
47extern DL_IMPORT(void) PyMem_Free(void *);
48
49/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
50   no longer supported. They used to call PyErr_NoMemory() on failure. */
51
52/* Macros (override these if you want to a different malloc */
53#ifndef PyMem_MALLOC
54#define PyMem_MALLOC(n)         malloc(n)
55#define PyMem_REALLOC(p, n)     realloc((void *)(p), (n))
56#define PyMem_FREE(p)           free((void *)(p))
57#endif
58
59/*
60 * Type-oriented memory interface
61 * ==============================
62 */
63
64/* Functions */
65#define PyMem_New(type, n) \
66	( (type *) PyMem_Malloc((n) * sizeof(type)) )
67#define PyMem_Resize(p, type, n) \
68	( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
69#define PyMem_Del(p) PyMem_Free(p)
70
71/* Macros */
72#define PyMem_NEW(type, n) \
73	( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
74
75/* See comment near MALLOC_ZERO_RETURNS_NULL in pyport.h. */
76#define PyMem_RESIZE(p, type, n)			\
77	do {						\
78		size_t _sum = (n) * sizeof(type);	\
79		if (!_sum)				\
80			_sum = 1;			\
81		(p) = (type *)((p) ?			\
82			       PyMem_REALLOC(p, _sum) :	\
83			       PyMem_MALLOC(_sum));	\
84	} while (0)
85
86#define PyMem_DEL(p) PyMem_FREE(p)
87
88/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
89   it is recommended to write the test explicitly in the code.
90   Note that according to ANSI C, free(NULL) has no effect. */
91
92
93/* pymalloc (private to the interpreter) */
94#ifdef WITH_PYMALLOC
95DL_IMPORT(void *) _PyMalloc_Malloc(size_t nbytes);
96DL_IMPORT(void *) _PyMalloc_Realloc(void *p, size_t nbytes);
97DL_IMPORT(void) _PyMalloc_Free(void *p);
98
99#ifdef PYMALLOC_DEBUG
100DL_IMPORT(void *) _PyMalloc_DebugMalloc(size_t nbytes, int family);
101DL_IMPORT(void *) _PyMalloc_DebugRealloc(void *p, size_t nbytes, int family);
102DL_IMPORT(void) _PyMalloc_DebugFree(void *p, int family);
103DL_IMPORT(void) _PyMalloc_DebugDumpAddress(const void *p);
104DL_IMPORT(void) _PyMalloc_DebugCheckAddress(const void *p);
105#define _PyMalloc_MALLOC(N) _PyMalloc_DebugMalloc(N, 0)
106#define _PyMalloc_REALLOC(P, N) _PyMalloc_DebugRealloc(P, N, 0)
107#define _PyMalloc_FREE(P) _PyMalloc_DebugFree(P, 0)
108
109#else	/* WITH_PYMALLOC && ! PYMALLOC_DEBUG */
110#define _PyMalloc_MALLOC _PyMalloc_Malloc
111#define _PyMalloc_REALLOC _PyMalloc_Realloc
112#define _PyMalloc_FREE _PyMalloc_Free
113#endif
114
115#else	/* ! WITH_PYMALLOC */
116#define _PyMalloc_MALLOC PyMem_MALLOC
117#define _PyMalloc_REALLOC PyMem_REALLOC
118#define _PyMalloc_FREE PyMem_FREE
119#endif	/* WITH_PYMALLOC */
120
121
122#ifdef __cplusplus
123}
124#endif
125
126#endif /* !Py_PYMEM_H */
127