File indexing completed on 2026-05-19 08:08:31
0001
0002
0003 #ifndef _CL_OBJECT_H
0004 #define _CL_OBJECT_H
0005
0006 #include "cln/types.h"
0007 #include "cln/modules.h"
0008 #include <cstdlib>
0009
0010 namespace cln {
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 typedef sintP cl_sint;
0049 typedef uintP cl_uint;
0050 #define cl_pointer_size intPsize
0051
0052 #if (cl_pointer_size==64)
0053 #define CL_WIDE_POINTERS
0054 #endif
0055
0056
0057 inline bool cl_pointer_p (cl_uint word)
0058 {
0059 return (word & (cl_word_alignment-1)) == 0;
0060 }
0061 inline bool cl_immediate_p (cl_uint word)
0062 {
0063 return (word & (cl_word_alignment-1)) != 0;
0064 }
0065
0066
0067
0068
0069
0070
0071
0072 #if (cl_word_alignment <= 4)
0073 #define cl_tag_len 2
0074 #else
0075 #define cl_tag_len 3
0076 #endif
0077 #define cl_tag_shift 0
0078 #define cl_value_shift (cl_tag_len+cl_tag_shift)
0079 #define cl_value_len (cl_pointer_size - cl_value_shift)
0080 #define cl_tag_mask ((((cl_uint)1 << cl_tag_len) - 1) << cl_tag_shift)
0081 #define cl_value_mask ((((cl_uint)1 << cl_value_len) - 1) << cl_value_shift)
0082
0083
0084 inline cl_uint cl_tag (cl_uint word)
0085 {
0086 return (word & cl_tag_mask) >> cl_tag_shift;
0087 }
0088
0089
0090 inline cl_uint cl_value (cl_uint word)
0091 {
0092
0093 return word >> cl_value_shift;
0094 }
0095
0096
0097 inline cl_uint cl_combine_impl (cl_uint tag, cl_uint value)
0098 {
0099 return (value << cl_value_shift) + (tag << cl_tag_shift);
0100 }
0101 inline cl_uint cl_combine_impl (cl_uint tag, cl_sint value)
0102 {
0103
0104 return (value << cl_value_shift) + (tag << cl_tag_shift);
0105 }
0106 inline cl_uint cl_combine (cl_uint tag, unsigned int value)
0107 { return cl_combine_impl(tag, (cl_uint)value); }
0108 inline cl_uint cl_combine (cl_uint tag, int value)
0109 { return cl_combine_impl(tag, (cl_sint)value); }
0110 inline cl_uint cl_combine (cl_uint tag, unsigned long value)
0111 { return cl_combine_impl(tag, (cl_uint)value); }
0112 inline cl_uint cl_combine (cl_uint tag, long value)
0113 { return cl_combine_impl(tag, (cl_sint)value); }
0114 inline cl_uint cl_combine (cl_uint tag, unsigned long long value)
0115 { return cl_combine_impl(tag, (cl_uint)value); }
0116 inline cl_uint cl_combine (cl_uint tag, long long value)
0117 { return cl_combine_impl(tag, (cl_uint)value); }
0118
0119
0120 #if !defined(CL_WIDE_POINTERS)
0121 #if (cl_word_alignment == 2)
0122 #define cl_FN_tag 1
0123 #define cl_SF_tag 3
0124 #endif
0125 #if (cl_word_alignment == 4)
0126 #define cl_FN_tag 1
0127 #define cl_SF_tag 2
0128 #endif
0129 #else
0130
0131 #define cl_FN_tag 1
0132 #define cl_SF_tag 2
0133 #define cl_FF_tag 3
0134 #endif
0135
0136
0137 extern const struct cl_class * cl_immediate_classes [1<<cl_tag_len];
0138
0139
0140
0141
0142
0143
0144 struct cl_heap {
0145 int refcount;
0146 const struct cl_class * type;
0147 };
0148
0149
0150 typedef void (*cl_heap_destructor_function) (cl_heap* pointer);
0151
0152 #define cl_class_flags_subclass_complex 1
0153 #define cl_class_flags_subclass_real 2
0154 #define cl_class_flags_subclass_float 4
0155 #define cl_class_flags_subclass_rational 8
0156 #define cl_class_flags_number_ring 16
0157
0158 #define cl_class_flags_modint_ring 32
0159
0160 #define cl_class_flags_univpoly_ring 64
0161
0162
0163 typedef void (*cl_heap_dprint_function) (cl_heap* pointer);
0164
0165 struct cl_class {
0166 cl_heap_destructor_function destruct;
0167 int flags;
0168 cl_heap_dprint_function dprint;
0169 };
0170
0171
0172 extern void cl_free_heap_object (cl_heap* pointer);
0173
0174
0175 #define cl_register_type_printer(type,printer) \
0176 { extern cl_class type; type.dprint = (printer); }
0177
0178
0179
0180
0181
0182
0183
0184 typedef struct cl_anything * cl_private_thing;
0185
0186
0187 inline void cl_inc_pointer_refcount (cl_heap* pointer)
0188 {
0189 pointer->refcount++;
0190 }
0191
0192
0193 inline void cl_gc_dec_pointer_refcount (cl_heap* pointer)
0194 {
0195 if (--pointer->refcount == 0)
0196 cl_free_heap_object(pointer);
0197 }
0198
0199 inline void cl_rc_dec_pointer_refcount (cl_heap* pointer)
0200 {
0201 --pointer->refcount;
0202 }
0203
0204
0205
0206
0207
0208 #define cl_inc_refcount(x) \
0209 if ((x).pointer_p()) \
0210 (x).inc_pointer_refcount(); \
0211
0212
0213
0214
0215
0216 #define cl_dec_refcount(x) \
0217 if ((x).pointer_p()) \
0218 (x).dec_pointer_refcount(); \
0219
0220
0221
0222
0223 #define CL_DEFINE_COPY_CONSTRUCTOR1(_class_) \
0224 _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_class_)
0225 #define _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_classname_) \
0226 inline _class_::_classname_ (const _class_& x) \
0227 { \
0228 cl_uint x_word = x.word; \
0229 cl_inc_refcount(x); \
0230 this->word = x_word; \
0231 }
0232
0233
0234
0235
0236 #define CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_baseclass_) \
0237 _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_class_,_baseclass_)
0238 #define _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_classname_,_baseclass_) \
0239 inline _class_::_classname_ (const _class_& x) \
0240 : _baseclass_ (as_cl_private_thing(x)) {}
0241
0242
0243 #define CL_DEFINE_ASSIGNMENT_OPERATOR(dest_class,src_class) \
0244 inline dest_class& dest_class::operator= (const src_class& x) \
0245 { \
0246 \
0247 cl_uint x_word = x.word; \
0248 cl_inc_refcount(x); \
0249 cl_dec_refcount(*this); \
0250 this->word = x_word; \
0251 return *this; \
0252 }
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276 template <class key1_type, class value_type> struct cl_htentry1;
0277
0278
0279
0280 class cl_gcobject {
0281 public:
0282 union {
0283 void* pointer;
0284 cl_heap* heappointer;
0285 cl_uint word;
0286 };
0287 public:
0288
0289 cl_gcobject ();
0290
0291 ~cl_gcobject ();
0292
0293 cl_gcobject (const cl_gcobject&);
0294
0295 cl_gcobject& operator= (const cl_gcobject&);
0296
0297 bool pointer_p() const
0298 { return cl_pointer_p(word); }
0299
0300 void inc_pointer_refcount () const
0301 { cl_inc_pointer_refcount(heappointer); }
0302 void dec_pointer_refcount () const
0303 { cl_gc_dec_pointer_refcount(heappointer); }
0304
0305 cl_uint nonpointer_tag () const
0306 { return cl_tag(word); }
0307
0308 const cl_class * pointer_type () const
0309 { return heappointer->type; }
0310
0311 cl_private_thing _as_cl_private_thing () const;
0312
0313 cl_gcobject (cl_private_thing p)
0314 : pointer (p) {}
0315
0316 void debug_print () const;
0317
0318 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
0319 void* operator new (size_t size) { return ::operator new (size); }
0320 };
0321 inline cl_gcobject::cl_gcobject () {}
0322 inline cl_gcobject::~cl_gcobject () { cl_dec_refcount(*this); }
0323 CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcobject)
0324 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcobject,cl_gcobject)
0325
0326 class cl_gcpointer {
0327 public:
0328 union {
0329 void* pointer;
0330 cl_heap* heappointer;
0331 cl_uint word;
0332 };
0333 public:
0334
0335 cl_gcpointer ();
0336
0337 ~cl_gcpointer ();
0338
0339 cl_gcpointer (const cl_gcpointer&);
0340
0341 cl_gcpointer& operator= (const cl_gcpointer&);
0342
0343 bool pointer_p() const
0344 { return true; }
0345
0346 void inc_pointer_refcount () const
0347 { cl_inc_pointer_refcount(heappointer); }
0348 void dec_pointer_refcount () const
0349 { cl_gc_dec_pointer_refcount(heappointer); }
0350
0351 cl_uint nonpointer_tag () const
0352 { return cl_tag(word); }
0353
0354 const cl_class * pointer_type () const
0355 { return heappointer->type; }
0356
0357 cl_private_thing _as_cl_private_thing () const;
0358
0359 cl_gcpointer (cl_private_thing p)
0360 : pointer (p) {}
0361
0362 void debug_print () const;
0363
0364 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
0365 void* operator new (size_t size) { return ::operator new (size); }
0366 };
0367 inline cl_gcpointer::cl_gcpointer () {}
0368 inline cl_gcpointer::~cl_gcpointer () { cl_dec_refcount(*this); }
0369 CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcpointer)
0370 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcpointer,cl_gcpointer)
0371
0372 class cl_rcobject {
0373 public:
0374 union {
0375 void* pointer;
0376 cl_heap* heappointer;
0377 cl_uint word;
0378 };
0379 public:
0380
0381 cl_rcobject ();
0382
0383 ~cl_rcobject ();
0384
0385 cl_rcobject (const cl_rcobject&);
0386
0387 cl_rcobject& operator= (const cl_rcobject&);
0388
0389 bool pointer_p() const
0390 { return cl_pointer_p(word); }
0391
0392 void inc_pointer_refcount () const
0393 { cl_inc_pointer_refcount(heappointer); }
0394 void dec_pointer_refcount () const
0395 { cl_rc_dec_pointer_refcount(heappointer); }
0396
0397 cl_uint nonpointer_tag () const
0398 { return cl_tag(word); }
0399
0400 const cl_class * pointer_type () const
0401 { return heappointer->type; }
0402
0403 cl_private_thing _as_cl_private_thing () const;
0404
0405 cl_rcobject (cl_private_thing p)
0406 : pointer (p) {}
0407
0408 void debug_print () const;
0409
0410 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
0411 void* operator new (size_t size) { return ::operator new (size); }
0412 };
0413 inline cl_rcobject::cl_rcobject () {}
0414 inline cl_rcobject::~cl_rcobject () { cl_dec_refcount(*this); }
0415 CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcobject)
0416 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcobject,cl_rcobject)
0417
0418 class cl_rcpointer {
0419 public:
0420 union {
0421 void* pointer;
0422 cl_heap* heappointer;
0423 cl_uint word;
0424 };
0425 public:
0426
0427 cl_rcpointer ();
0428
0429 ~cl_rcpointer ();
0430
0431 cl_rcpointer (const cl_rcpointer&);
0432
0433 cl_rcpointer& operator= (const cl_rcpointer&);
0434
0435 bool pointer_p() const
0436 { return true; }
0437
0438 void inc_pointer_refcount () const
0439 { cl_inc_pointer_refcount(heappointer); }
0440 void dec_pointer_refcount () const
0441 { cl_rc_dec_pointer_refcount(heappointer); }
0442
0443 cl_uint nonpointer_tag () const
0444 { return cl_tag(word); }
0445
0446 const cl_class * pointer_type () const
0447 { return heappointer->type; }
0448
0449 cl_private_thing _as_cl_private_thing () const;
0450
0451 cl_rcpointer (cl_private_thing p)
0452 : pointer (p) {}
0453
0454 void debug_print () const;
0455
0456 void* operator new (size_t size, void* ptr) { (void)size; return ptr; }
0457 void* operator new (size_t size) { return ::operator new (size); }
0458 };
0459 inline cl_rcpointer::cl_rcpointer () {}
0460 inline cl_rcpointer::~cl_rcpointer () { cl_dec_refcount(*this); }
0461 CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcpointer)
0462 CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcpointer,cl_rcpointer)
0463
0464
0465
0466 inline cl_private_thing cl_gcobject::_as_cl_private_thing () const
0467 {
0468 cl_private_thing p = (cl_private_thing) pointer;
0469 cl_inc_refcount(*this);
0470 return p;
0471 }
0472 inline cl_private_thing as_cl_private_thing (const cl_gcobject& x)
0473 {
0474 return x._as_cl_private_thing();
0475 }
0476
0477 inline cl_private_thing cl_gcpointer::_as_cl_private_thing () const
0478 {
0479 cl_private_thing p = (cl_private_thing) pointer;
0480 cl_inc_refcount(*this);
0481 return p;
0482 }
0483 inline cl_private_thing as_cl_private_thing (const cl_gcpointer& x)
0484 {
0485 return x._as_cl_private_thing();
0486 }
0487
0488 inline cl_private_thing cl_rcobject::_as_cl_private_thing () const
0489 {
0490 cl_private_thing p = (cl_private_thing) pointer;
0491 cl_inc_refcount(*this);
0492 return p;
0493 }
0494 inline cl_private_thing as_cl_private_thing (const cl_rcobject& x)
0495 {
0496 return x._as_cl_private_thing();
0497 }
0498
0499 inline cl_private_thing cl_rcpointer::_as_cl_private_thing () const
0500 {
0501 cl_private_thing p = (cl_private_thing) pointer;
0502 cl_inc_refcount(*this);
0503 return p;
0504 }
0505 inline cl_private_thing as_cl_private_thing (const cl_rcpointer& x)
0506 {
0507 return x._as_cl_private_thing();
0508 }
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522 #define CL_DEFINE_CONVERTER(target_class) \
0523 operator const target_class & () const \
0524 { \
0525 int (*dummy1)(int assert1 [2*(sizeof(target_class)==sizeof(*this))-1]); (void)dummy1; \
0526 return * (const target_class *) (void*) this; \
0527 }
0528
0529 }
0530
0531 #endif