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