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