blob: 416ca77964f6c2539f95825121dafa73521213ea [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_OBJECT_H
2#define Py_OBJECT_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossumf70e43a1991-02-19 12:39:46 +00007/***********************************************************
Guido van Rossumb6775db1994-08-01 11:34:53 +00008Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossum9bfef441993-03-29 10:43:31 +00009Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000010
11 All Rights Reserved
12
13Permission to use, copy, modify, and distribute this software and its
14documentation for any purpose and without fee is hereby granted,
15provided that the above copyright notice appear in all copies and that
16both that copyright notice and this permission notice appear in
17supporting documentation, and that the names of Stichting Mathematisch
18Centrum or CWI not be used in advertising or publicity pertaining to
19distribution of the software without specific, written prior permission.
20
21STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
22THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
23FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
24FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
25WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
26ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
27OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
28
29******************************************************************/
30
Guido van Rossumf2c8beb1992-09-03 20:34:07 +000031#ifndef DEBUG
Guido van Rossum3f5da241990-12-20 15:06:42 +000032#define NDEBUG
Guido van Rossumf2c8beb1992-09-03 20:34:07 +000033#endif
34
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035/* Object and type object interface */
36
37/*
38123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
39
40Objects are structures allocated on the heap. Special rules apply to
41the use of objects to ensure they are properly garbage-collected.
42Objects are never allocated statically or on the stack; they must be
43accessed through special macros and functions only. (Type objects are
44exceptions to the first rule; the standard types are represented by
45statically initialized type objects.)
46
47An object has a 'reference count' that is increased or decreased when a
48pointer to the object is copied or deleted; when the reference count
49reaches zero there are no references to the object left and it can be
50removed from the heap.
51
52An object has a 'type' that determines what it represents and what kind
53of data it contains. An object's type is fixed when it is created.
54Types themselves are represented as objects; an object contains a
55pointer to the corresponding type object. The type itself has a type
56pointer pointing to the object representing the type 'type', which
57contains a pointer to itself!).
58
59Objects do not float around in memory; once allocated an object keeps
60the same size and address. Objects that must hold variable-size data
61can contain pointers to variable-size parts of the object. Not all
62objects of the same type have the same size; but the size cannot change
63after allocation. (These restrictions are made so a reference to an
64object can be simply a pointer -- moving an object would require
65updating all the pointers, and changing an object's size would require
66moving it if there was another object right next to it.)
67
68Objects are always accessed through pointers of the type 'object *'.
69The type 'object' is a structure that only contains the reference count
70and the type pointer. The actual memory allocated for an object
71contains other data that can only be accessed after casting the pointer
72to a pointer to a longer structure type. This longer type must start
73with the reference count and type fields; the macro OB_HEAD should be
74used for this (to accomodate for future changes). The implementation
75of a particular object type can cast the object pointer to the proper
76type and back.
77
78A standard interface exists for objects that contain an array of items
79whose size is determined when the object is allocated.
80
81123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
82*/
83
Guido van Rossum3f5da241990-12-20 15:06:42 +000084#ifndef NDEBUG
85
86/* Turn on heavy reference debugging */
87#define TRACE_REFS
88
89/* Turn on reference counting */
90#define REF_DEBUG
91
92#endif /* NDEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093
94#ifdef TRACE_REFS
95#define OB_HEAD \
96 struct _object *_ob_next, *_ob_prev; \
Guido van Rossumc8564cd1990-11-02 17:51:56 +000097 int ob_refcnt; \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098 struct _typeobject *ob_type;
99#define OB_HEAD_INIT(type) 0, 0, 1, type,
100#else
101#define OB_HEAD \
102 unsigned int ob_refcnt; \
103 struct _typeobject *ob_type;
104#define OB_HEAD_INIT(type) 1, type,
105#endif
106
107#define OB_VARHEAD \
108 OB_HEAD \
109 unsigned int ob_size; /* Number of items in variable part */
110
111typedef struct _object {
112 OB_HEAD
113} object;
114
115typedef struct {
116 OB_VARHEAD
117} varobject;
118
119
120/*
121123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
122
123Type objects contain a string containing the type name (to help somewhat
124in debugging), the allocation parameters (see newobj() and newvarobj()),
125and methods for accessing objects of the type. Methods are optional,a
126nil pointer meaning that particular kind of access is not available for
127this type. The DECREF() macro uses the tp_dealloc method without
128checking for a nil pointer; it should always be implemented except if
129the implementation can guarantee that the reference count will never
130reach zero (e.g., for type objects).
131
132NB: the methods for certain type groups are now contained in separate
133method blocks.
134*/
135
Guido van Rossumb6775db1994-08-01 11:34:53 +0000136typedef object * (*unaryfunc) PROTO((object *));
137typedef object * (*binaryfunc) PROTO((object *, object *));
138typedef int (*inquiry) PROTO((object *));
139typedef int (*coercion) PROTO((object **, object **));
140typedef object *(*intargfunc) PROTO((object *, int));
141typedef object *(*intintargfunc) PROTO((object *, int, int));
142typedef int(*intobjargproc) PROTO((object *, int, object *));
143typedef int(*intintobjargproc) PROTO((object *, int, int, object *));
144typedef int(*objobjargproc) PROTO((object *, object *, object *));
145
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000147 binaryfunc nb_add;
148 binaryfunc nb_subtract;
149 binaryfunc nb_multiply;
150 binaryfunc nb_divide;
151 binaryfunc nb_remainder;
152 binaryfunc nb_divmod;
153 binaryfunc nb_power;
154 unaryfunc nb_negative;
155 unaryfunc nb_positive;
156 unaryfunc nb_absolute;
157 inquiry nb_nonzero;
158 unaryfunc nb_invert;
159 binaryfunc nb_lshift;
160 binaryfunc nb_rshift;
161 binaryfunc nb_and;
162 binaryfunc nb_xor;
163 binaryfunc nb_or;
164 coercion nb_coerce;
165 unaryfunc nb_int;
166 unaryfunc nb_long;
167 unaryfunc nb_float;
168 unaryfunc nb_oct;
169 unaryfunc nb_hex;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170} number_methods;
171
172typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000173 inquiry sq_length;
174 binaryfunc sq_concat;
175 intargfunc sq_repeat;
176 intargfunc sq_item;
177 intintargfunc sq_slice;
178 intobjargproc sq_ass_item;
179 intintobjargproc sq_ass_slice;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000180} sequence_methods;
181
182typedef struct {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000183 inquiry mp_length;
184 binaryfunc mp_subscript;
185 objobjargproc mp_ass_subscript;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186} mapping_methods;
187
Guido van Rossumb6775db1994-08-01 11:34:53 +0000188typedef void (*destructor) PROTO((object *));
189typedef int (*printfunc) PROTO((object *, FILE *, int));
190typedef object *(*getattrfunc) PROTO((object *, char *));
191typedef int (*setattrfunc) PROTO((object *, char *, object *));
192typedef int (*cmpfunc) PROTO((object *, object *));
193typedef object *(*reprfunc) PROTO((object *));
194typedef long (*hashfunc) PROTO((object *));
195
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000196typedef struct _typeobject {
197 OB_VARHEAD
198 char *tp_name; /* For printing */
199 unsigned int tp_basicsize, tp_itemsize; /* For allocation */
200
201 /* Methods to implement standard operations */
202
Guido van Rossumb6775db1994-08-01 11:34:53 +0000203 destructor tp_dealloc;
204 printfunc tp_print;
205 getattrfunc tp_getattr;
206 setattrfunc tp_setattr;
207 cmpfunc tp_compare;
208 reprfunc tp_repr;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000209
210 /* Method suites for standard classes */
211
212 number_methods *tp_as_number;
213 sequence_methods *tp_as_sequence;
214 mapping_methods *tp_as_mapping;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000215
216 /* More standard operations (at end for binary compatibility) */
217
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218 hashfunc tp_hash;
219 binaryfunc tp_call;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000220#ifdef COUNT_ALLOCS
221 /* these must be last */
222 int tp_alloc;
223 int tp_free;
224 int tp_maxalloc;
225 struct _typeobject *tp_next;
226#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227} typeobject;
228
229extern typeobject Typetype; /* The type of type objects */
230
231#define is_typeobject(op) ((op)->ob_type == &Typetype)
232
Guido van Rossum3f5da241990-12-20 15:06:42 +0000233/* Generic operations on objects */
Guido van Rossumd783a461991-06-07 22:35:42 +0000234extern int printobject PROTO((object *, FILE *, int));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000235extern object * reprobject PROTO((object *));
Guido van Rossumc6004111993-11-05 10:22:19 +0000236extern object * strobject PROTO((object *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237extern int cmpobject PROTO((object *, object *));
Guido van Rossum3f5da241990-12-20 15:06:42 +0000238extern object *getattr PROTO((object *, char *));
Guido van Rossumed18fdc1993-07-11 19:55:34 +0000239extern int hasattr PROTO((object *, char *));
Guido van Rossum9bfef441993-03-29 10:43:31 +0000240extern object *getattro PROTO((object *, object *));
241extern int setattro PROTO((object *, object *, object *));
242extern long hashobject PROTO((object *));
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243
244/* Flag bits for printing: */
245#define PRINT_RAW 1 /* No string quotes etc. */
246
247/*
248123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
249
250The macros INCREF(op) and DECREF(op) are used to increment or decrement
251reference counts. DECREF calls the object's deallocator function; for
252objects that don't contain references to other objects or heap memory
253this can be the standard function free(). Both macros can be used
254whereever a void expression is allowed. The argument shouldn't be a
255NIL pointer. The macro NEWREF(op) is used only to initialize reference
256counts to 1; it is defined here for convenience.
257
258We assume that the reference count field can never overflow; this can
259be proven when the size of the field is the same as the pointer size
260but even with a 16-bit reference count field it is pretty unlikely so
261we ignore the possibility. (If you are paranoid, make it a long.)
262
263Type objects should never be deallocated; the type pointer in an object
264is not considered to be a reference to the type object, to save
265complications in the deallocation function. (This is actually a
266decision that's up to the implementer of each new type so if you want,
267you can count such references to the type object.)
268
269*** WARNING*** The DECREF macro must have a side-effect-free argument
270since it may evaluate its argument multiple times. (The alternative
271would be to mace it a proper function or assign it to a global temporary
272variable first, both of which are slower; and in a multi-threaded
273environment the global variable trick is not safe.)
274*/
275
276#ifdef TRACE_REFS
277#ifndef REF_DEBUG
278#define REF_DEBUG
279#endif
280#endif
281
282#ifndef TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000283#ifdef COUNT_ALLOCS
284#define DELREF(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((object *)(op)))
285#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000286#define DELREF(op) (*(op)->ob_type->tp_dealloc)((object *)(op))
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000287#endif
Guido van Rossumd5b70f51990-11-18 17:27:10 +0000288#define UNREF(op) /*empty*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000289#endif
290
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000291#ifdef COUNT_ALLOCS
292extern void inc_count PROTO((typeobject *));
293#endif
294
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000295#ifdef REF_DEBUG
296extern long ref_total;
297#ifndef TRACE_REFS
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000298#ifdef COUNT_ALLOCS
299#define NEWREF(op) (inc_count((op)->ob_type), ref_total++, (op)->ob_refcnt = 1)
300#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000301#define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1)
302#endif
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000303#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304#define INCREF(op) (ref_total++, (op)->ob_refcnt++)
305#define DECREF(op) \
Guido van Rossumc8564cd1990-11-02 17:51:56 +0000306 if (--ref_total, --(op)->ob_refcnt > 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000307 ; \
308 else \
309 DELREF(op)
310#else
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000311#ifdef COUNT_ALLOCS
312#define NEWREF(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1)
313#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314#define NEWREF(op) ((op)->ob_refcnt = 1)
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000315#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000316#define INCREF(op) ((op)->ob_refcnt++)
317#define DECREF(op) \
Guido van Rossumc8564cd1990-11-02 17:51:56 +0000318 if (--(op)->ob_refcnt > 0) \
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000319 ; \
320 else \
321 DELREF(op)
322#endif
323
Guido van Rossum3f5da241990-12-20 15:06:42 +0000324/* Macros to use in case the object pointer may be NULL: */
325
326#define XINCREF(op) if ((op) == NULL) ; else INCREF(op)
327#define XDECREF(op) if ((op) == NULL) ; else DECREF(op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328
329/* Definition of NULL, so you don't have to include <stdio.h> */
330
331#ifndef NULL
332#define NULL 0
333#endif
334
335
336/*
337NoObject is an object of undefined type which can be used in contexts
338where NULL (nil) is not suitable (since NULL often means 'error').
339
340Don't forget to apply INCREF() when returning this value!!!
341*/
342
343extern object NoObject; /* Don't use this directly */
344
345#define None (&NoObject)
346
347
348/*
Guido van Rossumb6775db1994-08-01 11:34:53 +0000349A common programming style in Python requires the forward declaration
350of static, initialized structures, e.g. for a typeobject that is used
351by the functions whose address must be used in the initializer.
352Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as
353well) botch this if you use the static keyword for both declarations
354(they allocate two objects, and use the first, uninitialized one until
355the second declaration is encountered). Therefore, the forward
356declaration should use the 'forwardstatic' keyword. This expands to
357static on most systems, but to extern on a few. The actual storage
358and name will still be static because the second declaration is
359static, so no linker visible symbols will be generated. (Standard C
360compilers take offense to the extern forward declaration of a static
361object, so I can't just put extern in all cases. :-( )
362*/
363
364#ifdef BAD_STATIC_FORWARD
365#define staticforward extern
366#else
367#define staticforward static
368#endif /* BAD_STATIC_FORWARD */
369
370
371/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
373
374More conventions
375================
376
377Argument Checking
378-----------------
379
380Functions that take objects as arguments normally don't check for nil
381arguments, but they do check the type of the argument, and return an
382error if the function doesn't apply to the type.
383
384Failure Modes
385-------------
386
387Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000388memory. This is communicated to the caller in two ways: an error string
389is set (see errors.h), and the function result differs: functions that
390normally return a pointer return NULL for failure, functions returning
391an integer return -1 (which could be a legal return value too!), and
392other functions return 0 for success and -1 for failure.
393Callers should always check for errors before using the result.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000394
395Reference Counts
396----------------
397
398It takes a while to get used to the proper usage of reference counts.
399
400Functions that create an object set the reference count to 1; such new
401objects must be stored somewhere or destroyed again with DECREF().
402Functions that 'store' objects such as settupleitem() and dictinsert()
403don't increment the reference count of the object, since the most
404frequent use is to store a fresh object. Functions that 'retrieve'
405objects such as gettupleitem() and dictlookup() also don't increment
406the reference count, since most frequently the object is only looked at
407quickly. Thus, to retrieve an object and store it again, the caller
408must call INCREF() explicitly.
409
410NOTE: functions that 'consume' a reference count like dictinsert() even
411consume the reference if the object wasn't stored, to simplify error
412handling.
413
414It seems attractive to make other functions that take an object as
415argument consume a reference count; however this may quickly get
416confusing (even the current practice is already confusing). Consider
417it carefully, it may safe lots of calls to INCREF() and DECREF() at
418times.
419
420123456789-123456789-123456789-123456789-123456789-123456789-123456789-12
421*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000422
423#ifdef __cplusplus
424}
425#endif
426#endif /* !Py_OBJECT_H */