comparison src/data.c @ 858:2c12fe2da451

[xemacs-hg @ 2002-05-31 09:38:45 by michaels] 2002-05-28 Martin Köbele <mkoebele@mkoebele.de>, Jens Müller <jmueller@informatik.uni-tuebingen.de> * lrecord.h (lrecord_type): Add lrecord_type_weak_box to lrecord_type enumeration. * alloc.c (garbage_collect_1): Call prune_weak_boxes(). * lisp.h (struct weak_box): * data.c: (prune_weak_boxes): (mark_weak_box): (print_weak_box): (weak_box_equal): (weak_box_hash): (make_weak_box): (Fmake_weak_box): (Fweak_box_ref): (Fweak_boxp): (syms_of_data): (vars_of_data): Add implementation of weak boxes.
author michaels
date Fri, 31 May 2002 09:38:49 +0000
parents 6728e641994e
children 804517e16990
comparison
equal deleted inserted replaced
857:b5278486690c 858:2c12fe2da451
2002 CHECK_WEAK_LIST (weak); 2002 CHECK_WEAK_LIST (weak);
2003 XWEAK_LIST_LIST (weak) = new_list; 2003 XWEAK_LIST_LIST (weak) = new_list;
2004 return new_list; 2004 return new_list;
2005 } 2005 }
2006 2006
2007 /************************************************************************/
2008 /* weak boxes */
2009 /************************************************************************/
2010
2011 static Lisp_Object Vall_weak_boxes; /* Gemarke es niemals ever!!! */
2012
2013 void
2014 prune_weak_boxes (void)
2015 {
2016 Lisp_Object rest, prev = Qnil;
2017
2018 for (rest = Vall_weak_boxes;
2019 !NILP(rest);
2020 rest = XWEAK_BOX (rest)->next_weak_box)
2021 {
2022 if (! (marked_p (rest)))
2023 /* This weak box itself is garbage. Remove it from the list. */
2024 if (NILP (prev))
2025 Vall_weak_boxes = XWEAK_BOX (rest)->next_weak_box;
2026 else
2027 XWEAK_BOX (prev)->next_weak_box = XWEAK_BOX (rest)->next_weak_box;
2028 else if (!marked_p(XWEAK_BOX (rest)->value))
2029 XSET_WEAK_BOX (rest, Qnil);
2030 }
2031 }
2032
2033 static Lisp_Object
2034 mark_weak_box (Lisp_Object obj)
2035 {
2036 return Qnil;
2037 }
2038
2039 static void
2040 print_weak_box (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
2041 {
2042 if (print_readably)
2043 printing_unreadable_object ("#<weak_box>");
2044 write_fmt_string (printcharfun, "#<weak_box>");
2045 }
2046
2047 static int
2048 weak_box_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
2049 {
2050 struct weak_box *b1 = XWEAK_BOX (obj1);
2051 struct weak_box *b2 = XWEAK_BOX (obj2);
2052
2053 return (internal_equal (b1->value, b2->value, depth + 1));
2054 }
2055
2056 static Hashcode
2057 weak_box_hash (Lisp_Object obj, int depth)
2058 {
2059 struct weak_box *b = XWEAK_BOX (obj);
2060
2061 return internal_hash (b->value, depth + 1);
2062 }
2063
2064 Lisp_Object
2065 make_weak_box (Lisp_Object value)
2066 {
2067 Lisp_Object result;
2068
2069 struct weak_box *wb =
2070 alloc_lcrecord_type (struct weak_box, &lrecord_weak_box);
2071
2072 wb->value = value;
2073 result = wrap_weak_box (wb);
2074 wb->next_weak_box = Vall_weak_boxes;
2075 Vall_weak_boxes = result;
2076 return result;
2077 }
2078
2079 static const struct lrecord_description weak_box_description[] = {
2080 { XD_LO_LINK, offsetof (struct weak_box, value) },
2081 { XD_END}
2082 };
2083
2084 DEFINE_LRECORD_IMPLEMENTATION ("weak_box", weak_box,
2085 mark_weak_box, print_weak_box,
2086 0, weak_box_equal, weak_box_hash,
2087 weak_box_description,
2088 struct weak_box);
2089
2090 DEFUN ("make-weak-box", Fmake_weak_box, 1, 1, 0, /*
2091 Return a new weak box from value CONTENTS.
2092 The weak box is a reference to CONTENTS which may be extracted with
2093 `weak-box-ref'. However, the weak box does not contribute to the
2094 reachability of CONTENTS. When CONTENTS is garbage-collected,
2095 `weak-box-ref' will return NIL.
2096 */
2097 (value))
2098 {
2099 return make_weak_box(value);
2100 }
2101
2102 DEFUN ("weak-box-ref", Fweak_box_ref, 1, 1, 0, /*
2103 Return the contents of weak box WEAK-BOX.
2104 If the contents have been GCed, return NIL.
2105 */
2106 (box))
2107 {
2108 return XWEAK_BOX(box)->value;
2109 }
2110
2111 DEFUN ("weak-box-p", Fweak_boxp, 1, 1, 0, /*
2112 Return non-nil if OBJECT is a weak box.
2113 */
2114 (object))
2115 {
2116 return WEAK_BOXP (object) ? Qt : Qnil;
2117 }
2118
2007 2119
2008 /************************************************************************/ 2120 /************************************************************************/
2009 /* initialization */ 2121 /* initialization */
2010 /************************************************************************/ 2122 /************************************************************************/
2011 2123
2110 2222
2111 void 2223 void
2112 syms_of_data (void) 2224 syms_of_data (void)
2113 { 2225 {
2114 INIT_LRECORD_IMPLEMENTATION (weak_list); 2226 INIT_LRECORD_IMPLEMENTATION (weak_list);
2227 INIT_LRECORD_IMPLEMENTATION (weak_box);
2115 2228
2116 DEFSYMBOL (Qquote); 2229 DEFSYMBOL (Qquote);
2117 DEFSYMBOL (Qlambda); 2230 DEFSYMBOL (Qlambda);
2118 DEFSYMBOL (Qlistp); 2231 DEFSYMBOL (Qlistp);
2119 DEFSYMBOL (Qtrue_list_p); 2232 DEFSYMBOL (Qtrue_list_p);
2226 DEFSUBR (Fweak_list_p); 2339 DEFSUBR (Fweak_list_p);
2227 DEFSUBR (Fmake_weak_list); 2340 DEFSUBR (Fmake_weak_list);
2228 DEFSUBR (Fweak_list_type); 2341 DEFSUBR (Fweak_list_type);
2229 DEFSUBR (Fweak_list_list); 2342 DEFSUBR (Fweak_list_list);
2230 DEFSUBR (Fset_weak_list_list); 2343 DEFSUBR (Fset_weak_list_list);
2344
2345 DEFSUBR (Fmake_weak_box);
2346 DEFSUBR (Fweak_box_ref);
2347 DEFSUBR (Fweak_boxp);
2231 } 2348 }
2232 2349
2233 void 2350 void
2234 vars_of_data (void) 2351 vars_of_data (void)
2235 { 2352 {
2236 /* This must not be staticpro'd */ 2353 /* This must not be staticpro'd */
2237 Vall_weak_lists = Qnil; 2354 Vall_weak_lists = Qnil;
2238 dump_add_weak_object_chain (&Vall_weak_lists); 2355 dump_add_weak_object_chain (&Vall_weak_lists);
2356
2357 Vall_weak_boxes = Qnil;
2358 dump_add_weak_object_chain (&Vall_weak_boxes);
2239 2359
2240 #ifdef DEBUG_XEMACS 2360 #ifdef DEBUG_XEMACS
2241 DEFVAR_BOOL ("debug-issue-ebola-notices", &debug_issue_ebola_notices /* 2361 DEFVAR_BOOL ("debug-issue-ebola-notices", &debug_issue_ebola_notices /*
2242 If non-zero, note when your code may be suffering from char-int confoundance. 2362 If non-zero, note when your code may be suffering from char-int confoundance.
2243 That is to say, if XEmacs encounters a usage of `eq', `memq', `equal', 2363 That is to say, if XEmacs encounters a usage of `eq', `memq', `equal',