]> www.ginac.de Git - cln.git/blob - include/cln/object.h
4a254433ce753189ece68fa2579dc0fa1aa5a177
[cln.git] / include / cln / object.h
1 // General object definitions: pointers, reference counting, garbage collection.
2
3 #ifndef _CL_OBJECT_H
4 #define _CL_OBJECT_H
5
6 #include "cln/types.h"
7 #include "cln/modules.h"
8 #include <cstdlib>
9
10 namespace cln {
11
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.
14
15
16 // An object is either a pointer to heap allocated data
17 //              or immediate data.
18
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.
22 #if defined(__m68k__)
23   #define cl_word_alignment  2
24 #endif
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
27 #endif
28 #if defined(__alpha__) || defined(__ia64__) || defined(__mips64__) || defined(__powerpc64__) || defined(__sparc64__) || defined(__x86_64__)
29   #define cl_word_alignment  8
30 #endif
31 #if !defined(cl_word_alignment)
32   #error "Define cl_word_alignment for your CPU!"
33 #endif
34
35
36 // Four basic classes are introduced:
37 //
38 //   gcobject      rcobject
39 //
40 //   gcpointer     rcpointer
41 //
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.
46 //
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.)
55 //
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
58 // overhead.
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.
62
63
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
71 #endif
72
73 // Distinguish immediate data from pointers.
74 inline cl_boolean cl_pointer_p (cl_uint word)
75 {
76         return (cl_boolean)((word & (cl_word_alignment-1)) == 0);
77 }
78 inline cl_boolean cl_immediate_p (cl_uint word)
79 {
80         return (cl_boolean)((word & (cl_word_alignment-1)) != 0);
81 }
82
83 // Immediate data: Fixnum, Short Float, maybe Single Float.
84 // They have type tags.
85 //   |...............................|......|
86 //               cl_value             cl_tag
87
88 // Number of bits reserved for tagging information:
89 #if (cl_word_alignment <= 4)
90   #define cl_tag_len    2
91 #else
92   #define cl_tag_len    3
93 #endif
94 #define cl_tag_shift    0
95 #if (cl_pointer_size == 64)
96   #define cl_value_shift  32
97 #else
98   #define cl_value_shift  (cl_tag_len+cl_tag_shift)
99 #endif
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)
103
104 // Return the tag of a word.
105 inline cl_uint cl_tag (cl_uint word)
106 {
107         return (word & cl_tag_mask) >> cl_tag_shift;
108 }
109
110 // Return the value (unsigned) of a word.
111 inline cl_uint cl_value (cl_uint word)
112 {
113         // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
114         return word >> cl_value_shift;
115 }
116
117 // Return a word, combining a value and a tag.
118 inline cl_uint cl_combine (cl_uint tag, cl_uint value)
119 {
120         return (value << cl_value_shift) + (tag << cl_tag_shift);
121 }
122 inline cl_uint cl_combine (cl_uint tag, cl_sint value)
123 {
124         // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
125         return (value << cl_value_shift) + (tag << cl_tag_shift);
126 }
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); }
132 #ifdef HAVE_LONGLONG
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); }
137 #endif
138
139 // Definition of the tags.
140 #if !defined(CL_WIDE_POINTERS)
141   #if (cl_word_alignment == 2)
142     #define cl_FN_tag   1
143     #define cl_SF_tag   3       // must satisfy the cl_immediate_p predicate!
144   #endif
145   #if (cl_word_alignment == 4)
146     #define cl_FN_tag   1
147     #define cl_SF_tag   2
148   #endif
149 #else // CL_WIDE_POINTERS
150   // Single Floats are immediate as well.
151   #define cl_FN_tag     1
152   #define cl_SF_tag     2
153   #define cl_FF_tag     3
154 #endif
155
156 // Corresponding classes.
157 extern const struct cl_class * cl_immediate_classes [1<<cl_tag_len];
158
159
160 // Heap allocated data contains a header, for two purposes:
161 // - dynamic typing,
162 // - reference count (a portable alternative to garbage collection,
163 //   or the basis for a portable and interoperable garbage collection).
164 struct cl_heap {
165         int refcount;                   // reference count
166         const struct cl_class * type;   // type tag
167 };
168
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);
182
183 struct cl_class {
184         cl_heap_destructor_function destruct;
185         int flags;
186         cl_heap_dprint_function dprint;
187 };
188
189 // Free an object on heap.
190 extern void cl_free_heap_object (cl_heap* pointer);
191
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); }
195
196
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;
203
204 // Increment the reference count.
205 inline void cl_inc_pointer_refcount (cl_heap* pointer)
206 {
207         pointer->refcount++;
208 }
209
210 // Decrement the reference count of a garbage collected pointer.
211 inline void cl_gc_dec_pointer_refcount (cl_heap* pointer)
212 {
213         if (--pointer->refcount == 0)
214                 cl_free_heap_object(pointer);
215 }
216 // Decrement the reference count of a reference counted pointer.
217 inline void cl_rc_dec_pointer_refcount (cl_heap* pointer)
218 {
219         --pointer->refcount;
220 }
221
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();                     \
229
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();                     \
237
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)                  \
245 {                                                               \
246         cl_uint x_word = x.word;                                \
247         cl_inc_refcount(x);                                     \
248         this->word = x_word;                                    \
249 }
250
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)) {}
259
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)   \
263 {                                                               \
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;                                    \
269         return *this;                                           \
270 }
271
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"
285 // error.
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.)
291
292
293 // This is obnoxious.
294 template <class key1_type, class value_type> struct cl_htentry1;
295
296 // The four concrete classes of all objects.
297
298 class cl_gcobject {
299 public: /* ugh */
300         union {
301                 void*   pointer;
302                 cl_heap* heappointer;
303                 cl_uint word;
304         };
305 public:
306 // Default constructor. (Used for objects with no initializer.)
307         cl_gcobject ();
308 // Destructor. (Used when a variable goes out of scope.)
309         ~cl_gcobject ();
310 // Copy constructor.
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__))
333                 : pointer (p) {}
334                 #else
335                 { pointer = p; }
336                 #endif
337 // Debugging output.
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); }
342 };
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)
347
348 class cl_gcpointer {
349 public: /* ugh */
350         union {
351                 void*   pointer;
352                 cl_heap* heappointer;
353                 cl_uint word;
354         };
355 public:
356 // Default constructor. (Used for objects with no initializer.)
357         cl_gcpointer ();
358 // Destructor. (Used when a variable goes out of scope.)
359         ~cl_gcpointer ();
360 // Copy constructor.
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
366                 { return cl_true; }
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__))
383                 : pointer (p) {}
384                 #else
385                 { pointer = p; }
386                 #endif
387 // Debugging output.
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); }
392 };
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)
397
398 class cl_rcobject {
399 public: /* ugh */
400         union {
401                 void*   pointer;
402                 cl_heap* heappointer;
403                 cl_uint word;
404         };
405 public:
406 // Default constructor. (Used for objects with no initializer.)
407         cl_rcobject ();
408 // Destructor. (Used when a variable goes out of scope.)
409         ~cl_rcobject ();
410 // Copy constructor.
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__))
433                 : pointer (p) {}
434                 #else
435                 { pointer = p; }
436                 #endif
437 // Debugging output.
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); }
442 };
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)
447
448 class cl_rcpointer {
449 public: /* ugh */
450         union {
451                 void*   pointer;
452                 cl_heap* heappointer;
453                 cl_uint word;
454         };
455 public:
456 // Default constructor. (Used for objects with no initializer.)
457         cl_rcpointer ();
458 // Destructor. (Used when a variable goes out of scope.)
459         ~cl_rcpointer ();
460 // Copy constructor.
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
466                 { return cl_true; }
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__))
483                 : pointer (p) {}
484                 #else
485                 { pointer = p; }
486                 #endif
487 // Debugging output.
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); }
492 };
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)
497
498 // Private pointer manipulations.
499
500 inline cl_private_thing cl_gcobject::_as_cl_private_thing () const
501 {
502         cl_private_thing p = (cl_private_thing) pointer;
503         cl_inc_refcount(*this);
504         return p;
505 }
506 inline cl_private_thing as_cl_private_thing (const cl_gcobject& x)
507 {
508         return x._as_cl_private_thing();
509 }
510
511 inline cl_private_thing cl_gcpointer::_as_cl_private_thing () const
512 {
513         cl_private_thing p = (cl_private_thing) pointer;
514         cl_inc_refcount(*this);
515         return p;
516 }
517 inline cl_private_thing as_cl_private_thing (const cl_gcpointer& x)
518 {
519         return x._as_cl_private_thing();
520 }
521
522 inline cl_private_thing cl_rcobject::_as_cl_private_thing () const
523 {
524         cl_private_thing p = (cl_private_thing) pointer;
525         cl_inc_refcount(*this);
526         return p;
527 }
528 inline cl_private_thing as_cl_private_thing (const cl_rcobject& x)
529 {
530         return x._as_cl_private_thing();
531 }
532
533 inline cl_private_thing cl_rcpointer::_as_cl_private_thing () const
534 {
535         cl_private_thing p = (cl_private_thing) pointer;
536         cl_inc_refcount(*this);
537         return p;
538 }
539 inline cl_private_thing as_cl_private_thing (const cl_rcpointer& x)
540 {
541         return x._as_cl_private_thing();
542 }
543
544 // Note: When we define a function that returns a class object by value,
545 // we normally return it as const value. The declarations
546 //            T func (...);                    (A)
547 // and
548 //            const T func (...);              (B)
549 // behave identically and generate identical code, except that the code
550 //            func(...) = foo;
551 // compiles fine with (A) but is an error (and yields a warning) with (B).
552 // We want this warning.
553
554 // Define a conversion operator from one object to another object of the
555 // same size.
556   #define CL_DEFINE_CONVERTER(target_class)  \
557     operator const target_class & () const                              \
558     {                                                                   \
559       if (sizeof(*this) != sizeof(target_class)) cl_abort();            \
560       return * (const target_class *) (void*) this;                     \
561     }
562
563 }  // namespace cln
564
565 #endif /* _CL_OBJECT_H */