annotate Include/objimpl.h @ 20737:00a581bdedeb legacy-trunk

When overriding __str__ or __repr__, set the tp_print slot to NULL.
author Guido van Rossum <guido@python.org>
date Mon, 29 Oct 2001 14:33:44 +0000
parents b4f829941f3d
children 7065135f9202
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1
13445
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
2 #ifndef Py_OBJIMPL_H
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
3 #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
4
33df3b516711 patch from Vladimir (move Py_Mem* interface to Include/pymem.h)
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13761
diff changeset
5 #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
6
13445
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
7 #ifdef __cplusplus
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
8 extern "C" {
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
9 #endif
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
10
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 /*
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
12 Functions and macros for modules that implement new object types.
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 You must first include "object.h".
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
15 - PyObject_New(type, typeobj) allocates memory for a new object of
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
16 the given type; here 'type' must be the C structure type used to
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
17 represent the object and 'typeobj' the address of the corresponding
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
18 type object. Reference count and type pointer are filled in; the
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
19 rest of the bytes of the object are *undefined*! The resulting
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
20 expression type is 'type *'. The size of the object is actually
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
21 determined by the tp_basicsize field of the type object.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
22
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
23 - PyObject_NewVar(type, typeobj, n) is similar but allocates a
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
24 variable-size object with n extra items. The size is computed as
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
25 tp_basicsize plus n * tp_itemsize. This fills in the ob_size field
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
26 as well.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
27
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
28 - PyObject_Del(op) releases the memory allocated for an object.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
29
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
30 - PyObject_Init(op, typeobj) and PyObject_InitVar(op, typeobj, n) are
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
31 similar to PyObject_{New, NewVar} except that they don't allocate
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
32 the memory needed for an object. Instead of the 'type' parameter,
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
33 they accept the pointer of a new object (allocated by an arbitrary
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
34 allocator) and initialize its object header fields.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
35
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
36 Note that objects created with PyObject_{New, NewVar} are allocated
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
37 within the Python heap by an object allocator, the latter being
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
38 implemented (by default) on top of the Python raw memory
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
39 allocator. This ensures that Python keeps control on the user's
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
40 objects regarding their memory management; for instance, they may be
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
41 subject to automatic garbage collection.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
42
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
43 In case a specific form of memory management is needed, implying that
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
44 the objects would not reside in the Python heap (for example standard
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
45 malloc heap(s) are mandatory, use of shared memory, C++ local storage
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
46 or operator new), you must first allocate the object with your custom
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
47 allocator, then pass its pointer to PyObject_{Init, InitVar} for
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
48 filling in its Python-specific fields: reference count, type pointer,
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
49 possibly others. You should be aware that Python has very limited
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
50 control over these objects because they don't cooperate with the
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
51 Python memory manager. Such objects may not be eligible for automatic
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
52 garbage collection and you have to make sure that they are released
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
53 accordingly whenever their destructor gets called (cf. the specific
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
54 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
55
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
56 Unless you have specific memory management requirements, it is
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
57 recommended to use PyObject_{New, NewVar, Del}. */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
58
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
59 /*
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
60 * Core object memory allocator
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
61 * ============================
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
62 */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
63
13488
b31311a78a5d A small comment fix just to make sure I got my cvs/ssh setup right.
Vladimir Marangozov <vladimir.marangozov@t-online.de>
parents: 13445
diff changeset
64 /* The purpose of the object allocator is to make the distinction
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
65 between "object memory" and the rest within the Python heap.
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
66
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
67 Object memory is the one allocated by PyObject_{New, NewVar}, i.e.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
68 the one that holds the object's representation defined by its C
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
69 type structure, *excluding* any object-specific memory buffers that
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
70 might be referenced by the structure (for type structures that have
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
71 pointer fields). By default, the object memory allocator is
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
72 implemented on top of the raw memory allocator.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
73
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
74 The PyCore_* macros can be defined to make the interpreter use a
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
75 custom object memory allocator. They are reserved for internal
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
76 memory management purposes exclusively. Both the core and extension
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
77 modules should use the PyObject_* API. */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
16782
5864a9c58c81 Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16420
diff changeset
79 #ifdef WITH_PYMALLOC
5864a9c58c81 Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16420
diff changeset
80 #define PyCore_OBJECT_MALLOC_FUNC _PyCore_ObjectMalloc
5864a9c58c81 Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16420
diff changeset
81 #define PyCore_OBJECT_REALLOC_FUNC _PyCore_ObjectRealloc
5864a9c58c81 Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16420
diff changeset
82 #define PyCore_OBJECT_FREE_FUNC _PyCore_ObjectFree
5864a9c58c81 Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16420
diff changeset
83 #define NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
5864a9c58c81 Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16420
diff changeset
84 #endif /* !WITH_PYMALLOC */
5864a9c58c81 Add Vladimir Marangozov's object allocator. It is disabled by default. This
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 16420
diff changeset
85
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
86 #ifndef PyCore_OBJECT_MALLOC_FUNC
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
87 #undef PyCore_OBJECT_REALLOC_FUNC
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
88 #undef PyCore_OBJECT_FREE_FUNC
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
89 #define PyCore_OBJECT_MALLOC_FUNC PyCore_MALLOC_FUNC
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
90 #define PyCore_OBJECT_REALLOC_FUNC PyCore_REALLOC_FUNC
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
91 #define PyCore_OBJECT_FREE_FUNC PyCore_FREE_FUNC
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
92 #endif
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
93
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
94 #ifndef PyCore_OBJECT_MALLOC_PROTO
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
95 #undef PyCore_OBJECT_REALLOC_PROTO
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
96 #undef PyCore_OBJECT_FREE_PROTO
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
97 #define PyCore_OBJECT_MALLOC_PROTO PyCore_MALLOC_PROTO
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
98 #define PyCore_OBJECT_REALLOC_PROTO PyCore_REALLOC_PROTO
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
99 #define PyCore_OBJECT_FREE_PROTO PyCore_FREE_PROTO
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
100 #endif
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
101
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
102 #ifdef NEED_TO_DECLARE_OBJECT_MALLOC_AND_FRIEND
13709
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13488
diff changeset
103 extern void *PyCore_OBJECT_MALLOC_FUNC PyCore_OBJECT_MALLOC_PROTO;
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13488
diff changeset
104 extern void *PyCore_OBJECT_REALLOC_FUNC PyCore_OBJECT_REALLOC_PROTO;
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
105 extern void PyCore_OBJECT_FREE_FUNC PyCore_OBJECT_FREE_PROTO;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
106 #endif
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
107
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
108 #ifndef PyCore_OBJECT_MALLOC
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
109 #undef PyCore_OBJECT_REALLOC
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
110 #undef PyCore_OBJECT_FREE
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
111 #define PyCore_OBJECT_MALLOC(n) PyCore_OBJECT_MALLOC_FUNC(n)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
112 #define PyCore_OBJECT_REALLOC(p, n) PyCore_OBJECT_REALLOC_FUNC((p), (n))
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
113 #define PyCore_OBJECT_FREE(p) PyCore_OBJECT_FREE_FUNC(p)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
114 #endif
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
115
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
116 /*
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
117 * Raw object memory interface
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
118 * ===========================
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
119 */
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
121 /* The use of this API should be avoided, unless a builtin object
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
122 constructor inlines PyObject_{New, NewVar}, either because the
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
123 latter functions cannot allocate the exact amount of needed memory,
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
124 either for speed. This situation is exceptional, but occurs for
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
125 some object constructors (PyBuffer_New, PyList_New...). Inlining
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
126 PyObject_{New, NewVar} for objects that are supposed to belong to
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
127 the Python heap is discouraged. If you really have to, make sure
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
128 the object is initialized with PyObject_{Init, InitVar}. Do *not*
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
129 inline PyObject_{Init, InitVar} for user-extension types or you
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
130 might seriously interfere with Python's memory management. */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
131
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
132 /* Functions */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
133
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
134 /* Wrappers around PyCore_OBJECT_MALLOC and friends; useful if you
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
135 need to be sure that you are using the same object memory allocator
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
136 as Python. These wrappers *do not* make sure that allocating 0
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
137 bytes returns a non-NULL pointer. Returned pointers must be checked
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
138 for NULL explicitly; no action is performed on failure. */
13709
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13488
diff changeset
139 extern DL_IMPORT(void *) PyObject_Malloc(size_t);
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13488
diff changeset
140 extern DL_IMPORT(void *) PyObject_Realloc(void *, size_t);
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13488
diff changeset
141 extern DL_IMPORT(void) PyObject_Free(void *);
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 /* Macros */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
144 #define PyObject_MALLOC(n) PyCore_OBJECT_MALLOC(n)
13709
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13488
diff changeset
145 #define PyObject_REALLOC(op, n) PyCore_OBJECT_REALLOC((void *)(op), (n))
4e2aaf1e722a Use 'void' directly instead of the ANY #define, now that all code is ANSI C.
Thomas Wouters <thomas@python.org>
parents: 13488
diff changeset
146 #define PyObject_FREE(op) PyCore_OBJECT_FREE((void *)(op))
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
147
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
148 /*
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
149 * Generic object allocator interface
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
150 * ==================================
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
151 */
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
152
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
153 /* Functions */
13445
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
154 extern DL_IMPORT(PyObject *) PyObject_Init(PyObject *, PyTypeObject *);
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
155 extern DL_IMPORT(PyVarObject *) PyObject_InitVar(PyVarObject *,
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
156 PyTypeObject *, int);
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
157 extern DL_IMPORT(PyObject *) _PyObject_New(PyTypeObject *);
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
158 extern DL_IMPORT(PyVarObject *) _PyObject_NewVar(PyTypeObject *, int);
091ca6b9348f ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents: 13265
diff changeset
159 extern DL_IMPORT(void) _PyObject_Del(PyObject *);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
161 #define PyObject_New(type, typeobj) \
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
162 ( (type *) _PyObject_New(typeobj) )
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
163 #define PyObject_NewVar(type, typeobj, n) \
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
164 ( (type *) _PyObject_NewVar((typeobj), (n)) )
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
165 #define PyObject_Del(op) _PyObject_Del((PyObject *)(op))
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
166
14006
ed369f0aa32b Updated comment
Andrew M. Kuchling <amk@amk.ca>
parents: 13776
diff changeset
167 /* 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
168 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
169 #define PyObject_INIT(op, typeobj) \
17154
3b8a5bbfd2e5 A small change to the C API for weakly-referencable types: Such types
Fred Drake <fdrake@acm.org>
parents: 16782
diff changeset
170 ( (op)->ob_type = (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
171 #define PyObject_INIT_VAR(op, typeobj, size) \
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
172 ( (op)->ob_size = (size), PyObject_INIT((op), (typeobj)) )
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
173
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
174 #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
175
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
176 /* _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
177 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
178 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
179 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
180 for the platform (this is of special importance for subclasses of, e.g.,
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
181 str or long, 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
182
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
183 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
184 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
185 */
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
186 #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
187 # 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
188 #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
189
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
190 #define _PyObject_VAR_SIZE(typeobj, nitems) \
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
191 (size_t) \
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
192 ( ( (typeobj)->tp_basicsize + \
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
193 (nitems)*(typeobj)->tp_itemsize + \
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
194 (SIZEOF_VOID_P - 1) \
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
195 ) & ~(SIZEOF_VOID_P - 1) \
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
196 )
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
197
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
198 #define PyObject_NEW(type, typeobj) \
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
199 ( (type *) PyObject_Init( \
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
200 (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
201
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
202 #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
203 ( (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
204 (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
205 (typeobj), (n)) )
1329
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
206
13265
e8bb0d536633 Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
207 #define PyObject_DEL(op) PyObject_FREE(op)
e8bb0d536633 Neil Schemenauer: small fixes for GC
Guido van Rossum <guido@python.org>
parents: 13256
diff changeset
208
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
209 /* 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
210 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
211 distinction between two steps (at least):
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
212 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
213 2) the initialization of the Python specific fields
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
214 in this storage with PyObject_{Init, InitVar}.
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
215
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
216 PyObject *
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
217 YourObject_New(...)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
218 {
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
219 PyObject *op;
3764
29fe6a0de366 Hacks for MS_COREDLL
Guido van Rossum <guido@python.org>
parents: 2305
diff changeset
220
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
221 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
222 if (op == NULL)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
223 return PyErr_NoMemory();
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
224
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
225 op = PyObject_Init(op, &YourTypeStruct);
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
226 if (op == NULL)
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
227 return NULL;
3764
29fe6a0de366 Hacks for MS_COREDLL
Guido van Rossum <guido@python.org>
parents: 2305
diff changeset
228
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
229 op->ob_field = value;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
230 ...
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
231 return op;
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
232 }
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
233
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
234 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
235 the 1st step is performed automatically for you, so in a C++ class
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 11891
diff changeset
236 constructor you would start directly with PyObject_Init/InitVar. */
3764
29fe6a0de366 Hacks for MS_COREDLL
Guido van Rossum <guido@python.org>
parents: 2305
diff changeset
237
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
238 /*
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
239 * Garbage Collection Support
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
240 * ==========================
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 19668
diff changeset
241 *
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 19668
diff changeset
242 * Some of the functions and macros below are always defined; when
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 19668
diff changeset
243 * WITH_CYCLE_GC is undefined, they simply don't do anything different
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 19668
diff changeset
244 * than their non-GC counterparts.
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
245 */
12993
8f79c5b69e0a part 2 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
246
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
247 /* 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
248 #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
249
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
250 /* Test if an object has a GC head */
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 19668
diff changeset
251 #define PyObject_IS_GC(o) (PyType_IS_GC((o)->ob_type) && \
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 19668
diff changeset
252 ((o)->ob_type->tp_is_gc == NULL || (o)->ob_type->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
253
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
254 extern DL_IMPORT(PyObject *) _PyObject_GC_Malloc(PyTypeObject *, int);
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
255 extern DL_IMPORT(PyVarObject *) _PyObject_GC_Resize(PyVarObject *, int);
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
256
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
257 #define PyObject_GC_Resize(type, op, n) \
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
258 ( (type *) _PyObject_GC_Resize((PyVarObject *)(op), (n)) )
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
259
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
260 extern DL_IMPORT(PyObject *) _PyObject_GC_New(PyTypeObject *);
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
261 extern DL_IMPORT(PyVarObject *) _PyObject_GC_NewVar(PyTypeObject *, int);
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
262 extern DL_IMPORT(void) _PyObject_GC_Del(PyObject *);
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
263 extern DL_IMPORT(void) _PyObject_GC_Track(PyObject *);
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
264 extern DL_IMPORT(void) _PyObject_GC_UnTrack(PyObject *);
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
265
20220
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 19668
diff changeset
266 #ifdef WITH_CYCLE_GC
0b99174d6b17 Add Garbage Collection support to new-style classes (not yet to their
Guido van Rossum <guido@python.org>
parents: 19668
diff changeset
267
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
268 /* GC information is stored BEFORE the object structure */
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
269 typedef union _gc_head {
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
270 struct {
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
271 union _gc_head *gc_next; /* not NULL if object is tracked */
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
272 union _gc_head *gc_prev;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
273 int gc_refs;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
274 } gc;
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
275 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
276 } 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
277
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
278 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
279
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
280 /* 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
281 * 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
282 #define _PyObject_GC_TRACK(o) do { \
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
283 PyGC_Head *g = (PyGC_Head *)(o)-1; \
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
284 if (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
285 Py_FatalError("GC object already in linked list"); \
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
286 g->gc.gc_next = &_PyGC_generation0; \
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
287 g->gc.gc_prev = _PyGC_generation0.gc.gc_prev; \
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
288 g->gc.gc_prev->gc.gc_next = g; \
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
289 _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
290 } 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
291
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
292 /* Tell the GC to stop tracking this object. */
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
293 #define _PyObject_GC_UNTRACK(o) do { \
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
294 PyGC_Head *g = (PyGC_Head *)(o)-1; \
20381
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
295 g->gc.gc_prev->gc.gc_next = g->gc.gc_next; \
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
296 g->gc.gc_next->gc.gc_prev = g->gc.gc_prev; \
b4f829941f3d SF bug [#467145] Python 2.2a4 build problem on HPUX 11.0.
Tim Peters <tim.peters@gmail.com>
parents: 20296
diff changeset
297 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
298 } while (0);
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
299
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
300 #define PyObject_GC_Track(op) _PyObject_GC_Track((PyObject *)op)
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
301 #define PyObject_GC_UnTrack(op) _PyObject_GC_UnTrack((PyObject *)op)
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
302
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
303
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_New(type, typeobj) \
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
305 ( (type *) _PyObject_GC_New(typeobj) )
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
306 #define PyObject_GC_NewVar(type, typeobj, n) \
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
307 ( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
308 #define PyObject_GC_Del(op) _PyObject_GC_Del((PyObject *)(op))
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
309
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
310 #else /* !WITH_CYCLE_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
311
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
312 #define PyObject_GC_New PyObject_New
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
313 #define PyObject_GC_NewVar PyObject_NewVar
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
314 #define PyObject_GC_Del PyObject_Del
19668
cc604b71036c Fix the names of _PyObject_GC_TRACK and _PyObject_GC_UNTRACK when the GC is
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19577
diff changeset
315 #define _PyObject_GC_TRACK(op)
cc604b71036c Fix the names of _PyObject_GC_TRACK and _PyObject_GC_UNTRACK when the GC is
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 19577
diff changeset
316 #define _PyObject_GC_UNTRACK(op)
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
317 #define PyObject_GC_Track(op)
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
318 #define PyObject_GC_UnTrack(op)
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
319
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
320 #endif
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
321
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
322 /* This is here for the sake of backwards compatibility. Extensions that
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
323 * use the old GC API will still compile but the objects will not be
fbd49d1717c4 Change the GC type flag since the API has changed. Allow types using
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 18956
diff changeset
324 * tracked by the GC. */
13183
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
325 #define PyGC_HEAD_SIZE 0
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
326 #define PyObject_GC_Init(op)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
327 #define PyObject_GC_Fini(op)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
328 #define PyObject_AS_GC(op) (op)
54bffdddc0ef final patches from Neil Schemenauer for garbage collection
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12993
diff changeset
329 #define PyObject_FROM_GC(op) (op)
18933
09df3254b49d Merge of descr-branch back into trunk.
Tim Peters <tim.peters@gmail.com>
parents: 17154
diff changeset
330
12993
8f79c5b69e0a part 2 of Neil Schemenauer's GC patches:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12576
diff changeset
331
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents: 16196
diff changeset
332 /* Test if a type supports weak references */
16420
38204093d9c9 Use a type flag to determine the applicability of the tp_weaklistoffset
Fred Drake <fdrake@acm.org>
parents: 16385
diff changeset
333 #define PyType_SUPPORTS_WEAKREFS(t) \
38204093d9c9 Use a type flag to determine the applicability of the tp_weaklistoffset
Fred Drake <fdrake@acm.org>
parents: 16385
diff changeset
334 (PyType_HasFeature((t), Py_TPFLAGS_HAVE_WEAKREFS) \
38204093d9c9 Use a type flag to determine the applicability of the tp_weaklistoffset
Fred Drake <fdrake@acm.org>
parents: 16385
diff changeset
335 && ((t)->tp_weaklistoffset > 0))
16385
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents: 16196
diff changeset
336
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents: 16196
diff changeset
337 #define PyObject_GET_WEAKREFS_LISTPTR(o) \
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents: 16196
diff changeset
338 ((PyObject **) (((char *) (o)) + (o)->ob_type->tp_weaklistoffset))
1fb6672b20ef PEP 205, Weak References -- initial checkin.
Fred Drake <fdrake@acm.org>
parents: 16196
diff changeset
339
1329
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
340 #ifdef __cplusplus
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
341 }
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
342 #endif
519c25039c6a * Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
343 #endif /* !Py_OBJIMPL_H */