blob: b9c5d2c3da9371a7f3d914405a2b3651225aa895 [file] [log] [blame]
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +00001
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
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000013/* 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
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000034/* To make sure the interpreter is user-malloc friendly, all memory
35 APIs are implemented on top of this one. */
36
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000037/* Functions */
38
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000039/* Function wrappers around PyMem_MALLOC and friends; useful if you
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000040 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
Neil Schemenauer25f3dc22002-03-18 21:06:21 +000052/* 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
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000058
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) \
Vladimir Marangozovdcb45c32000-08-13 11:59:08 +000068 ( (p) = (type *) PyMem_Realloc((p), (n) * sizeof(type)) )
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000069#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)) )
Tim Petersa5d78cc2002-03-02 08:43:19 +000074
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
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000086#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
Tim Petersddea2082002-03-23 10:03:50 +000092
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000093#ifdef __cplusplus
94}
95#endif
96
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000097#endif /* !Py_PYMEM_H */