1 // General object definitions: pointers, reference counting, garbage collection.
7 #include "cln/modules.h"
12 // We don't have to deal with circular structures, so normal reference counting
13 // is sufficient. Is also has the advantage of being mostly non-interrupting.
16 // An object is either a pointer to heap allocated data
19 // It is possible to distinguish these because pointers are aligned.
20 // cl_uint_alignment is the guaranteed alignment of a `void*' or `long'
21 // in memory. Must be > 1.
23 #define cl_word_alignment 2
25 #if defined(__i386__) || defined(__mips__) || defined(__mipsel__) || defined(__sparc__) || defined(__hppa__) || defined(__arm__) || defined(__rs6000__) || defined(__m88k__) || defined(__convex__) || defined(__s390__)
26 #define cl_word_alignment 4
28 #if defined(__alpha__) || defined(__mips64__) || defined(__sparc64__) || defined(__ia64__) || defined(__x86_64__)
29 #define cl_word_alignment 8
31 #if !defined(cl_word_alignment)
32 #error "Define cl_word_alignment for your CPU!"
36 // Four basic classes are introduced:
40 // gcpointer rcpointer
42 // `gcobject' = garbage collectible object (pointer or immediate),
43 // `gcpointer' = garbage collectible pointer,
44 // `rcobject' = reference counted object (pointer or immediate),
45 // `rcpointer' = reference counted pointer.
47 // "garbage collectible" means that a reference count is maintained, and
48 // when the reference count drops to 0, the object is freed. This is useful
49 // for all kind of short- or long-lived objects.
50 // "reference counted" means that a reference count is maintained, which
51 // cannot drop to 0. This is useful for objects which are registered in a
52 // global cache table, in order to know which objects can be thrown away
53 // when the cache is cleaned. (If the cache were never cleaned, its objects
54 // would never be freed, and we could get away with normal C pointers.)
56 // It is permissible to treat a `rcobject' as a `gcobject', and a `rcpointer'
57 // as a `gcpointer', but this just increases the destructor and copy-constructor
59 // It is also permissible to treat a `gcpointer' as a `gcobject', and a
60 // `rcpointer' as a `rcobject', but this just increases the destructor and
61 // copy-constructor overhead.
64 // Immediate data is a word, as wide as a pointer.
65 typedef sintP cl_sint;
66 typedef uintP cl_uint; // This ought to be called `cl_word'.
67 #define cl_pointer_size intPsize
68 // NB: (cl_pointer_size==64) implies defined(HAVE_FAST_LONGLONG)
69 #if (cl_pointer_size==64)
70 #define CL_WIDE_POINTERS
73 // Distinguish immediate data from pointers.
74 inline cl_boolean cl_pointer_p (cl_uint word)
76 return (cl_boolean)((word & (cl_word_alignment-1)) == 0);
78 inline cl_boolean cl_immediate_p (cl_uint word)
80 return (cl_boolean)((word & (cl_word_alignment-1)) != 0);
83 // Immediate data: Fixnum, Short Float, maybe Single Float.
84 // They have type tags.
85 // |...............................|......|
88 // Number of bits reserved for tagging information:
89 #if (cl_word_alignment <= 4)
94 #define cl_tag_shift 0
95 #if (cl_pointer_size == 64)
96 #define cl_value_shift 32
98 #define cl_value_shift (cl_tag_len+cl_tag_shift)
100 #define cl_value_len (cl_pointer_size - cl_value_shift)
101 #define cl_tag_mask (((1UL << cl_tag_len) - 1) << cl_tag_shift)
102 #define cl_value_mask (((1UL << cl_value_len) - 1) << cl_value_shift)
104 // Return the tag of a word.
105 inline cl_uint cl_tag (cl_uint word)
107 return (word & cl_tag_mask) >> cl_tag_shift;
110 // Return the value (unsigned) of a word.
111 inline cl_uint cl_value (cl_uint word)
113 // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
114 return word >> cl_value_shift;
117 // Return a word, combining a value and a tag.
118 inline cl_uint cl_combine (cl_uint tag, cl_uint value)
120 return (value << cl_value_shift) + (tag << cl_tag_shift);
122 inline cl_uint cl_combine (cl_uint tag, cl_sint value)
124 // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
125 return (value << cl_value_shift) + (tag << cl_tag_shift);
127 // Keep the compiler happy.
128 inline cl_uint cl_combine (cl_uint tag, unsigned int value)
129 { return cl_combine(tag, (cl_uint)value); }
130 inline cl_uint cl_combine (cl_uint tag, int value)
131 { return cl_combine(tag, (cl_sint)value); }
133 inline cl_uint cl_combine (cl_uint tag, unsigned long long value)
134 { return cl_combine(tag, (cl_uint)value); }
135 inline cl_uint cl_combine (cl_uint tag, long long value)
136 { return cl_combine(tag, (cl_uint)value); }
139 // Definition of the tags.
140 #if !defined(CL_WIDE_POINTERS)
141 #if (cl_word_alignment == 2)
143 #define cl_SF_tag 3 // must satisfy the cl_immediate_p predicate!
145 #if (cl_word_alignment == 4)
149 #else // CL_WIDE_POINTERS
150 // Single Floats are immediate as well.
156 // Corresponding classes.
157 extern const struct cl_class * cl_immediate_classes [1<<cl_tag_len];
160 // Heap allocated data contains a header, for two purposes:
162 // - reference count (a portable alternative to garbage collection,
163 // or the basis for a portable and interoperable garbage collection).
165 int refcount; // reference count
166 const struct cl_class * type; // type tag
169 // Function to destroy the contents of a heap object.
170 typedef void (*cl_heap_destructor_function) (cl_heap* pointer);
171 // Flags, may be ORed together.
172 #define cl_class_flags_subclass_complex 1 // all instances belong to cl_N
173 #define cl_class_flags_subclass_real 2 // all instances belong to cl_R
174 #define cl_class_flags_subclass_float 4 // all instances belong to cl_F
175 #define cl_class_flags_subclass_rational 8 // all instances belong to cl_RA
176 #define cl_class_flags_number_ring 16 // all instances are rings whose
177 // elements belong to cl_number
178 #define cl_class_flags_modint_ring 32 // all instances are rings whose
179 // elements belong to cl_MI
180 // Function to print an object for debugging, to cerr.
181 typedef void (*cl_heap_dprint_function) (cl_heap* pointer);
184 cl_heap_destructor_function destruct;
186 cl_heap_dprint_function dprint;
189 // Free an object on heap.
190 extern void cl_free_heap_object (cl_heap* pointer);
192 // Debugging support for dynamic typing: Register a debugging print function.
193 #define cl_register_type_printer(type,printer) \
194 { extern cl_class type; type.dprint = (printer); }
197 // cl_private_thing: An immediate value or a pointer into the heap.
198 // This must be as wide as a `cl_uint'.
199 // (Actually, this ought to be a union { void*; cl_uint; }, but using
200 // a pointer type generates better code.)
201 // Never throw away a cl_private_thing, or reference counts will be wrong!
202 typedef struct cl_anything * cl_private_thing;
204 // Increment the reference count.
205 inline void cl_inc_pointer_refcount (cl_heap* pointer)
210 // Decrement the reference count of a garbage collected pointer.
211 inline void cl_gc_dec_pointer_refcount (cl_heap* pointer)
213 if (--pointer->refcount == 0)
214 cl_free_heap_object(pointer);
216 // Decrement the reference count of a reference counted pointer.
217 inline void cl_rc_dec_pointer_refcount (cl_heap* pointer)
222 // Increment the reference count.
223 // This must be a macro, not an inline function, because pointer_p() and
224 // inc_pointer_refcount() are non-virtual member functions, so that the
225 // compiler can optimize it.
226 #define cl_inc_refcount(x) \
227 if ((x).pointer_p()) \
228 (x).inc_pointer_refcount(); \
230 // Decrement the reference count.
231 // This must be a macro, not an inline function, because pointer_p() and
232 // dec_pointer_refcount() are non-virtual member functions, so that the
233 // compiler can optimize it.
234 #define cl_dec_refcount(x) \
235 if ((x).pointer_p()) \
236 (x).dec_pointer_refcount(); \
238 // The declaration of a copy constructor.
239 // Restriction: The base class's default constructor must do nothing or
240 // initialize `pointer' to a constant expression.
241 #define CL_DEFINE_COPY_CONSTRUCTOR1(_class_) \
242 _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_class_)
243 #define _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_classname_) \
244 inline _class_::_classname_ (const _class_& x) \
246 cl_uint x_word = x.word; \
247 cl_inc_refcount(x); \
248 this->word = x_word; \
251 // The declaration of a copy constructor.
252 // Restriction: The base class must have the usual `cl_private_thing'
253 // constructor. Drawback: The base class must be known here.
254 #define CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_baseclass_) \
255 _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_class_,_baseclass_)
256 #define _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_classname_,_baseclass_) \
257 inline _class_::_classname_ (const _class_& x) \
258 : _baseclass_ (as_cl_private_thing(x)) {}
260 // The declaration of an assignment operator.
261 #define CL_DEFINE_ASSIGNMENT_OPERATOR(dest_class,src_class) \
262 inline dest_class& dest_class::operator= (const src_class& x) \
264 /* Be careful, we might be assigning x to itself. */ \
265 cl_uint x_word = x.word; \
266 cl_inc_refcount(x); \
267 cl_dec_refcount(*this); \
268 this->word = x_word; \
272 // We have a small problem with destructors: The specialized destructor
273 // of a leaf class such as `cl_SF' should be more efficient than the
274 // general destructor for `cl_N'. Since (by C++ specs) destructing a cl_SF
275 // would run the destructors for cl_SF, cl_F, cl_R, cl_N (in that order),
276 // and in the last step the compiler does not know any more that the object
277 // actually is a cl_SF, there is no way to optimize the destructor!
278 // ("progn-reversed" method combination is evil.)
279 // And if we define "mirror"/"shadow" classes with no destructors (such
280 // that `cl_F' inherits from `cl_F_no_destructor' buts adds a destructor)
281 // then we need to add explicit conversion operators cl_SF -> cl_F -> cl_R ...,
282 // with the effect that calling an overloaded function like `as_cl_F'
283 // (which has two signatures `as_cl_F(cl_number)' and `as_cl_F(cl_F)')
284 // with a cl_SF argument gives an "call of overloaded function is ambiguous"
286 // There is no help: If we want overloaded functions to be callable in a way
287 // that makes sense, `cl_SF' has to be a subclass of `cl_F', and then the
288 // destructor of `cl_SF' will do at least as much computation as the `cl_F'
289 // destructor. Praise C++ ! :-((
290 // (Even making `pointer_p()' a virtual function would not help.)
293 // This is obnoxious.
294 template <class key1_type, class value_type> struct cl_htentry1;
296 // The four concrete classes of all objects.
302 cl_heap* heappointer;
306 // Default constructor. (Used for objects with no initializer.)
308 // Destructor. (Used when a variable goes out of scope.)
311 cl_gcobject (const cl_gcobject&);
312 // Assignment operator.
313 cl_gcobject& operator= (const cl_gcobject&);
314 // Distinguish immediate data from pointer.
315 cl_boolean pointer_p() const
316 { return cl_pointer_p(word); }
317 // Reference counting.
318 void inc_pointer_refcount () const
319 { cl_inc_pointer_refcount(heappointer); }
320 void dec_pointer_refcount () const
321 { cl_gc_dec_pointer_refcount(heappointer); }
322 // Return the type tag of an immediate number.
323 cl_uint nonpointer_tag () const
324 { return cl_tag(word); }
325 // Return the type tag of a heap-allocated number.
326 const cl_class * pointer_type () const
327 { return heappointer->type; }
328 // Private pointer manipulations.
329 cl_private_thing _as_cl_private_thing () const;
330 // Private constructor.
331 cl_gcobject (cl_private_thing p)
332 #if !(defined(__alpha__) && !defined(__GNUC__))
338 void debug_print () const;
339 // Ability to place an object at a given address.
340 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
341 void* operator new (size_t size) { return ::operator new (size); }
343 inline cl_gcobject::cl_gcobject () {}
344 inline cl_gcobject::~cl_gcobject () { cl_dec_refcount(*this); }
345 CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcobject)
346 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcobject,cl_gcobject)
352 cl_heap* heappointer;
356 // Default constructor. (Used for objects with no initializer.)
358 // Destructor. (Used when a variable goes out of scope.)
361 cl_gcpointer (const cl_gcpointer&);
362 // Assignment operator.
363 cl_gcpointer& operator= (const cl_gcpointer&);
364 // Distinguish immediate data from pointer.
365 cl_boolean pointer_p() const
367 // Reference counting.
368 void inc_pointer_refcount () const
369 { cl_inc_pointer_refcount(heappointer); }
370 void dec_pointer_refcount () const
371 { cl_gc_dec_pointer_refcount(heappointer); }
372 // Return the type tag of an immediate number.
373 cl_uint nonpointer_tag () const
374 { return cl_tag(word); }
375 // Return the type tag of a heap-allocated number.
376 const cl_class * pointer_type () const
377 { return heappointer->type; }
378 // Private pointer manipulations.
379 cl_private_thing _as_cl_private_thing () const;
380 // Private constructor.
381 cl_gcpointer (cl_private_thing p)
382 #if !(defined(__alpha__) && !defined(__GNUC__))
388 void debug_print () const;
389 // Ability to place an object at a given address.
390 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
391 void* operator new (size_t size) { return ::operator new (size); }
393 inline cl_gcpointer::cl_gcpointer () {}
394 inline cl_gcpointer::~cl_gcpointer () { cl_dec_refcount(*this); }
395 CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcpointer)
396 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcpointer,cl_gcpointer)
402 cl_heap* heappointer;
406 // Default constructor. (Used for objects with no initializer.)
408 // Destructor. (Used when a variable goes out of scope.)
411 cl_rcobject (const cl_rcobject&);
412 // Assignment operator.
413 cl_rcobject& operator= (const cl_rcobject&);
414 // Distinguish immediate data from pointer.
415 cl_boolean pointer_p() const
416 { return cl_pointer_p(word); }
417 // Reference counting.
418 void inc_pointer_refcount () const
419 { cl_inc_pointer_refcount(heappointer); }
420 void dec_pointer_refcount () const
421 { cl_rc_dec_pointer_refcount(heappointer); }
422 // Return the type tag of an immediate number.
423 cl_uint nonpointer_tag () const
424 { return cl_tag(word); }
425 // Return the type tag of a heap-allocated number.
426 const cl_class * pointer_type () const
427 { return heappointer->type; }
428 // Private pointer manipulations.
429 cl_private_thing _as_cl_private_thing () const;
430 // Private constructor.
431 cl_rcobject (cl_private_thing p)
432 #if !(defined(__alpha__) && !defined(__GNUC__))
438 void debug_print () const;
439 // Ability to place an object at a given address.
440 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
441 void* operator new (size_t size) { return ::operator new (size); }
443 inline cl_rcobject::cl_rcobject () {}
444 inline cl_rcobject::~cl_rcobject () { cl_dec_refcount(*this); }
445 CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcobject)
446 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcobject,cl_rcobject)
452 cl_heap* heappointer;
456 // Default constructor. (Used for objects with no initializer.)
458 // Destructor. (Used when a variable goes out of scope.)
461 cl_rcpointer (const cl_rcpointer&);
462 // Assignment operator.
463 cl_rcpointer& operator= (const cl_rcpointer&);
464 // Distinguish immediate data from pointer.
465 cl_boolean pointer_p() const
467 // Reference counting.
468 void inc_pointer_refcount () const
469 { cl_inc_pointer_refcount(heappointer); }
470 void dec_pointer_refcount () const
471 { cl_rc_dec_pointer_refcount(heappointer); }
472 // Return the type tag of an immediate number.
473 cl_uint nonpointer_tag () const
474 { return cl_tag(word); }
475 // Return the type tag of a heap-allocated number.
476 const cl_class * pointer_type () const
477 { return heappointer->type; }
478 // Private pointer manipulations.
479 cl_private_thing _as_cl_private_thing () const;
480 // Private constructor.
481 cl_rcpointer (cl_private_thing p)
482 #if !(defined(__alpha__) && !defined(__GNUC__))
488 void debug_print () const;
489 // Ability to place an object at a given address.
490 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
491 void* operator new (size_t size) { return ::operator new (size); }
493 inline cl_rcpointer::cl_rcpointer () {}
494 inline cl_rcpointer::~cl_rcpointer () { cl_dec_refcount(*this); }
495 CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcpointer)
496 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcpointer,cl_rcpointer)
498 // Private pointer manipulations.
500 inline cl_private_thing cl_gcobject::_as_cl_private_thing () const
502 cl_private_thing p = (cl_private_thing) pointer;
503 cl_inc_refcount(*this);
506 inline cl_private_thing as_cl_private_thing (const cl_gcobject& x)
508 return x._as_cl_private_thing();
511 inline cl_private_thing cl_gcpointer::_as_cl_private_thing () const
513 cl_private_thing p = (cl_private_thing) pointer;
514 cl_inc_refcount(*this);
517 inline cl_private_thing as_cl_private_thing (const cl_gcpointer& x)
519 return x._as_cl_private_thing();
522 inline cl_private_thing cl_rcobject::_as_cl_private_thing () const
524 cl_private_thing p = (cl_private_thing) pointer;
525 cl_inc_refcount(*this);
528 inline cl_private_thing as_cl_private_thing (const cl_rcobject& x)
530 return x._as_cl_private_thing();
533 inline cl_private_thing cl_rcpointer::_as_cl_private_thing () const
535 cl_private_thing p = (cl_private_thing) pointer;
536 cl_inc_refcount(*this);
539 inline cl_private_thing as_cl_private_thing (const cl_rcpointer& x)
541 return x._as_cl_private_thing();
544 // Note: When we define a function that returns a class object by value,
545 // we normally return it as const value. The declarations
548 // const T func (...); (B)
549 // behave identically and generate identical code, except that the code
551 // compiles fine with (A) but is an error (and yields a warning) with (B).
552 // We want this warning.
554 // Define a conversion operator from one object to another object of the
556 #define CL_DEFINE_CONVERTER(target_class) \
557 operator const target_class & () const \
559 if (sizeof(*this) != sizeof(target_class)) cl_abort(); \
560 return * (const target_class *) (void*) this; \
565 #endif /* _CL_OBJECT_H */