blob: 081bb8624cbd629502045efc9dfa1c301f0ce878 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum5113f5f1992-04-05 14:20:22 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossumf2c8beb1992-09-03 20:34:07 +000025#ifndef DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000026#define NDEBUG
Guido van Rossumf2c8beb1992-09-03 20:34:07 +000027#endif
28
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029/* Object and type object interface */
30
31/*
32123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
33
34Objects are structures allocated on the heap. Special rules apply to
35the use of objects to ensure they are properly garbage-collected.
36Objects are never allocated statically or on the stack; they must be
37accessed through special macros and functions only. (Type objects are
38exceptions to the first rule; the standard types are represented by
39statically initialized type objects.)
40
41An object has a 'reference count' that is increased or decreased when a
42pointer to the object is copied or deleted; when the reference count
43reaches zero there are no references to the object left and it can be
44removed from the heap.
45
46An object has a 'type' that determines what it represents and what kind
47of data it contains. An object's type is fixed when it is created.
48Types themselves are represented as objects; an object contains a
49pointer to the corresponding type object. The type itself has a type
50pointer pointing to the object representing the type 'type', which
51contains a pointer to itself!).
52
53Objects do not float around in memory; once allocated an object keeps
54the same size and address. Objects that must hold variable-size data
55can contain pointers to variable-size parts of the object. Not all
56objects of the same type have the same size; but the size cannot change
57after allocation. (These restrictions are made so a reference to an
58object can be simply a pointer -- moving an object would require
59updating all the pointers, and changing an object's size would require
60moving it if there was another object right next to it.)
61
62Objects are always accessed through pointers of the type 'object *'.
63The type 'object' is a structure that only contains the reference count
64and the type pointer. The actual memory allocated for an object
65contains other data that can only be accessed after casting the pointer
66to a pointer to a longer structure type. This longer type must start
67with the reference count and type fields; the macro OB_HEAD should be
68used for this (to accomodate for future changes). The implementation
69of a particular object type can cast the object pointer to the proper
70type and back.
71
72A standard interface exists for objects that contain an array of items
73whose size is determined when the object is allocated.
74
75123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
76*/
77
Guido van Rossum3f5da241990-12-20 15:06:42 +000078#ifndef NDEBUG
79
80/* Turn on heavy reference debugging */
81#define TRACE_REFS
82
83/* Turn on reference counting */
84#define REF_DEBUG
85
86#endif /* NDEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087
88#ifdef TRACE_REFS
89#define OB_HEAD \
90 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +000091 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000092 struct _typeobject *ob_type;
93#define OB_HEAD_INIT(type) 0, 0, 1, type,
94#else
95#define OB_HEAD \
96 unsigned int ob_refcnt; \
97 struct _typeobject *ob_type;
98#define OB_HEAD_INIT(type) 1, type,
99#endif
100
101#define OB_VARHEAD \
102 OB_HEAD \
103 unsigned int ob_size; /* Number of items in variable part */
104
105typedef struct _object {
106 OB_HEAD
107} object;
108
109typedef struct {
110 OB_VARHEAD
111} varobject;
112
113
114/*
115123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
116
117Type objects contain a string containing the type name (to help somewhat
118in debugging), the allocation parameters (see newobj() and newvarobj()),
119and methods for accessing objects of the type. Methods are optional,a
120nil pointer meaning that particular kind of access is not available for
121this type. The DECREF() macro uses the tp_dealloc method without
122checking for a nil pointer; it should always be implemented except if
123the implementation can guarantee that the reference count will never
124reach zero (e.g., for type objects).
125
126NB: the methods for certain type groups are now contained in separate
127method blocks.
128*/
129
130typedef struct {
131 object *(*nb_add) FPROTO((object *, object *));
132 object *(*nb_subtract) FPROTO((object *, object *));
133 object *(*nb_multiply) FPROTO((object *, object *));
134 object *(*nb_divide) FPROTO((object *, object *));
135 object *(*nb_remainder) FPROTO((object *, object *));
Guido van Rossum97ad2d81991-05-05 20:11:43 +0000136 object *(*nb_divmod) FPROTO((object *, object *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137 object *(*nb_power) FPROTO((object *, object *));
138 object *(*nb_negative) FPROTO((object *));
139 object *(*nb_positive) FPROTO((object *));
Guido van Rossum97ad2d81991-05-05 20:11:43 +0000140 object *(*nb_absolute) FPROTO((object *));
Guido van Rossumcf7423a1991-05-14 12:08:10 +0000141 int (*nb_nonzero) FPROTO((object *));
Guido van Rossum7a6dfa71991-10-24 14:58:18 +0000142 object *(*nb_invert) FPROTO((object *));
143 object *(*nb_lshift) FPROTO((object *, object *));
144 object *(*nb_rshift) FPROTO((object *, object *));
145 object *(*nb_and) FPROTO((object *, object *));
146 object *(*nb_xor) FPROTO((object *, object *));
147 object *(*nb_or) FPROTO((object *, object *));
Guido van Rossume6eefc21992-08-14 12:06:52 +0000148 int (*nb_coerce) FPROTO((object **, object **));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149} number_methods;
150
151typedef struct {
152 int (*sq_length) FPROTO((object *));
153 object *(*sq_concat) FPROTO((object *, object *));
154 object *(*sq_repeat) FPROTO((object *, int));
155 object *(*sq_item) FPROTO((object *, int));
156 object *(*sq_slice) FPROTO((object *, int, int));
157 int (*sq_ass_item) FPROTO((object *, int, object *));
158 int (*sq_ass_slice) FPROTO((object *, int, int, object *));
159} sequence_methods;
160
161typedef struct {
162 int (*mp_length) FPROTO((object *));
163 object *(*mp_subscript) FPROTO((object *, object *));
164 int (*mp_ass_subscript) FPROTO((object *, object *, object *));
165} mapping_methods;
166
167typedef struct _typeobject {
168 OB_VARHEAD
169 char *tp_name; /* For printing */
170 unsigned int tp_basicsize, tp_itemsize; /* For allocation */
171
172 /* Methods to implement standard operations */
173
174 void (*tp_dealloc) FPROTO((object *));
Guido van Rossumd783a461991-06-07 22:35:42 +0000175 int (*tp_print) FPROTO((object *, FILE *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176 object *(*tp_getattr) FPROTO((object *, char *));
177 int (*tp_setattr) FPROTO((object *, char *, object *));
178 int (*tp_compare) FPROTO((object *, object *));
179 object *(*tp_repr) FPROTO((object *));
180
181 /* Method suites for standard classes */
182
183 number_methods *tp_as_number;
184 sequence_methods *tp_as_sequence;
185 mapping_methods *tp_as_mapping;
186} typeobject;
187
188extern typeobject Typetype; /* The type of type objects */
189
190#define is_typeobject(op) ((op)->ob_type == &Typetype)
191
Guido van Rossum3f5da241990-12-20 15:06:42 +0000192/* Generic operations on objects */
Guido van Rossumd783a461991-06-07 22:35:42 +0000193extern int printobject PROTO((object *, FILE *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194extern object * reprobject PROTO((object *));
195extern int cmpobject PROTO((object *, object *));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000196extern object *getattr PROTO((object *, char *));
197extern int setattr PROTO((object *, char *, object *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198
199/* Flag bits for printing: */
200#define PRINT_RAW 1 /* No string quotes etc. */
201
202/*
203123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
204
205The macros INCREF(op) and DECREF(op) are used to increment or decrement
206reference counts. DECREF calls the object's deallocator function; for
207objects that don't contain references to other objects or heap memory
208this can be the standard function free(). Both macros can be used
209whereever a void expression is allowed. The argument shouldn't be a
210NIL pointer. The macro NEWREF(op) is used only to initialize reference
211counts to 1; it is defined here for convenience.
212
213We assume that the reference count field can never overflow; this can
214be proven when the size of the field is the same as the pointer size
215but even with a 16-bit reference count field it is pretty unlikely so
216we ignore the possibility. (If you are paranoid, make it a long.)
217
218Type objects should never be deallocated; the type pointer in an object
219is not considered to be a reference to the type object, to save
220complications in the deallocation function. (This is actually a
221decision that's up to the implementer of each new type so if you want,
222you can count such references to the type object.)
223
224*** WARNING*** The DECREF macro must have a side-effect-free argument
225since it may evaluate its argument multiple times. (The alternative
226would be to mace it a proper function or assign it to a global temporary
227variable first, both of which are slower; and in a multi-threaded
228environment the global variable trick is not safe.)
229*/
230
231#ifdef TRACE_REFS
232#ifndef REF_DEBUG
233#define REF_DEBUG
234#endif
235#endif
236
237#ifndef TRACE_REFS
238#define DELREF(op) (*(op)->ob_type->tp_dealloc)((object *)(op))
Guido van Rossumd5b70f51990-11-18 17:27:10 +0000239#define UNREF(op) /*empty*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000240#endif
241
242#ifdef REF_DEBUG
243extern long ref_total;
244#ifndef TRACE_REFS
245#define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1)
246#endif
247#define INCREF(op) (ref_total++, (op)->ob_refcnt++)
248#define DECREF(op) \
Guido van Rossumc8564cd1990-11-02 17:51:56 +0000249 if (--ref_total, --(op)->ob_refcnt > 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000250 ; \
251 else \
252 DELREF(op)
253#else
254#define NEWREF(op) ((op)->ob_refcnt = 1)
255#define INCREF(op) ((op)->ob_refcnt++)
256#define DECREF(op) \
Guido van Rossumc8564cd1990-11-02 17:51:56 +0000257 if (--(op)->ob_refcnt > 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258 ; \
259 else \
260 DELREF(op)
261#endif
262
Guido van Rossum3f5da241990-12-20 15:06:42 +0000263/* Macros to use in case the object pointer may be NULL: */
264
265#define XINCREF(op) if ((op) == NULL) ; else INCREF(op)
266#define XDECREF(op) if ((op) == NULL) ; else DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267
268/* Definition of NULL, so you don't have to include <stdio.h> */
269
270#ifndef NULL
271#define NULL 0
272#endif
273
274
275/*
276NoObject is an object of undefined type which can be used in contexts
277where NULL (nil) is not suitable (since NULL often means 'error').
278
279Don't forget to apply INCREF() when returning this value!!!
280*/
281
282extern object NoObject; /* Don't use this directly */
283
284#define None (&NoObject)
285
286
287/*
288123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
289
290More conventions
291================
292
293Argument Checking
294-----------------
295
296Functions that take objects as arguments normally don't check for nil
297arguments, but they do check the type of the argument, and return an
298error if the function doesn't apply to the type.
299
300Failure Modes
301-------------
302
303Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000304memory. This is communicated to the caller in two ways: an error string
305is set (see errors.h), and the function result differs: functions that
306normally return a pointer return NULL for failure, functions returning
307an integer return -1 (which could be a legal return value too!), and
308other functions return 0 for success and -1 for failure.
309Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310
311Reference Counts
312----------------
313
314It takes a while to get used to the proper usage of reference counts.
315
316Functions that create an object set the reference count to 1; such new
317objects must be stored somewhere or destroyed again with DECREF().
318Functions that 'store' objects such as settupleitem() and dictinsert()
319don't increment the reference count of the object, since the most
320frequent use is to store a fresh object. Functions that 'retrieve'
321objects such as gettupleitem() and dictlookup() also don't increment
322the reference count, since most frequently the object is only looked at
323quickly. Thus, to retrieve an object and store it again, the caller
324must call INCREF() explicitly.
325
326NOTE: functions that 'consume' a reference count like dictinsert() even
327consume the reference if the object wasn't stored, to simplify error
328handling.
329
330It seems attractive to make other functions that take an object as
331argument consume a reference count; however this may quickly get
332confusing (even the current practice is already confusing). Consider
333it carefully, it may safe lots of calls to INCREF() and DECREF() at
334times.
335
336123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
337*/