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[] */
65 /* If `mark' is 0 after the GC mark phase, the object will be freed
66 during the GC sweep phase. There are 2 ways that `mark' can be 1:
67 - by being referenced from other objects during the GC mark phase
68 - because it is permanently on, for c_readonly objects */
71 /* 1 if the object resides in logically read-only space, and does not
72 reference other non-c_readonly objects.
73 Invariant: if (c_readonly == 1), then (mark == 1 && lisp_readonly == 1) */
74 unsigned int c_readonly :1;
76 /* 1 if the object is readonly from lisp */
77 unsigned int lisp_readonly :1;
80 /* The `older field is a flag that indicates whether this lcrecord
81 is on a "older storage". */
82 unsigned int older :1;
86 struct lrecord_implementation;
87 int lrecord_type_index (const struct lrecord_implementation *implementation);
90 #define set_lheader_implementation(header,imp) do { \
91 struct lrecord_header* SLI_header = (header); \
92 SLI_header->type = (imp)->lrecord_type_index; \
93 SLI_header->mark = 0; \
94 SLI_header->older = 0; \
95 SLI_header->c_readonly = 0; \
96 SLI_header->lisp_readonly = 0; \
98 #define set_lheader_older_implementation(header,imp) do { \
99 struct lrecord_header* SLI_header = (header); \
100 SLI_header->type = (imp)->lrecord_type_index; \
101 SLI_header->mark = 0; \
102 SLI_header->older = 1; \
103 SLI_header->c_readonly = 0; \
104 SLI_header->lisp_readonly = 0; \
107 #define set_lheader_implementation(header,imp) do { \
108 struct lrecord_header* SLI_header = (header); \
109 SLI_header->type = (imp)->lrecord_type_index; \
110 SLI_header->mark = 0; \
111 SLI_header->c_readonly = 0; \
112 SLI_header->lisp_readonly = 0; \
116 struct lcrecord_header
118 struct lrecord_header lheader;
120 /* The `next' field is normally used to chain all lcrecords together
121 so that the GC can find (and free) all of them.
122 `alloc_lcrecord' threads lcrecords together.
124 The `next' field may be used for other purposes as long as some
125 other mechanism is provided for letting the GC do its work.
127 For example, the event and marker object types allocate members
128 out of memory chunks, and are able to find all unmarked members
129 by sweeping through the elements of the list of chunks. */
130 struct lcrecord_header *next;
132 /* The `uid' field is just for debugging/printing convenience.
133 Having this slot doesn't hurt us much spacewise, since an
134 lcrecord already has the above slots plus malloc overhead. */
135 unsigned int uid :31;
137 /* The `free' field is a flag that indicates whether this lcrecord
138 is on a "free list". Free lists are used to minimize the number
139 of calls to malloc() when we're repeatedly allocating and freeing
140 a number of the same sort of lcrecord. Lcrecords on a free list
141 always get marked in a different fashion, so we can use this flag
142 as a sanity check to make sure that free lists only have freed
143 lcrecords and there are no freed lcrecords elsewhere. */
144 unsigned int free :1;
147 /* Used for lcrecords in an lcrecord-list. */
148 struct free_lcrecord_header
150 struct lcrecord_header lcheader;
156 /* Symbol value magic types come first to make SYMBOL_VALUE_MAGIC_P fast.
157 #### This should be replaced by a symbol_value_magic_p flag
158 in the Lisp_Symbol lrecord_header. */
159 lrecord_type_symbol_value_forward,
160 lrecord_type_symbol_value_varalias,
161 lrecord_type_symbol_value_lisp_magic,
162 lrecord_type_symbol_value_buffer_local,
163 lrecord_type_max_symbol_value_magic = lrecord_type_symbol_value_buffer_local,
170 lrecord_type_lcrecord_list,
171 lrecord_type_compiled_function,
172 lrecord_type_weak_list,
173 lrecord_type_bit_vector,
175 lrecord_type_hash_table,
176 lrecord_type_lstream,
177 lrecord_type_process,
178 lrecord_type_charset,
179 lrecord_type_coding_system,
180 lrecord_type_char_table,
181 lrecord_type_char_table_entry,
182 lrecord_type_char_id_table,
183 lrecord_type_byte_table,
184 lrecord_type_range_table,
186 lrecord_type_opaque_ptr,
189 lrecord_type_extent_info,
190 lrecord_type_extent_auxiliary,
194 lrecord_type_command_builder,
195 lrecord_type_timeout,
196 lrecord_type_specifier,
197 lrecord_type_console,
201 lrecord_type_window_configuration,
202 lrecord_type_gui_item,
203 lrecord_type_popup_data,
204 lrecord_type_toolbar_button,
205 lrecord_type_color_instance,
206 lrecord_type_font_instance,
207 lrecord_type_image_instance,
210 lrecord_type_database,
211 lrecord_type_tooltalk_message,
212 lrecord_type_tooltalk_pattern,
215 lrecord_type_pgresult,
216 lrecord_type_count /* must be last */
219 struct lrecord_implementation
223 /* `marker' is called at GC time, to make sure that all Lisp_Objects
224 pointed to by this object get properly marked. It should call
225 the mark_object function on all Lisp_Objects in the object. If
226 the return value is non-nil, it should be a Lisp_Object to be
227 marked (don't call the mark_object function explicitly on it,
228 because the GC routines will do this). Doing it this way reduces
229 recursion, so the object returned should preferably be the one
230 with the deepest level of Lisp_Object pointers. This function
231 can be NULL, meaning no GC marking is necessary. */
232 Lisp_Object (*marker) (Lisp_Object);
234 /* `printer' converts the object to a printed representation.
235 This can be NULL; in this case default_object_printer() will be
237 void (*printer) (Lisp_Object, Lisp_Object printcharfun, int escapeflag);
239 /* `finalizer' is called at GC time when the object is about to
240 be freed, and at dump time (FOR_DISKSAVE will be non-zero in this
241 case). It should perform any necessary cleanup (e.g. freeing
242 malloc()ed memory). This can be NULL, meaning no special
243 finalization is necessary.
245 WARNING: remember that `finalizer' is called at dump time even
246 though the object is not being freed. */
247 void (*finalizer) (void *header, int for_disksave);
249 /* This can be NULL, meaning compare objects with EQ(). */
250 int (*equal) (Lisp_Object obj1, Lisp_Object obj2, int depth);
252 /* `hash' generates hash values for use with hash tables that have
253 `equal' as their test function. This can be NULL, meaning use
254 the Lisp_Object itself as the hash. But, you must still satisfy
255 the constraint that if two objects are `equal', then they *must*
256 hash to the same value in order for hash tables to work properly.
257 This means that `hash' can be NULL only if the `equal' method is
259 unsigned long (*hash) (Lisp_Object, int);
261 /* External data layout description */
262 const struct lrecord_description *description;
264 /* These functions allow any object type to have builtin property
265 lists that can be manipulated from the lisp level with
266 `get', `put', `remprop', and `object-plist'. */
267 Lisp_Object (*getprop) (Lisp_Object obj, Lisp_Object prop);
268 int (*putprop) (Lisp_Object obj, Lisp_Object prop, Lisp_Object val);
269 int (*remprop) (Lisp_Object obj, Lisp_Object prop);
270 Lisp_Object (*plist) (Lisp_Object obj);
272 /* Only one of `static_size' and `size_in_bytes_method' is non-0.
273 If both are 0, this type is not instantiable by alloc_lcrecord(). */
275 size_t (*size_in_bytes_method) (const void *header);
277 /* The (constant) index into lrecord_implementations_table */
278 enum lrecord_type lrecord_type_index;
280 /* A "basic" lrecord is any lrecord that's not an lcrecord, i.e.
281 one that does not have an lcrecord_header at the front and which
282 is (usually) allocated in frob blocks. We only use this flag for
283 some consistency checking, and that only when error-checking is
285 unsigned int basic_p :1;
288 extern const struct lrecord_implementation *lrecord_implementations_table[];
290 #define XRECORD_LHEADER_IMPLEMENTATION(obj) \
291 LHEADER_IMPLEMENTATION (XRECORD_LHEADER (obj))
292 #define LHEADER_IMPLEMENTATION(lh) lrecord_implementations_table[(lh)->type]
294 extern int gc_in_progress;
296 #define MARKED_RECORD_P(obj) (XRECORD_LHEADER (obj)->mark)
297 #define MARKED_RECORD_HEADER_P(lheader) ((lheader)->mark)
298 #define MARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 1))
299 #define UNMARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 0))
301 #define OLDER_RECORD_P(obj) (XRECORD_LHEADER (obj)->older)
302 #define OLDER_RECORD_HEADER_P(lheader) ((lheader)->older)
305 #define C_READONLY_RECORD_HEADER_P(lheader) ((lheader)->c_readonly)
306 #define LISP_READONLY_RECORD_HEADER_P(lheader) ((lheader)->lisp_readonly)
307 #define SET_C_READONLY_RECORD_HEADER(lheader) do { \
308 struct lrecord_header *SCRRH_lheader = (lheader); \
309 SCRRH_lheader->c_readonly = 1; \
310 SCRRH_lheader->lisp_readonly = 1; \
311 SCRRH_lheader->mark = 1; \
313 #define SET_LISP_READONLY_RECORD_HEADER(lheader) \
314 ((void) ((lheader)->lisp_readonly = 1))
315 #define RECORD_MARKER(lheader) lrecord_markers[(lheader)->type]
317 /* External description stuff
319 A lrecord external description is an array of values. The first
320 value of each line is a type, the second the offset in the lrecord
321 structure. Following values are parameters, their presence, type
322 and number is type-dependant.
324 The description ends with a "XD_END" or "XD_SPECIFIER_END" record.
326 Some example descriptions :
328 static const struct lrecord_description cons_description[] = {
329 { XD_LISP_OBJECT, offsetof (Lisp_Cons, car) },
330 { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr) },
334 Which means "two lisp objects starting at the 'car' and 'cdr' elements"
336 static const struct lrecord_description string_description[] = {
337 { XD_BYTECOUNT, offsetof (Lisp_String, size) },
338 { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
339 { XD_LISP_OBJECT, offsetof (Lisp_String, plist) },
342 "A pointer to string data at 'data', the size of the pointed array being the value
343 of the size variable plus 1, and one lisp object at 'plist'"
347 A Lisp object. This is also the type to use for pointers to other lrecords.
350 An array of Lisp objects or pointers to lrecords.
351 The third element is the count.
354 Lisp objects which will be reset to Qnil when dumping. Useful for cleaning
358 Link in a linked list of objects of the same type.
361 Pointer to undumpable data. Must be NULL when dumping.
364 Pointer to described struct. Parameters are number of structures and
368 Pointer to dumpable opaque data. Parameter is the size of the data.
369 Pointed data must be relocatable without changes.
372 Pointer to a C string.
375 Pointer to a doc string (C string if positive, opaque value if negative)
378 An integer which will be reset to a given value in the dump file.
382 size_t value. Used for counts.
385 int value. Used for counts.
388 long value. Used for counts.
391 bytecount value. Used for counts.
394 Special type indicating the end of the array.
397 Special type indicating the end of the array for a specifier. Extra
398 description is going to be fetched from the specifier methods.
402 XD_INDIRECT(line, delta)
403 Usable where a "count" or "size" is requested. Gives the value of
404 the element which is at line number 'line' in the description (count
405 starts at zero) and adds delta to it.
408 enum lrecord_description_type {
409 XD_LISP_OBJECT_ARRAY,
427 struct lrecord_description {
428 enum lrecord_description_type type;
431 const struct struct_description *data2;
434 struct struct_description {
436 const struct lrecord_description *description;
439 #define XD_INDIRECT(val, delta) (-1-((val)|(delta<<8)))
441 #define XD_IS_INDIRECT(code) (code<0)
442 #define XD_INDIRECT_VAL(code) ((-1-code) & 255)
443 #define XD_INDIRECT_DELTA(code) (((-1-code)>>8) & 255)
445 #define XD_DYNARR_DESC(base_type, sub_desc) \
446 { XD_STRUCT_PTR, offsetof (base_type, base), XD_INDIRECT(1, 0), sub_desc }, \
447 { XD_INT, offsetof (base_type, cur) }, \
448 { XD_INT_RESET, offsetof (base_type, max), XD_INDIRECT(1, 0) }
450 /* DEFINE_LRECORD_IMPLEMENTATION is for objects with constant size.
451 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION is for objects whose size varies.
454 #if defined (ERROR_CHECK_TYPECHECK)
455 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
457 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
460 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
461 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
463 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
464 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof(structtype),0,1,structtype)
466 #define DEFINE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
467 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
469 #define DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
470 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
472 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
473 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
475 #define DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
476 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,0,sizer,1,structtype)
478 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
479 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype) \
481 #define MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
482 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
483 const struct lrecord_implementation lrecord_##c_name = \
484 { name, marker, printer, nuker, equal, hash, desc, \
485 getprop, putprop, remprop, plist, size, sizer, \
486 lrecord_type_##c_name, basic_p }
488 extern Lisp_Object (*lrecord_markers[]) (Lisp_Object);
490 #define INIT_LRECORD_IMPLEMENTATION(type) do { \
491 lrecord_implementations_table[lrecord_type_##type] = &lrecord_##type; \
492 lrecord_markers[lrecord_type_##type] = \
493 lrecord_implementations_table[lrecord_type_##type]->marker; \
496 #define LRECORDP(a) (XTYPE (a) == Lisp_Type_Record)
497 #define XRECORD_LHEADER(a) ((struct lrecord_header *) XPNTR (a))
499 #define RECORD_TYPEP(x, ty) \
500 (LRECORDP (x) && XRECORD_LHEADER (x)->type == (ty))
502 /* NOTE: the DECLARE_LRECORD() must come before the associated
503 DEFINE_LRECORD_*() or you will get compile errors.
505 Furthermore, you always need to put the DECLARE_LRECORD() in a header
506 file, and make sure the header file is included in inline.c, even
507 if the type is private to a particular file. Otherwise, you will
508 get undefined references for the error_check_foo() inline function
511 #ifdef ERROR_CHECK_TYPECHECK
513 # define DECLARE_LRECORD(c_name, structtype) \
514 extern const struct lrecord_implementation lrecord_##c_name; \
515 INLINE_HEADER structtype * \
516 error_check_##c_name (Lisp_Object obj); \
517 INLINE_HEADER structtype * \
518 error_check_##c_name (Lisp_Object obj) \
520 assert (RECORD_TYPEP (obj, lrecord_type_##c_name)); \
521 return (structtype *) XPNTR (obj); \
523 extern Lisp_Object Q##c_name##p
525 # define DECLARE_NONRECORD(c_name, type_enum, structtype) \
526 INLINE_HEADER structtype * \
527 error_check_##c_name (Lisp_Object obj); \
528 INLINE_HEADER structtype * \
529 error_check_##c_name (Lisp_Object obj) \
531 assert (XTYPE (obj) == type_enum); \
532 return (structtype *) XPNTR (obj); \
534 extern Lisp_Object Q##c_name##p
536 # define XRECORD(x, c_name, structtype) error_check_##c_name (x)
537 # define XNONRECORD(x, c_name, type_enum, structtype) error_check_##c_name (x)
539 # define XSETRECORD(var, p, c_name) do \
541 XSETOBJ (var, Lisp_Type_Record, p); \
542 assert (RECORD_TYPEP (var, lrecord_type_##c_name)); \
545 #else /* not ERROR_CHECK_TYPECHECK */
547 # define DECLARE_LRECORD(c_name, structtype) \
548 extern Lisp_Object Q##c_name##p; \
549 extern const struct lrecord_implementation lrecord_##c_name
550 # define DECLARE_NONRECORD(c_name, type_enum, structtype) \
551 extern Lisp_Object Q##c_name##p
552 # define XRECORD(x, c_name, structtype) ((structtype *) XPNTR (x))
553 # define XNONRECORD(x, c_name, type_enum, structtype) \
554 ((structtype *) XPNTR (x))
555 # define XSETRECORD(var, p, c_name) XSETOBJ (var, Lisp_Type_Record, p)
557 #endif /* not ERROR_CHECK_TYPECHECK */
559 #define RECORDP(x, c_name) RECORD_TYPEP (x, lrecord_type_##c_name)
561 /* Note: we now have two different kinds of type-checking macros.
562 The "old" kind has now been renamed CONCHECK_foo. The reason for
563 this is that the CONCHECK_foo macros signal a continuable error,
564 allowing the user (through debug-on-error) to substitute a different
565 value and return from the signal, which causes the lvalue argument
566 to get changed. Quite a lot of code would crash if that happened,
567 because it did things like
572 and later on did XSTRING (XCAR (list)), assuming that the type
573 is correct (when it might be wrong, if the user substituted a
574 correct value in the debugger).
576 To get around this, I made all the CHECK_foo macros signal a
577 non-continuable error. Places where a continuable error is OK
578 (generally only when called directly on the argument of a Lisp
579 primitive) should be changed to use CONCHECK().
581 FSF Emacs does not have this problem because RMS took the cheesy
582 way out and disabled returning from a signal entirely. */
584 #define CONCHECK_RECORD(x, c_name) do { \
585 if (!RECORD_TYPEP (x, lrecord_type_##c_name)) \
586 x = wrong_type_argument (Q##c_name##p, x); \
588 #define CONCHECK_NONRECORD(x, lisp_enum, predicate) do {\
589 if (XTYPE (x) != lisp_enum) \
590 x = wrong_type_argument (predicate, x); \
592 #define CHECK_RECORD(x, c_name) do { \
593 if (!RECORD_TYPEP (x, lrecord_type_##c_name)) \
594 dead_wrong_type_argument (Q##c_name##p, x); \
596 #define CHECK_NONRECORD(x, lisp_enum, predicate) do { \
597 if (XTYPE (x) != lisp_enum) \
598 dead_wrong_type_argument (predicate, x); \
601 void *alloc_lcrecord (size_t size, const struct lrecord_implementation *);
603 #define alloc_lcrecord_type(type, lrecord_implementation) \
604 ((type *) alloc_lcrecord (sizeof (type), lrecord_implementation))
608 alloc_older_lcrecord (size_t size, const struct lrecord_implementation *);
610 #define alloc_older_lcrecord_type(type, lrecord_implementation) \
611 ((type *) alloc_older_lcrecord (sizeof (type), lrecord_implementation))
614 /* Copy the data from one lcrecord structure into another, but don't
615 overwrite the header information. */
617 #define copy_lcrecord(dst, src) \
618 memcpy ((char *) (dst) + sizeof (struct lcrecord_header), \
619 (char *) (src) + sizeof (struct lcrecord_header), \
620 sizeof (*(dst)) - sizeof (struct lcrecord_header))
622 #define zero_lcrecord(lcr) \
623 memset ((char *) (lcr) + sizeof (struct lcrecord_header), 0, \
624 sizeof (*(lcr)) - sizeof (struct lcrecord_header))
626 #endif /* INCLUDED_lrecord_h_ */