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