0
|
1 /* Opaque Lisp objects.
|
|
2 Copyright (C) 1993, 1994, 1995 Sun Microsystems, Inc.
|
|
3 Copyright (C) 1995, 1996 Ben Wing.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* Written by Ben Wing, October 1993. */
|
|
25
|
|
26 /* "Opaque" is used internally to hold keep track of allocated memory
|
|
27 so it gets GC'd properly, and to store arbitrary data in places
|
|
28 where a Lisp_Object is required and which may get GC'd. (e.g. as
|
|
29 the argument to record_unwind_protect()). Once created in C,
|
|
30 opaque objects cannot be resized.
|
|
31
|
|
32 OPAQUE OBJECTS SHOULD NEVER ESCAPE TO THE LISP LEVEL. Some code
|
|
33 depends on this. As such, opaque objects are a generalization
|
|
34 of the Qunbound marker.
|
|
35
|
|
36 "Opaque lists" are used to keep track of lots of opaque objects
|
|
37 of a particular size so that they can be efficiently "freed" and
|
|
38 re-used again without actually entering the Lisp allocation system
|
|
39 (and consequently doing a malloc()).
|
|
40 */
|
|
41
|
|
42 #include <config.h>
|
|
43 #include "lisp.h"
|
|
44 #include "opaque.h"
|
|
45
|
|
46 Lisp_Object Qopaquep;
|
|
47
|
|
48 static int in_opaque_list_marking;
|
|
49
|
|
50 /* Holds freed opaque objects created with make_opaque_ptr().
|
|
51 We do this quite often so it's a noticeable win if we don't
|
|
52 create GC junk. */
|
|
53 Lisp_Object Vopaque_ptr_free_list;
|
|
54
|
|
55 static Lisp_Object mark_opaque (Lisp_Object, void (*) (Lisp_Object));
|
|
56 static unsigned int sizeof_opaque (CONST void *header);
|
|
57 static void print_opaque (Lisp_Object obj, Lisp_Object printcharfun,
|
|
58 int escapeflag);
|
231
|
59 static int equal_opaque (Lisp_Object obj1, Lisp_Object obj2, int depth);
|
|
60 static unsigned long hash_opaque (Lisp_Object obj, int depth);
|
|
61
|
0
|
62 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("opaque", opaque,
|
231
|
63 mark_opaque, print_opaque, 0,
|
|
64 equal_opaque, hash_opaque,
|
0
|
65 sizeof_opaque, struct Lisp_Opaque);
|
|
66
|
|
67 static Lisp_Object
|
|
68 mark_opaque (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
69 {
|
|
70 #ifdef ERROR_CHECK_GC
|
|
71 if (!in_opaque_list_marking)
|
|
72 /* size is non-int for objects on an opaque free list. We sure
|
|
73 as hell better not be marking any of these objects unless
|
|
74 we're marking an opaque list. */
|
|
75 assert (INTP (XOPAQUE (obj)->size_or_chain));
|
|
76 else
|
|
77 /* marking an opaque on the free list doesn't do any recursive
|
|
78 markings, so we better not have non-freed opaques on a free
|
|
79 list. */
|
|
80 assert (!INTP (XOPAQUE (obj)->size_or_chain));
|
|
81 #endif
|
|
82 if (INTP (XOPAQUE (obj)->size_or_chain) && XOPAQUE_MARKFUN (obj))
|
173
|
83 return XOPAQUE_MARKFUN (obj) (obj, markobj);
|
0
|
84 else
|
|
85 return XOPAQUE (obj)->size_or_chain;
|
|
86 }
|
|
87
|
|
88 /* Should never, ever be called. (except by an external debugger) */
|
|
89 static void
|
|
90 print_opaque (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
91 {
|
|
92 char buf[200];
|
|
93 if (INTP (XOPAQUE (obj)->size_or_chain))
|
173
|
94 sprintf (buf, "#<INTERNAL EMACS BUG (opaque, size=%ld) 0x%p>",
|
|
95 (long) XOPAQUE_SIZE (obj), (void *) XPNTR (obj));
|
0
|
96 else
|
173
|
97 sprintf (buf, "#<INTERNAL EMACS BUG (opaque, freed) 0x%p>",
|
|
98 (void *) XPNTR (obj));
|
0
|
99 write_c_string (buf, printcharfun);
|
|
100 }
|
|
101
|
|
102 static unsigned int
|
|
103 sizeof_opaque (CONST void *header)
|
|
104 {
|
|
105 CONST struct Lisp_Opaque *p = (CONST struct Lisp_Opaque *) header;
|
|
106 if (!INTP (p->size_or_chain))
|
|
107 return sizeof (*p);
|
|
108 return sizeof (*p) + XINT (p->size_or_chain) - sizeof (int);
|
|
109 }
|
|
110
|
|
111 Lisp_Object
|
|
112 make_opaque (int size, CONST void *data)
|
|
113 {
|
185
|
114 struct Lisp_Opaque *p = (struct Lisp_Opaque *)
|
|
115 alloc_lcrecord (sizeof (*p) + size - sizeof (int), lrecord_opaque);
|
0
|
116 Lisp_Object val;
|
|
117
|
|
118 p->markfun = 0;
|
|
119 p->size_or_chain = make_int (size);
|
|
120 if (data)
|
|
121 memcpy (p->data, data, size);
|
|
122 else
|
|
123 memset (p->data, 0, size);
|
|
124 XSETOPAQUE (val, p);
|
|
125 return val;
|
|
126 }
|
|
127
|
231
|
128 /* This will not work correctly for opaques with subobjects! */
|
|
129
|
|
130 static int
|
|
131 equal_opaque (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
132 {
|
|
133 #ifdef DEBUG_XEMACS
|
|
134 assert (!XOPAQUE_MARKFUN (obj1) && !XOPAQUE_MARKFUN (obj2));
|
|
135 assert (INTP (XOPAQUE(obj1)->size_or_chain));
|
|
136 assert (INTP (XOPAQUE(obj2)->size_or_chain));
|
|
137 #endif
|
|
138 if (XOPAQUE_SIZE(obj1) != XOPAQUE_SIZE(obj2))
|
|
139 return 0;
|
|
140 return (XOPAQUE_SIZE(obj1) == sizeof(*XOPAQUE_DATA(obj1))
|
|
141 ? *XOPAQUE_DATA(obj1) == *XOPAQUE_DATA(obj2)
|
|
142 : memcmp (XOPAQUE_DATA(obj1), XOPAQUE_DATA(obj2),
|
|
143 XOPAQUE_SIZE(obj1)) == 0);
|
|
144 }
|
|
145
|
|
146 /* This will not work correctly for opaques with subobjects! */
|
|
147
|
|
148 static unsigned long
|
|
149 hash_opaque (Lisp_Object obj, int depth)
|
|
150 {
|
|
151 #ifdef DEBUG_XEMACS
|
|
152 assert (!XOPAQUE_MARKFUN (obj));
|
|
153 assert (INTP (XOPAQUE(obj)->size_or_chain));
|
|
154 #endif
|
|
155 if (XOPAQUE_SIZE(obj) == sizeof (unsigned long))
|
|
156 return (unsigned int) *XOPAQUE_DATA(obj);
|
|
157 else
|
|
158 return memory_hash (XOPAQUE_DATA(obj), XOPAQUE_SIZE(obj));
|
|
159 }
|
|
160
|
0
|
161 static Lisp_Object mark_opaque_list (Lisp_Object, void (*) (Lisp_Object));
|
|
162 DEFINE_LRECORD_IMPLEMENTATION ("opaque-list", opaque_list,
|
|
163 mark_opaque_list, internal_object_printer,
|
|
164 0, 0, 0, struct Lisp_Opaque_List);
|
|
165
|
|
166 static Lisp_Object
|
|
167 mark_opaque_list (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
168 {
|
|
169 in_opaque_list_marking++;
|
|
170 (markobj) (XOPAQUE_LIST (obj)->free);
|
|
171 in_opaque_list_marking--;
|
|
172 return Qnil;
|
|
173 }
|
|
174
|
|
175 Lisp_Object
|
|
176 make_opaque_list (int size,
|
|
177 Lisp_Object (*markfun) (Lisp_Object obj,
|
|
178 void (*markobj) (Lisp_Object)))
|
|
179 {
|
|
180 Lisp_Object val = Qnil;
|
185
|
181 struct Lisp_Opaque_List *p =
|
|
182 alloc_lcrecord_type (struct Lisp_Opaque_List, lrecord_opaque_list);
|
0
|
183
|
|
184 p->markfun = markfun;
|
|
185 p->size = size;
|
|
186 p->free = Qnil;
|
|
187 XSETOPAQUE_LIST (val, p);
|
|
188 return val;
|
|
189 }
|
|
190
|
|
191 Lisp_Object
|
|
192 allocate_managed_opaque (Lisp_Object opaque_list, CONST void *data)
|
|
193 {
|
|
194 struct Lisp_Opaque_List *li = XOPAQUE_LIST (opaque_list);
|
|
195 Lisp_Object val;
|
|
196
|
|
197 if (!NILP (li->free))
|
|
198 {
|
|
199 val = li->free;
|
|
200 li->free = XOPAQUE (val)->size_or_chain;
|
|
201 #ifdef ERROR_CHECK_GC
|
|
202 assert (NILP (li->free) || OPAQUEP (li->free));
|
|
203 #endif
|
|
204 XOPAQUE (val)->size_or_chain = make_int (li->size);
|
|
205 if (data)
|
|
206 memcpy (XOPAQUE (val)->data, data, li->size);
|
|
207 else
|
|
208 memset (XOPAQUE (val)->data, 0, li->size);
|
|
209 }
|
|
210 else
|
|
211 val = make_opaque (li->size, data);
|
|
212 XOPAQUE (val)->markfun = li->markfun;
|
|
213 return val;
|
|
214 }
|
|
215
|
|
216 void
|
|
217 free_managed_opaque (Lisp_Object opaque_list, Lisp_Object opaque)
|
|
218 {
|
|
219 struct Lisp_Opaque_List *li = XOPAQUE_LIST (opaque_list);
|
|
220
|
|
221 #ifdef ERROR_CHECK_GC
|
|
222 assert (INTP (XOPAQUE (opaque)->size_or_chain));
|
|
223 #endif
|
|
224 XOPAQUE (opaque)->size_or_chain = li->free;
|
|
225 li->free = opaque;
|
|
226 }
|
|
227
|
|
228 /* stuff to handle opaque pointers */
|
|
229
|
|
230 Lisp_Object
|
|
231 make_opaque_ptr (CONST void *val)
|
|
232 {
|
|
233 return allocate_managed_opaque (Vopaque_ptr_free_list,
|
|
234 (CONST void *) &val);
|
|
235 }
|
|
236
|
|
237 /* Be wery wery careful with this. Same admonitions as with
|
|
238 free_cons() apply. */
|
|
239
|
|
240 void
|
|
241 free_opaque_ptr (Lisp_Object ptr)
|
|
242 {
|
|
243 free_managed_opaque (Vopaque_ptr_free_list, ptr);
|
|
244 }
|
|
245
|
|
246 Lisp_Object
|
|
247 make_opaque_long (long val)
|
|
248 {
|
|
249 return make_opaque (sizeof (val), (void *) &val);
|
|
250 }
|
|
251
|
|
252 void
|
|
253 init_opaque_once_early (void)
|
|
254 {
|
|
255 Vopaque_ptr_free_list = make_opaque_list (sizeof (void *), 0);
|
|
256 staticpro (&Vopaque_ptr_free_list);
|
|
257 }
|