gcc_support.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /***************************************************************************
  2. Interface between g++ and Boehm GC
  3. Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
  4. THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. Permission is hereby granted to copy this code for any purpose,
  7. provided the above notices are retained on all copies.
  8. Last modified on Sun Jul 16 23:21:14 PDT 1995 by ellis
  9. This module provides runtime support for implementing the
  10. Ellis/Detlefs GC proposal, "Safe, Efficient Garbage Collection for
  11. C++", within g++, using its -fgc-keyword extension. It defines
  12. versions of __builtin_new, __builtin_new_gc, __builtin_vec_new,
  13. __builtin_vec_new_gc, __builtin_delete, and __builtin_vec_delete that
  14. invoke the Bohem GC. It also implements the WeakPointer.h interface.
  15. This module assumes the following configuration options of the Boehm GC:
  16. -DALL_INTERIOR_POINTERS
  17. -DDONT_ADD_BYTE_AT_END
  18. This module adds its own required padding to the end of objects to
  19. support C/C++ "one-past-the-object" pointer semantics.
  20. ****************************************************************************/
  21. #include <stddef.h>
  22. #include "gc.h"
  23. #if defined(__STDC__)
  24. # define PROTO( args ) args
  25. #else
  26. # define PROTO( args ) ()
  27. # endif
  28. #define BITSPERBYTE 8
  29. /* What's the portable way to do this? */
  30. typedef void (*vfp) PROTO(( void ));
  31. extern vfp __new_handler;
  32. extern void __default_new_handler PROTO(( void ));
  33. /* A destructor_proc is the compiler generated procedure representing a
  34. C++ destructor. The "flag" argument is a hidden argument following some
  35. compiler convention. */
  36. typedef (*destructor_proc) PROTO(( void* this, int flag ));
  37. /***************************************************************************
  38. A BI_header is the header the compiler adds to the front of
  39. new-allocated arrays of objects with destructors. The header is
  40. padded out to a double, because that's what the compiler does to
  41. ensure proper alignment of array elements on some architectures.
  42. int NUM_ARRAY_ELEMENTS (void* o)
  43. returns the number of array elements for array object o.
  44. char* FIRST_ELEMENT_P (void* o)
  45. returns the address of the first element of array object o.
  46. ***************************************************************************/
  47. typedef struct BI_header {
  48. int nelts;
  49. char padding [sizeof( double ) - sizeof( int )];
  50. /* Better way to do this? */
  51. } BI_header;
  52. #define NUM_ARRAY_ELEMENTS( o ) \
  53. (((BI_header*) o)->nelts)
  54. #define FIRST_ELEMENT_P( o ) \
  55. ((char*) o + sizeof( BI_header ))
  56. /***************************************************************************
  57. The __builtin_new routines add a descriptor word to the end of each
  58. object. The descriptor serves two purposes.
  59. First, the descriptor acts as padding, implementing C/C++ pointer
  60. semantics. C and C++ allow a valid array pointer to be incremented
  61. one past the end of an object. The extra padding ensures that the
  62. collector will recognize that such a pointer points to the object and
  63. not the next object in memory.
  64. Second, the descriptor stores three extra pieces of information,
  65. whether an object has a registered finalizer (destructor), whether it
  66. may have any weak pointers referencing it, and for collectible arrays,
  67. the element size of the array. The element size is required for the
  68. array's finalizer to iterate through the elements of the array. (An
  69. alternative design would have the compiler generate a finalizer
  70. procedure for each different array type. But given the overhead of
  71. finalization, there isn't any efficiency to be gained by that.)
  72. The descriptor must be added to non-collectible as well as collectible
  73. objects, since the Ellis/Detlefs proposal allows "pointer to gc T" to
  74. be assigned to a "pointer to T", which could then be deleted. Thus,
  75. __builtin_delete must determine at runtime whether an object is
  76. collectible, whether it has weak pointers referencing it, and whether
  77. it may have a finalizer that needs unregistering. Though
  78. GC_REGISTER_FINALIZER doesn't care if you ask it to unregister a
  79. finalizer for an object that doesn't have one, it is a non-trivial
  80. procedure that does a hash look-up, etc. The descriptor trades a
  81. little extra space for a significant increase in time on the fast path
  82. through delete. (A similar argument applies to
  83. GC_UNREGISTER_DISAPPEARING_LINK).
  84. For non-array types, the space for the descriptor could be shrunk to a
  85. single byte for storing the "has finalizer" flag. But this would save
  86. space only on arrays of char (whose size is not a multiple of the word
  87. size) and structs whose largest member is less than a word in size
  88. (very infrequent). And it would require that programmers actually
  89. remember to call "delete[]" instead of "delete" (which they should,
  90. but there are probably lots of buggy programs out there). For the
  91. moment, the space savings seems not worthwhile, especially considering
  92. that the Boehm GC is already quite space competitive with other
  93. malloc's.
  94. Given a pointer o to the base of an object:
  95. Descriptor* DESCRIPTOR (void* o)
  96. returns a pointer to the descriptor for o.
  97. The implementation of descriptors relies on the fact that the GC
  98. implementation allocates objects in units of the machine's natural
  99. word size (e.g. 32 bits on a SPARC, 64 bits on an Alpha).
  100. **************************************************************************/
  101. typedef struct Descriptor {
  102. unsigned has_weak_pointers: 1;
  103. unsigned has_finalizer: 1;
  104. unsigned element_size: BITSPERBYTE * sizeof( unsigned ) - 2;
  105. } Descriptor;
  106. #define DESCRIPTOR( o ) \
  107. ((Descriptor*) ((char*)(o) + GC_size( o ) - sizeof( Descriptor )))
  108. /**************************************************************************
  109. Implementations of global operator new() and operator delete()
  110. ***************************************************************************/
  111. void* __builtin_new( size )
  112. size_t size;
  113. /*
  114. For non-gc non-array types, the compiler generates calls to
  115. __builtin_new, which allocates non-collected storage via
  116. GC_MALLOC_UNCOLLECTABLE. This ensures that the non-collected
  117. storage will be part of the collector's root set, required by the
  118. Ellis/Detlefs semantics. */
  119. {
  120. vfp handler = __new_handler ? __new_handler : __default_new_handler;
  121. while (1) {
  122. void* o = GC_MALLOC_UNCOLLECTABLE( size + sizeof( Descriptor ) );
  123. if (o != 0) return o;
  124. (*handler) ();}}
  125. void* __builtin_vec_new( size )
  126. size_t size;
  127. /*
  128. For non-gc array types, the compiler generates calls to
  129. __builtin_vec_new. */
  130. {
  131. return __builtin_new( size );}
  132. void* __builtin_new_gc( size )
  133. size_t size;
  134. /*
  135. For gc non-array types, the compiler generates calls to
  136. __builtin_new_gc, which allocates collected storage via
  137. GC_MALLOC. */
  138. {
  139. vfp handler = __new_handler ? __new_handler : __default_new_handler;
  140. while (1) {
  141. void* o = GC_MALLOC( size + sizeof( Descriptor ) );
  142. if (o != 0) return o;
  143. (*handler) ();}}
  144. void* __builtin_new_gc_a( size )
  145. size_t size;
  146. /*
  147. For non-pointer-containing gc non-array types, the compiler
  148. generates calls to __builtin_new_gc_a, which allocates collected
  149. storage via GC_MALLOC_ATOMIC. */
  150. {
  151. vfp handler = __new_handler ? __new_handler : __default_new_handler;
  152. while (1) {
  153. void* o = GC_MALLOC_ATOMIC( size + sizeof( Descriptor ) );
  154. if (o != 0) return o;
  155. (*handler) ();}}
  156. void* __builtin_vec_new_gc( size )
  157. size_t size;
  158. /*
  159. For gc array types, the compiler generates calls to
  160. __builtin_vec_new_gc. */
  161. {
  162. return __builtin_new_gc( size );}
  163. void* __builtin_vec_new_gc_a( size )
  164. size_t size;
  165. /*
  166. For non-pointer-containing gc array types, the compiler generates
  167. calls to __builtin_vec_new_gc_a. */
  168. {
  169. return __builtin_new_gc_a( size );}
  170. static void call_destructor( o, data )
  171. void* o;
  172. void* data;
  173. /*
  174. call_destructor is the GC finalizer proc registered for non-array
  175. gc objects with destructors. Its client data is the destructor
  176. proc, which it calls with the magic integer 2, a special flag
  177. obeying the compiler convention for destructors. */
  178. {
  179. ((destructor_proc) data)( o, 2 );}
  180. void* __builtin_new_gc_dtor( o, d )
  181. void* o;
  182. destructor_proc d;
  183. /*
  184. The compiler generates a call to __builtin_new_gc_dtor to register
  185. the destructor "d" of a non-array gc object "o" as a GC finalizer.
  186. The destructor is registered via
  187. GC_REGISTER_FINALIZER_IGNORE_SELF, which causes the collector to
  188. ignore pointers from the object to itself when determining when
  189. the object can be finalized. This is necessary due to the self
  190. pointers used in the internal representation of multiply-inherited
  191. objects. */
  192. {
  193. Descriptor* desc = DESCRIPTOR( o );
  194. GC_REGISTER_FINALIZER_IGNORE_SELF( o, call_destructor, d, 0, 0 );
  195. desc->has_finalizer = 1;}
  196. static void call_array_destructor( o, data )
  197. void* o;
  198. void* data;
  199. /*
  200. call_array_destructor is the GC finalizer proc registered for gc
  201. array objects whose elements have destructors. Its client data is
  202. the destructor proc. It iterates through the elements of the
  203. array in reverse order, calling the destructor on each. */
  204. {
  205. int num = NUM_ARRAY_ELEMENTS( o );
  206. Descriptor* desc = DESCRIPTOR( o );
  207. size_t size = desc->element_size;
  208. char* first_p = FIRST_ELEMENT_P( o );
  209. char* p = first_p + (num - 1) * size;
  210. if (num > 0) {
  211. while (1) {
  212. ((destructor_proc) data)( p, 2 );
  213. if (p == first_p) break;
  214. p -= size;}}}
  215. void* __builtin_vec_new_gc_dtor( first_elem, d, element_size )
  216. void* first_elem;
  217. destructor_proc d;
  218. size_t element_size;
  219. /*
  220. The compiler generates a call to __builtin_vec_new_gc_dtor to
  221. register the destructor "d" of a gc array object as a GC
  222. finalizer. "first_elem" points to the first element of the array,
  223. *not* the beginning of the object (this makes the generated call
  224. to this function smaller). The elements of the array are of size
  225. "element_size". The destructor is registered as in
  226. _builtin_new_gc_dtor. */
  227. {
  228. void* o = (char*) first_elem - sizeof( BI_header );
  229. Descriptor* desc = DESCRIPTOR( o );
  230. GC_REGISTER_FINALIZER_IGNORE_SELF( o, call_array_destructor, d, 0, 0 );
  231. desc->element_size = element_size;
  232. desc->has_finalizer = 1;}
  233. void __builtin_delete( o )
  234. void* o;
  235. /*
  236. The compiler generates calls to __builtin_delete for operator
  237. delete(). The GC currently requires that any registered
  238. finalizers be unregistered before explicitly freeing an object.
  239. If the object has any weak pointers referencing it, we can't
  240. actually free it now. */
  241. {
  242. if (o != 0) {
  243. Descriptor* desc = DESCRIPTOR( o );
  244. if (desc->has_finalizer) GC_REGISTER_FINALIZER( o, 0, 0, 0, 0 );
  245. if (! desc->has_weak_pointers) GC_FREE( o );}}
  246. void __builtin_vec_delete( o )
  247. void* o;
  248. /*
  249. The compiler generates calls to __builitn_vec_delete for operator
  250. delete[](). */
  251. {
  252. __builtin_delete( o );}
  253. /**************************************************************************
  254. Implementations of the template class WeakPointer from WeakPointer.h
  255. ***************************************************************************/
  256. typedef struct WeakPointer {
  257. void* pointer;
  258. } WeakPointer;
  259. void* _WeakPointer_New( t )
  260. void* t;
  261. {
  262. if (t == 0) {
  263. return 0;}
  264. else {
  265. void* base = GC_base( t );
  266. WeakPointer* wp =
  267. (WeakPointer*) GC_MALLOC_ATOMIC( sizeof( WeakPointer ) );
  268. Descriptor* desc = DESCRIPTOR( base );
  269. wp->pointer = t;
  270. desc->has_weak_pointers = 1;
  271. GC_general_register_disappearing_link( &wp->pointer, base );
  272. return wp;}}
  273. static void* PointerWithLock( wp )
  274. WeakPointer* wp;
  275. {
  276. if (wp == 0 || wp->pointer == 0) {
  277. return 0;}
  278. else {
  279. return (void*) wp->pointer;}}
  280. void* _WeakPointer_Pointer( wp )
  281. WeakPointer* wp;
  282. {
  283. return (void*) GC_call_with_alloc_lock( PointerWithLock, wp );}
  284. typedef struct EqualClosure {
  285. WeakPointer* wp1;
  286. WeakPointer* wp2;
  287. } EqualClosure;
  288. static void* EqualWithLock( ec )
  289. EqualClosure* ec;
  290. {
  291. if (ec->wp1 == 0 || ec->wp2 == 0) {
  292. return (void*) (ec->wp1 == ec->wp2);}
  293. else {
  294. return (void*) (ec->wp1->pointer == ec->wp2->pointer);}}
  295. int _WeakPointer_Equal( wp1, wp2 )
  296. WeakPointer* wp1;
  297. WeakPointer* wp2;
  298. {
  299. EqualClosure ec;
  300. ec.wp1 = wp1;
  301. ec.wp2 = wp2;
  302. return (int) GC_call_with_alloc_lock( EqualWithLock, &ec );}
  303. int _WeakPointer_Hash( wp )
  304. WeakPointer* wp;
  305. {
  306. return (int) _WeakPointer_Pointer( wp );}
  307. /**************************************************************************
  308. Implementations of the template class CleanUp from WeakPointer.h
  309. ***************************************************************************/
  310. typedef struct Closure {
  311. void (*c) PROTO(( void* d, void* t ));
  312. ptrdiff_t t_offset;
  313. void* d;
  314. } Closure;
  315. static void _CleanUp_CallClosure( obj, data )
  316. void* obj;
  317. void* data;
  318. {
  319. Closure* closure = (Closure*) data;
  320. closure->c( closure->d, (char*) obj + closure->t_offset );}
  321. void _CleanUp_Set( t, c, d )
  322. void* t;
  323. void (*c) PROTO(( void* d, void* t ));
  324. void* d;
  325. {
  326. void* base = GC_base( t );
  327. Descriptor* desc = DESCRIPTOR( t );
  328. if (c == 0) {
  329. GC_REGISTER_FINALIZER_IGNORE_SELF( base, 0, 0, 0, 0 );
  330. desc->has_finalizer = 0;}
  331. else {
  332. Closure* closure = (Closure*) GC_MALLOC( sizeof( Closure ) );
  333. closure->c = c;
  334. closure->t_offset = (char*) t - (char*) base;
  335. closure->d = d;
  336. GC_REGISTER_FINALIZER_IGNORE_SELF( base, _CleanUp_CallClosure,
  337. closure, 0, 0 );
  338. desc->has_finalizer = 1;}}
  339. void _CleanUp_Call( t )
  340. void* t;
  341. {
  342. /* ? Aren't we supposed to deactivate weak pointers to t too?
  343. Why? */
  344. void* base = GC_base( t );
  345. void* d;
  346. GC_finalization_proc f;
  347. GC_REGISTER_FINALIZER( base, 0, 0, &f, &d );
  348. f( base, d );}
  349. typedef struct QueueElem {
  350. void* o;
  351. GC_finalization_proc f;
  352. void* d;
  353. struct QueueElem* next;
  354. } QueueElem;
  355. void* _CleanUp_Queue_NewHead()
  356. {
  357. return GC_MALLOC( sizeof( QueueElem ) );}
  358. static void _CleanUp_Queue_Enqueue( obj, data )
  359. void* obj;
  360. void* data;
  361. {
  362. QueueElem* q = (QueueElem*) data;
  363. QueueElem* head = q->next;
  364. q->o = obj;
  365. q->next = head->next;
  366. head->next = q;}
  367. void _CleanUp_Queue_Set( h, t )
  368. void* h;
  369. void* t;
  370. {
  371. QueueElem* head = (QueueElem*) h;
  372. void* base = GC_base( t );
  373. void* d;
  374. GC_finalization_proc f;
  375. QueueElem* q = (QueueElem*) GC_MALLOC( sizeof( QueueElem ) );
  376. GC_REGISTER_FINALIZER( base, _CleanUp_Queue_Enqueue, q, &f, &d );
  377. q->f = f;
  378. q->d = d;
  379. q->next = head;}
  380. int _CleanUp_Queue_Call( h )
  381. void* h;
  382. {
  383. QueueElem* head = (QueueElem*) h;
  384. QueueElem* q = head->next;
  385. if (q == 0) {
  386. return 0;}
  387. else {
  388. head->next = q->next;
  389. q->next = 0;
  390. if (q->f != 0) q->f( q->o, q->d );
  391. return 1;}}