0
|
1 /* The "lrecord" structure (header of a compound lisp object).
|
|
2 Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
|
|
3 Copyright (C) 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 #ifndef _XEMACS_LRECORD_H_
|
|
25 #define _XEMACS_LRECORD_H_
|
|
26
|
|
27 /* The "lrecord" type of Lisp object is used for all object types
|
|
28 other than a few simple ones. This allows many types to be
|
|
29 implemented but only a few bits required in a Lisp object for
|
|
30 type information. (The tradeoff is that each object has its
|
|
31 type marked in it, thereby increasing its size.) The first
|
|
32 four bytes of all lrecords is a pointer to a struct
|
|
33 lrecord_implementation, which contains methods describing
|
|
34 how to process this object.
|
|
35
|
|
36 lrecords are of two types: straight lrecords, and lcrecords.
|
|
37 Straight lrecords are used for those types of objects that
|
|
38 have their own allocation routines (typically allocated out
|
|
39 of 2K chunks of memory). These objects have a
|
|
40 `struct lrecord_header' at the top, containing only the
|
|
41 implementation pointer. There are special routines in alloc.c
|
|
42 to deal with each such object type.
|
|
43
|
|
44 Lcrecords are used for less common sorts of objects that don't
|
|
45 do their own allocation. Each such object is malloc()ed
|
|
46 individually, and the objects are chained together through
|
|
47 a `next' pointer. Lcrecords have a `struct lcrecord_header'
|
|
48 at the top, which contains an implementation pointer and
|
|
49 a `next' pointer, and are allocated using alloc_lcrecord().
|
|
50
|
|
51 Creating a new lcrecord type is fairly easy; just follow the
|
|
52 lead of some existing type (e.g. hashtables). Note that you
|
|
53 do not need to supply all the methods (see below); reasonable
|
|
54 defaults are provided for many of them. Alternatively, if you're
|
|
55 just looking for a way of encapsulating data (which possibly
|
|
56 could contain Lisp_Objects in it), you may well be able to use
|
|
57 the opaque type. */
|
|
58
|
|
59 struct lrecord_header
|
2
|
60 {
|
|
61 /* It would be better to put the mark-bit together with the
|
|
62 following datatype identification field in an 8- or 16-bit
|
|
63 integer rather than playing funny games with changing
|
|
64 header->implementation and "wasting" 32 bits on the below
|
|
65 pointer. The type-id would then be a 7 or 15 bit index into a
|
|
66 table of lrecord-implementations rather than a direct pointer.
|
|
67 There would be 24 (or 16) bits left over for datatype-specific
|
|
68 per-instance flags.
|
|
69
|
|
70 The below is the simplest thing to do for the present,
|
|
71 and doesn't incur that much overhead as most Emacs records
|
|
72 are of such a size that the overhead isn't too bad.
|
|
73 (The marker datatype is the worst case.)
|
|
74
|
|
75 It also has the very very very slight advantage that type-checking
|
|
76 involves one memory read (of the "implementation" slot) and a
|
|
77 comparison against a link-time constant address rather than a
|
|
78 read and a comparison against a variable value. (Variable since
|
|
79 it is a very good idea to assign the indices into the hypothetical
|
|
80 type-code table dynamically rather that pre-defining them.)
|
|
81 I think I remember that Elk Lisp does something like this.
|
|
82 Gee, I wonder if some cretin has patented it? */
|
|
83 CONST struct lrecord_implementation *implementation;
|
|
84 };
|
0
|
85 #define set_lheader_implementation(header,imp) (header)->implementation=(imp)
|
|
86
|
|
87 struct lcrecord_header
|
2
|
88 {
|
|
89 struct lrecord_header lheader;
|
|
90 /* The "next" field is normally used to chain all lrecords together
|
|
91 so that the GC can find (and free) all of them.
|
|
92 "alloc_lcrecord" threads records together.
|
|
93
|
|
94 The "next" field may be used for other purposes as long as some
|
|
95 other mechanism is provided for letting the GC do its work. (For
|
|
96 example, the event and marker datatypes allocate members out of
|
|
97 memory chunks, and are able to find all unmarked members by
|
|
98 sweeping through the elements of the list of chunks) */
|
|
99 struct lcrecord_header *next;
|
|
100 /* This is just for debugging/printing convenience.
|
|
101 Having this slot doesn't hurt us much spacewise, since an lcrecord
|
|
102 already has the above slots together with malloc overhead. */
|
|
103 unsigned int uid :31;
|
|
104 /* A flag that indicates whether this lcrecord is on a "free list".
|
|
105 Free lists are used to minimize the number of calls to malloc()
|
|
106 when we're repeatedly allocating and freeing a number of the
|
|
107 same sort of lcrecord. Lcrecords on a free list always get
|
|
108 marked in a different fashion, so we can use this flag as a
|
|
109 sanity check to make sure that free lists only have freed lcrecords
|
|
110 and there are no freed lcrecords elsewhere. */
|
|
111 unsigned int free :1;
|
|
112 };
|
0
|
113
|
|
114 /* Used for lcrecords in an lcrecord-list. */
|
|
115 struct free_lcrecord_header
|
2
|
116 {
|
|
117 struct lcrecord_header lcheader;
|
|
118 Lisp_Object chain;
|
|
119 };
|
0
|
120
|
|
121 /* This as the value of lheader->implementation->finalizer
|
|
122 * means that this record is already marked */
|
|
123 extern void this_marks_a_marked_record (void *, int);
|
|
124
|
|
125 /* see alloc.c for an explanation */
|
|
126 extern Lisp_Object this_one_is_unmarkable (Lisp_Object obj,
|
|
127 void (*markobj) (Lisp_Object));
|
|
128
|
|
129 struct lrecord_implementation
|
2
|
130 {
|
|
131 CONST char *name;
|
|
132 /* This function is called at GC time, to make sure that all Lisp_Objects
|
|
133 pointed to by this object get properly marked. It should call
|
|
134 the mark_object function on all Lisp_Objects in the object. If
|
|
135 the return value is non-nil, it should be a Lisp_Object to be
|
|
136 marked (don't call the mark_object function explicitly on it,
|
|
137 because the GC routines will do this). Doing it this way reduces
|
|
138 recursion, so the object returned should preferably be the one
|
|
139 with the deepest level of Lisp_Object pointers. This function
|
|
140 can be NULL, meaning no GC marking is necessary. */
|
|
141 Lisp_Object (*marker) (Lisp_Object, void (*mark_object) (Lisp_Object));
|
|
142 /* This can be NULL if the object is an lcrecord; the
|
|
143 default_object_printer() in print.c will be used. */
|
|
144 void (*printer) (Lisp_Object, Lisp_Object printcharfun, int escapeflag);
|
|
145 /* This function is called at GC time when the object is about to
|
|
146 be freed, and at dump time (FOR_DISKSAVE will be non-zero in this
|
|
147 case). It should perform any necessary cleanup (e.g. freeing
|
|
148 malloc()ed memory. This can be NULL, meaning no special
|
|
149 finalization is necessary.
|
0
|
150
|
2
|
151 WARNING: remember that the finalizer is called at dump time even
|
|
152 though the object is not being freed. */
|
|
153 void (*finalizer) (void *header, int for_disksave);
|
|
154 /* This can be NULL, meaning compare objects with EQ(). */
|
|
155 int (*equal) (Lisp_Object obj1, Lisp_Object obj2, int depth);
|
|
156 /* This can be NULL, meaning use the Lisp_Object itself as the hash;
|
|
157 but *only* if the `equal' function is EQ (if two objects are
|
|
158 `equal', they *must* hash to the same value or the hashing won't
|
|
159 work). */
|
|
160 unsigned long (*hash) (Lisp_Object, int);
|
|
161 Lisp_Object (*getprop) (Lisp_Object obj, Lisp_Object prop);
|
|
162 int (*putprop) (Lisp_Object obj, Lisp_Object prop, Lisp_Object val);
|
|
163 int (*remprop) (Lisp_Object obj, Lisp_Object prop);
|
|
164 Lisp_Object (*plist) (Lisp_Object obj);
|
0
|
165
|
2
|
166 /* Only one of these is non-0. If both are 0, it means that this type
|
|
167 is not instantiable by alloc_lcrecord(). */
|
|
168 unsigned int static_size;
|
|
169 unsigned int (*size_in_bytes_method) (CONST void *header);
|
|
170 /* A unique subtag-code (dynamically) assigned to this datatype. */
|
|
171 /* (This is a pointer so the rest of this structure can be read-only.) */
|
|
172 int *lrecord_type_index;
|
|
173 /* A "basic" lrecord is any lrecord that's not an lcrecord, i.e.
|
|
174 one that does not have an lcrecord_header at the front and which
|
|
175 is (usually) allocated in frob blocks. We only use this flag for
|
|
176 some consistency checking, and that only when error-checking is
|
|
177 enabled. */
|
|
178 int basic_p;
|
|
179 };
|
0
|
180
|
|
181 extern int gc_in_progress;
|
|
182
|
|
183 #define MARKED_RECORD_P(obj) (gc_in_progress && \
|
|
184 XRECORD_LHEADER (obj)->implementation->finalizer == \
|
|
185 this_marks_a_marked_record)
|
|
186
|
|
187 /* Declaring the following structures as const puts them in the
|
|
188 text (read-only) segment, which makes debugging inconvenient
|
|
189 because this segment is not mapped when processing a core-
|
|
190 dump file */
|
|
191
|
|
192 #ifdef DEBUG_XEMACS
|
|
193 #define CONST_IF_NOT_DEBUG
|
|
194 #else
|
|
195 #define CONST_IF_NOT_DEBUG CONST
|
|
196 #endif
|
|
197
|
|
198 /* DEFINE_LRECORD_IMPLEMENTATION is for objects with constant size.
|
|
199 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION is for objects whose size varies.
|
|
200 */
|
|
201
|
|
202 #if defined (ERROR_CHECK_TYPECHECK)
|
|
203 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
|
|
204 #else
|
|
205 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
|
|
206 #endif
|
|
207
|
|
208 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,structtype) \
|
|
209 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
|
|
210 static int lrecord_##c_name##_lrecord_type_index; \
|
|
211 CONST_IF_NOT_DEBUG struct lrecord_implementation lrecord_##c_name[2] = \
|
|
212 { { name, marker, printer, nuker, equal, hash, \
|
|
213 0, 0, 0, 0, sizeof (structtype), 0, \
|
|
214 &(lrecord_##c_name##_lrecord_type_index), 1 }, \
|
|
215 { 0, 0, 0, this_marks_a_marked_record, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } }
|
|
216
|
|
217 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,getprop,putprop,remprop,props,structtype) \
|
|
218 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
|
|
219 static int lrecord_##c_name##_lrecord_type_index; \
|
|
220 CONST_IF_NOT_DEBUG struct lrecord_implementation lrecord_##c_name[2] = \
|
|
221 { { name, marker, printer, nuker, equal, hash, \
|
|
222 getprop, putprop, remprop, props, sizeof (structtype), 0, \
|
|
223 &(lrecord_##c_name##_lrecord_type_index), 1 }, \
|
|
224 { 0, 0, 0, this_marks_a_marked_record, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 } }
|
|
225
|
|
226 #define DEFINE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,structtype) \
|
|
227 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
|
|
228 static int lrecord_##c_name##_lrecord_type_index; \
|
|
229 CONST_IF_NOT_DEBUG struct lrecord_implementation lrecord_##c_name[2] = \
|
|
230 { { name, marker, printer, nuker, equal, hash, \
|
|
231 0, 0, 0, 0, sizeof (structtype), 0, \
|
|
232 &(lrecord_##c_name##_lrecord_type_index), 0 }, \
|
|
233 { 0, 0, 0, this_marks_a_marked_record, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
|
|
234
|
|
235 #define DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,getprop,putprop,remprop,props,structtype) \
|
|
236 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
|
|
237 static int lrecord_##c_name##_lrecord_type_index; \
|
|
238 CONST_IF_NOT_DEBUG struct lrecord_implementation lrecord_##c_name[2] = \
|
|
239 { { name, marker, printer, nuker, equal, hash, \
|
|
240 getprop, putprop, remprop, props, sizeof (structtype), 0, \
|
|
241 &(lrecord_##c_name##_lrecord_type_index), 0 }, \
|
|
242 { 0, 0, 0, this_marks_a_marked_record, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
|
|
243
|
|
244 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,sizer,structtype) \
|
|
245 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
|
|
246 static int lrecord_##c_name##_lrecord_type_index; \
|
|
247 CONST_IF_NOT_DEBUG struct lrecord_implementation lrecord_##c_name[2] = \
|
|
248 { { name, marker, printer, nuker, equal, hash, \
|
|
249 0, 0, 0, 0, 0, sizer, \
|
|
250 &(lrecord_##c_name##_lrecord_type_index), 0 }, \
|
|
251 { 0, 0, 0, this_marks_a_marked_record, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
|
|
252
|
|
253 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,getprop,putprop,remprop,props,sizer,structtype) \
|
|
254 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
|
|
255 static int lrecord_##c_name##_lrecord_type_index; \
|
|
256 CONST_IF_NOT_DEBUG struct lrecord_implementation lrecord_##c_name[2] = \
|
|
257 { { name, marker, printer, nuker, equal, hash, \
|
|
258 getprop, putprop, remprop, props, 0, sizer, \
|
|
259 &(lrecord_##c_name##_lrecord_type_index), 0 }, \
|
|
260 { 0, 0, 0, this_marks_a_marked_record, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
|
|
261
|
|
262 #define LRECORDP(a) (XTYPE ((a)) == Lisp_Record)
|
|
263 #define XRECORD_LHEADER(a) ((struct lrecord_header *) XPNTR (a))
|
|
264 #define RECORD_TYPEP(x, ty) \
|
|
265 (LRECORDP (x) && XRECORD_LHEADER (x)->implementation == (ty))
|
|
266
|
|
267 /* NOTE: the DECLARE_LRECORD() must come before the associated
|
|
268 DEFINE_LRECORD_*() or you will get compile errors.
|
|
269
|
|
270 Furthermore, you always need to put the DECLARE_LRECORD() in a header
|
|
271 file, and make sure the header file is included in inline.c, even
|
|
272 if the type is private to a particular file. Otherwise, you will
|
|
273 get undefined references for the error_check_foo() inline function
|
|
274 under GCC. */
|
|
275
|
|
276 #ifdef ERROR_CHECK_TYPECHECK
|
|
277
|
2
|
278 # define DECLARE_LRECORD(c_name, structtype) \
|
|
279 extern CONST_IF_NOT_DEBUG struct lrecord_implementation \
|
|
280 lrecord_##c_name[]; \
|
|
281 INLINE structtype *error_check_##c_name (Lisp_Object _obj); \
|
|
282 INLINE structtype * \
|
|
283 error_check_##c_name (Lisp_Object _obj) \
|
|
284 { \
|
|
285 XUNMARK (_obj); \
|
|
286 assert (RECORD_TYPEP (_obj, lrecord_##c_name) || \
|
|
287 MARKED_RECORD_P (_obj)); \
|
|
288 return (structtype *) XPNTR (_obj); \
|
|
289 } \
|
0
|
290 extern Lisp_Object Q##c_name##p
|
|
291
|
2
|
292 # define DECLARE_NONRECORD(c_name, type_enum, structtype) \
|
|
293 INLINE structtype *error_check_##c_name (Lisp_Object _obj); \
|
|
294 INLINE structtype * \
|
|
295 error_check_##c_name (Lisp_Object _obj) \
|
|
296 { \
|
|
297 XUNMARK (_obj); \
|
|
298 assert (XGCTYPE (_obj) == type_enum); \
|
|
299 return (structtype *) XPNTR (_obj); \
|
|
300 } \
|
0
|
301 extern Lisp_Object Q##c_name##p
|
|
302
|
|
303 # define XRECORD(x, c_name, structtype) error_check_##c_name (x)
|
|
304 # define XNONRECORD(x, c_name, type_enum, structtype) error_check_##c_name (x)
|
|
305
|
2
|
306 # define XSETRECORD(var, p, c_name) do \
|
|
307 { \
|
|
308 XSETOBJ (var, Lisp_Record, p); \
|
|
309 assert (RECORD_TYPEP (var, lrecord_##c_name) || \
|
|
310 MARKED_RECORD_P (var)); \
|
0
|
311 } while (0)
|
|
312
|
|
313 #else /* not ERROR_CHECK_TYPECHECK */
|
|
314
|
2
|
315 # define DECLARE_LRECORD(c_name, structtype) \
|
|
316 extern Lisp_Object Q##c_name##p; \
|
|
317 extern CONST_IF_NOT_DEBUG struct lrecord_implementation \
|
0
|
318 lrecord_##c_name[]
|
2
|
319 # define DECLARE_NONRECORD(c_name, type_enum, structtype) \
|
0
|
320 extern Lisp_Object Q##c_name##p
|
|
321 # define XRECORD(x, c_name, structtype) ((structtype *) XPNTR (x))
|
2
|
322 # define XNONRECORD(x, c_name, type_enum, structtype) \
|
0
|
323 ((structtype *) XPNTR (x))
|
|
324 # define XSETRECORD(var, p, c_name) XSETOBJ (var, Lisp_Record, p)
|
|
325
|
|
326 #endif /* not ERROR_CHECK_TYPECHECK */
|
|
327
|
|
328 #define RECORDP(x, c_name) RECORD_TYPEP (x, lrecord_##c_name)
|
|
329 #define GC_RECORDP(x, c_name) gc_record_type_p (x, lrecord_##c_name)
|
|
330
|
|
331 /* Note: we now have two different kinds of type-checking macros.
|
|
332 The "old" kind has now been renamed CONCHECK_foo. The reason for
|
|
333 this is that the CONCHECK_foo macros signal a continuable error,
|
|
334 allowing the user (through debug-on-error) to subsitute a different
|
|
335 value and return from the signal, which causes the lvalue argument
|
|
336 to get changed. Quite a lot of code would crash if that happened,
|
|
337 because it did things like
|
|
338
|
|
339 foo = XCAR (list);
|
|
340 CHECK_STRING (foo);
|
|
341
|
|
342 and later on did XSTRING (XCAR (list)), assuming that the type
|
|
343 is correct (when it might be wrong, if the user substituted a
|
|
344 correct value in the debugger).
|
|
345
|
|
346 To get around this, I made all the CHECK_foo macros signal a
|
|
347 non-continuable error. Places where a continuable error is OK
|
|
348 (generally only when called directly on the argument of a Lisp
|
|
349 primitive) should be changed to use CONCHECK().
|
|
350
|
|
351 FSF Emacs does not have this problem because RMS took the cheesy
|
|
352 way out and disabled returning from a signal entirely. */
|
|
353
|
2
|
354 #define CONCHECK_RECORD(x, c_name) do \
|
|
355 { if (!RECORD_TYPEP (x, lrecord_##c_name)) \
|
|
356 x = wrong_type_argument (Q##c_name##p, x); } \
|
0
|
357 while (0)
|
2
|
358 #define CONCHECK_NONRECORD(x, lisp_enum, predicate) do \
|
|
359 { if (XTYPE (x) != lisp_enum) \
|
|
360 x = wrong_type_argument (predicate, x); } \
|
0
|
361 while (0)
|
2
|
362 #define CHECK_RECORD(x, c_name) do \
|
|
363 { if (!RECORD_TYPEP (x, lrecord_##c_name)) \
|
|
364 dead_wrong_type_argument (Q##c_name##p, x); } \
|
0
|
365 while (0)
|
2
|
366 #define CHECK_NONRECORD(x, lisp_enum, predicate) do \
|
|
367 { if (XTYPE (x) != lisp_enum) \
|
|
368 dead_wrong_type_argument (predicate, x); } \
|
0
|
369 while (0)
|
|
370
|
|
371 void *alloc_lcrecord (int size, CONST struct lrecord_implementation *);
|
|
372
|
|
373 int gc_record_type_p (Lisp_Object frob,
|
|
374 CONST struct lrecord_implementation *type);
|
|
375
|
|
376 /* Copy the data from one lcrecord structure into another, but don't
|
|
377 overwrite the header information. */
|
|
378
|
2
|
379 #define copy_lcrecord(dst, src) \
|
|
380 memcpy ((char *) dst + sizeof (struct lcrecord_header), \
|
|
381 (char *) src + sizeof (struct lcrecord_header), \
|
0
|
382 sizeof (*dst) - sizeof (struct lcrecord_header))
|
|
383
|
2
|
384 #define zero_lcrecord(lcr) \
|
|
385 memset ((char *) lcr + sizeof (struct lcrecord_header), 0, \
|
0
|
386 sizeof (*lcr) - sizeof (struct lcrecord_header))
|
|
387
|
|
388 #endif /* _XEMACS_LRECORD_H_ */
|