blob: c0deb7d10c34544f0f6e9af7c1ca656f6c9c8a9d [file] [log] [blame]
Guido van Rossum3f5da241990-12-20 15:06:42 +00001#define NDEBUG
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Object and type object interface */
3
4/*
5123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
6
7Objects are structures allocated on the heap. Special rules apply to
8the use of objects to ensure they are properly garbage-collected.
9Objects are never allocated statically or on the stack; they must be
10accessed through special macros and functions only. (Type objects are
11exceptions to the first rule; the standard types are represented by
12statically initialized type objects.)
13
14An object has a 'reference count' that is increased or decreased when a
15pointer to the object is copied or deleted; when the reference count
16reaches zero there are no references to the object left and it can be
17removed from the heap.
18
19An object has a 'type' that determines what it represents and what kind
20of data it contains. An object's type is fixed when it is created.
21Types themselves are represented as objects; an object contains a
22pointer to the corresponding type object. The type itself has a type
23pointer pointing to the object representing the type 'type', which
24contains a pointer to itself!).
25
26Objects do not float around in memory; once allocated an object keeps
27the same size and address. Objects that must hold variable-size data
28can contain pointers to variable-size parts of the object. Not all
29objects of the same type have the same size; but the size cannot change
30after allocation. (These restrictions are made so a reference to an
31object can be simply a pointer -- moving an object would require
32updating all the pointers, and changing an object's size would require
33moving it if there was another object right next to it.)
34
35Objects are always accessed through pointers of the type 'object *'.
36The type 'object' is a structure that only contains the reference count
37and the type pointer. The actual memory allocated for an object
38contains other data that can only be accessed after casting the pointer
39to a pointer to a longer structure type. This longer type must start
40with the reference count and type fields; the macro OB_HEAD should be
41used for this (to accomodate for future changes). The implementation
42of a particular object type can cast the object pointer to the proper
43type and back.
44
45A standard interface exists for objects that contain an array of items
46whose size is determined when the object is allocated.
47
48123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
49*/
50
Guido van Rossum3f5da241990-12-20 15:06:42 +000051#ifndef NDEBUG
52
53/* Turn on heavy reference debugging */
54#define TRACE_REFS
55
56/* Turn on reference counting */
57#define REF_DEBUG
58
59#endif /* NDEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060
61#ifdef TRACE_REFS
62#define OB_HEAD \
63 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +000064 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065 struct _typeobject *ob_type;
66#define OB_HEAD_INIT(type) 0, 0, 1, type,
67#else
68#define OB_HEAD \
69 unsigned int ob_refcnt; \
70 struct _typeobject *ob_type;
71#define OB_HEAD_INIT(type) 1, type,
72#endif
73
74#define OB_VARHEAD \
75 OB_HEAD \
76 unsigned int ob_size; /* Number of items in variable part */
77
78typedef struct _object {
79 OB_HEAD
80} object;
81
82typedef struct {
83 OB_VARHEAD
84} varobject;
85
86
87/*
88123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
89
90Type objects contain a string containing the type name (to help somewhat
91in debugging), the allocation parameters (see newobj() and newvarobj()),
92and methods for accessing objects of the type. Methods are optional,a
93nil pointer meaning that particular kind of access is not available for
94this type. The DECREF() macro uses the tp_dealloc method without
95checking for a nil pointer; it should always be implemented except if
96the implementation can guarantee that the reference count will never
97reach zero (e.g., for type objects).
98
99NB: the methods for certain type groups are now contained in separate
100method blocks.
101*/
102
103typedef struct {
104 object *(*nb_add) FPROTO((object *, object *));
105 object *(*nb_subtract) FPROTO((object *, object *));
106 object *(*nb_multiply) FPROTO((object *, object *));
107 object *(*nb_divide) FPROTO((object *, object *));
108 object *(*nb_remainder) FPROTO((object *, object *));
109 object *(*nb_power) FPROTO((object *, object *));
110 object *(*nb_negative) FPROTO((object *));
111 object *(*nb_positive) FPROTO((object *));
112} number_methods;
113
114typedef struct {
115 int (*sq_length) FPROTO((object *));
116 object *(*sq_concat) FPROTO((object *, object *));
117 object *(*sq_repeat) FPROTO((object *, int));
118 object *(*sq_item) FPROTO((object *, int));
119 object *(*sq_slice) FPROTO((object *, int, int));
120 int (*sq_ass_item) FPROTO((object *, int, object *));
121 int (*sq_ass_slice) FPROTO((object *, int, int, object *));
122} sequence_methods;
123
124typedef struct {
125 int (*mp_length) FPROTO((object *));
126 object *(*mp_subscript) FPROTO((object *, object *));
127 int (*mp_ass_subscript) FPROTO((object *, object *, object *));
128} mapping_methods;
129
130typedef struct _typeobject {
131 OB_VARHEAD
132 char *tp_name; /* For printing */
133 unsigned int tp_basicsize, tp_itemsize; /* For allocation */
134
135 /* Methods to implement standard operations */
136
137 void (*tp_dealloc) FPROTO((object *));
138 void (*tp_print) FPROTO((object *, FILE *, int));
139 object *(*tp_getattr) FPROTO((object *, char *));
140 int (*tp_setattr) FPROTO((object *, char *, object *));
141 int (*tp_compare) FPROTO((object *, object *));
142 object *(*tp_repr) FPROTO((object *));
143
144 /* Method suites for standard classes */
145
146 number_methods *tp_as_number;
147 sequence_methods *tp_as_sequence;
148 mapping_methods *tp_as_mapping;
149} typeobject;
150
151extern typeobject Typetype; /* The type of type objects */
152
153#define is_typeobject(op) ((op)->ob_type == &Typetype)
154
Guido van Rossum3f5da241990-12-20 15:06:42 +0000155/* Generic operations on objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000156extern void printobject PROTO((object *, FILE *, int));
157extern object * reprobject PROTO((object *));
158extern int cmpobject PROTO((object *, object *));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000159extern object *getattr PROTO((object *, char *));
160extern int setattr PROTO((object *, char *, object *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161
162/* Flag bits for printing: */
163#define PRINT_RAW 1 /* No string quotes etc. */
164
165/*
166123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
167
168The macros INCREF(op) and DECREF(op) are used to increment or decrement
169reference counts. DECREF calls the object's deallocator function; for
170objects that don't contain references to other objects or heap memory
171this can be the standard function free(). Both macros can be used
172whereever a void expression is allowed. The argument shouldn't be a
173NIL pointer. The macro NEWREF(op) is used only to initialize reference
174counts to 1; it is defined here for convenience.
175
176We assume that the reference count field can never overflow; this can
177be proven when the size of the field is the same as the pointer size
178but even with a 16-bit reference count field it is pretty unlikely so
179we ignore the possibility. (If you are paranoid, make it a long.)
180
181Type objects should never be deallocated; the type pointer in an object
182is not considered to be a reference to the type object, to save
183complications in the deallocation function. (This is actually a
184decision that's up to the implementer of each new type so if you want,
185you can count such references to the type object.)
186
187*** WARNING*** The DECREF macro must have a side-effect-free argument
188since it may evaluate its argument multiple times. (The alternative
189would be to mace it a proper function or assign it to a global temporary
190variable first, both of which are slower; and in a multi-threaded
191environment the global variable trick is not safe.)
192*/
193
194#ifdef TRACE_REFS
195#ifndef REF_DEBUG
196#define REF_DEBUG
197#endif
198#endif
199
200#ifndef TRACE_REFS
201#define DELREF(op) (*(op)->ob_type->tp_dealloc)((object *)(op))
Guido van Rossumd5b70f51990-11-18 17:27:10 +0000202#define UNREF(op) /*empty*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000203#endif
204
205#ifdef REF_DEBUG
206extern long ref_total;
207#ifndef TRACE_REFS
208#define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1)
209#endif
210#define INCREF(op) (ref_total++, (op)->ob_refcnt++)
211#define DECREF(op) \
Guido van Rossumc8564cd1990-11-02 17:51:56 +0000212 if (--ref_total, --(op)->ob_refcnt > 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000213 ; \
214 else \
215 DELREF(op)
216#else
217#define NEWREF(op) ((op)->ob_refcnt = 1)
218#define INCREF(op) ((op)->ob_refcnt++)
219#define DECREF(op) \
Guido van Rossumc8564cd1990-11-02 17:51:56 +0000220 if (--(op)->ob_refcnt > 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221 ; \
222 else \
223 DELREF(op)
224#endif
225
Guido van Rossum3f5da241990-12-20 15:06:42 +0000226/* Macros to use in case the object pointer may be NULL: */
227
228#define XINCREF(op) if ((op) == NULL) ; else INCREF(op)
229#define XDECREF(op) if ((op) == NULL) ; else DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230
231/* Definition of NULL, so you don't have to include <stdio.h> */
232
233#ifndef NULL
234#define NULL 0
235#endif
236
237
238/*
239NoObject is an object of undefined type which can be used in contexts
240where NULL (nil) is not suitable (since NULL often means 'error').
241
242Don't forget to apply INCREF() when returning this value!!!
243*/
244
245extern object NoObject; /* Don't use this directly */
246
247#define None (&NoObject)
248
249
250/*
251123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
252
253More conventions
254================
255
256Argument Checking
257-----------------
258
259Functions that take objects as arguments normally don't check for nil
260arguments, but they do check the type of the argument, and return an
261error if the function doesn't apply to the type.
262
263Failure Modes
264-------------
265
266Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000267memory. This is communicated to the caller in two ways: an error string
268is set (see errors.h), and the function result differs: functions that
269normally return a pointer return NULL for failure, functions returning
270an integer return -1 (which could be a legal return value too!), and
271other functions return 0 for success and -1 for failure.
272Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273
274Reference Counts
275----------------
276
277It takes a while to get used to the proper usage of reference counts.
278
279Functions that create an object set the reference count to 1; such new
280objects must be stored somewhere or destroyed again with DECREF().
281Functions that 'store' objects such as settupleitem() and dictinsert()
282don't increment the reference count of the object, since the most
283frequent use is to store a fresh object. Functions that 'retrieve'
284objects such as gettupleitem() and dictlookup() also don't increment
285the reference count, since most frequently the object is only looked at
286quickly. Thus, to retrieve an object and store it again, the caller
287must call INCREF() explicitly.
288
289NOTE: functions that 'consume' a reference count like dictinsert() even
290consume the reference if the object wasn't stored, to simplify error
291handling.
292
293It seems attractive to make other functions that take an object as
294argument consume a reference count; however this may quickly get
295confusing (even the current practice is already confusing). Consider
296it carefully, it may safe lots of calls to INCREF() and DECREF() at
297times.
298
299123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
300*/