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
|
|
56 mark_opaque (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
57 {
|
337
|
58 struct Lisp_Opaque *p = XOPAQUE (obj);
|
|
59 /* Egcs 1.1.1 sometimes crashes on INTP (p->size_or_chain) */
|
|
60 Lisp_Object size_or_chain = p->size_or_chain;
|
0
|
61 #ifdef ERROR_CHECK_GC
|
|
62 if (!in_opaque_list_marking)
|
|
63 /* size is non-int for objects on an opaque free list. We sure
|
|
64 as hell better not be marking any of these objects unless
|
|
65 we're marking an opaque list. */
|
337
|
66 assert (GC_INTP (size_or_chain));
|
0
|
67 else
|
|
68 /* marking an opaque on the free list doesn't do any recursive
|
|
69 markings, so we better not have non-freed opaques on a free
|
|
70 list. */
|
337
|
71 assert (!GC_INTP (size_or_chain));
|
0
|
72 #endif
|
337
|
73 if (GC_INTP (size_or_chain) && OPAQUE_MARKFUN (p))
|
|
74 return OPAQUE_MARKFUN (p) (obj, markobj);
|
0
|
75 else
|
337
|
76 return size_or_chain;
|
0
|
77 }
|
|
78
|
|
79 /* Should never, ever be called. (except by an external debugger) */
|
|
80 static void
|
|
81 print_opaque (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
82 {
|
337
|
83 CONST struct Lisp_Opaque *p = XOPAQUE (obj);
|
|
84 /* Egcs 1.1.1 sometimes crashes on INTP (p->size_or_chain) */
|
|
85 Lisp_Object size_or_chain = p->size_or_chain;
|
0
|
86 char buf[200];
|
337
|
87 if (GC_INTP (size_or_chain))
|
267
|
88 sprintf (buf, "#<INTERNAL EMACS BUG (opaque, size=%ld) 0x%lx>",
|
337
|
89 (long) OPAQUE_SIZE (p), (unsigned long) XPNTR (obj));
|
0
|
90 else
|
267
|
91 sprintf (buf, "#<INTERNAL EMACS BUG (opaque, freed) 0x%lx>",
|
|
92 (unsigned long) XPNTR (obj));
|
0
|
93 write_c_string (buf, printcharfun);
|
|
94 }
|
|
95
|
272
|
96 static size_t
|
0
|
97 sizeof_opaque (CONST void *header)
|
|
98 {
|
|
99 CONST struct Lisp_Opaque *p = (CONST struct Lisp_Opaque *) header;
|
337
|
100 /* Egcs 1.1.1 sometimes crashes on INTP (p->size_or_chain) */
|
|
101 Lisp_Object size_or_chain = p->size_or_chain;
|
|
102 if (!GC_INTP (size_or_chain))
|
0
|
103 return sizeof (*p);
|
337
|
104 return sizeof (*p) + XINT (size_or_chain) - sizeof (int);
|
0
|
105 }
|
|
106
|
|
107 Lisp_Object
|
|
108 make_opaque (int size, CONST void *data)
|
|
109 {
|
185
|
110 struct Lisp_Opaque *p = (struct Lisp_Opaque *)
|
|
111 alloc_lcrecord (sizeof (*p) + size - sizeof (int), lrecord_opaque);
|
0
|
112 Lisp_Object val;
|
|
113
|
|
114 p->markfun = 0;
|
|
115 p->size_or_chain = make_int (size);
|
|
116 if (data)
|
|
117 memcpy (p->data, data, size);
|
|
118 else
|
|
119 memset (p->data, 0, size);
|
|
120 XSETOPAQUE (val, p);
|
|
121 return val;
|
|
122 }
|
|
123
|
231
|
124 /* This will not work correctly for opaques with subobjects! */
|
|
125
|
|
126 static int
|
|
127 equal_opaque (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
128 {
|
|
129 #ifdef DEBUG_XEMACS
|
337
|
130 {
|
|
131 /* Egcs 1.1.1 sometimes crashes on INTP (p->size_or_chain) */
|
|
132 Lisp_Object size_or_chain_1 = XOPAQUE (obj1)->size_or_chain;
|
|
133 Lisp_Object size_or_chain_2 = XOPAQUE (obj2)->size_or_chain;
|
|
134 assert (INTP (size_or_chain_1));
|
|
135 assert (INTP (size_or_chain_2));
|
|
136 assert (!XOPAQUE_MARKFUN (obj1) && !XOPAQUE_MARKFUN (obj2));
|
|
137 }
|
231
|
138 #endif
|
337
|
139 {
|
|
140 size_t size;
|
|
141 return ((size = XOPAQUE_SIZE (obj1)) == XOPAQUE_SIZE (obj2) &&
|
|
142 !memcmp (XOPAQUE_DATA (obj1), XOPAQUE_DATA (obj2), size));
|
|
143 }
|
231
|
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
|
337
|
152 {
|
|
153 /* Egcs 1.1.1 sometimes crashes on INTP (p->size_or_chain) */
|
|
154 Lisp_Object size_or_chain = XOPAQUE (obj)->size_or_chain;
|
|
155 assert (INTP (size_or_chain));
|
|
156 assert (!XOPAQUE_MARKFUN (obj));
|
|
157 }
|
231
|
158 #endif
|
337
|
159 if (XOPAQUE_SIZE (obj) == sizeof (unsigned long))
|
|
160 return *((unsigned long *) XOPAQUE_DATA (obj));
|
231
|
161 else
|
337
|
162 return memory_hash (XOPAQUE_DATA (obj), XOPAQUE_SIZE (obj));
|
231
|
163 }
|
|
164
|
272
|
165 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION ("opaque", opaque,
|
|
166 mark_opaque, print_opaque, 0,
|
|
167 equal_opaque, hash_opaque,
|
|
168 sizeof_opaque, struct Lisp_Opaque);
|
0
|
169
|
|
170 static Lisp_Object
|
|
171 mark_opaque_list (Lisp_Object obj, void (*markobj) (Lisp_Object))
|
|
172 {
|
|
173 in_opaque_list_marking++;
|
|
174 (markobj) (XOPAQUE_LIST (obj)->free);
|
|
175 in_opaque_list_marking--;
|
|
176 return Qnil;
|
|
177 }
|
|
178
|
|
179 Lisp_Object
|
|
180 make_opaque_list (int size,
|
|
181 Lisp_Object (*markfun) (Lisp_Object obj,
|
|
182 void (*markobj) (Lisp_Object)))
|
|
183 {
|
272
|
184 Lisp_Object val;
|
185
|
185 struct Lisp_Opaque_List *p =
|
|
186 alloc_lcrecord_type (struct Lisp_Opaque_List, lrecord_opaque_list);
|
0
|
187
|
|
188 p->markfun = markfun;
|
|
189 p->size = size;
|
|
190 p->free = Qnil;
|
|
191 XSETOPAQUE_LIST (val, p);
|
|
192 return val;
|
|
193 }
|
|
194
|
272
|
195 DEFINE_LRECORD_IMPLEMENTATION ("opaque-list", opaque_list,
|
|
196 mark_opaque_list, internal_object_printer,
|
|
197 0, 0, 0, struct Lisp_Opaque_List);
|
|
198
|
0
|
199 Lisp_Object
|
|
200 allocate_managed_opaque (Lisp_Object opaque_list, CONST void *data)
|
|
201 {
|
|
202 struct Lisp_Opaque_List *li = XOPAQUE_LIST (opaque_list);
|
|
203 Lisp_Object val;
|
|
204
|
|
205 if (!NILP (li->free))
|
|
206 {
|
|
207 val = li->free;
|
|
208 li->free = XOPAQUE (val)->size_or_chain;
|
|
209 #ifdef ERROR_CHECK_GC
|
|
210 assert (NILP (li->free) || OPAQUEP (li->free));
|
|
211 #endif
|
|
212 XOPAQUE (val)->size_or_chain = make_int (li->size);
|
|
213 if (data)
|
|
214 memcpy (XOPAQUE (val)->data, data, li->size);
|
|
215 else
|
|
216 memset (XOPAQUE (val)->data, 0, li->size);
|
|
217 }
|
|
218 else
|
|
219 val = make_opaque (li->size, data);
|
|
220 XOPAQUE (val)->markfun = li->markfun;
|
|
221 return val;
|
|
222 }
|
|
223
|
|
224 void
|
|
225 free_managed_opaque (Lisp_Object opaque_list, Lisp_Object opaque)
|
|
226 {
|
|
227 struct Lisp_Opaque_List *li = XOPAQUE_LIST (opaque_list);
|
|
228
|
|
229 #ifdef ERROR_CHECK_GC
|
337
|
230 {
|
|
231 /* Egcs 1.1.1 sometimes crashes on INTP (p->size_or_chain) */
|
|
232 Lisp_Object size_or_chain = XOPAQUE (opaque)->size_or_chain;
|
|
233 assert (INTP (size_or_chain));
|
|
234 }
|
0
|
235 #endif
|
|
236 XOPAQUE (opaque)->size_or_chain = li->free;
|
|
237 li->free = opaque;
|
|
238 }
|
|
239
|
|
240 /* stuff to handle opaque pointers */
|
|
241
|
|
242 Lisp_Object
|
|
243 make_opaque_ptr (CONST void *val)
|
|
244 {
|
|
245 return allocate_managed_opaque (Vopaque_ptr_free_list,
|
|
246 (CONST void *) &val);
|
|
247 }
|
|
248
|
|
249 /* Be wery wery careful with this. Same admonitions as with
|
|
250 free_cons() apply. */
|
|
251
|
|
252 void
|
|
253 free_opaque_ptr (Lisp_Object ptr)
|
|
254 {
|
|
255 free_managed_opaque (Vopaque_ptr_free_list, ptr);
|
|
256 }
|
|
257
|
|
258 Lisp_Object
|
|
259 make_opaque_long (long val)
|
|
260 {
|
|
261 return make_opaque (sizeof (val), (void *) &val);
|
|
262 }
|
|
263
|
|
264 void
|
|
265 init_opaque_once_early (void)
|
|
266 {
|
|
267 Vopaque_ptr_free_list = make_opaque_list (sizeof (void *), 0);
|
|
268 staticpro (&Vopaque_ptr_free_list);
|
|
269 }
|