You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
19 KiB

25 years ago
  1. // General object definitions: pointers, reference counting, garbage collection.
  2. #ifndef _CL_OBJECT_H
  3. #define _CL_OBJECT_H
  4. #include "cl_types.h"
  5. #include "cl_modules.h"
  6. #include <stdlib.h>
  7. // We don't have to deal with circular structures, so normal reference counting
  8. // is sufficient. Is also has the advantage of being mostly non-interrupting.
  9. // An object is either a pointer to heap allocated data
  10. // or immediate data.
  11. // It is possible to distinguish these because pointers are aligned.
  12. // cl_uint_alignment is the guaranteed alignment of a `void*' or `long'
  13. // in memory. Must be > 1.
  14. #if defined(__m68k__)
  15. #define cl_word_alignment 2
  16. #endif
  17. #if defined(__i386__) || defined(__mips__) || defined(__sparc__) || defined(__hppa__) || defined(__arm__) || defined(__rs6000__) || defined(__m88k__) || defined(__convex__)
  18. #define cl_word_alignment 4
  19. #endif
  20. #if defined(__alpha__) || defined(__mips64__) || defined(__sparc64__)
  21. #define cl_word_alignment 8
  22. #endif
  23. #if !defined(cl_word_alignment)
  24. #error "Define cl_word_alignment for your CPU!"
  25. #endif
  26. // Four basic classes are introduced:
  27. //
  28. // gcobject rcobject
  29. //
  30. // gcpointer rcpointer
  31. //
  32. // `gcobject' = garbage collectible object (pointer or immediate),
  33. // `gcpointer' = garbage collectible pointer,
  34. // `rcobject' = reference counted object (pointer or immediate),
  35. // `rcpointer' = reference counted pointer.
  36. //
  37. // "garbage collectible" means that a reference count is maintained, and
  38. // when the reference count drops to 0, the object is freed. This is useful
  39. // for all kind of short- or long-lived objects.
  40. // "reference counted" means that a reference count is maintained, which
  41. // cannot drop to 0. This is useful for objects which are registered in a
  42. // global cache table, in order to know which objects can be thrown away
  43. // when the cache is cleaned. (If the cache were never cleaned, its objects
  44. // would never be freed, and we could get away with normal C pointers.)
  45. //
  46. // It is permissible to treat a `rcobject' as a `gcobject', and a `rcpointer'
  47. // as a `gcpointer', but this just increases the destructor and copy-constructor
  48. // overhead.
  49. // It is also permissible to treat a `gcpointer' as a `gcobject', and a
  50. // `rcpointer' as a `rcobject', but this just increases the destructor and
  51. // copy-constructor overhead.
  52. // Immediate data is a word, as wide as a pointer.
  53. typedef sintP cl_sint;
  54. typedef uintP cl_uint; // This ought to be called `cl_word'.
  55. #define cl_word_size intPsize
  56. // NB: (cl_word_size==64) implies defined(HAVE_FAST_LONGLONG)
  57. #if (cl_word_size==64)
  58. #define CL_WIDE_POINTERS
  59. #endif
  60. // Distinguish immediate data from pointers.
  61. inline cl_boolean cl_pointer_p (cl_uint word)
  62. {
  63. return (cl_boolean)((word & (cl_word_alignment-1)) == 0);
  64. }
  65. inline cl_boolean cl_immediate_p (cl_uint word)
  66. {
  67. return (cl_boolean)((word & (cl_word_alignment-1)) != 0);
  68. }
  69. // Immediate data: Fixnum, Short Float, maybe Single Float.
  70. // They have type tags.
  71. // |...............................|......|
  72. // cl_value cl_tag
  73. // Number of bits reserved for tagging information:
  74. #if (cl_word_alignment <= 4)
  75. #define cl_tag_len 2
  76. #else
  77. #define cl_tag_len 3
  78. #endif
  79. #define cl_tag_shift 0
  80. #if (cl_word_size == 64)
  81. #define cl_value_shift 32
  82. #else
  83. #define cl_value_shift (cl_tag_len+cl_tag_shift)
  84. #endif
  85. #define cl_value_len (cl_word_size - cl_value_shift)
  86. #define cl_tag_mask (((1UL << cl_tag_len) - 1) << cl_tag_shift)
  87. #define cl_value_mask (((1UL << cl_value_len) - 1) << cl_value_shift)
  88. // Return the tag of a word.
  89. inline cl_uint cl_tag (cl_uint word)
  90. {
  91. return (word & cl_tag_mask) >> cl_tag_shift;
  92. }
  93. // Return the value (unsigned) of a word.
  94. inline cl_uint cl_value (cl_uint word)
  95. {
  96. // This assumes cl_value_shift + cl_value_len == cl_word_size.
  97. return word >> cl_value_shift;
  98. }
  99. // Return a word, combining a value and a tag.
  100. inline cl_uint cl_combine (cl_uint tag, cl_uint value)
  101. {
  102. return (value << cl_value_shift) + (tag << cl_tag_shift);
  103. }
  104. inline cl_uint cl_combine (cl_uint tag, cl_sint value)
  105. {
  106. // This assumes cl_value_shift + cl_value_len == cl_word_size.
  107. return (value << cl_value_shift) + (tag << cl_tag_shift);
  108. }
  109. // Keep the compiler happy.
  110. inline cl_uint cl_combine (cl_uint tag, unsigned int value)
  111. { return cl_combine(tag,(cl_uint)value); }
  112. inline cl_uint cl_combine (cl_uint tag, int value)
  113. { return cl_combine(tag,(cl_sint)value); }
  114. // Definition of the tags.
  115. #if !defined(CL_WIDE_POINTERS)
  116. #if (cl_word_alignment == 2)
  117. #define cl_FN_tag 1
  118. #define cl_SF_tag 3 // must satisfy the cl_immediate_p predicate!
  119. #endif
  120. #if (cl_word_alignment == 4)
  121. #define cl_FN_tag 1
  122. #define cl_SF_tag 2
  123. #endif
  124. #else // CL_WIDE_POINTERS
  125. // Single Floats are immediate as well.
  126. #define cl_FN_tag 1
  127. #define cl_SF_tag 2
  128. #define cl_FF_tag 3
  129. #endif
  130. // Corresponding classes.
  131. extern const struct cl_class * cl_immediate_classes [1<<cl_tag_len];
  132. // Heap allocated data contains a header, for two purposes:
  133. // - dynamic typing,
  134. // - reference count (a portable alternative to garbage collection,
  135. // or the basis for a portable and interoperable garbage collection).
  136. struct cl_heap {
  137. int refcount; // reference count
  138. const struct cl_class * type; // type tag
  139. };
  140. // Function to destroy the contents of a heap object.
  141. typedef void (*cl_heap_destructor_function) (cl_heap* pointer);
  142. // Flags, to be ORed together.
  143. #define cl_class_flags_subclass_complex 1 // all instances belong to cl_N
  144. #define cl_class_flags_subclass_real 2 // all instances belong to cl_R
  145. #define cl_class_flags_subclass_float 4 // all instances belong to cl_F
  146. #define cl_class_flags_subclass_rational 8 // all instances belong to cl_RA
  147. #define cl_class_flags_number_ring 16 // all instances are rings whose
  148. // elements belong to cl_number
  149. // Function to print an object for debugging, to cl_stderr.
  150. typedef void (*cl_heap_dprint_function) (cl_heap* pointer);
  151. struct cl_class {
  152. cl_heap_destructor_function destruct;
  153. int flags;
  154. cl_heap_dprint_function dprint;
  155. };
  156. // Free an object on heap.
  157. extern void cl_free_heap_object (cl_heap* pointer);
  158. // Debugging support for dynamic typing: Register a debugging print function.
  159. #define cl_register_type_printer(type,printer) \
  160. { extern cl_class type; type.dprint = (printer); }
  161. // cl_private_thing: An immediate value or a pointer into the heap.
  162. // This must be as wide as a `cl_uint'.
  163. // (Actually, this ought to be a union { void*; cl_uint; }, but using
  164. // a pointer type generates better code.)
  165. // Never throw away a cl_private_thing, or reference counts will be wrong!
  166. typedef struct cl_anything * cl_private_thing;
  167. // Increment the reference count.
  168. inline void cl_inc_pointer_refcount (cl_heap* pointer)
  169. {
  170. pointer->refcount++;
  171. }
  172. // Decrement the reference count of a garbage collected pointer.
  173. inline void cl_gc_dec_pointer_refcount (cl_heap* pointer)
  174. {
  175. if (--pointer->refcount == 0)
  176. cl_free_heap_object(pointer);
  177. }
  178. // Decrement the reference count of a reference counted pointer.
  179. inline void cl_rc_dec_pointer_refcount (cl_heap* pointer)
  180. {
  181. --pointer->refcount;
  182. }
  183. // Increment the reference count.
  184. // This must be a macro, not an inline function, because pointer_p() and
  185. // inc_pointer_refcount() are non-virtual member functions, so that the
  186. // compiler can optimize it.
  187. #define cl_inc_refcount(x) \
  188. if ((x).pointer_p()) \
  189. (x).inc_pointer_refcount(); \
  190. // Decrement the reference count.
  191. // This must be a macro, not an inline function, because pointer_p() and
  192. // dec_pointer_refcount() are non-virtual member functions, so that the
  193. // compiler can optimize it.
  194. #define cl_dec_refcount(x) \
  195. if ((x).pointer_p()) \
  196. (x).dec_pointer_refcount(); \
  197. // The declaration of a copy constructor.
  198. // Restriction: The base class's default constructor must do nothing or
  199. // initialize `pointer' to a constant expression.
  200. #define CL_DEFINE_COPY_CONSTRUCTOR1(_class_) \
  201. _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_class_)
  202. #define _CL_DEFINE_COPY_CONSTRUCTOR1(_class_,_classname_) \
  203. inline _class_::_classname_ (const _class_& x) \
  204. { \
  205. cl_uint x_word = x.word; \
  206. cl_inc_refcount(x); \
  207. word = x_word; \
  208. }
  209. // The declaration of a copy constructor.
  210. // Restriction: The base class must have the usual `cl_private_thing'
  211. // constructor. Drawback: The base class must be known here.
  212. #define CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_baseclass_) \
  213. _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_class_,_baseclass_)
  214. #define _CL_DEFINE_COPY_CONSTRUCTOR2(_class_,_classname_,_baseclass_) \
  215. inline _class_::_classname_ (const _class_& x) \
  216. : _baseclass_ (as_cl_private_thing(x)) {}
  217. // The declaration of an assignment operator.
  218. #define CL_DEFINE_ASSIGNMENT_OPERATOR(dest_class,src_class) \
  219. inline dest_class& dest_class::operator= (const src_class& x) \
  220. { \
  221. /* Be careful, we might be assigning x to itself. */ \
  222. cl_uint x_word = x.word; \
  223. cl_inc_refcount(x); \
  224. cl_dec_refcount(*this); \
  225. word = x_word; \
  226. return *this; \
  227. }
  228. // We have a small problem with destructors: The specialized destructor
  229. // of a leaf class such as `cl_SF' should be more efficient than the
  230. // general destructor for `cl_N'. Since (by C++ specs) destructing a cl_SF
  231. // would run the destructors for cl_SF, cl_F, cl_R, cl_N (in that order),
  232. // and in the last step the compiler does not know any more that the object
  233. // actually is a cl_SF, there is no way to optimize the destructor!
  234. // ("progn-reversed" method combination is evil.)
  235. // And if we define "mirror"/"shadow" classes with no destructors (such
  236. // that `cl_F' inherits from `cl_F_no_destructor' buts adds a destructor)
  237. // then we need to add explicit conversion operators cl_SF -> cl_F -> cl_R ...,
  238. // with the effect that calling an overloaded function like `as_cl_F'
  239. // (which has two signatures `as_cl_F(cl_number)' and `as_cl_F(cl_F)')
  240. // with a cl_SF argument gives an "call of overloaded function is ambiguous"
  241. // error.
  242. // There is no help: If we want overloaded functions to be callable in a way
  243. // that makes sense, `cl_SF' has to be a subclass of `cl_F', and then the
  244. // destructor of `cl_SF' will do at least as much computation as the `cl_F'
  245. // destructor. Praise C++ ! :-((
  246. // (Even making `pointer_p()' a virtual function would not help.)
  247. // This is obnoxious.
  248. template <class key1_type, class value_type> struct cl_htentry1;
  249. // The four concrete classes of all objects.
  250. class cl_gcobject {
  251. public: /* ugh */
  252. union {
  253. void* pointer;
  254. cl_heap* heappointer;
  255. cl_uint word;
  256. };
  257. public:
  258. // Default constructor. (Used for objects with no initializer.)
  259. cl_gcobject ();
  260. // Destructor. (Used when a variable goes out of scope.)
  261. ~cl_gcobject ();
  262. // Copy constructor.
  263. cl_gcobject (const cl_gcobject&);
  264. // Assignment operator.
  265. cl_gcobject& operator= (const cl_gcobject&);
  266. // Distinguish immediate data from pointer.
  267. cl_boolean pointer_p() const
  268. { return cl_pointer_p(word); }
  269. // Reference counting.
  270. void inc_pointer_refcount () const
  271. { cl_inc_pointer_refcount(heappointer); }
  272. void dec_pointer_refcount () const
  273. { cl_gc_dec_pointer_refcount(heappointer); }
  274. // Return the type tag of an immediate number.
  275. cl_uint nonpointer_tag () const
  276. { return cl_tag(word); }
  277. // Return the type tag of a heap-allocated number.
  278. const cl_class * pointer_type () const
  279. { return heappointer->type; }
  280. // Private pointer manipulations.
  281. cl_private_thing _as_cl_private_thing () const;
  282. // Private constructor.
  283. cl_gcobject (cl_private_thing p)
  284. #if !(defined(__alpha__) && !defined(__GNUC__))
  285. : pointer (p) {}
  286. #else
  287. { pointer = p; }
  288. #endif
  289. // Debugging output.
  290. void debug_print () const;
  291. // Ability to place an object at a given address.
  292. void* operator new (size_t size, cl_gcobject* ptr) { (void)size; return ptr; }
  293. void* operator new (size_t size) { return ::operator new (size); }
  294. };
  295. inline cl_gcobject::cl_gcobject () {}
  296. inline cl_gcobject::~cl_gcobject () { cl_dec_refcount(*this); }
  297. CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcobject)
  298. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcobject,cl_gcobject)
  299. class cl_gcpointer {
  300. public: /* ugh */
  301. union {
  302. void* pointer;
  303. cl_heap* heappointer;
  304. cl_uint word;
  305. };
  306. public:
  307. // Default constructor. (Used for objects with no initializer.)
  308. cl_gcpointer ();
  309. // Destructor. (Used when a variable goes out of scope.)
  310. ~cl_gcpointer ();
  311. // Copy constructor.
  312. cl_gcpointer (const cl_gcpointer&);
  313. // Assignment operator.
  314. cl_gcpointer& operator= (const cl_gcpointer&);
  315. // Distinguish immediate data from pointer.
  316. cl_boolean pointer_p() const
  317. { return cl_true; }
  318. // Reference counting.
  319. void inc_pointer_refcount () const
  320. { cl_inc_pointer_refcount(heappointer); }
  321. void dec_pointer_refcount () const
  322. { cl_gc_dec_pointer_refcount(heappointer); }
  323. // Return the type tag of an immediate number.
  324. cl_uint nonpointer_tag () const
  325. { return cl_tag(word); }
  326. // Return the type tag of a heap-allocated number.
  327. const cl_class * pointer_type () const
  328. { return heappointer->type; }
  329. // Private pointer manipulations.
  330. cl_private_thing _as_cl_private_thing () const;
  331. // Private constructor.
  332. cl_gcpointer (cl_private_thing p)
  333. #if !(defined(__alpha__) && !defined(__GNUC__))
  334. : pointer (p) {}
  335. #else
  336. { pointer = p; }
  337. #endif
  338. // Debugging output.
  339. void debug_print () const;
  340. // Ability to place an object at a given address.
  341. void* operator new (size_t size, cl_gcpointer* ptr) { (void)size; return ptr; }
  342. void* operator new (size_t size) { return ::operator new (size); }
  343. };
  344. inline cl_gcpointer::cl_gcpointer () {}
  345. inline cl_gcpointer::~cl_gcpointer () { cl_dec_refcount(*this); }
  346. CL_DEFINE_COPY_CONSTRUCTOR1(cl_gcpointer)
  347. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_gcpointer,cl_gcpointer)
  348. class cl_rcobject {
  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_rcobject ();
  358. // Destructor. (Used when a variable goes out of scope.)
  359. ~cl_rcobject ();
  360. // Copy constructor.
  361. cl_rcobject (const cl_rcobject&);
  362. // Assignment operator.
  363. cl_rcobject& operator= (const cl_rcobject&);
  364. // Distinguish immediate data from pointer.
  365. cl_boolean pointer_p() const
  366. { return cl_pointer_p(word); }
  367. // Reference counting.
  368. void inc_pointer_refcount () const
  369. { cl_inc_pointer_refcount(heappointer); }
  370. void dec_pointer_refcount () const
  371. { cl_rc_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_rcobject (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, cl_rcobject* ptr) { (void)size; return ptr; }
  391. void* operator new (size_t size) { return ::operator new (size); }
  392. };
  393. inline cl_rcobject::cl_rcobject () {}
  394. inline cl_rcobject::~cl_rcobject () { cl_dec_refcount(*this); }
  395. CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcobject)
  396. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcobject,cl_rcobject)
  397. class cl_rcpointer {
  398. public: /* ugh */
  399. union {
  400. void* pointer;
  401. cl_heap* heappointer;
  402. cl_uint word;
  403. };
  404. public:
  405. // Default constructor. (Used for objects with no initializer.)
  406. cl_rcpointer ();
  407. // Destructor. (Used when a variable goes out of scope.)
  408. ~cl_rcpointer ();
  409. // Copy constructor.
  410. cl_rcpointer (const cl_rcpointer&);
  411. // Assignment operator.
  412. cl_rcpointer& operator= (const cl_rcpointer&);
  413. // Distinguish immediate data from pointer.
  414. cl_boolean pointer_p() const
  415. { return cl_true; }
  416. // Reference counting.
  417. void inc_pointer_refcount () const
  418. { cl_inc_pointer_refcount(heappointer); }
  419. void dec_pointer_refcount () const
  420. { cl_rc_dec_pointer_refcount(heappointer); }
  421. // Return the type tag of an immediate number.
  422. cl_uint nonpointer_tag () const
  423. { return cl_tag(word); }
  424. // Return the type tag of a heap-allocated number.
  425. const cl_class * pointer_type () const
  426. { return heappointer->type; }
  427. // Private pointer manipulations.
  428. cl_private_thing _as_cl_private_thing () const;
  429. // Private constructor.
  430. cl_rcpointer (cl_private_thing p)
  431. #if !(defined(__alpha__) && !defined(__GNUC__))
  432. : pointer (p) {}
  433. #else
  434. { pointer = p; }
  435. #endif
  436. // Debugging output.
  437. void debug_print () const;
  438. // Ability to place an object at a given address.
  439. void* operator new (size_t size, cl_rcpointer* ptr) { (void)size; return ptr; }
  440. void* operator new (size_t size) { return ::operator new (size); }
  441. };
  442. inline cl_rcpointer::cl_rcpointer () {}
  443. inline cl_rcpointer::~cl_rcpointer () { cl_dec_refcount(*this); }
  444. CL_DEFINE_COPY_CONSTRUCTOR1(cl_rcpointer)
  445. CL_DEFINE_ASSIGNMENT_OPERATOR(cl_rcpointer,cl_rcpointer)
  446. // Private pointer manipulations.
  447. inline cl_private_thing cl_gcobject::_as_cl_private_thing () const
  448. {
  449. cl_private_thing p = (cl_private_thing) pointer;
  450. cl_inc_refcount(*this);
  451. return p;
  452. }
  453. inline cl_private_thing as_cl_private_thing (const cl_gcobject& x)
  454. {
  455. return x._as_cl_private_thing();
  456. }
  457. inline cl_private_thing cl_gcpointer::_as_cl_private_thing () const
  458. {
  459. cl_private_thing p = (cl_private_thing) pointer;
  460. cl_inc_refcount(*this);
  461. return p;
  462. }
  463. inline cl_private_thing as_cl_private_thing (const cl_gcpointer& x)
  464. {
  465. return x._as_cl_private_thing();
  466. }
  467. inline cl_private_thing cl_rcobject::_as_cl_private_thing () const
  468. {
  469. cl_private_thing p = (cl_private_thing) pointer;
  470. cl_inc_refcount(*this);
  471. return p;
  472. }
  473. inline cl_private_thing as_cl_private_thing (const cl_rcobject& x)
  474. {
  475. return x._as_cl_private_thing();
  476. }
  477. inline cl_private_thing cl_rcpointer::_as_cl_private_thing () const
  478. {
  479. cl_private_thing p = (cl_private_thing) pointer;
  480. cl_inc_refcount(*this);
  481. return p;
  482. }
  483. inline cl_private_thing as_cl_private_thing (const cl_rcpointer& x)
  484. {
  485. return x._as_cl_private_thing();
  486. }
  487. // Note: When we define a function that returns a class object by value,
  488. // we normally return it as const value. The declarations
  489. // T func (...); (A)
  490. // and
  491. // const T func (...); (B)
  492. // behave identically and generate identical code, except that the code
  493. // func(...) = foo;
  494. // compiles fine with (A) but is an error (and yields a warning) with (B).
  495. // We want this warning.
  496. // Define a conversion operator from one object to another object of the
  497. // same size.
  498. #if (defined(__GNUC__) && (__GNUC__ == 2) && (__GNUC_MINOR__ <= 7)) // workaround g++ bug
  499. #define CL_DEFINE_CONVERTER(target_class) \
  500. operator target_class () const \
  501. { \
  502. if (sizeof(*this) != sizeof(target_class)) cl_abort(); \
  503. return * (const target_class *) (void*) this; \
  504. }
  505. #else
  506. #define CL_DEFINE_CONVERTER(target_class) \
  507. operator const target_class & () const \
  508. { \
  509. if (sizeof(*this) != sizeof(target_class)) cl_abort(); \
  510. return * (const target_class *) (void*) this; \
  511. }
  512. #endif
  513. #endif /* _CL_OBJECT_H */