]> www.ginac.de Git - cln.git/blob - include/cln/object.h
Remove exception hooks in favor of real C++ exceptions:
[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 #define cl_value_shift  (cl_tag_len+cl_tag_shift)
96 #define cl_value_len    (cl_pointer_size - cl_value_shift)
97 #define cl_tag_mask     (((1UL << cl_tag_len) - 1) << cl_tag_shift)
98 #define cl_value_mask   (((1UL << cl_value_len) - 1) << cl_value_shift)
99
100 // Return the tag of a word.
101 inline cl_uint cl_tag (cl_uint word)
102 {
103         return (word & cl_tag_mask) >> cl_tag_shift;
104 }
105
106 // Return the value (unsigned) of a word.
107 inline cl_uint cl_value (cl_uint word)
108 {
109         // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
110         return word >> cl_value_shift;
111 }
112
113 // Return a word, combining a value and a tag.
114 inline cl_uint cl_combine (cl_uint tag, cl_uint value)
115 {
116         return (value << cl_value_shift) + (tag << cl_tag_shift);
117 }
118 inline cl_uint cl_combine (cl_uint tag, cl_sint value)
119 {
120         // This assumes cl_value_shift + cl_value_len == cl_pointer_size.
121         return (value << cl_value_shift) + (tag << cl_tag_shift);
122 }
123 // Keep the compiler happy.
124 inline cl_uint cl_combine (cl_uint tag, unsigned int value)
125 { return cl_combine(tag, (cl_uint)value); }
126 inline cl_uint cl_combine (cl_uint tag, int value)
127 { return cl_combine(tag, (cl_sint)value); }
128 #ifdef HAVE_LONGLONG
129 inline cl_uint cl_combine (cl_uint tag, unsigned long long value)
130 { return cl_combine(tag, (cl_uint)value); }
131 inline cl_uint cl_combine (cl_uint tag, long long value)
132 { return cl_combine(tag, (cl_uint)value); }
133 #endif
134
135 // Definition of the tags.
136 #if !defined(CL_WIDE_POINTERS)
137   #if (cl_word_alignment == 2)
138     #define cl_FN_tag   1
139     #define cl_SF_tag   3       // must satisfy the cl_immediate_p predicate!
140   #endif
141   #if (cl_word_alignment == 4)
142     #define cl_FN_tag   1
143     #define cl_SF_tag   2
144   #endif
145 #else // CL_WIDE_POINTERS
146   // Single Floats are immediate as well.
147   #define cl_FN_tag     1
148   #define cl_SF_tag     2
149   #define cl_FF_tag     3
150 #endif
151
152 // Corresponding classes.
153 extern const struct cl_class * cl_immediate_classes [1<<cl_tag_len];
154
155
156 // Heap allocated data contains a header, for two purposes:
157 // - dynamic typing,
158 // - reference count (a portable alternative to garbage collection,
159 //   or the basis for a portable and interoperable garbage collection).
160 struct cl_heap {
161         int refcount;                   // reference count
162         const struct cl_class * type;   // type tag
163 };
164
165 // Function to destroy the contents of a heap object.
166 typedef void (*cl_heap_destructor_function) (cl_heap* pointer);
167 // Flags, may be ORed together.
168 #define cl_class_flags_subclass_complex   1  // all instances belong to cl_N
169 #define cl_class_flags_subclass_real      2  // all instances belong to cl_R
170 #define cl_class_flags_subclass_float     4  // all instances belong to cl_F
171 #define cl_class_flags_subclass_rational  8  // all instances belong to cl_RA
172 #define cl_class_flags_number_ring       16  // all instances are rings whose
173                                              // elements belong to cl_number
174 #define cl_class_flags_modint_ring       32  // all instances are rings whose
175                                              // elements belong to cl_MI
176 #define cl_class_flags_univpoly_ring     64  // all instances are rings whose
177                                              // elements belong to cl_UP
178 // Function to print an object for debugging, to cerr.
179 typedef void (*cl_heap_dprint_function) (cl_heap* pointer);
180
181 struct cl_class {
182         cl_heap_destructor_function destruct;
183         int flags;
184         cl_heap_dprint_function dprint;
185 };
186
187 // Free an object on heap.
188 extern void cl_free_heap_object (cl_heap* pointer);
189
190 // Debugging support for dynamic typing: Register a debugging print function.
191 #define cl_register_type_printer(type,printer)  \
192   { extern cl_class type; type.dprint = (printer); }
193
194
195 // cl_private_thing: An immediate value or a pointer into the heap.
196 // This must be as wide as a `cl_uint'.
197 // (Actually, this ought to be a  union { void*; cl_uint; }, but using
198 // a pointer type generates better code.)
199 // Never throw away a cl_private_thing, or reference counts will be wrong!
200 typedef struct cl_anything * cl_private_thing;
201
202 // Increment the reference count.
203 inline void cl_inc_pointer_refcount (cl_heap* pointer)
204 {
205         pointer->refcount++;
206 }
207
208 // Decrement the reference count of a garbage collected pointer.
209 inline void cl_gc_dec_pointer_refcount (cl_heap* pointer)
210 {
211         if (--pointer->refcount == 0)
212                 cl_free_heap_object(pointer);
213 }
214 // Decrement the reference count of a reference counted pointer.
215 inline void cl_rc_dec_pointer_refcount (cl_heap* pointer)
216 {
217         --pointer->refcount;
218 }
219
220 // Increment the reference count.
221 // This must be a macro, not an inline function, because pointer_p() and
222 // inc_pointer_refcount() are non-virtual member functions, so that the
223 // compiler can optimize it.
224 #define cl_inc_refcount(x)  \
225         if ((x).pointer_p())                                    \
226                 (x).inc_pointer_refcount();                     \
227
228 // Decrement the reference count.
229 // This must be a macro, not an inline function, because pointer_p() and
230 // dec_pointer_refcount() are non-virtual member functions, so that the
231 // compiler can optimize it.
232 #define cl_dec_refcount(x)  \
233         if ((x).pointer_p())                                    \
234                 (x).dec_pointer_refcount();                     \
235
236 // The declaration of a copy constructor.
237 // Restriction: The base class's default constructor must do nothing or
238 // initialize `pointer' to a constant expression.
239 #define CL_DEFINE_COPY_CONSTRUCTOR1(_class_)                    \
240         _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_class_)
241 #define _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_classname_)       \
242 inline _class_::_classname_ (const _class_& x)                  \
243 {                                                               \
244         cl_uint x_word = x.word;                                \
245         cl_inc_refcount(x);                                     \
246         this->word = x_word;                                    \
247 }
248
249 // The declaration of a copy constructor.
250 // Restriction: The base class must have the usual `cl_private_thing'
251 // constructor. Drawback: The base class must be known here.
252 #define CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_baseclass_)                \
253         _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_class_,_baseclass_)
254 #define _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_classname_,_baseclass_) \
255 inline _class_::_classname_ (const _class_& x)                  \
256         : _baseclass_ (as_cl_private_thing(x)) {}
257
258 // The declaration of an assignment operator.
259 #define CL_DEFINE_ASSIGNMENT_OPERATOR(dest_class,src_class)     \
260 inline dest_class& dest_class::operator= (const src_class& x)   \
261 {                                                               \
262         /* Be careful, we might be assigning x to itself. */    \
263         cl_uint x_word = x.word;                                \
264         cl_inc_refcount(x);                                     \
265         cl_dec_refcount(*this);                                 \
266         this->word = x_word;                                    \
267         return *this;                                           \
268 }
269
270 // We have a small problem with destructors: The specialized destructor
271 // of a leaf class such as `cl_SF' should be more efficient than the
272 // general destructor for `cl_N'. Since (by C++ specs) destructing a cl_SF
273 // would run the destructors for cl_SF, cl_F, cl_R, cl_N (in that order),
274 // and in the last step the compiler does not know any more that the object
275 // actually is a cl_SF, there is no way to optimize the destructor!
276 // ("progn-reversed" method combination is evil.)
277 // And if we define "mirror"/"shadow" classes with no destructors (such
278 // that `cl_F' inherits from `cl_F_no_destructor' buts adds a destructor)
279 // then we need to add explicit conversion operators cl_SF -> cl_F -> cl_R ...,
280 // with the effect that calling an overloaded function like `as_cl_F'
281 // (which has two signatures `as_cl_F(cl_number)' and `as_cl_F(cl_F)')
282 // with a cl_SF argument gives an "call of overloaded function is ambiguous"
283 // error.
284 // There is no help: If we want overloaded functions to be callable in a way
285 // that makes sense, `cl_SF' has to be a subclass of `cl_F', and then the
286 // destructor of `cl_SF' will do at least as much computation as the `cl_F'
287 // destructor. Praise C++ ! :-((
288 // (Even making `pointer_p()' a virtual function would not help.)
289
290
291 // This is obnoxious.
292 template <class key1_type, class value_type> struct cl_htentry1;
293
294 // The four concrete classes of all objects.
295
296 class cl_gcobject {
297 public: /* ugh */
298         union {
299                 void*   pointer;
300                 cl_heap* heappointer;
301                 cl_uint word;
302         };
303 public:
304 // Default constructor. (Used for objects with no initializer.)
305         cl_gcobject ();
306 // Destructor. (Used when a variable goes out of scope.)
307         ~cl_gcobject ();
308 // Copy constructor.
309         cl_gcobject (const cl_gcobject&);
310 // Assignment operator.
311         cl_gcobject& operator= (const cl_gcobject&);
312 // Distinguish immediate data from pointer.
313         cl_boolean pointer_p() const
314                 { return cl_pointer_p(word); }
315 // Reference counting.
316         void inc_pointer_refcount () const
317                 { cl_inc_pointer_refcount(heappointer); }
318         void dec_pointer_refcount () const
319                 { cl_gc_dec_pointer_refcount(heappointer); }
320 // Return the type tag of an immediate number.
321         cl_uint nonpointer_tag () const
322                 { return cl_tag(word); }
323 // Return the type tag of a heap-allocated number.
324         const cl_class * pointer_type () const
325                 { return heappointer->type; }
326 // Private pointer manipulations.
327         cl_private_thing _as_cl_private_thing () const;
328 // Private constructor.
329         cl_gcobject (cl_private_thing p)
330                 #if !(defined(__alpha__) && !defined(__GNUC__))
331                 : pointer (p) {}
332                 #else
333                 { pointer = p; }
334                 #endif
335 // Debugging output.
336         void debug_print () const;
337 // Ability to place an object at a given address.
338         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
339         void* operator new (size_t size) { return ::operator new (size); }
340 };
341 inline cl_gcobject::cl_gcobject () {}
342 inline cl_gcobject::~cl_gcobject () { cl_dec_refcount(*this); }
343 CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcobject)
344 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcobject,cl_gcobject)
345
346 class cl_gcpointer {
347 public: /* ugh */
348         union {
349                 void*   pointer;
350                 cl_heap* heappointer;
351                 cl_uint word;
352         };
353 public:
354 // Default constructor. (Used for objects with no initializer.)
355         cl_gcpointer ();
356 // Destructor. (Used when a variable goes out of scope.)
357         ~cl_gcpointer ();
358 // Copy constructor.
359         cl_gcpointer (const cl_gcpointer&);
360 // Assignment operator.
361         cl_gcpointer& operator= (const cl_gcpointer&);
362 // Distinguish immediate data from pointer.
363         cl_boolean pointer_p() const
364                 { return cl_true; }
365 // Reference counting.
366         void inc_pointer_refcount () const
367                 { cl_inc_pointer_refcount(heappointer); }
368         void dec_pointer_refcount () const
369                 { cl_gc_dec_pointer_refcount(heappointer); }
370 // Return the type tag of an immediate number.
371         cl_uint nonpointer_tag () const
372                 { return cl_tag(word); }
373 // Return the type tag of a heap-allocated number.
374         const cl_class * pointer_type () const
375                 { return heappointer->type; }
376 // Private pointer manipulations.
377         cl_private_thing _as_cl_private_thing () const;
378 // Private constructor.
379         cl_gcpointer (cl_private_thing p)
380                 #if !(defined(__alpha__) && !defined(__GNUC__))
381                 : pointer (p) {}
382                 #else
383                 { pointer = p; }
384                 #endif
385 // Debugging output.
386         void debug_print () const;
387 // Ability to place an object at a given address.
388         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
389         void* operator new (size_t size) { return ::operator new (size); }
390 };
391 inline cl_gcpointer::cl_gcpointer () {}
392 inline cl_gcpointer::~cl_gcpointer () { cl_dec_refcount(*this); }
393 CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcpointer)
394 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcpointer,cl_gcpointer)
395
396 class cl_rcobject {
397 public: /* ugh */
398         union {
399                 void*   pointer;
400                 cl_heap* heappointer;
401                 cl_uint word;
402         };
403 public:
404 // Default constructor. (Used for objects with no initializer.)
405         cl_rcobject ();
406 // Destructor. (Used when a variable goes out of scope.)
407         ~cl_rcobject ();
408 // Copy constructor.
409         cl_rcobject (const cl_rcobject&);
410 // Assignment operator.
411         cl_rcobject& operator= (const cl_rcobject&);
412 // Distinguish immediate data from pointer.
413         cl_boolean pointer_p() const
414                 { return cl_pointer_p(word); }
415 // Reference counting.
416         void inc_pointer_refcount () const
417                 { cl_inc_pointer_refcount(heappointer); }
418         void dec_pointer_refcount () const
419                 { cl_rc_dec_pointer_refcount(heappointer); }
420 // Return the type tag of an immediate number.
421         cl_uint nonpointer_tag () const
422                 { return cl_tag(word); }
423 // Return the type tag of a heap-allocated number.
424         const cl_class * pointer_type () const
425                 { return heappointer->type; }
426 // Private pointer manipulations.
427         cl_private_thing _as_cl_private_thing () const;
428 // Private constructor.
429         cl_rcobject (cl_private_thing p)
430                 #if !(defined(__alpha__) && !defined(__GNUC__))
431                 : pointer (p) {}
432                 #else
433                 { pointer = p; }
434                 #endif
435 // Debugging output.
436         void debug_print () const;
437 // Ability to place an object at a given address.
438         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
439         void* operator new (size_t size) { return ::operator new (size); }
440 };
441 inline cl_rcobject::cl_rcobject () {}
442 inline cl_rcobject::~cl_rcobject () { cl_dec_refcount(*this); }
443 CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcobject)
444 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcobject,cl_rcobject)
445
446 class cl_rcpointer {
447 public: /* ugh */
448         union {
449                 void*   pointer;
450                 cl_heap* heappointer;
451                 cl_uint word;
452         };
453 public:
454 // Default constructor. (Used for objects with no initializer.)
455         cl_rcpointer ();
456 // Destructor. (Used when a variable goes out of scope.)
457         ~cl_rcpointer ();
458 // Copy constructor.
459         cl_rcpointer (const cl_rcpointer&);
460 // Assignment operator.
461         cl_rcpointer& operator= (const cl_rcpointer&);
462 // Distinguish immediate data from pointer.
463         cl_boolean pointer_p() const
464                 { return cl_true; }
465 // Reference counting.
466         void inc_pointer_refcount () const
467                 { cl_inc_pointer_refcount(heappointer); }
468         void dec_pointer_refcount () const
469                 { cl_rc_dec_pointer_refcount(heappointer); }
470 // Return the type tag of an immediate number.
471         cl_uint nonpointer_tag () const
472                 { return cl_tag(word); }
473 // Return the type tag of a heap-allocated number.
474         const cl_class * pointer_type () const
475                 { return heappointer->type; }
476 // Private pointer manipulations.
477         cl_private_thing _as_cl_private_thing () const;
478 // Private constructor.
479         cl_rcpointer (cl_private_thing p)
480                 #if !(defined(__alpha__) && !defined(__GNUC__))
481                 : pointer (p) {}
482                 #else
483                 { pointer = p; }
484                 #endif
485 // Debugging output.
486         void debug_print () const;
487 // Ability to place an object at a given address.
488         void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
489         void* operator new (size_t size) { return ::operator new (size); }
490 };
491 inline cl_rcpointer::cl_rcpointer () {}
492 inline cl_rcpointer::~cl_rcpointer () { cl_dec_refcount(*this); }
493 CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcpointer)
494 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcpointer,cl_rcpointer)
495
496 // Private pointer manipulations.
497
498 inline cl_private_thing cl_gcobject::_as_cl_private_thing () const
499 {
500         cl_private_thing p = (cl_private_thing) pointer;
501         cl_inc_refcount(*this);
502         return p;
503 }
504 inline cl_private_thing as_cl_private_thing (const cl_gcobject& x)
505 {
506         return x._as_cl_private_thing();
507 }
508
509 inline cl_private_thing cl_gcpointer::_as_cl_private_thing () const
510 {
511         cl_private_thing p = (cl_private_thing) pointer;
512         cl_inc_refcount(*this);
513         return p;
514 }
515 inline cl_private_thing as_cl_private_thing (const cl_gcpointer& x)
516 {
517         return x._as_cl_private_thing();
518 }
519
520 inline cl_private_thing cl_rcobject::_as_cl_private_thing () const
521 {
522         cl_private_thing p = (cl_private_thing) pointer;
523         cl_inc_refcount(*this);
524         return p;
525 }
526 inline cl_private_thing as_cl_private_thing (const cl_rcobject& x)
527 {
528         return x._as_cl_private_thing();
529 }
530
531 inline cl_private_thing cl_rcpointer::_as_cl_private_thing () const
532 {
533         cl_private_thing p = (cl_private_thing) pointer;
534         cl_inc_refcount(*this);
535         return p;
536 }
537 inline cl_private_thing as_cl_private_thing (const cl_rcpointer& x)
538 {
539         return x._as_cl_private_thing();
540 }
541
542 // Note: When we define a function that returns a class object by value,
543 // we normally return it as const value. The declarations
544 //            T func (...);                    (A)
545 // and
546 //            const T func (...);              (B)
547 // behave identically and generate identical code, except that the code
548 //            func(...) = foo;
549 // compiles fine with (A) but is an error (and yields a warning) with (B).
550 // We want this warning.
551
552 // Define a conversion operator from one object to another object of the
553 // same size.
554   #define CL_DEFINE_CONVERTER(target_class)  \
555     operator const target_class & () const                              \
556     {                                                                   \
557       typedef int assert1 [2*(sizeof(target_class)==sizeof(*this))-1];  \
558       return * (const target_class *) (void*) this;                     \
559     }
560
561 }  // namespace cln
562
563 #endif /* _CL_OBJECT_H */