finalize.c
上传用户:fbh598
上传日期:2007-07-05
资源大小:5960k
文件大小:26k
源码类别:

Java编程

开发平台:

Unix_Linux

  1. /*
  2.  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
  3.  * Copyright (c) 1991-1996 by Xerox Corporation.  All rights reserved.
  4.  * Copyright (c) 1996-1999 by Silicon Graphics.  All rights reserved.
  5.  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  6.  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  7.  *
  8.  * Permission is hereby granted to use or copy this program
  9.  * for any purpose,  provided the above notices are retained on all copies.
  10.  * Permission to modify the code and to distribute modified code is granted,
  11.  * provided the above notices are retained, and a notice that the code was
  12.  * modified is included with the above copyright notice.
  13.  */
  14. /* Boehm, February 1, 1996 1:19 pm PST */
  15. # define I_HIDE_POINTERS
  16. # include "private/gc_pmark.h"
  17. # ifdef FINALIZE_ON_DEMAND
  18.     int GC_finalize_on_demand = 1;
  19. # else
  20.     int GC_finalize_on_demand = 0;
  21. # endif
  22. # ifdef JAVA_FINALIZATION
  23.     int GC_java_finalization = 1;
  24. # else
  25.     int GC_java_finalization = 0;
  26. # endif
  27. /* Type of mark procedure used for marking from finalizable object. */
  28. /* This procedure normally does not mark the object, only its */
  29. /* descendents. */
  30. typedef void finalization_mark_proc(/* ptr_t finalizable_obj_ptr */); 
  31. # define HASH3(addr,size,log_size) 
  32.     ((((word)(addr) >> 3) ^ ((word)(addr) >> (3+(log_size)))) 
  33.     & ((size) - 1))
  34. #define HASH2(addr,log_size) HASH3(addr, 1 << log_size, log_size)
  35. struct hash_chain_entry {
  36.     word hidden_key;
  37.     struct hash_chain_entry * next;
  38. };
  39. unsigned GC_finalization_failures = 0;
  40. /* Number of finalization requests that failed for lack of memory. */
  41. static struct disappearing_link {
  42.     struct hash_chain_entry prolog;
  43. #   define dl_hidden_link prolog.hidden_key
  44. /* Field to be cleared. */
  45. #   define dl_next(x) (struct disappearing_link *)((x) -> prolog.next)
  46. #   define dl_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
  47.     word dl_hidden_obj; /* Pointer to object base */
  48. } **dl_head = 0;
  49. static signed_word log_dl_table_size = -1;
  50. /* Binary log of */
  51. /* current size of array pointed to by dl_head. */
  52. /* -1 ==> size is 0. */
  53. word GC_dl_entries = 0; /* Number of entries currently in disappearing */
  54. /* link table. */
  55. static struct finalizable_object {
  56.     struct hash_chain_entry prolog;
  57. #   define fo_hidden_base prolog.hidden_key
  58. /* Pointer to object base. */
  59. /* No longer hidden once object */
  60. /* is on finalize_now queue. */
  61. #   define fo_next(x) (struct finalizable_object *)((x) -> prolog.next)
  62. #   define fo_set_next(x,y) (x) -> prolog.next = (struct hash_chain_entry *)(y)
  63.     GC_finalization_proc fo_fn; /* Finalizer. */
  64.     ptr_t fo_client_data;
  65.     word fo_object_size; /* In bytes. */
  66.     finalization_mark_proc * fo_mark_proc; /* Mark-through procedure */
  67. } **fo_head = 0;
  68. struct finalizable_object * GC_finalize_now = 0;
  69. /* LIst of objects that should be finalized now. */
  70. static signed_word log_fo_table_size = -1;
  71. word GC_fo_entries = 0;
  72. void GC_push_finalizer_structures GC_PROTO((void))
  73. {
  74.     GC_push_all((ptr_t)(&dl_head), (ptr_t)(&dl_head) + sizeof(word));
  75.     GC_push_all((ptr_t)(&fo_head), (ptr_t)(&fo_head) + sizeof(word));
  76.     GC_push_all((ptr_t)(&GC_finalize_now),
  77. (ptr_t)(&GC_finalize_now) + sizeof(word));
  78. }
  79. /* Double the size of a hash table. *size_ptr is the log of its current */
  80. /* size.  May be a noop. */
  81. /* *table is a pointer to an array of hash headers.  If we succeed, we */
  82. /* update both *table and *log_size_ptr. */
  83. /* Lock is held.  Signals are disabled. */
  84. void GC_grow_table(table, log_size_ptr)
  85. struct hash_chain_entry ***table;
  86. signed_word * log_size_ptr;
  87. {
  88.     register word i;
  89.     register struct hash_chain_entry *p;
  90.     int log_old_size = *log_size_ptr;
  91.     register int log_new_size = log_old_size + 1;
  92.     word old_size = ((log_old_size == -1)? 0: (1 << log_old_size));
  93.     register word new_size = 1 << log_new_size;
  94.     struct hash_chain_entry **new_table = (struct hash_chain_entry **)
  95.      GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(
  96.      (size_t)new_size * sizeof(struct hash_chain_entry *), NORMAL);
  97.     
  98.     if (new_table == 0) {
  99.      if (table == 0) {
  100.          ABORT("Insufficient space for initial table allocation");
  101.      } else {
  102.          return;
  103.      }
  104.     }
  105.     for (i = 0; i < old_size; i++) {
  106.       p = (*table)[i];
  107.       while (p != 0) {
  108.         register ptr_t real_key = (ptr_t)REVEAL_POINTER(p -> hidden_key);
  109.         register struct hash_chain_entry *next = p -> next;
  110.         register int new_hash = HASH3(real_key, new_size, log_new_size);
  111.         
  112.         p -> next = new_table[new_hash];
  113.         new_table[new_hash] = p;
  114.         p = next;
  115.       }
  116.     }
  117.     *log_size_ptr = log_new_size;
  118.     *table = new_table;
  119. }
  120. # if defined(__STDC__) || defined(__cplusplus)
  121.     int GC_register_disappearing_link(GC_PTR * link)
  122. # else
  123.     int GC_register_disappearing_link(link)
  124.     GC_PTR * link;
  125. # endif
  126. {
  127.     ptr_t base;
  128.     
  129.     base = (ptr_t)GC_base((GC_PTR)link);
  130.     if (base == 0)
  131.      ABORT("Bad arg to GC_register_disappearing_link");
  132.     return(GC_general_register_disappearing_link(link, base));
  133. }
  134. # if defined(__STDC__) || defined(__cplusplus)
  135.     int GC_general_register_disappearing_link(GC_PTR * link,
  136.            GC_PTR obj)
  137. # else
  138.     int GC_general_register_disappearing_link(link, obj)
  139.     GC_PTR * link;
  140.     GC_PTR obj;
  141. # endif
  142. {
  143.     struct disappearing_link *curr_dl;
  144.     int index;
  145.     struct disappearing_link * new_dl;
  146.     DCL_LOCK_STATE;
  147.     
  148.     if ((word)link & (ALIGNMENT-1))
  149.      ABORT("Bad arg to GC_general_register_disappearing_link");
  150. #   ifdef THREADS
  151.      DISABLE_SIGNALS();
  152.      LOCK();
  153. #   endif
  154.     if (log_dl_table_size == -1
  155.         || GC_dl_entries > ((word)1 << log_dl_table_size)) {
  156. # ifndef THREADS
  157.     DISABLE_SIGNALS();
  158. # endif
  159.      GC_grow_table((struct hash_chain_entry ***)(&dl_head),
  160.            &log_dl_table_size);
  161. # ifdef CONDPRINT
  162.   if (GC_print_stats) {
  163.     GC_printf1("Grew dl table to %lu entriesn",
  164.      (unsigned long)(1 << log_dl_table_size));
  165.   }
  166. # endif
  167. # ifndef THREADS
  168.     ENABLE_SIGNALS();
  169. # endif
  170.     }
  171.     index = HASH2(link, log_dl_table_size);
  172.     curr_dl = dl_head[index];
  173.     for (curr_dl = dl_head[index]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
  174.         if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
  175.             curr_dl -> dl_hidden_obj = HIDE_POINTER(obj);
  176. #     ifdef THREADS
  177.                 UNLOCK();
  178.              ENABLE_SIGNALS();
  179. #     endif
  180.             return(1);
  181.         }
  182.     }
  183.     new_dl = (struct disappearing_link *)
  184.      GC_INTERNAL_MALLOC(sizeof(struct disappearing_link),NORMAL);
  185.     if (0 == new_dl) {
  186. #     ifdef THREADS
  187. UNLOCK();
  188.      ENABLE_SIGNALS();
  189. #     endif
  190.       new_dl = (struct disappearing_link *)
  191.       GC_oom_fn(sizeof(struct disappearing_link));
  192.       if (0 == new_dl) {
  193. GC_finalization_failures++;
  194. return(0);
  195.       }
  196.       /* It's not likely we'll make it here, but ... */
  197. #     ifdef THREADS
  198.         DISABLE_SIGNALS();
  199. LOCK();
  200. #     endif
  201.     }
  202.     new_dl -> dl_hidden_obj = HIDE_POINTER(obj);
  203.     new_dl -> dl_hidden_link = HIDE_POINTER(link);
  204.     dl_set_next(new_dl, dl_head[index]);
  205.     dl_head[index] = new_dl;
  206.     GC_dl_entries++;
  207. #   ifdef THREADS
  208.         UNLOCK();
  209.         ENABLE_SIGNALS();
  210. #   endif
  211.     return(0);
  212. }
  213. # if defined(__STDC__) || defined(__cplusplus)
  214.     int GC_unregister_disappearing_link(GC_PTR * link)
  215. # else
  216.     int GC_unregister_disappearing_link(link)
  217.     GC_PTR * link;
  218. # endif
  219. {
  220.     struct disappearing_link *curr_dl, *prev_dl;
  221.     int index;
  222.     DCL_LOCK_STATE;
  223.     
  224.     DISABLE_SIGNALS();
  225.     LOCK();
  226.     index = HASH2(link, log_dl_table_size);
  227.     if (((unsigned long)link & (ALIGNMENT-1))) goto out;
  228.     prev_dl = 0; curr_dl = dl_head[index];
  229.     while (curr_dl != 0) {
  230.         if (curr_dl -> dl_hidden_link == HIDE_POINTER(link)) {
  231.             if (prev_dl == 0) {
  232.                 dl_head[index] = dl_next(curr_dl);
  233.             } else {
  234.                 dl_set_next(prev_dl, dl_next(curr_dl));
  235.             }
  236.             GC_dl_entries--;
  237.             UNLOCK();
  238.          ENABLE_SIGNALS();
  239. #     ifdef DBG_HDRS_ALL
  240.       dl_set_next(curr_dl, 0);
  241. #     else
  242.               GC_free((GC_PTR)curr_dl);
  243. #     endif
  244.             return(1);
  245.         }
  246.         prev_dl = curr_dl;
  247.         curr_dl = dl_next(curr_dl);
  248.     }
  249. out:
  250.     UNLOCK();
  251.     ENABLE_SIGNALS();
  252.     return(0);
  253. }
  254. /* Possible finalization_marker procedures.  Note that mark stack */
  255. /* overflow is handled by the caller, and is not a disaster. */
  256. GC_API void GC_normal_finalize_mark_proc(p)
  257. ptr_t p;
  258. {
  259.     hdr * hhdr = HDR(p);
  260.     
  261.     PUSH_OBJ((word *)p, hhdr, GC_mark_stack_top,
  262.      &(GC_mark_stack[GC_mark_stack_size]));
  263. }
  264. /* This only pays very partial attention to the mark descriptor. */
  265. /* It does the right thing for normal and atomic objects, and treats */
  266. /* most others as normal. */
  267. GC_API void GC_ignore_self_finalize_mark_proc(p)
  268. ptr_t p;
  269. {
  270.     hdr * hhdr = HDR(p);
  271.     word descr = hhdr -> hb_descr;
  272.     ptr_t q, r;
  273.     ptr_t scan_limit;
  274.     ptr_t target_limit = p + WORDS_TO_BYTES(hhdr -> hb_sz) - 1;
  275.     
  276.     if ((descr & GC_DS_TAGS) == GC_DS_LENGTH) {
  277.        scan_limit = p + descr - sizeof(word);
  278.     } else {
  279.        scan_limit = target_limit + 1 - sizeof(word);
  280.     }
  281.     for (q = p; q <= scan_limit; q += ALIGNMENT) {
  282.      r = *(ptr_t *)q;
  283.      if (r < p || r > target_limit) {
  284.          GC_PUSH_ONE_HEAP((word)r, q);
  285.      }
  286.     }
  287. }
  288. /*ARGSUSED*/
  289. GC_API void GC_null_finalize_mark_proc(p)
  290. ptr_t p;
  291. {
  292. }
  293. /* Register a finalization function.  See gc.h for details. */
  294. /* in the nonthreads case, we try to avoid disabling signals, */
  295. /* since it can be expensive.  Threads packages typically */
  296. /* make it cheaper. */
  297. /* The last parameter is a procedure that determines */
  298. /* marking for finalization ordering.  Any objects marked */
  299. /* by that procedure will be guaranteed to not have been */
  300. /* finalized when this finalizer is invoked. */
  301. GC_API void GC_register_finalizer_inner(obj, fn, cd, ofn, ocd, mp)
  302. GC_PTR obj;
  303. GC_finalization_proc fn;
  304. GC_PTR cd;
  305. GC_finalization_proc * ofn;
  306. GC_PTR * ocd;
  307. finalization_mark_proc * mp;
  308. {
  309.     ptr_t base;
  310.     struct finalizable_object * curr_fo, * prev_fo;
  311.     int index;
  312.     struct finalizable_object *new_fo;
  313.     hdr *hhdr;
  314.     DCL_LOCK_STATE;
  315. #   ifdef THREADS
  316. DISABLE_SIGNALS();
  317. LOCK();
  318. #   endif
  319.     if (log_fo_table_size == -1
  320.         || GC_fo_entries > ((word)1 << log_fo_table_size)) {
  321. # ifndef THREADS
  322.          DISABLE_SIGNALS();
  323. # endif
  324.      GC_grow_table((struct hash_chain_entry ***)(&fo_head),
  325.            &log_fo_table_size);
  326. # ifdef CONDPRINT
  327.   if (GC_print_stats) {
  328.     GC_printf1("Grew fo table to %lu entriesn",
  329.      (unsigned long)(1 << log_fo_table_size));
  330.   }
  331. # endif
  332. # ifndef THREADS
  333.     ENABLE_SIGNALS();
  334. # endif
  335.     }
  336.     /* in the THREADS case signals are disabled and we hold allocation */
  337.     /* lock; otherwise neither is true.  Proceed carefully. */
  338.     base = (ptr_t)obj;
  339.     index = HASH2(base, log_fo_table_size);
  340.     prev_fo = 0; curr_fo = fo_head[index];
  341.     while (curr_fo != 0) {
  342.         if (curr_fo -> fo_hidden_base == HIDE_POINTER(base)) {
  343.             /* Interruption by a signal in the middle of this */
  344.             /* should be safe.  The client may see only *ocd */
  345.             /* updated, but we'll declare that to be his */
  346.             /* problem. */
  347.             if (ocd) *ocd = (GC_PTR) curr_fo -> fo_client_data;
  348.             if (ofn) *ofn = curr_fo -> fo_fn;
  349.             /* Delete the structure for base. */
  350.                 if (prev_fo == 0) {
  351.                   fo_head[index] = fo_next(curr_fo);
  352.                 } else {
  353.                   fo_set_next(prev_fo, fo_next(curr_fo));
  354.                 }
  355.             if (fn == 0) {
  356.                 GC_fo_entries--;
  357.                   /* May not happen if we get a signal.  But a high */
  358.                   /* estimate will only make the table larger than */
  359.                   /* necessary. */
  360. # if !defined(THREADS) && !defined(DBG_HDRS_ALL)
  361.                   GC_free((GC_PTR)curr_fo);
  362. # endif
  363.             } else {
  364.                 curr_fo -> fo_fn = fn;
  365.                 curr_fo -> fo_client_data = (ptr_t)cd;
  366.                 curr_fo -> fo_mark_proc = mp;
  367. /* Reinsert it.  We deleted it first to maintain */
  368. /* consistency in the event of a signal. */
  369. if (prev_fo == 0) {
  370.                   fo_head[index] = curr_fo;
  371.                 } else {
  372.                   fo_set_next(prev_fo, curr_fo);
  373.                 }
  374.             }
  375. #     ifdef THREADS
  376.                 UNLOCK();
  377.           ENABLE_SIGNALS();
  378. #     endif
  379.             return;
  380.         }
  381.         prev_fo = curr_fo;
  382.         curr_fo = fo_next(curr_fo);
  383.     }
  384.     if (ofn) *ofn = 0;
  385.     if (ocd) *ocd = 0;
  386.     if (fn == 0) {
  387. # ifdef THREADS
  388.             UNLOCK();
  389.          ENABLE_SIGNALS();
  390. # endif
  391.         return;
  392.     }
  393.     GET_HDR(base, hhdr);
  394.     if (0 == hhdr) {
  395.       /* We won't collect it, hence finalizer wouldn't be run. */
  396. #     ifdef THREADS
  397.           UNLOCK();
  398.        ENABLE_SIGNALS();
  399. #     endif
  400.       return;
  401.     }
  402.     new_fo = (struct finalizable_object *)
  403.      GC_INTERNAL_MALLOC(sizeof(struct finalizable_object),NORMAL);
  404.     if (0 == new_fo) {
  405. #     ifdef THREADS
  406. UNLOCK();
  407.      ENABLE_SIGNALS();
  408. #     endif
  409.       new_fo = (struct finalizable_object *)
  410.       GC_oom_fn(sizeof(struct finalizable_object));
  411.       if (0 == new_fo) {
  412. GC_finalization_failures++;
  413. return;
  414.       }
  415.       /* It's not likely we'll make it here, but ... */
  416. #     ifdef THREADS
  417.         DISABLE_SIGNALS();
  418. LOCK();
  419. #     endif
  420.     }
  421.     new_fo -> fo_hidden_base = (word)HIDE_POINTER(base);
  422.     new_fo -> fo_fn = fn;
  423.     new_fo -> fo_client_data = (ptr_t)cd;
  424.     new_fo -> fo_object_size = hhdr -> hb_sz;
  425.     new_fo -> fo_mark_proc = mp;
  426.     fo_set_next(new_fo, fo_head[index]);
  427.     GC_fo_entries++;
  428.     fo_head[index] = new_fo;
  429. #   ifdef THREADS
  430.         UNLOCK();
  431.      ENABLE_SIGNALS();
  432. #   endif
  433. }
  434. # if defined(__STDC__)
  435.     void GC_register_finalizer(void * obj,
  436.        GC_finalization_proc fn, void * cd,
  437.        GC_finalization_proc *ofn, void ** ocd)
  438. # else
  439.     void GC_register_finalizer(obj, fn, cd, ofn, ocd)
  440.     GC_PTR obj;
  441.     GC_finalization_proc fn;
  442.     GC_PTR cd;
  443.     GC_finalization_proc * ofn;
  444.     GC_PTR * ocd;
  445. # endif
  446. {
  447.     GC_register_finalizer_inner(obj, fn, cd, ofn,
  448.      ocd, GC_normal_finalize_mark_proc);
  449. }
  450. # if defined(__STDC__)
  451.     void GC_register_finalizer_ignore_self(void * obj,
  452.        GC_finalization_proc fn, void * cd,
  453.        GC_finalization_proc *ofn, void ** ocd)
  454. # else
  455.     void GC_register_finalizer_ignore_self(obj, fn, cd, ofn, ocd)
  456.     GC_PTR obj;
  457.     GC_finalization_proc fn;
  458.     GC_PTR cd;
  459.     GC_finalization_proc * ofn;
  460.     GC_PTR * ocd;
  461. # endif
  462. {
  463.     GC_register_finalizer_inner(obj, fn, cd, ofn,
  464.      ocd, GC_ignore_self_finalize_mark_proc);
  465. }
  466. # if defined(__STDC__)
  467.     void GC_register_finalizer_no_order(void * obj,
  468.        GC_finalization_proc fn, void * cd,
  469.        GC_finalization_proc *ofn, void ** ocd)
  470. # else
  471.     void GC_register_finalizer_no_order(obj, fn, cd, ofn, ocd)
  472.     GC_PTR obj;
  473.     GC_finalization_proc fn;
  474.     GC_PTR cd;
  475.     GC_finalization_proc * ofn;
  476.     GC_PTR * ocd;
  477. # endif
  478. {
  479.     GC_register_finalizer_inner(obj, fn, cd, ofn,
  480.      ocd, GC_null_finalize_mark_proc);
  481. }
  482. #ifndef NO_DEBUGGING
  483. void GC_dump_finalization()
  484. {
  485.     struct disappearing_link * curr_dl;
  486.     struct finalizable_object * curr_fo;
  487.     ptr_t real_ptr, real_link;
  488.     int dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
  489.     int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
  490.     int i;
  491.     GC_printf0("Disappearing links:n");
  492.     for (i = 0; i < dl_size; i++) {
  493.       for (curr_dl = dl_head[i]; curr_dl != 0; curr_dl = dl_next(curr_dl)) {
  494.         real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
  495.         real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
  496.         GC_printf2("Object: 0x%lx, Link:0x%lxn", real_ptr, real_link);
  497.       }
  498.     }
  499.     GC_printf0("Finalizers:n");
  500.     for (i = 0; i < fo_size; i++) {
  501.       for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
  502.         real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
  503.         GC_printf1("Finalizable object: 0x%lxn", real_ptr);
  504.       }
  505.     }
  506. }
  507. #endif
  508. /* Called with world stopped.  Cause disappearing links to disappear, */
  509. /* and invoke finalizers. */
  510. void GC_finalize()
  511. {
  512.     struct disappearing_link * curr_dl, * prev_dl, * next_dl;
  513.     struct finalizable_object * curr_fo, * prev_fo, * next_fo;
  514.     ptr_t real_ptr, real_link;
  515.     register int i;
  516.     int dl_size = (log_dl_table_size == -1 ) ? 0 : (1 << log_dl_table_size);
  517.     int fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
  518.     
  519.   /* Make disappearing links disappear */
  520.     for (i = 0; i < dl_size; i++) {
  521.       curr_dl = dl_head[i];
  522.       prev_dl = 0;
  523.       while (curr_dl != 0) {
  524.         real_ptr = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_obj);
  525.         real_link = (ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link);
  526.         if (!GC_is_marked(real_ptr)) {
  527.             *(word *)real_link = 0;
  528.             next_dl = dl_next(curr_dl);
  529.             if (prev_dl == 0) {
  530.                 dl_head[i] = next_dl;
  531.             } else {
  532.                 dl_set_next(prev_dl, next_dl);
  533.             }
  534.             GC_clear_mark_bit((ptr_t)curr_dl);
  535.             GC_dl_entries--;
  536.             curr_dl = next_dl;
  537.         } else {
  538.             prev_dl = curr_dl;
  539.             curr_dl = dl_next(curr_dl);
  540.         }
  541.       }
  542.     }
  543.   /* Mark all objects reachable via chains of 1 or more pointers */
  544.   /* from finalizable objects. */
  545.     GC_ASSERT(GC_mark_state == MS_NONE);
  546.     for (i = 0; i < fo_size; i++) {
  547.       for (curr_fo = fo_head[i]; curr_fo != 0; curr_fo = fo_next(curr_fo)) {
  548.         real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
  549.         if (!GC_is_marked(real_ptr)) {
  550.     GC_MARKED_FOR_FINALIZATION(real_ptr);
  551.             GC_MARK_FO(real_ptr, curr_fo -> fo_mark_proc);
  552.             if (GC_is_marked(real_ptr)) {
  553.                 WARN("Finalization cycle involving %lxn", real_ptr);
  554.             }
  555.         }
  556.       }
  557.     }
  558.   /* Enqueue for finalization all objects that are still */
  559.   /* unreachable. */
  560.     GC_words_finalized = 0;
  561.     for (i = 0; i < fo_size; i++) {
  562.       curr_fo = fo_head[i];
  563.       prev_fo = 0;
  564.       while (curr_fo != 0) {
  565.         real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
  566.         if (!GC_is_marked(real_ptr)) {
  567.     if (!GC_java_finalization) {
  568.               GC_set_mark_bit(real_ptr);
  569.     }
  570.             /* Delete from hash table */
  571.               next_fo = fo_next(curr_fo);
  572.               if (prev_fo == 0) {
  573.                 fo_head[i] = next_fo;
  574.               } else {
  575.                 fo_set_next(prev_fo, next_fo);
  576.               }
  577.               GC_fo_entries--;
  578.             /* Add to list of objects awaiting finalization. */
  579.               fo_set_next(curr_fo, GC_finalize_now);
  580.               GC_finalize_now = curr_fo;
  581.               /* unhide object pointer so any future collections will */
  582.               /* see it. */
  583.               curr_fo -> fo_hidden_base = 
  584.                (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
  585.               GC_words_finalized +=
  586.                   ALIGNED_WORDS(curr_fo -> fo_object_size)
  587.                + ALIGNED_WORDS(sizeof(struct finalizable_object));
  588.     GC_ASSERT(GC_is_marked(GC_base((ptr_t)curr_fo)));
  589.             curr_fo = next_fo;
  590.         } else {
  591.             prev_fo = curr_fo;
  592.             curr_fo = fo_next(curr_fo);
  593.         }
  594.       }
  595.     }
  596.   if (GC_java_finalization) {
  597.     /* make sure we mark everything reachable from objects finalized
  598.        using the no_order mark_proc */
  599.       for (curr_fo = GC_finalize_now; 
  600.     curr_fo != NULL; curr_fo = fo_next(curr_fo)) {
  601.    real_ptr = (ptr_t)curr_fo -> fo_hidden_base;
  602.    if (!GC_is_marked(real_ptr)) {
  603.        if (curr_fo -> fo_mark_proc == GC_null_finalize_mark_proc) {
  604.            GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
  605.        }
  606.        GC_set_mark_bit(real_ptr);
  607.    }
  608.       }
  609.   }
  610.   /* Remove dangling disappearing links. */
  611.     for (i = 0; i < dl_size; i++) {
  612.       curr_dl = dl_head[i];
  613.       prev_dl = 0;
  614.       while (curr_dl != 0) {
  615.         real_link = GC_base((ptr_t)REVEAL_POINTER(curr_dl -> dl_hidden_link));
  616.         if (real_link != 0 && !GC_is_marked(real_link)) {
  617.             next_dl = dl_next(curr_dl);
  618.             if (prev_dl == 0) {
  619.                 dl_head[i] = next_dl;
  620.             } else {
  621.                 dl_set_next(prev_dl, next_dl);
  622.             }
  623.             GC_clear_mark_bit((ptr_t)curr_dl);
  624.             GC_dl_entries--;
  625.             curr_dl = next_dl;
  626.         } else {
  627.             prev_dl = curr_dl;
  628.             curr_dl = dl_next(curr_dl);
  629.         }
  630.       }
  631.     }
  632. }
  633. #ifndef JAVA_FINALIZATION_NOT_NEEDED
  634. /* Enqueue all remaining finalizers to be run - Assumes lock is
  635.  * held, and signals are disabled */
  636. void GC_enqueue_all_finalizers()
  637. {
  638.     struct finalizable_object * curr_fo, * prev_fo, * next_fo;
  639.     ptr_t real_ptr;
  640.     register int i;
  641.     int fo_size;
  642.     
  643.     fo_size = (log_fo_table_size == -1 ) ? 0 : (1 << log_fo_table_size);
  644.     GC_words_finalized = 0;
  645.     for (i = 0; i < fo_size; i++) {
  646.         curr_fo = fo_head[i];
  647.         prev_fo = 0;
  648.       while (curr_fo != 0) {
  649.           real_ptr = (ptr_t)REVEAL_POINTER(curr_fo -> fo_hidden_base);
  650.           GC_MARK_FO(real_ptr, GC_normal_finalize_mark_proc);
  651.           GC_set_mark_bit(real_ptr);
  652.  
  653.           /* Delete from hash table */
  654.           next_fo = fo_next(curr_fo);
  655.           if (prev_fo == 0) {
  656.               fo_head[i] = next_fo;
  657.           } else {
  658.               fo_set_next(prev_fo, next_fo);
  659.           }
  660.           GC_fo_entries--;
  661.           /* Add to list of objects awaiting finalization. */
  662.           fo_set_next(curr_fo, GC_finalize_now);
  663.           GC_finalize_now = curr_fo;
  664.           /* unhide object pointer so any future collections will */
  665.           /* see it. */
  666.           curr_fo -> fo_hidden_base = 
  667.          (word) REVEAL_POINTER(curr_fo -> fo_hidden_base);
  668.           GC_words_finalized +=
  669.             ALIGNED_WORDS(curr_fo -> fo_object_size)
  670.          + ALIGNED_WORDS(sizeof(struct finalizable_object));
  671.           curr_fo = next_fo;
  672.         }
  673.     }
  674.     return;
  675. }
  676. /* Invoke all remaining finalizers that haven't yet been run. 
  677.  * This is needed for strict compliance with the Java standard, 
  678.  * which can make the runtime guarantee that all finalizers are run.
  679.  * Unfortunately, the Java standard implies we have to keep running
  680.  * finalizers until there are no more left, a potential infinite loop.
  681.  * YUCK.
  682.  * Note that this is even more dangerous than the usual Java
  683.  * finalizers, in that objects reachable from static variables
  684.  * may have been finalized when these finalizers are run.
  685.  * Finalizers run at this point must be prepared to deal with a
  686.  * mostly broken world.
  687.  * This routine is externally callable, so is called without 
  688.  * the allocation lock. 
  689.  */
  690. GC_API void GC_finalize_all()
  691. {
  692.     DCL_LOCK_STATE;
  693.     DISABLE_SIGNALS();
  694.     LOCK();
  695.     while (GC_fo_entries > 0) {
  696.       GC_enqueue_all_finalizers();
  697.       UNLOCK();
  698.       ENABLE_SIGNALS();
  699.       GC_INVOKE_FINALIZERS();
  700.       DISABLE_SIGNALS();
  701.       LOCK();
  702.     }
  703.     UNLOCK();
  704.     ENABLE_SIGNALS();
  705. }
  706. #endif
  707. /* Returns true if it is worth calling GC_invoke_finalizers. (Useful if */
  708. /* finalizers can only be called from some kind of `safe state' and */
  709. /* getting into that safe state is expensive.) */
  710. int GC_should_invoke_finalizers GC_PROTO((void))
  711. {
  712.     return GC_finalize_now != 0;
  713. }
  714. /* Invoke finalizers for all objects that are ready to be finalized. */
  715. /* Should be called without allocation lock. */
  716. int GC_invoke_finalizers()
  717. {
  718.     struct finalizable_object * curr_fo;
  719.     int count = 0;
  720.     word mem_freed_before;
  721.     DCL_LOCK_STATE;
  722.     
  723.     while (GC_finalize_now != 0) {
  724. # ifdef THREADS
  725.     DISABLE_SIGNALS();
  726.     LOCK();
  727. # endif
  728. if (count == 0) {
  729.     mem_freed_before = GC_mem_freed;
  730. }
  731.      curr_fo = GC_finalize_now;
  732. # ifdef THREADS
  733.       if (curr_fo != 0) GC_finalize_now = fo_next(curr_fo);
  734.     UNLOCK();
  735.     ENABLE_SIGNALS();
  736.     if (curr_fo == 0) break;
  737. # else
  738.     GC_finalize_now = fo_next(curr_fo);
  739. # endif
  740.   fo_set_next(curr_fo, 0);
  741.      (*(curr_fo -> fo_fn))((ptr_t)(curr_fo -> fo_hidden_base),
  742.            curr_fo -> fo_client_data);
  743.      curr_fo -> fo_client_data = 0;
  744. ++count;
  745. # ifdef UNDEFINED
  746.     /* This is probably a bad idea.  It throws off accounting if */
  747.     /* nearly all objects are finalizable.  O.w. it shouldn't  */
  748.     /* matter.  */
  749.          GC_free((GC_PTR)curr_fo);
  750. # endif
  751.     }
  752.     if (count != 0 && mem_freed_before != GC_mem_freed) {
  753.         LOCK();
  754. GC_finalizer_mem_freed += (GC_mem_freed - mem_freed_before);
  755. UNLOCK();
  756.     }
  757.     return count;
  758. }
  759. void (* GC_finalizer_notifier)() = (void (*) GC_PROTO((void)))0;
  760. static GC_word last_finalizer_notification = 0;
  761. void GC_notify_or_invoke_finalizers GC_PROTO((void))
  762. {
  763.     if (GC_finalize_now == 0) return;
  764.     if (!GC_finalize_on_demand) {
  765. (void) GC_invoke_finalizers();
  766. # ifndef THREADS
  767.   GC_ASSERT(GC_finalize_now == 0);
  768. # endif /* Otherwise GC can run concurrently and add more */
  769. return;
  770.     }
  771.     if (GC_finalizer_notifier != (void (*) GC_PROTO((void)))0
  772. && last_finalizer_notification != GC_gc_no) {
  773. last_finalizer_notification = GC_gc_no;
  774. GC_finalizer_notifier();
  775.     }
  776. }
  777. # ifdef __STDC__
  778.     GC_PTR GC_call_with_alloc_lock(GC_fn_type fn,
  779.       GC_PTR client_data)
  780. # else
  781.     GC_PTR GC_call_with_alloc_lock(fn, client_data)
  782.     GC_fn_type fn;
  783.     GC_PTR client_data;
  784. # endif
  785. {
  786.     GC_PTR result;
  787.     DCL_LOCK_STATE;
  788.     
  789. #   ifdef THREADS
  790.       DISABLE_SIGNALS();
  791.       LOCK();
  792.       SET_LOCK_HOLDER();
  793. #   endif
  794.     result = (*fn)(client_data);
  795. #   ifdef THREADS
  796. #     ifndef GC_ASSERTIONS
  797.         UNSET_LOCK_HOLDER();
  798. #     endif /* o.w. UNLOCK() does it implicitly */
  799.       UNLOCK();
  800.       ENABLE_SIGNALS();
  801. #   endif
  802.     return(result);
  803. }
  804. #if !defined(NO_DEBUGGING)
  805. void GC_print_finalization_stats()
  806. {
  807.     struct finalizable_object *fo = GC_finalize_now;
  808.     size_t ready = 0;
  809.     GC_printf2("%lu finalization table entries; %lu disappearing linksn",
  810.        GC_fo_entries, GC_dl_entries);
  811.     for (; 0 != fo; fo = fo_next(fo)) ++ready;
  812.     GC_printf1("%lu objects are eligible for immediate finalizationn", ready);
  813. }
  814. #endif /* NO_DEBUGGING */