Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* Object and type object interface */ |
| 2 | |
| 3 | /* |
| 4 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 5 | |
| 6 | Objects are structures allocated on the heap. Special rules apply to |
| 7 | the use of objects to ensure they are properly garbage-collected. |
| 8 | Objects are never allocated statically or on the stack; they must be |
| 9 | accessed through special macros and functions only. (Type objects are |
| 10 | exceptions to the first rule; the standard types are represented by |
| 11 | statically initialized type objects.) |
| 12 | |
| 13 | An object has a 'reference count' that is increased or decreased when a |
| 14 | pointer to the object is copied or deleted; when the reference count |
| 15 | reaches zero there are no references to the object left and it can be |
| 16 | removed from the heap. |
| 17 | |
| 18 | An object has a 'type' that determines what it represents and what kind |
| 19 | of data it contains. An object's type is fixed when it is created. |
| 20 | Types themselves are represented as objects; an object contains a |
| 21 | pointer to the corresponding type object. The type itself has a type |
| 22 | pointer pointing to the object representing the type 'type', which |
| 23 | contains a pointer to itself!). |
| 24 | |
| 25 | Objects do not float around in memory; once allocated an object keeps |
| 26 | the same size and address. Objects that must hold variable-size data |
| 27 | can contain pointers to variable-size parts of the object. Not all |
| 28 | objects of the same type have the same size; but the size cannot change |
| 29 | after allocation. (These restrictions are made so a reference to an |
| 30 | object can be simply a pointer -- moving an object would require |
| 31 | updating all the pointers, and changing an object's size would require |
| 32 | moving it if there was another object right next to it.) |
| 33 | |
| 34 | Objects are always accessed through pointers of the type 'object *'. |
| 35 | The type 'object' is a structure that only contains the reference count |
| 36 | and the type pointer. The actual memory allocated for an object |
| 37 | contains other data that can only be accessed after casting the pointer |
| 38 | to a pointer to a longer structure type. This longer type must start |
| 39 | with the reference count and type fields; the macro OB_HEAD should be |
| 40 | used for this (to accomodate for future changes). The implementation |
| 41 | of a particular object type can cast the object pointer to the proper |
| 42 | type and back. |
| 43 | |
| 44 | A standard interface exists for objects that contain an array of items |
| 45 | whose size is determined when the object is allocated. |
| 46 | |
| 47 | 123456789-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 Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 59 | int ob_refcnt; \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | 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 | |
| 73 | typedef struct _object { |
| 74 | OB_HEAD |
| 75 | } object; |
| 76 | |
| 77 | typedef struct { |
| 78 | OB_VARHEAD |
| 79 | } varobject; |
| 80 | |
| 81 | |
| 82 | /* |
| 83 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 84 | |
| 85 | Type objects contain a string containing the type name (to help somewhat |
| 86 | in debugging), the allocation parameters (see newobj() and newvarobj()), |
| 87 | and methods for accessing objects of the type. Methods are optional,a |
| 88 | nil pointer meaning that particular kind of access is not available for |
| 89 | this type. The DECREF() macro uses the tp_dealloc method without |
| 90 | checking for a nil pointer; it should always be implemented except if |
| 91 | the implementation can guarantee that the reference count will never |
| 92 | reach zero (e.g., for type objects). |
| 93 | |
| 94 | NB: the methods for certain type groups are now contained in separate |
| 95 | method blocks. |
| 96 | */ |
| 97 | |
| 98 | typedef 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 | |
| 109 | typedef 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 | |
| 119 | typedef 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 | |
| 125 | typedef 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 | |
| 146 | extern typeobject Typetype; /* The type of type objects */ |
| 147 | |
| 148 | #define is_typeobject(op) ((op)->ob_type == &Typetype) |
| 149 | |
| 150 | extern void printobject PROTO((object *, FILE *, int)); |
| 151 | extern object * reprobject PROTO((object *)); |
| 152 | extern int cmpobject PROTO((object *, object *)); |
| 153 | |
| 154 | /* Flag bits for printing: */ |
| 155 | #define PRINT_RAW 1 /* No string quotes etc. */ |
| 156 | |
| 157 | /* |
| 158 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 159 | |
| 160 | The macros INCREF(op) and DECREF(op) are used to increment or decrement |
| 161 | reference counts. DECREF calls the object's deallocator function; for |
| 162 | objects that don't contain references to other objects or heap memory |
| 163 | this can be the standard function free(). Both macros can be used |
| 164 | whereever a void expression is allowed. The argument shouldn't be a |
| 165 | NIL pointer. The macro NEWREF(op) is used only to initialize reference |
| 166 | counts to 1; it is defined here for convenience. |
| 167 | |
| 168 | We assume that the reference count field can never overflow; this can |
| 169 | be proven when the size of the field is the same as the pointer size |
| 170 | but even with a 16-bit reference count field it is pretty unlikely so |
| 171 | we ignore the possibility. (If you are paranoid, make it a long.) |
| 172 | |
| 173 | Type objects should never be deallocated; the type pointer in an object |
| 174 | is not considered to be a reference to the type object, to save |
| 175 | complications in the deallocation function. (This is actually a |
| 176 | decision that's up to the implementer of each new type so if you want, |
| 177 | you can count such references to the type object.) |
| 178 | |
| 179 | *** WARNING*** The DECREF macro must have a side-effect-free argument |
| 180 | since it may evaluate its argument multiple times. (The alternative |
| 181 | would be to mace it a proper function or assign it to a global temporary |
| 182 | variable first, both of which are slower; and in a multi-threaded |
| 183 | environment 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)) |
Guido van Rossum | d5b70f5 | 1990-11-18 17:27:10 +0000 | [diff] [blame^] | 194 | #define UNREF(op) /*empty*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
| 197 | #ifdef REF_DEBUG |
| 198 | extern long ref_total; |
| 199 | #ifndef TRACE_REFS |
| 200 | #define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1) |
| 201 | #endif |
| 202 | #define INCREF(op) (ref_total++, (op)->ob_refcnt++) |
| 203 | #define DECREF(op) \ |
Guido van Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 204 | if (--ref_total, --(op)->ob_refcnt > 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 205 | ; \ |
| 206 | else \ |
| 207 | DELREF(op) |
| 208 | #else |
| 209 | #define NEWREF(op) ((op)->ob_refcnt = 1) |
| 210 | #define INCREF(op) ((op)->ob_refcnt++) |
| 211 | #define DECREF(op) \ |
Guido van Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 212 | if (--(op)->ob_refcnt > 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 213 | ; \ |
| 214 | else \ |
| 215 | DELREF(op) |
| 216 | #endif |
| 217 | |
| 218 | |
| 219 | /* Definition of NULL, so you don't have to include <stdio.h> */ |
| 220 | |
| 221 | #ifndef NULL |
| 222 | #define NULL 0 |
| 223 | #endif |
| 224 | |
| 225 | |
| 226 | /* |
| 227 | NoObject is an object of undefined type which can be used in contexts |
| 228 | where NULL (nil) is not suitable (since NULL often means 'error'). |
| 229 | |
| 230 | Don't forget to apply INCREF() when returning this value!!! |
| 231 | */ |
| 232 | |
| 233 | extern object NoObject; /* Don't use this directly */ |
| 234 | |
| 235 | #define None (&NoObject) |
| 236 | |
| 237 | |
| 238 | /* |
| 239 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 240 | |
| 241 | More conventions |
| 242 | ================ |
| 243 | |
| 244 | Argument Checking |
| 245 | ----------------- |
| 246 | |
| 247 | Functions that take objects as arguments normally don't check for nil |
| 248 | arguments, but they do check the type of the argument, and return an |
| 249 | error if the function doesn't apply to the type. |
| 250 | |
| 251 | Failure Modes |
| 252 | ------------- |
| 253 | |
| 254 | Functions may fail for a variety of reasons, including running out of |
| 255 | memory. This is communicated to the caller in two ways: 'errno' is set |
| 256 | to indicate the error, and the function result differs: functions that |
| 257 | normally return a pointer return nil for failure, functions returning |
| 258 | an integer return -1 (which can be a legal return value too!), and |
| 259 | other functions return 0 for success and the error number for failure. |
| 260 | Callers should always check for errors before using the result. The |
| 261 | following error codes are used: |
| 262 | |
| 263 | EBADF bad object type (first argument only) |
| 264 | EINVAL bad argument type (second and further arguments) |
| 265 | ENOMEM no memory (malloc failed) |
| 266 | ENOENT key not found in dictionary |
| 267 | EDOM index out of range or division by zero |
| 268 | ERANGE result not representable |
| 269 | |
| 270 | XXX any others? |
| 271 | |
| 272 | Reference Counts |
| 273 | ---------------- |
| 274 | |
| 275 | It takes a while to get used to the proper usage of reference counts. |
| 276 | |
| 277 | Functions that create an object set the reference count to 1; such new |
| 278 | objects must be stored somewhere or destroyed again with DECREF(). |
| 279 | Functions that 'store' objects such as settupleitem() and dictinsert() |
| 280 | don't increment the reference count of the object, since the most |
| 281 | frequent use is to store a fresh object. Functions that 'retrieve' |
| 282 | objects such as gettupleitem() and dictlookup() also don't increment |
| 283 | the reference count, since most frequently the object is only looked at |
| 284 | quickly. Thus, to retrieve an object and store it again, the caller |
| 285 | must call INCREF() explicitly. |
| 286 | |
| 287 | NOTE: functions that 'consume' a reference count like dictinsert() even |
| 288 | consume the reference if the object wasn't stored, to simplify error |
| 289 | handling. |
| 290 | |
| 291 | It seems attractive to make other functions that take an object as |
| 292 | argument consume a reference count; however this may quickly get |
| 293 | confusing (even the current practice is already confusing). Consider |
| 294 | it carefully, it may safe lots of calls to INCREF() and DECREF() at |
| 295 | times. |
| 296 | |
| 297 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 298 | */ |
| 299 | |
| 300 | /* Error number interface */ |
| 301 | #include <errno.h> |
| 302 | |
| 303 | #ifndef errno |
| 304 | extern int errno; |
| 305 | #endif |
| 306 | |
| 307 | #ifdef THINK_C |
| 308 | /* Lightspeed C doesn't define these in <errno.h> */ |
| 309 | #define EDOM 33 |
| 310 | #define ERANGE 34 |
| 311 | #endif |