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(__sparc__) || defined(__hppa__) || defined(__arm__) || defined(__rs6000__) || defined(__m88k__) || defined(__convex__)
26 #define cl_word_alignment 4
28 #if defined(__alpha__) || defined(__mips64__) || defined(__sparc64__)
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 // Definition of the tags.
134 #if !defined(CL_WIDE_POINTERS)
135 #if (cl_word_alignment == 2)
137 #define cl_SF_tag 3 // must satisfy the cl_immediate_p predicate!
139 #if (cl_word_alignment == 4)
143 #else // CL_WIDE_POINTERS
144 // Single Floats are immediate as well.
150 // Corresponding classes.
151 extern const struct cl_class * cl_immediate_classes [1<<cl_tag_len];
154 // Heap allocated data contains a header, for two purposes:
156 // - reference count (a portable alternative to garbage collection,
157 // or the basis for a portable and interoperable garbage collection).
159 int refcount; // reference count
160 const struct cl_class * type; // type tag
163 // Function to destroy the contents of a heap object.
164 typedef void (*cl_heap_destructor_function) (cl_heap* pointer);
165 // Flags, to be ORed together.
166 #define cl_class_flags_subclass_complex 1 // all instances belong to cl_N
167 #define cl_class_flags_subclass_real 2 // all instances belong to cl_R
168 #define cl_class_flags_subclass_float 4 // all instances belong to cl_F
169 #define cl_class_flags_subclass_rational 8 // all instances belong to cl_RA
170 #define cl_class_flags_number_ring 16 // all instances are rings whose
171 // elements belong to cl_number
172 // Function to print an object for debugging, to stderr.
173 typedef void (*cl_heap_dprint_function) (cl_heap* pointer);
176 cl_heap_destructor_function destruct;
178 cl_heap_dprint_function dprint;
181 // Free an object on heap.
182 extern void cl_free_heap_object (cl_heap* pointer);
184 // Debugging support for dynamic typing: Register a debugging print function.
185 #define cl_register_type_printer(type,printer) \
186 { extern cl_class type; type.dprint = (printer); }
189 // cl_private_thing: An immediate value or a pointer into the heap.
190 // This must be as wide as a `cl_uint'.
191 // (Actually, this ought to be a union { void*; cl_uint; }, but using
192 // a pointer type generates better code.)
193 // Never throw away a cl_private_thing, or reference counts will be wrong!
194 typedef struct cl_anything * cl_private_thing;
196 // Increment the reference count.
197 inline void cl_inc_pointer_refcount (cl_heap* pointer)
202 // Decrement the reference count of a garbage collected pointer.
203 inline void cl_gc_dec_pointer_refcount (cl_heap* pointer)
205 if (--pointer->refcount == 0)
206 cl_free_heap_object(pointer);
208 // Decrement the reference count of a reference counted pointer.
209 inline void cl_rc_dec_pointer_refcount (cl_heap* pointer)
214 // Increment the reference count.
215 // This must be a macro, not an inline function, because pointer_p() and
216 // inc_pointer_refcount() are non-virtual member functions, so that the
217 // compiler can optimize it.
218 #define cl_inc_refcount(x) \
219 if ((x).pointer_p()) \
220 (x).inc_pointer_refcount(); \
222 // Decrement the reference count.
223 // This must be a macro, not an inline function, because pointer_p() and
224 // dec_pointer_refcount() are non-virtual member functions, so that the
225 // compiler can optimize it.
226 #define cl_dec_refcount(x) \
227 if ((x).pointer_p()) \
228 (x).dec_pointer_refcount(); \
230 // The declaration of a copy constructor.
231 // Restriction: The base class's default constructor must do nothing or
232 // initialize `pointer' to a constant expression.
233 #define CL_DEFINE_COPY_CONSTRUCTOR1(_class_) \
234 _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_class_)
235 #define _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_classname_) \
236 inline _class_::_classname_ (const _class_& x) \
238 cl_uint x_word = x.word; \
239 cl_inc_refcount(x); \
243 // The declaration of a copy constructor.
244 // Restriction: The base class must have the usual `cl_private_thing'
245 // constructor. Drawback: The base class must be known here.
246 #define CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_baseclass_) \
247 _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_class_,_baseclass_)
248 #define _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_classname_,_baseclass_) \
249 inline _class_::_classname_ (const _class_& x) \
250 : _baseclass_ (as_cl_private_thing(x)) {}
252 // The declaration of an assignment operator.
253 #define CL_DEFINE_ASSIGNMENT_OPERATOR(dest_class,src_class) \
254 inline dest_class& dest_class::operator= (const src_class& x) \
256 /* Be careful, we might be assigning x to itself. */ \
257 cl_uint x_word = x.word; \
258 cl_inc_refcount(x); \
259 cl_dec_refcount(*this); \
264 // We have a small problem with destructors: The specialized destructor
265 // of a leaf class such as `cl_SF' should be more efficient than the
266 // general destructor for `cl_N'. Since (by C++ specs) destructing a cl_SF
267 // would run the destructors for cl_SF, cl_F, cl_R, cl_N (in that order),
268 // and in the last step the compiler does not know any more that the object
269 // actually is a cl_SF, there is no way to optimize the destructor!
270 // ("progn-reversed" method combination is evil.)
271 // And if we define "mirror"/"shadow" classes with no destructors (such
272 // that `cl_F' inherits from `cl_F_no_destructor' buts adds a destructor)
273 // then we need to add explicit conversion operators cl_SF -> cl_F -> cl_R ...,
274 // with the effect that calling an overloaded function like `as_cl_F'
275 // (which has two signatures `as_cl_F(cl_number)' and `as_cl_F(cl_F)')
276 // with a cl_SF argument gives an "call of overloaded function is ambiguous"
278 // There is no help: If we want overloaded functions to be callable in a way
279 // that makes sense, `cl_SF' has to be a subclass of `cl_F', and then the
280 // destructor of `cl_SF' will do at least as much computation as the `cl_F'
281 // destructor. Praise C++ ! :-((
282 // (Even making `pointer_p()' a virtual function would not help.)
285 // This is obnoxious.
286 template <class key1_type, class value_type> struct cl_htentry1;
288 // The four concrete classes of all objects.
294 cl_heap* heappointer;
298 // Default constructor. (Used for objects with no initializer.)
300 // Destructor. (Used when a variable goes out of scope.)
303 cl_gcobject (const cl_gcobject&);
304 // Assignment operator.
305 cl_gcobject& operator= (const cl_gcobject&);
306 // Distinguish immediate data from pointer.
307 cl_boolean pointer_p() const
308 { return cl_pointer_p(word); }
309 // Reference counting.
310 void inc_pointer_refcount () const
311 { cl_inc_pointer_refcount(heappointer); }
312 void dec_pointer_refcount () const
313 { cl_gc_dec_pointer_refcount(heappointer); }
314 // Return the type tag of an immediate number.
315 cl_uint nonpointer_tag () const
316 { return cl_tag(word); }
317 // Return the type tag of a heap-allocated number.
318 const cl_class * pointer_type () const
319 { return heappointer->type; }
320 // Private pointer manipulations.
321 cl_private_thing _as_cl_private_thing () const;
322 // Private constructor.
323 cl_gcobject (cl_private_thing p)
324 #if !(defined(__alpha__) && !defined(__GNUC__))
330 void debug_print () const;
331 // Ability to place an object at a given address.
332 void* operator new (size_t size, cl_gcobject* ptr) { (void)size; return ptr; }
333 void* operator new (size_t size) { return ::operator new (size); }
335 inline cl_gcobject::cl_gcobject () {}
336 inline cl_gcobject::~cl_gcobject () { cl_dec_refcount(*this); }
337 CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcobject)
338 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcobject,cl_gcobject)
344 cl_heap* heappointer;
348 // Default constructor. (Used for objects with no initializer.)
350 // Destructor. (Used when a variable goes out of scope.)
353 cl_gcpointer (const cl_gcpointer&);
354 // Assignment operator.
355 cl_gcpointer& operator= (const cl_gcpointer&);
356 // Distinguish immediate data from pointer.
357 cl_boolean pointer_p() const
359 // Reference counting.
360 void inc_pointer_refcount () const
361 { cl_inc_pointer_refcount(heappointer); }
362 void dec_pointer_refcount () const
363 { cl_gc_dec_pointer_refcount(heappointer); }
364 // Return the type tag of an immediate number.
365 cl_uint nonpointer_tag () const
366 { return cl_tag(word); }
367 // Return the type tag of a heap-allocated number.
368 const cl_class * pointer_type () const
369 { return heappointer->type; }
370 // Private pointer manipulations.
371 cl_private_thing _as_cl_private_thing () const;
372 // Private constructor.
373 cl_gcpointer (cl_private_thing p)
374 #if !(defined(__alpha__) && !defined(__GNUC__))
380 void debug_print () const;
381 // Ability to place an object at a given address.
382 void* operator new (size_t size, cl_gcpointer* ptr) { (void)size; return ptr; }
383 void* operator new (size_t size) { return ::operator new (size); }
385 inline cl_gcpointer::cl_gcpointer () {}
386 inline cl_gcpointer::~cl_gcpointer () { cl_dec_refcount(*this); }
387 CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcpointer)
388 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcpointer,cl_gcpointer)
394 cl_heap* heappointer;
398 // Default constructor. (Used for objects with no initializer.)
400 // Destructor. (Used when a variable goes out of scope.)
403 cl_rcobject (const cl_rcobject&);
404 // Assignment operator.
405 cl_rcobject& operator= (const cl_rcobject&);
406 // Distinguish immediate data from pointer.
407 cl_boolean pointer_p() const
408 { return cl_pointer_p(word); }
409 // Reference counting.
410 void inc_pointer_refcount () const
411 { cl_inc_pointer_refcount(heappointer); }
412 void dec_pointer_refcount () const
413 { cl_rc_dec_pointer_refcount(heappointer); }
414 // Return the type tag of an immediate number.
415 cl_uint nonpointer_tag () const
416 { return cl_tag(word); }
417 // Return the type tag of a heap-allocated number.
418 const cl_class * pointer_type () const
419 { return heappointer->type; }
420 // Private pointer manipulations.
421 cl_private_thing _as_cl_private_thing () const;
422 // Private constructor.
423 cl_rcobject (cl_private_thing p)
424 #if !(defined(__alpha__) && !defined(__GNUC__))
430 void debug_print () const;
431 // Ability to place an object at a given address.
432 void* operator new (size_t size, cl_rcobject* ptr) { (void)size; return ptr; }
433 void* operator new (size_t size) { return ::operator new (size); }
435 inline cl_rcobject::cl_rcobject () {}
436 inline cl_rcobject::~cl_rcobject () { cl_dec_refcount(*this); }
437 CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcobject)
438 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcobject,cl_rcobject)
444 cl_heap* heappointer;
448 // Default constructor. (Used for objects with no initializer.)
450 // Destructor. (Used when a variable goes out of scope.)
453 cl_rcpointer (const cl_rcpointer&);
454 // Assignment operator.
455 cl_rcpointer& operator= (const cl_rcpointer&);
456 // Distinguish immediate data from pointer.
457 cl_boolean pointer_p() const
459 // Reference counting.
460 void inc_pointer_refcount () const
461 { cl_inc_pointer_refcount(heappointer); }
462 void dec_pointer_refcount () const
463 { cl_rc_dec_pointer_refcount(heappointer); }
464 // Return the type tag of an immediate number.
465 cl_uint nonpointer_tag () const
466 { return cl_tag(word); }
467 // Return the type tag of a heap-allocated number.
468 const cl_class * pointer_type () const
469 { return heappointer->type; }
470 // Private pointer manipulations.
471 cl_private_thing _as_cl_private_thing () const;
472 // Private constructor.
473 cl_rcpointer (cl_private_thing p)
474 #if !(defined(__alpha__) && !defined(__GNUC__))
480 void debug_print () const;
481 // Ability to place an object at a given address.
482 void* operator new (size_t size, cl_rcpointer* ptr) { (void)size; return ptr; }
483 void* operator new (size_t size) { return ::operator new (size); }
485 inline cl_rcpointer::cl_rcpointer () {}
486 inline cl_rcpointer::~cl_rcpointer () { cl_dec_refcount(*this); }
487 CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcpointer)
488 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcpointer,cl_rcpointer)
490 // Private pointer manipulations.
492 inline cl_private_thing cl_gcobject::_as_cl_private_thing () const
494 cl_private_thing p = (cl_private_thing) pointer;
495 cl_inc_refcount(*this);
498 inline cl_private_thing as_cl_private_thing (const cl_gcobject& x)
500 return x._as_cl_private_thing();
503 inline cl_private_thing cl_gcpointer::_as_cl_private_thing () const
505 cl_private_thing p = (cl_private_thing) pointer;
506 cl_inc_refcount(*this);
509 inline cl_private_thing as_cl_private_thing (const cl_gcpointer& x)
511 return x._as_cl_private_thing();
514 inline cl_private_thing cl_rcobject::_as_cl_private_thing () const
516 cl_private_thing p = (cl_private_thing) pointer;
517 cl_inc_refcount(*this);
520 inline cl_private_thing as_cl_private_thing (const cl_rcobject& x)
522 return x._as_cl_private_thing();
525 inline cl_private_thing cl_rcpointer::_as_cl_private_thing () const
527 cl_private_thing p = (cl_private_thing) pointer;
528 cl_inc_refcount(*this);
531 inline cl_private_thing as_cl_private_thing (const cl_rcpointer& x)
533 return x._as_cl_private_thing();
536 // Note: When we define a function that returns a class object by value,
537 // we normally return it as const value. The declarations
540 // const T func (...); (B)
541 // behave identically and generate identical code, except that the code
543 // compiles fine with (A) but is an error (and yields a warning) with (B).
544 // We want this warning.
546 // Define a conversion operator from one object to another object of the
548 #define CL_DEFINE_CONVERTER(target_class) \
549 operator const target_class & () const \
551 if (sizeof(*this) != sizeof(target_class)) cl_abort(); \
552 return * (const target_class *) (void*) this; \
557 #endif /* _CL_OBJECT_H */