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.
5 This file is part of XEmacs.
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
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
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. */
22 /* Synched up with: Not in FSF. */
24 #ifndef INCLUDED_lrecord_h_
25 #define INCLUDED_lrecord_h_
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 type
30 information. (The tradeoff is that each object has its type marked
31 in it, thereby increasing its size.) All lrecords begin with a
32 `struct lrecord_header', which identifies the lisp object type, by
33 providing an index into a table of `struct lrecord_implementation',
34 which describes the behavior of the lisp object. It also contains
37 Lrecords are of two types: straight lrecords, and lcrecords.
38 Straight lrecords are used for those types of objects that have
39 their own allocation routines (typically allocated out of 2K chunks
40 of memory called `frob blocks'). These objects have a `struct
41 lrecord_header' at the top, containing only the bits needed to find
42 the lrecord_implementation for the object. There are special
43 routines in alloc.c to deal with each such object type.
45 Lcrecords are used for less common sorts of objects that don't do
46 their own allocation. Each such object is malloc()ed individually,
47 and the objects are chained together through a `next' pointer.
48 Lcrecords have a `struct lcrecord_header' at the top, which
49 contains a `struct lrecord_header' and a `next' pointer, and are
50 allocated using alloc_lcrecord().
52 Creating a new lcrecord type is fairly easy; just follow the
53 lead of some existing type (e.g. hash tables). Note that you
54 do not need to supply all the methods (see below); reasonable
55 defaults are provided for many of them. Alternatively, if you're
56 just looking for a way of encapsulating data (which possibly
57 could contain Lisp_Objects in it), you may well be able to use
62 /* index into lrecord_implementations_table[] */
64 /* 1 if the object is marked during GC. */
66 /* 1 if the object resides in read-only space */
67 unsigned int c_readonly :1;
68 /* 1 if the object is readonly from lisp */
69 unsigned int lisp_readonly :1;
72 struct lrecord_implementation;
73 int lrecord_type_index (const struct lrecord_implementation *implementation);
75 #define set_lheader_implementation(header,imp) do { \
76 struct lrecord_header* SLI_header = (header); \
77 SLI_header->type = lrecord_type_index (imp); \
78 SLI_header->mark = 0; \
79 SLI_header->c_readonly = 0; \
80 SLI_header->lisp_readonly = 0; \
83 struct lcrecord_header
85 struct lrecord_header lheader;
87 /* The `next' field is normally used to chain all lcrecords together
88 so that the GC can find (and free) all of them.
89 `alloc_lcrecord' threads lcrecords together.
91 The `next' field may be used for other purposes as long as some
92 other mechanism is provided for letting the GC do its work.
94 For example, the event and marker object types allocate members
95 out of memory chunks, and are able to find all unmarked members
96 by sweeping through the elements of the list of chunks. */
97 struct lcrecord_header *next;
99 /* The `uid' field is just for debugging/printing convenience.
100 Having this slot doesn't hurt us much spacewise, since an
101 lcrecord already has the above slots plus malloc overhead. */
102 unsigned int uid :31;
104 /* The `free' field is a flag that indicates whether this lcrecord
105 is on a "free list". Free lists are used to minimize the number
106 of calls to malloc() when we're repeatedly allocating and freeing
107 a number of the same sort of lcrecord. Lcrecords on a free list
108 always get marked in a different fashion, so we can use this flag
109 as a sanity check to make sure that free lists only have freed
110 lcrecords and there are no freed lcrecords elsewhere. */
111 unsigned int free :1;
114 /* Used for lcrecords in an lcrecord-list. */
115 struct free_lcrecord_header
117 struct lcrecord_header lcheader;
121 /* see alloc.c for an explanation */
122 Lisp_Object this_one_is_unmarkable (Lisp_Object obj);
124 struct lrecord_implementation
128 /* `marker' is called at GC time, to make sure that all Lisp_Objects
129 pointed to by this object get properly marked. It should call
130 the mark_object function on all Lisp_Objects in the object. If
131 the return value is non-nil, it should be a Lisp_Object to be
132 marked (don't call the mark_object function explicitly on it,
133 because the GC routines will do this). Doing it this way reduces
134 recursion, so the object returned should preferably be the one
135 with the deepest level of Lisp_Object pointers. This function
136 can be NULL, meaning no GC marking is necessary. */
137 Lisp_Object (*marker) (Lisp_Object);
139 /* `printer' converts the object to a printed representation.
140 This can be NULL; in this case default_object_printer() will be
142 void (*printer) (Lisp_Object, Lisp_Object printcharfun, int escapeflag);
144 /* `finalizer' is called at GC time when the object is about to
145 be freed, and at dump time (FOR_DISKSAVE will be non-zero in this
146 case). It should perform any necessary cleanup (e.g. freeing
147 malloc()ed memory). This can be NULL, meaning no special
148 finalization is necessary.
150 WARNING: remember that `finalizer' is called at dump time even
151 though the object is not being freed. */
152 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);
157 /* `hash' generates hash values for use with hash tables that have
158 `equal' as their test function. This can be NULL, meaning use
159 the Lisp_Object itself as the hash. But, you must still satisfy
160 the constraint that if two objects are `equal', then they *must*
161 hash to the same value in order for hash tables to work properly.
162 This means that `hash' can be NULL only if the `equal' method is
164 unsigned long (*hash) (Lisp_Object, int);
166 /* External data layout description */
167 const struct lrecord_description *description;
169 /* These functions allow any object type to have builtin property
170 lists that can be manipulated from the lisp level with
171 `get', `put', `remprop', and `object-plist'. */
172 Lisp_Object (*getprop) (Lisp_Object obj, Lisp_Object prop);
173 int (*putprop) (Lisp_Object obj, Lisp_Object prop, Lisp_Object val);
174 int (*remprop) (Lisp_Object obj, Lisp_Object prop);
175 Lisp_Object (*plist) (Lisp_Object obj);
177 /* Only one of `static_size' and `size_in_bytes_method' is non-0.
178 If both are 0, this type is not instantiable by alloc_lcrecord(). */
180 size_t (*size_in_bytes_method) (const void *header);
182 /* A unique subtag-code (dynamically) assigned to this datatype. */
183 /* (This is a pointer so the rest of this structure can be read-only.) */
184 int *lrecord_type_index;
186 /* A "basic" lrecord is any lrecord that's not an lcrecord, i.e.
187 one that does not have an lcrecord_header at the front and which
188 is (usually) allocated in frob blocks. We only use this flag for
189 some consistency checking, and that only when error-checking is
191 unsigned int basic_p :1;
194 extern const struct lrecord_implementation *lrecord_implementations_table[];
196 #define XRECORD_LHEADER_IMPLEMENTATION(obj) \
197 (lrecord_implementations_table[XRECORD_LHEADER (obj)->type])
198 #define LHEADER_IMPLEMENTATION(lh) (lrecord_implementations_table[(lh)->type])
200 extern int gc_in_progress;
202 #define MARKED_RECORD_P(obj) (gc_in_progress && XRECORD_LHEADER (obj)->mark)
203 #define MARKED_RECORD_HEADER_P(lheader) ((lheader)->mark)
204 #define MARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 1))
205 #define UNMARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 0))
207 #define UNMARKABLE_RECORD_HEADER_P(lheader) \
208 (LHEADER_IMPLEMENTATION (lheader)->marker == this_one_is_unmarkable)
210 #define C_READONLY_RECORD_HEADER_P(lheader) ((lheader)->c_readonly)
211 #define LISP_READONLY_RECORD_HEADER_P(lheader) ((lheader)->lisp_readonly)
212 #define SET_C_READONLY_RECORD_HEADER(lheader) \
213 ((void) ((lheader)->c_readonly = (lheader)->lisp_readonly = 1))
214 #define SET_LISP_READONLY_RECORD_HEADER(lheader) \
215 ((void) ((lheader)->lisp_readonly = 1))
217 /* External description stuff
219 A lrecord external description is an array of values. The first
220 value of each line is a type, the second the offset in the lrecord
221 structure. Following values are parameters, their presence, type
222 and number is type-dependant.
224 The description ends with a "XD_END" or "XD_SPECIFIER_END" record.
226 Some example descriptions :
228 static const struct lrecord_description cons_description[] = {
229 { XD_LISP_OBJECT, offsetof (Lisp_Cons, car) },
230 { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr) },
234 Which means "two lisp objects starting at the 'car' and 'cdr' elements"
236 static const struct lrecord_description string_description[] = {
237 { XD_BYTECOUNT, offsetof (Lisp_String, size) },
238 { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
239 { XD_LISP_OBJECT, offsetof (Lisp_String, plist) },
242 "A pointer to string data at 'data', the size of the pointed array being the value
243 of the size variable plus 1, and one lisp object at 'plist'"
247 A Lisp object. This is also the type to use for pointers to other lrecords.
250 An array of Lisp objects or pointers to lrecords.
251 The third element is the count.
254 Lisp objects which will be reset to Qnil when dumping. Useful for cleaning
258 Link in a linked list of objects of the same type.
261 Pointer to undumpable data. Must be NULL when dumping.
264 Pointer to described struct. Parameters are number of structures and
268 Pointer to dumpable opaque data. Parameter is the size of the data.
269 Pointed data must be relocatable without changes.
272 Pointer to a C string.
275 Pointer to a doc string (C string if positive, opaque value if negative)
278 An integer which will be reset to a given value in the dump file.
282 size_t value. Used for counts.
285 int value. Used for counts.
288 long value. Used for counts.
291 bytecount value. Used for counts.
294 Special type indicating the end of the array.
297 Special type indicating the end of the array for a specifier. Extra
298 description is going to be fetched from the specifier methods.
302 XD_INDIRECT(line, delta)
303 Usable where a "count" or "size" is requested. Gives the value of
304 the element which is at line number 'line' in the description (count
305 starts at zero) and adds delta to it.
308 enum lrecord_description_type {
309 XD_LISP_OBJECT_ARRAY,
327 struct lrecord_description {
328 enum lrecord_description_type type;
331 const struct struct_description *data2;
334 struct struct_description {
336 const struct lrecord_description *description;
339 #define XD_INDIRECT(val, delta) (-1-((val)|(delta<<8)))
341 #define XD_IS_INDIRECT(code) (code<0)
342 #define XD_INDIRECT_VAL(code) ((-1-code) & 255)
343 #define XD_INDIRECT_DELTA(code) (((-1-code)>>8) & 255)
345 #define XD_DYNARR_DESC(base_type, sub_desc) \
346 { XD_STRUCT_PTR, offsetof (base_type, base), XD_INDIRECT(1, 0), sub_desc }, \
347 { XD_INT, offsetof (base_type, cur) }, \
348 { XD_INT_RESET, offsetof (base_type, max), XD_INDIRECT(1, 0) }
350 /* Declaring the following structures as const puts them in the
351 text (read-only) segment, which makes debugging inconvenient
352 because this segment is not mapped when processing a core-
356 #define CONST_IF_NOT_DEBUG
358 #define CONST_IF_NOT_DEBUG const
361 /* DEFINE_LRECORD_IMPLEMENTATION is for objects with constant size.
362 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION is for objects whose size varies.
365 #if defined (ERROR_CHECK_TYPECHECK)
366 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
368 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
371 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
372 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
374 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
375 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof(structtype),0,1,structtype)
377 #define DEFINE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
378 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
380 #define DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
381 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
383 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
384 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
386 #define DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
387 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,0,sizer,1,structtype)
389 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
390 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype) \
392 #define MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
393 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
394 static int lrecord_##c_name##_lrecord_type_index; \
395 CONST_IF_NOT_DEBUG struct lrecord_implementation lrecord_##c_name = \
396 { name, marker, printer, nuker, equal, hash, desc, \
397 getprop, putprop, remprop, plist, size, sizer, \
398 &(lrecord_##c_name##_lrecord_type_index), basic_p } \
400 #define LRECORDP(a) (XTYPE (a) == Lisp_Type_Record)
401 #define XRECORD_LHEADER(a) ((struct lrecord_header *) XPNTR (a))
403 #define RECORD_TYPEP(x, ty) \
405 lrecord_implementations_table[XRECORD_LHEADER (x)->type] == (ty))
407 /* NOTE: the DECLARE_LRECORD() must come before the associated
408 DEFINE_LRECORD_*() or you will get compile errors.
410 Furthermore, you always need to put the DECLARE_LRECORD() in a header
411 file, and make sure the header file is included in inline.c, even
412 if the type is private to a particular file. Otherwise, you will
413 get undefined references for the error_check_foo() inline function
416 #ifdef ERROR_CHECK_TYPECHECK
418 # define DECLARE_LRECORD(c_name, structtype) \
419 extern CONST_IF_NOT_DEBUG struct lrecord_implementation \
421 INLINE structtype *error_check_##c_name (Lisp_Object obj); \
422 INLINE structtype * \
423 error_check_##c_name (Lisp_Object obj) \
425 assert (RECORD_TYPEP (obj, &lrecord_##c_name)); \
426 return (structtype *) XPNTR (obj); \
428 extern Lisp_Object Q##c_name##p
430 # define DECLARE_NONRECORD(c_name, type_enum, structtype) \
431 INLINE structtype *error_check_##c_name (Lisp_Object obj); \
432 INLINE structtype * \
433 error_check_##c_name (Lisp_Object obj) \
435 assert (XTYPE (obj) == type_enum); \
436 return (structtype *) XPNTR (obj); \
438 extern Lisp_Object Q##c_name##p
440 # define XRECORD(x, c_name, structtype) error_check_##c_name (x)
441 # define XNONRECORD(x, c_name, type_enum, structtype) error_check_##c_name (x)
443 # define XSETRECORD(var, p, c_name) do \
445 XSETOBJ (var, Lisp_Type_Record, p); \
446 assert (RECORD_TYPEP (var, &lrecord_##c_name)); \
449 #else /* not ERROR_CHECK_TYPECHECK */
451 # define DECLARE_LRECORD(c_name, structtype) \
452 extern Lisp_Object Q##c_name##p; \
453 extern CONST_IF_NOT_DEBUG struct lrecord_implementation \
455 # define DECLARE_NONRECORD(c_name, type_enum, structtype) \
456 extern Lisp_Object Q##c_name##p
457 # define XRECORD(x, c_name, structtype) ((structtype *) XPNTR (x))
458 # define XNONRECORD(x, c_name, type_enum, structtype) \
459 ((structtype *) XPNTR (x))
460 # define XSETRECORD(var, p, c_name) XSETOBJ (var, Lisp_Type_Record, p)
462 #endif /* not ERROR_CHECK_TYPECHECK */
464 #define RECORDP(x, c_name) RECORD_TYPEP (x, &lrecord_##c_name)
466 /* Note: we now have two different kinds of type-checking macros.
467 The "old" kind has now been renamed CONCHECK_foo. The reason for
468 this is that the CONCHECK_foo macros signal a continuable error,
469 allowing the user (through debug-on-error) to substitute a different
470 value and return from the signal, which causes the lvalue argument
471 to get changed. Quite a lot of code would crash if that happened,
472 because it did things like
477 and later on did XSTRING (XCAR (list)), assuming that the type
478 is correct (when it might be wrong, if the user substituted a
479 correct value in the debugger).
481 To get around this, I made all the CHECK_foo macros signal a
482 non-continuable error. Places where a continuable error is OK
483 (generally only when called directly on the argument of a Lisp
484 primitive) should be changed to use CONCHECK().
486 FSF Emacs does not have this problem because RMS took the cheesy
487 way out and disabled returning from a signal entirely. */
489 #define CONCHECK_RECORD(x, c_name) do { \
490 if (!RECORD_TYPEP (x, &lrecord_##c_name)) \
491 x = wrong_type_argument (Q##c_name##p, x); \
493 #define CONCHECK_NONRECORD(x, lisp_enum, predicate) do {\
494 if (XTYPE (x) != lisp_enum) \
495 x = wrong_type_argument (predicate, x); \
497 #define CHECK_RECORD(x, c_name) do { \
498 if (!RECORD_TYPEP (x, &lrecord_##c_name)) \
499 dead_wrong_type_argument (Q##c_name##p, x); \
501 #define CHECK_NONRECORD(x, lisp_enum, predicate) do { \
502 if (XTYPE (x) != lisp_enum) \
503 dead_wrong_type_argument (predicate, x); \
506 void *alloc_lcrecord (size_t size, const struct lrecord_implementation *);
508 #define alloc_lcrecord_type(type, lrecord_implementation) \
509 ((type *) alloc_lcrecord (sizeof (type), lrecord_implementation))
511 /* Copy the data from one lcrecord structure into another, but don't
512 overwrite the header information. */
514 #define copy_lcrecord(dst, src) \
515 memcpy ((char *) (dst) + sizeof (struct lcrecord_header), \
516 (char *) (src) + sizeof (struct lcrecord_header), \
517 sizeof (*(dst)) - sizeof (struct lcrecord_header))
519 #define zero_lcrecord(lcr) \
520 memset ((char *) (lcr) + sizeof (struct lcrecord_header), 0, \
521 sizeof (*(lcr)) - sizeof (struct lcrecord_header))
523 #endif /* INCLUDED_lrecord_h_ */