Mercurial > cpython-lru_cache-weakref
annotate Include/objimpl.h @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | 0872257752c2 |
| children |
| rev | line source |
|---|---|
| 23297 | 1 /* The PyObject_ memory family: high-level object memory interfaces. |
| 2 See pymem.h for the low-level PyMem_ family. | |
| 3 */ | |
| 145 | 4 |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
5 #ifndef Py_OBJIMPL_H |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
6 #define Py_OBJIMPL_H |
|
13776
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13761
diff
changeset
|
7 |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13761
diff
changeset
|
8 #include "pymem.h" |
|
33df3b516711
patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13761
diff
changeset
|
9 |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
10 #ifdef __cplusplus |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
11 extern "C" { |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
12 #endif |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13265
diff
changeset
|
13 |
| 23297 | 14 /* BEWARE: |
| 15 | |
| 16 Each interface exports both functions and macros. Extension modules should | |
| 17 use the functions, to ensure binary compatibility across Python versions. | |
| 18 Because the Python implementation is free to change internal details, and | |
| 19 the macros may (or may not) expose details for speed, if you do use the | |
| 20 macros you must recompile your extensions with each Python release. | |
| 21 | |
| 22 Never mix calls to PyObject_ memory functions with calls to the platform | |
| 23 malloc/realloc/ calloc/free, or with calls to PyMem_. | |
| 24 */ | |
| 25 | |
| 6 | 26 /* |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
27 Functions and macros for modules that implement new object types. |
| 6 | 28 |
| 23297 | 29 - PyObject_New(type, typeobj) allocates memory for a new object of the given |
| 30 type, and initializes part of it. 'type' must be the C structure type used | |
| 31 to represent the object, and 'typeobj' the address of the corresponding | |
| 32 type object. Reference count and type pointer are filled in; the rest of | |
| 33 the bytes of the object are *undefined*! The resulting expression type is | |
| 34 'type *'. The size of the object is determined by the tp_basicsize field | |
| 35 of the type object. | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
36 |
| 23297 | 37 - PyObject_NewVar(type, typeobj, n) is similar but allocates a variable-size |
| 38 object with room for n items. In addition to the refcount and type pointer | |
| 39 fields, this also fills in the ob_size field. | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
40 |
| 23297 | 41 - PyObject_Del(op) releases the memory allocated for an object. It does not |
| 42 run a destructor -- it only frees the memory. PyObject_Free is identical. | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
43 |
| 23297 | 44 - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) don't |
| 45 allocate memory. Instead of a 'type' parameter, they take a pointer to a | |
| 46 new object (allocated by an arbitrary allocator), and initialize its object | |
| 47 header fields. | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
48 |
| 23297 | 49 Note that objects created with PyObject_{New, NewVar} are allocated using the |
| 50 specialized Python allocator (implemented in obmalloc.c), if WITH_PYMALLOC is | |
| 51 enabled. In addition, a special debugging allocator is used if PYMALLOC_DEBUG | |
| 52 is also #defined. | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
53 |
| 23297 | 54 In case a specific form of memory management is needed (for example, if you |
| 55 must use the platform malloc heap(s), or shared memory, or C++ local storage or | |
| 56 operator new), you must first allocate the object with your custom allocator, | |
| 57 then pass its pointer to PyObject_{Init, InitVar} for filling in its Python- | |
| 58 specific fields: reference count, type pointer, possibly others. You should | |
| 59 be aware that Python no control over these objects because they don't | |
| 60 cooperate with the Python memory manager. Such objects may not be eligible | |
| 61 for automatic garbage collection and you have to make sure that they are | |
| 62 released accordingly whenever their destructor gets called (cf. the specific | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
63 form of memory management you're using). |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
64 |
| 23297 | 65 Unless you have specific memory management requirements, use |
| 66 PyObject_{New, NewVar, Del}. | |
| 67 */ | |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
68 |
|
20294
426b379a138b
_PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents:
20293
diff
changeset
|
69 /* |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
70 * Raw object memory interface |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
71 * =========================== |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
72 */ |
| 6 | 73 |
|
22984
c5a67f50532e
First cut at repairing out-of-date comments; make alignment of macro defs
Tim Peters <tim.peters@gmail.com>
parents:
22970
diff
changeset
|
74 /* Functions to call the same malloc/realloc/free as used by Python's |
|
c5a67f50532e
First cut at repairing out-of-date comments; make alignment of macro defs
Tim Peters <tim.peters@gmail.com>
parents:
22970
diff
changeset
|
75 object allocator. If WITH_PYMALLOC is enabled, these may differ from |
|
c5a67f50532e
First cut at repairing out-of-date comments; make alignment of macro defs
Tim Peters <tim.peters@gmail.com>
parents:
22970
diff
changeset
|
76 the platform malloc/realloc/free. The Python object allocator is |
|
c5a67f50532e
First cut at repairing out-of-date comments; make alignment of macro defs
Tim Peters <tim.peters@gmail.com>
parents:
22970
diff
changeset
|
77 designed for fast, cache-conscious allocation of many "small" objects, |
| 23297 | 78 and with low hidden memory overhead. |
| 79 | |
| 80 PyObject_Malloc(0) returns a unique non-NULL pointer if possible. | |
| 81 | |
| 82 PyObject_Realloc(NULL, n) acts like PyObject_Malloc(n). | |
| 83 PyObject_Realloc(p != NULL, 0) does not return NULL, or free the memory | |
| 84 at p. | |
| 85 | |
| 86 Returned pointers must be checked for NULL explicitly; no action is | |
| 87 performed on failure other than to return NULL (no warning it printed, no | |
| 88 exception is set, etc). | |
| 89 | |
| 90 For allocating objects, use PyObject_{New, NewVar} instead whenever | |
| 91 possible. The PyObject_{Malloc, Realloc, Free} family is exposed | |
| 92 so that you can exploit Python's small-block allocator for non-object | |
| 93 uses. If you must use these routines to allocate object memory, make sure | |
| 94 the object gets initialized via PyObject_{Init, InitVar} after obtaining | |
| 95 the raw memory. | |
| 96 */ | |
|
84471
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
97 PyAPI_FUNC(void *) PyObject_Malloc(size_t size); |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
98 PyAPI_FUNC(void *) PyObject_Realloc(void *ptr, size_t new_size); |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
99 PyAPI_FUNC(void) PyObject_Free(void *ptr); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
100 |
|
80777
c40f4c19d20b
Issue #13390: New function :func:`sys.getallocatedblocks()` returns the number of memory blocks currently allocated.
Antoine Pitrou <solipsis@pitrou.net>
parents:
79065
diff
changeset
|
101 /* This function returns the number of allocated memory blocks, regardless of size */ |
|
c40f4c19d20b
Issue #13390: New function :func:`sys.getallocatedblocks()` returns the number of memory blocks currently allocated.
Antoine Pitrou <solipsis@pitrou.net>
parents:
79065
diff
changeset
|
102 PyAPI_FUNC(Py_ssize_t) _Py_GetAllocatedBlocks(void); |
|
22970
f93a26d1bfa0
Remove PyMalloc_* symbols. PyObject_Malloc now uses pymalloc if
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22632
diff
changeset
|
103 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
104 /* Macros */ |
|
22970
f93a26d1bfa0
Remove PyMalloc_* symbols. PyObject_Malloc now uses pymalloc if
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22632
diff
changeset
|
105 #ifdef WITH_PYMALLOC |
|
77577
d63a80abfbec
Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents:
68257
diff
changeset
|
106 #ifndef Py_LIMITED_API |
|
d63a80abfbec
Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents:
68257
diff
changeset
|
107 PyAPI_FUNC(void) _PyObject_DebugMallocStats(FILE *out); |
|
d63a80abfbec
Issue #14785: Add sys._debugmallocstats() to help debug low-level memory allocation issues
David Malcolm <dmalcolm@redhat.com>
parents:
68257
diff
changeset
|
108 #endif /* #ifndef Py_LIMITED_API */ |
|
84471
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
109 #endif |
|
22970
f93a26d1bfa0
Remove PyMalloc_* symbols. PyObject_Malloc now uses pymalloc if
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22632
diff
changeset
|
110 |
|
84471
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
111 /* Macros */ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
112 #define PyObject_MALLOC PyObject_Malloc |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
113 #define PyObject_REALLOC PyObject_Realloc |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
114 #define PyObject_FREE PyObject_Free |
|
84471
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
115 #define PyObject_Del PyObject_Free |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
116 #define PyObject_DEL PyObject_Free |
|
22970
f93a26d1bfa0
Remove PyMalloc_* symbols. PyObject_Malloc now uses pymalloc if
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22632
diff
changeset
|
117 |
|
f93a26d1bfa0
Remove PyMalloc_* symbols. PyObject_Malloc now uses pymalloc if
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22632
diff
changeset
|
118 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
119 /* |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
120 * Generic object allocator interface |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
121 * ================================== |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
122 */ |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
123 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
124 /* Functions */ |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24121
diff
changeset
|
125 PyAPI_FUNC(PyObject *) PyObject_Init(PyObject *, PyTypeObject *); |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24121
diff
changeset
|
126 PyAPI_FUNC(PyVarObject *) PyObject_InitVar(PyVarObject *, |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36415
diff
changeset
|
127 PyTypeObject *, Py_ssize_t); |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24121
diff
changeset
|
128 PyAPI_FUNC(PyObject *) _PyObject_New(PyTypeObject *); |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36415
diff
changeset
|
129 PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t); |
| 6 | 130 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
131 #define PyObject_New(type, typeobj) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
132 ( (type *) _PyObject_New(typeobj) ) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
133 #define PyObject_NewVar(type, typeobj, n) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
134 ( (type *) _PyObject_NewVar((typeobj), (n)) ) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
135 |
| 14006 | 136 /* Macros trading binary compatibility for speed. See also pymem.h. |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
137 Note that these macros expect non-NULL object pointers.*/ |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
138 #define PyObject_INIT(op, typeobj) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
139 ( Py_TYPE(op) = (typeobj), _Py_NewReference((PyObject *)(op)), (op) ) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
140 #define PyObject_INIT_VAR(op, typeobj, size) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
141 ( Py_SIZE(op) = (size), PyObject_INIT((op), (typeobj)) ) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
142 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
143 #define _PyObject_SIZE(typeobj) ( (typeobj)->tp_basicsize ) |
|
20294
426b379a138b
_PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents:
20293
diff
changeset
|
144 |
|
20296
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
145 /* _PyObject_VAR_SIZE returns the number of bytes (as size_t) allocated for a |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
146 vrbl-size object with nitems items, exclusive of gc overhead (if any). The |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
147 value is rounded up to the closest multiple of sizeof(void *), in order to |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
148 ensure that pointer fields at the end of the object are correctly aligned |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
149 for the platform (this is of special importance for subclasses of, e.g., |
|
85418
4d62a62ba44d
Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents:
79065
diff
changeset
|
150 str or int, so that pointers can be stored after the embedded data). |
|
20294
426b379a138b
_PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents:
20293
diff
changeset
|
151 |
|
20296
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
152 Note that there's no memory wastage in doing this, as malloc has to |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
153 return (at worst) pointer-aligned memory anyway. |
|
20294
426b379a138b
_PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents:
20293
diff
changeset
|
154 */ |
|
20296
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
155 #if ((SIZEOF_VOID_P - 1) & SIZEOF_VOID_P) != 0 |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
156 # error "_PyObject_VAR_SIZE requires SIZEOF_VOID_P be a power of 2" |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
157 #endif |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
158 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
159 #define _PyObject_VAR_SIZE(typeobj, nitems) \ |
|
79065
99112b851b25
Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77577
diff
changeset
|
160 _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \ |
|
99112b851b25
Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77577
diff
changeset
|
161 (nitems)*(typeobj)->tp_itemsize, \ |
|
99112b851b25
Issue #15144: Fix possible integer overflow when handling pointers as integer values, by using Py_uintptr_t instead of size_t.
Antoine Pitrou <solipsis@pitrou.net>
parents:
77577
diff
changeset
|
162 SIZEOF_VOID_P) |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
163 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
164 #define PyObject_NEW(type, typeobj) \ |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
165 ( (type *) PyObject_Init( \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
166 (PyObject *) PyObject_MALLOC( _PyObject_SIZE(typeobj) ), (typeobj)) ) |
|
20294
426b379a138b
_PyObject_VAR_SIZE: always round up to a multiple-of-pointer-size value.
Tim Peters <tim.peters@gmail.com>
parents:
20293
diff
changeset
|
167 |
|
20296
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
168 #define PyObject_NEW_VAR(type, typeobj, n) \ |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
169 ( (type *) PyObject_InitVar( \ |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
170 (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE((typeobj),(n)) ),\ |
|
6cc0b48049ec
Guido suggests, and I agree, to insist that SIZEOF_VOID_P be a power of 2.
Tim Peters <tim.peters@gmail.com>
parents:
20294
diff
changeset
|
171 (typeobj), (n)) ) |
|
1329
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
172 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
173 /* This example code implements an object constructor with a custom |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
174 allocator, where PyObject_New is inlined, and shows the important |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
175 distinction between two steps (at least): |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
176 1) the actual allocation of the object storage; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
177 2) the initialization of the Python specific fields |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
178 in this storage with PyObject_{Init, InitVar}. |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
179 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
180 PyObject * |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
181 YourObject_New(...) |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
182 { |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
183 PyObject *op; |
| 3764 | 184 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
185 op = (PyObject *) Your_Allocator(_PyObject_SIZE(YourTypeStruct)); |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
186 if (op == NULL) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
187 return PyErr_NoMemory(); |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
188 |
| 23297 | 189 PyObject_Init(op, &YourTypeStruct); |
| 3764 | 190 |
|
12576
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
191 op->ob_field = value; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
192 ... |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
193 return op; |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
194 } |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
195 |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
196 Note that in C++, the use of the new operator usually implies that |
|
1c7c2dd1beb1
Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents:
11891
diff
changeset
|
197 the 1st step is performed automatically for you, so in a C++ class |
| 23297 | 198 constructor you would start directly with PyObject_Init/InitVar |
| 199 */ | |
| 3764 | 200 |
|
84471
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
201 #ifndef Py_LIMITED_API |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
202 typedef struct { |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
203 /* user context passed as the first argument to the 2 functions */ |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
204 void *ctx; |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
205 |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
206 /* allocate an arena of size bytes */ |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
207 void* (*alloc) (void *ctx, size_t size); |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
208 |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
209 /* free an arena */ |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
210 void (*free) (void *ctx, void *ptr, size_t size); |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
211 } PyObjectArenaAllocator; |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
212 |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
213 /* Get the arena allocator. */ |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
214 PyAPI_FUNC(void) PyObject_GetArenaAllocator(PyObjectArenaAllocator *allocator); |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
215 |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
216 /* Set the arena allocator. */ |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
217 PyAPI_FUNC(void) PyObject_SetArenaAllocator(PyObjectArenaAllocator *allocator); |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
218 #endif |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
219 |
|
ca78c974e938
Issue #3329: Implement the PEP 445
Victor Stinner <victor.stinner@gmail.com>
parents:
84131
diff
changeset
|
220 |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
221 /* |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
222 * Garbage Collection Support |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
223 * ========================== |
|
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
224 */ |
|
12993
8f79c5b69e0a
part 2 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
225 |
|
28651
7fda09f751ad
- New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents:
24752
diff
changeset
|
226 /* C equivalent of gc.collect(). */ |
|
36839
7c1aca8e3d7d
Make PyGC_Collect() use Py_ssize_t.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36769
diff
changeset
|
227 PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void); |
|
28651
7fda09f751ad
- New C API PyGC_Collect(), same as calling gc.collect().
Guido van Rossum <guido@python.org>
parents:
24752
diff
changeset
|
228 |
|
83835
5abe85aefe29
Issue #17937: Try harder to collect cyclic garbage at shutdown.
Antoine Pitrou <solipsis@pitrou.net>
parents:
80819
diff
changeset
|
229 #ifndef Py_LIMITED_API |
|
5abe85aefe29
Issue #17937: Try harder to collect cyclic garbage at shutdown.
Antoine Pitrou <solipsis@pitrou.net>
parents:
80819
diff
changeset
|
230 PyAPI_FUNC(Py_ssize_t) _PyGC_CollectNoFail(void); |
|
5abe85aefe29
Issue #17937: Try harder to collect cyclic garbage at shutdown.
Antoine Pitrou <solipsis@pitrou.net>
parents:
80819
diff
changeset
|
231 #endif |
|
5abe85aefe29
Issue #17937: Try harder to collect cyclic garbage at shutdown.
Antoine Pitrou <solipsis@pitrou.net>
parents:
80819
diff
changeset
|
232 |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
233 /* Test if a type has a GC head */ |
|
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
234 #define PyType_IS_GC(t) PyType_HasFeature((t), Py_TPFLAGS_HAVE_GC) |
|
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
235 |
|
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
236 /* Test if an object has a GC head */ |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
42673
diff
changeset
|
237 #define PyObject_IS_GC(o) (PyType_IS_GC(Py_TYPE(o)) && \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
238 (Py_TYPE(o)->tp_is_gc == NULL || Py_TYPE(o)->tp_is_gc(o))) |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
239 |
|
36524
4c8606191c0c
Change _PyObject_GC_Resize to expect Py_ssize_t.
Martin v. Löwis <martin@v.loewis.de>
parents:
36501
diff
changeset
|
240 PyAPI_FUNC(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, Py_ssize_t); |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
241 #define PyObject_GC_Resize(type, op, n) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
242 ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) ) |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
243 |
| 23297 | 244 /* GC information is stored BEFORE the object structure. */ |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
61466
diff
changeset
|
245 #ifndef Py_LIMITED_API |
|
20381
b4f829941f3d
SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents:
20296
diff
changeset
|
246 typedef union _gc_head { |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
247 struct { |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
248 union _gc_head *gc_next; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
249 union _gc_head *gc_prev; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
250 Py_ssize_t gc_refs; |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
251 } gc; |
|
80819
d61a1be9f5c5
Using 'long double' to force this structure to be worst case aligned is no
Gregory P. Smith <greg@krypto.org>
parents:
80777
diff
changeset
|
252 double dummy; /* force worst-case alignment */ |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
253 } PyGC_Head; |
|
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
254 |
|
23378
4d6beab766e2
_PyGC_generation0 is now a pointer
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
23297
diff
changeset
|
255 extern PyGC_Head *_PyGC_generation0; |
|
12993
8f79c5b69e0a
part 2 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
256 |
|
22632
349404426faa
Add _Py_AS_GC macro. It will be used by the trashcan code on object.c.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22444
diff
changeset
|
257 #define _Py_AS_GC(o) ((PyGC_Head *)(o)-1) |
|
349404426faa
Add _Py_AS_GC macro. It will be used by the trashcan code on object.c.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22444
diff
changeset
|
258 |
|
84911
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
259 /* Bit 0 is set when tp_finalize is called */ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
260 #define _PyGC_REFS_MASK_FINALIZED (1 << 0) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
261 /* The (N-1) most significant bits contain the gc state / refcount */ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
262 #define _PyGC_REFS_SHIFT (1) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
263 #define _PyGC_REFS_MASK (((size_t) -1) << _PyGC_REFS_SHIFT) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
264 |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
265 #define _PyGCHead_REFS(g) ((g)->gc.gc_refs >> _PyGC_REFS_SHIFT) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
266 #define _PyGCHead_SET_REFS(g, v) do { \ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
267 (g)->gc.gc_refs = ((g)->gc.gc_refs & ~_PyGC_REFS_MASK) \ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
268 | (v << _PyGC_REFS_SHIFT); \ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
269 } while (0) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
270 #define _PyGCHead_DECREF(g) ((g)->gc.gc_refs -= 1 << _PyGC_REFS_SHIFT) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
271 |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
272 #define _PyGCHead_FINALIZED(g) (((g)->gc.gc_refs & _PyGC_REFS_MASK_FINALIZED) != 0) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
273 #define _PyGCHead_SET_FINALIZED(g, v) do { \ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
274 (g)->gc.gc_refs = ((g)->gc.gc_refs & ~_PyGC_REFS_MASK_FINALIZED) \ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
275 | (v != 0); \ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
276 } while (0) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
277 |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
278 #define _PyGC_FINALIZED(o) _PyGCHead_FINALIZED(_Py_AS_GC(o)) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
279 #define _PyGC_SET_FINALIZED(o, v) _PyGCHead_SET_FINALIZED(_Py_AS_GC(o), v) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
280 |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
281 #define _PyGC_REFS(o) _PyGCHead_REFS(_Py_AS_GC(o)) |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
282 |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
283 #define _PyGC_REFS_UNTRACKED (-2) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
284 #define _PyGC_REFS_REACHABLE (-3) |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
285 #define _PyGC_REFS_TENTATIVELY_UNREACHABLE (-4) |
|
24077
a5162fd65c3d
Reserved another gc_refs value for untracked objects. Every live gc
Tim Peters <tim.peters@gmail.com>
parents:
23378
diff
changeset
|
286 |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
287 /* Tell the GC to track this object. NB: While the object is tracked the |
|
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
288 * collector it must be safe to call the ob_traverse method. */ |
|
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
289 #define _PyObject_GC_TRACK(o) do { \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
290 PyGC_Head *g = _Py_AS_GC(o); \ |
|
84911
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
291 if (_PyGCHead_REFS(g) != _PyGC_REFS_UNTRACKED) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
292 Py_FatalError("GC object already tracked"); \ |
|
84911
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
293 _PyGCHead_SET_REFS(g, _PyGC_REFS_REACHABLE); \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
294 g->gc.gc_next = _PyGC_generation0; \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
295 g->gc.gc_prev = _PyGC_generation0->gc.gc_prev; \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
296 g->gc.gc_prev->gc.gc_next = g; \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
297 _PyGC_generation0->gc.gc_prev = g; \ |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
298 } while (0); |
|
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
299 |
|
24085
22d2f037af9d
Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents:
24077
diff
changeset
|
300 /* Tell the GC to stop tracking this object. |
|
22d2f037af9d
Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents:
24077
diff
changeset
|
301 * gc_next doesn't need to be set to NULL, but doing so is a good |
|
22d2f037af9d
Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents:
24077
diff
changeset
|
302 * way to provoke memory errors if calling code is confused. |
|
22d2f037af9d
Finished transitioning to using gc_refs to track gc objects' states.
Tim Peters <tim.peters@gmail.com>
parents:
24077
diff
changeset
|
303 */ |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
304 #define _PyObject_GC_UNTRACK(o) do { \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
305 PyGC_Head *g = _Py_AS_GC(o); \ |
|
84911
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
306 assert(_PyGCHead_REFS(g) != _PyGC_REFS_UNTRACKED); \ |
|
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
307 _PyGCHead_SET_REFS(g, _PyGC_REFS_UNTRACKED); \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
308 g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
309 g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
310 g->gc.gc_next = NULL; \ |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
311 } while (0); |
|
13183
54bffdddc0ef
final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12993
diff
changeset
|
312 |
|
52291
94bd0666ffa7
Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
44351
diff
changeset
|
313 /* True if the object is currently tracked by the GC. */ |
|
94bd0666ffa7
Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
44351
diff
changeset
|
314 #define _PyObject_GC_IS_TRACKED(o) \ |
|
84911
3f994367a979
Issue #18112: PEP 442 implementation (safe object finalization).
Antoine Pitrou <solipsis@pitrou.net>
parents:
84471
diff
changeset
|
315 (_PyGC_REFS(o) != _PyGC_REFS_UNTRACKED) |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
316 |
|
52291
94bd0666ffa7
Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
44351
diff
changeset
|
317 /* True if the object may be tracked by the GC in the future, or already is. |
|
94bd0666ffa7
Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
44351
diff
changeset
|
318 This can be useful to implement some optimizations. */ |
|
94bd0666ffa7
Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
44351
diff
changeset
|
319 #define _PyObject_GC_MAY_BE_TRACKED(obj) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
320 (PyObject_IS_GC(obj) && \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
321 (!PyTuple_CheckExact(obj) || _PyObject_GC_IS_TRACKED(obj))) |
|
66664
62b61abd02b8
Merge branches/pep-0384.
Martin v. Löwis <martin@v.loewis.de>
parents:
61466
diff
changeset
|
322 #endif /* Py_LIMITED_API */ |
|
52291
94bd0666ffa7
Merged revisions 70546 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
44351
diff
changeset
|
323 |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24121
diff
changeset
|
324 PyAPI_FUNC(PyObject *) _PyObject_GC_Malloc(size_t); |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24121
diff
changeset
|
325 PyAPI_FUNC(PyObject *) _PyObject_GC_New(PyTypeObject *); |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
36415
diff
changeset
|
326 PyAPI_FUNC(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, Py_ssize_t); |
|
24752
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24121
diff
changeset
|
327 PyAPI_FUNC(void) PyObject_GC_Track(void *); |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24121
diff
changeset
|
328 PyAPI_FUNC(void) PyObject_GC_UnTrack(void *); |
|
c1eb668525e1
Excise DL_EXPORT from Include.
Mark Hammond <mhammond@skippinet.com.au>
parents:
24121
diff
changeset
|
329 PyAPI_FUNC(void) PyObject_GC_Del(void *); |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
330 |
|
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
331 #define PyObject_GC_New(type, typeobj) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
332 ( (type *) _PyObject_GC_New(typeobj) ) |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
333 #define PyObject_GC_NewVar(type, typeobj, n) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
334 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) ) |
|
22970
f93a26d1bfa0
Remove PyMalloc_* symbols. PyObject_Malloc now uses pymalloc if
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
22632
diff
changeset
|
335 |
|
19577
fbd49d1717c4
Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents:
18956
diff
changeset
|
336 |
|
32678
cada2e9b685c
Formalize that the Py_VISIT macro requires that the tp_traverse
Tim Peters <tim.peters@gmail.com>
parents:
32671
diff
changeset
|
337 /* Utility macro to help write tp_traverse functions. |
|
cada2e9b685c
Formalize that the Py_VISIT macro requires that the tp_traverse
Tim Peters <tim.peters@gmail.com>
parents:
32671
diff
changeset
|
338 * To use this macro, the tp_traverse function must name its arguments |
|
cada2e9b685c
Formalize that the Py_VISIT macro requires that the tp_traverse
Tim Peters <tim.peters@gmail.com>
parents:
32671
diff
changeset
|
339 * "visit" and "arg". This is intended to keep tp_traverse functions |
|
cada2e9b685c
Formalize that the Py_VISIT macro requires that the tp_traverse
Tim Peters <tim.peters@gmail.com>
parents:
32671
diff
changeset
|
340 * looking as much alike as possible. |
|
cada2e9b685c
Formalize that the Py_VISIT macro requires that the tp_traverse
Tim Peters <tim.peters@gmail.com>
parents:
32671
diff
changeset
|
341 */ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
342 #define Py_VISIT(op) \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
343 do { \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
344 if (op) { \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
345 int vret = visit((PyObject *)(op), arg); \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
346 if (vret) \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
347 return vret; \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
348 } \ |
|
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
349 } while (0) |
|
32671
52beafd936c8
Documented the new Py_VISIT macro to simplify implementation of
Jim Fulton <jim@zope.com>
parents:
28651
diff
changeset
|
350 |
|
12993
8f79c5b69e0a
part 2 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
12576
diff
changeset
|
351 |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16196
diff
changeset
|
352 /* Test if a type supports weak references */ |
|
39222
b0cdcbcc5f45
Get rid of most of the flags (in tp_flags) that keep track of various
Guido van Rossum <guido@python.org>
parents:
37879
diff
changeset
|
353 #define PyType_SUPPORTS_WEAKREFS(t) ((t)->tp_weaklistoffset > 0) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16196
diff
changeset
|
354 |
|
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16196
diff
changeset
|
355 #define PyObject_GET_WEAKREFS_LISTPTR(o) \ |
|
61467
f197407bdba2
Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
52291
diff
changeset
|
356 ((PyObject **) (((char *) (o)) + Py_TYPE(o)->tp_weaklistoffset)) |
|
16385
1fb6672b20ef
PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents:
16196
diff
changeset
|
357 |
|
1329
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
358 #ifdef __cplusplus |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
359 } |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
360 #endif |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
361 #endif /* !Py_OBJIMPL_H */ |
