]> www.ginac.de Git - cln.git/blob - include/cln/object.h
* All Files have been modified for inclusion of namespace cln;
[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 <stdlib.h>
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(__sparc__) || defined(__hppa__) || defined(__arm__) || defined(__rs6000__) || defined(__m88k__) || defined(__convex__)
26   #define cl_word_alignment  4
27 #endif
28 #if defined(__alpha__) || defined(__mips64__) || defined(__sparc64__)
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
133 // Definition of the tags.
134 #if !defined(CL_WIDE_POINTERS)
135   #if (cl_word_alignment == 2)
136     #define cl_FN_tag   1
137     #define cl_SF_tag   3       // must satisfy the cl_immediate_p predicate!
138   #endif
139   #if (cl_word_alignment == 4)
140     #define cl_FN_tag   1
141     #define cl_SF_tag   2
142   #endif
143 #else // CL_WIDE_POINTERS
144   // Single Floats are immediate as well.
145   #define cl_FN_tag     1
146   #define cl_SF_tag     2
147   #define cl_FF_tag     3
148 #endif
149
150 // Corresponding classes.
151 extern const struct cl_class * cl_immediate_classes [1<<cl_tag_len];
152
153
154 // Heap allocated data contains a header, for two purposes:
155 // - dynamic typing,
156 // - reference count (a portable alternative to garbage collection,
157 //   or the basis for a portable and interoperable garbage collection).
158 struct cl_heap {
159         int refcount;                   // reference count
160         const struct cl_class * type;   // type tag
161 };
162
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);
174
175 struct cl_class {
176         cl_heap_destructor_function destruct;
177         int flags;
178         cl_heap_dprint_function dprint;
179 };
180
181 // Free an object on heap.
182 extern void cl_free_heap_object (cl_heap* pointer);
183
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); }
187
188
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;
195
196 // Increment the reference count.
197 inline void cl_inc_pointer_refcount (cl_heap* pointer)
198 {
199         pointer->refcount++;
200 }
201
202 // Decrement the reference count of a garbage collected pointer.
203 inline void cl_gc_dec_pointer_refcount (cl_heap* pointer)
204 {
205         if (--pointer->refcount == 0)
206                 cl_free_heap_object(pointer);
207 }
208 // Decrement the reference count of a reference counted pointer.
209 inline void cl_rc_dec_pointer_refcount (cl_heap* pointer)
210 {
211         --pointer->refcount;
212 }
213
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();                     \
221
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();                     \
229
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)                  \
237 {                                                               \
238         cl_uint x_word = x.word;                                \
239         cl_inc_refcount(x);                                     \
240         word = x_word;                                          \
241 }
242
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)) {}
251
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)   \
255 {                                                               \
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);                                 \
260         word = x_word;                                          \
261         return *this;                                           \
262 }
263
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"
277 // error.
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.)
283
284
285 // This is obnoxious.
286 template <class key1_type, class value_type> struct cl_htentry1;
287
288 // The four concrete classes of all objects.
289
290 class cl_gcobject {
291 public: /* ugh */
292         union {
293                 void*   pointer;
294                 cl_heap* heappointer;
295                 cl_uint word;
296         };
297 public:
298 // Default constructor. (Used for objects with no initializer.)
299         cl_gcobject ();
300 // Destructor. (Used when a variable goes out of scope.)
301         ~cl_gcobject ();
302 // Copy constructor.
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__))
325                 : pointer (p) {}
326                 #else
327                 { pointer = p; }
328                 #endif
329 // Debugging output.
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); }
334 };
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)
339
340 class cl_gcpointer {
341 public: /* ugh */
342         union {
343                 void*   pointer;
344                 cl_heap* heappointer;
345                 cl_uint word;
346         };
347 public:
348 // Default constructor. (Used for objects with no initializer.)
349         cl_gcpointer ();
350 // Destructor. (Used when a variable goes out of scope.)
351         ~cl_gcpointer ();
352 // Copy constructor.
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
358                 { return cl_true; }
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__))
375                 : pointer (p) {}
376                 #else
377                 { pointer = p; }
378                 #endif
379 // Debugging output.
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); }
384 };
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)
389
390 class cl_rcobject {
391 public: /* ugh */
392         union {
393                 void*   pointer;
394                 cl_heap* heappointer;
395                 cl_uint word;
396         };
397 public:
398 // Default constructor. (Used for objects with no initializer.)
399         cl_rcobject ();
400 // Destructor. (Used when a variable goes out of scope.)
401         ~cl_rcobject ();
402 // Copy constructor.
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__))
425                 : pointer (p) {}
426                 #else
427                 { pointer = p; }
428                 #endif
429 // Debugging output.
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); }
434 };
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)
439
440 class cl_rcpointer {
441 public: /* ugh */
442         union {
443                 void*   pointer;
444                 cl_heap* heappointer;
445                 cl_uint word;
446         };
447 public:
448 // Default constructor. (Used for objects with no initializer.)
449         cl_rcpointer ();
450 // Destructor. (Used when a variable goes out of scope.)
451         ~cl_rcpointer ();
452 // Copy constructor.
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
458                 { return cl_true; }
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__))
475                 : pointer (p) {}
476                 #else
477                 { pointer = p; }
478                 #endif
479 // Debugging output.
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); }
484 };
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)
489
490 // Private pointer manipulations.
491
492 inline cl_private_thing cl_gcobject::_as_cl_private_thing () const
493 {
494         cl_private_thing p = (cl_private_thing) pointer;
495         cl_inc_refcount(*this);
496         return p;
497 }
498 inline cl_private_thing as_cl_private_thing (const cl_gcobject& x)
499 {
500         return x._as_cl_private_thing();
501 }
502
503 inline cl_private_thing cl_gcpointer::_as_cl_private_thing () const
504 {
505         cl_private_thing p = (cl_private_thing) pointer;
506         cl_inc_refcount(*this);
507         return p;
508 }
509 inline cl_private_thing as_cl_private_thing (const cl_gcpointer& x)
510 {
511         return x._as_cl_private_thing();
512 }
513
514 inline cl_private_thing cl_rcobject::_as_cl_private_thing () const
515 {
516         cl_private_thing p = (cl_private_thing) pointer;
517         cl_inc_refcount(*this);
518         return p;
519 }
520 inline cl_private_thing as_cl_private_thing (const cl_rcobject& x)
521 {
522         return x._as_cl_private_thing();
523 }
524
525 inline cl_private_thing cl_rcpointer::_as_cl_private_thing () const
526 {
527         cl_private_thing p = (cl_private_thing) pointer;
528         cl_inc_refcount(*this);
529         return p;
530 }
531 inline cl_private_thing as_cl_private_thing (const cl_rcpointer& x)
532 {
533         return x._as_cl_private_thing();
534 }
535
536 // Note: When we define a function that returns a class object by value,
537 // we normally return it as const value. The declarations
538 //            T func (...);                    (A)
539 // and
540 //            const T func (...);              (B)
541 // behave identically and generate identical code, except that the code
542 //            func(...) = foo;
543 // compiles fine with (A) but is an error (and yields a warning) with (B).
544 // We want this warning.
545
546 // Define a conversion operator from one object to another object of the
547 // same size.
548   #define CL_DEFINE_CONVERTER(target_class)  \
549     operator const target_class & () const                              \
550     {                                                                   \
551       if (sizeof(*this) != sizeof(target_class)) cl_abort();            \
552       return * (const target_class *) (void*) this;                     \
553     }
554
555 }  // namespace cln
556
557 #endif /* _CL_OBJECT_H */