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 struct lrecord_implementation;
81 int lrecord_type_index (const struct lrecord_implementation *implementation);
83 #define set_lheader_implementation(header,imp) do { \
84 struct lrecord_header* SLI_header = (header); \
85 SLI_header->type = (imp)->lrecord_type_index; \
86 SLI_header->mark = 0; \
87 SLI_header->c_readonly = 0; \
88 SLI_header->lisp_readonly = 0; \
91 struct lcrecord_header
93 struct lrecord_header lheader;
95 /* The `next' field is normally used to chain all lcrecords together
96 so that the GC can find (and free) all of them.
97 `alloc_lcrecord' threads lcrecords together.
99 The `next' field may be used for other purposes as long as some
100 other mechanism is provided for letting the GC do its work.
102 For example, the event and marker object types allocate members
103 out of memory chunks, and are able to find all unmarked members
104 by sweeping through the elements of the list of chunks. */
105 struct lcrecord_header *next;
107 /* The `uid' field is just for debugging/printing convenience.
108 Having this slot doesn't hurt us much spacewise, since an
109 lcrecord already has the above slots plus malloc overhead. */
110 unsigned int uid :31;
112 /* The `free' field is a flag that indicates whether this lcrecord
113 is on a "free list". Free lists are used to minimize the number
114 of calls to malloc() when we're repeatedly allocating and freeing
115 a number of the same sort of lcrecord. Lcrecords on a free list
116 always get marked in a different fashion, so we can use this flag
117 as a sanity check to make sure that free lists only have freed
118 lcrecords and there are no freed lcrecords elsewhere. */
119 unsigned int free :1;
122 /* Used for lcrecords in an lcrecord-list. */
123 struct free_lcrecord_header
125 struct lcrecord_header lcheader;
131 /* Symbol value magic types come first to make SYMBOL_VALUE_MAGIC_P fast.
132 #### This should be replaced by a symbol_value_magic_p flag
133 in the Lisp_Symbol lrecord_header. */
134 lrecord_type_symbol_value_forward,
135 lrecord_type_symbol_value_varalias,
136 lrecord_type_symbol_value_lisp_magic,
137 lrecord_type_symbol_value_buffer_local,
138 lrecord_type_max_symbol_value_magic = lrecord_type_symbol_value_buffer_local,
145 lrecord_type_lcrecord_list,
146 lrecord_type_compiled_function,
147 lrecord_type_weak_list,
148 lrecord_type_bit_vector,
150 lrecord_type_hash_table,
151 lrecord_type_lstream,
152 lrecord_type_process,
153 lrecord_type_charset,
154 lrecord_type_coding_system,
155 lrecord_type_char_table,
156 lrecord_type_char_table_entry,
157 lrecord_type_byte_table,
158 lrecord_type_uint16_byte_table,
159 lrecord_type_uint8_byte_table,
160 lrecord_type_range_table,
162 lrecord_type_opaque_ptr,
165 lrecord_type_extent_info,
166 lrecord_type_extent_auxiliary,
170 lrecord_type_command_builder,
171 lrecord_type_timeout,
172 lrecord_type_specifier,
173 lrecord_type_console,
177 lrecord_type_window_configuration,
178 lrecord_type_gui_item,
179 lrecord_type_popup_data,
180 lrecord_type_toolbar_button,
181 lrecord_type_color_instance,
182 lrecord_type_font_instance,
183 lrecord_type_image_instance,
186 lrecord_type_database,
187 lrecord_type_tooltalk_message,
188 lrecord_type_tooltalk_pattern,
190 lrecord_type_concord_ds,
191 lrecord_type_concord_object,
193 lrecord_type_pgresult,
194 lrecord_type_devmode,
195 lrecord_type_mswindows_dialog_id,
196 lrecord_type_case_table,
197 lrecord_type_emacs_ffi,
198 lrecord_type_emacs_gtk_object,
199 lrecord_type_emacs_gtk_boxed,
200 lrecord_type_free, /* only used for "free" lrecords */
201 lrecord_type_undefined, /* only used for debugging */
202 lrecord_type_last_built_in_type /* must be last */
205 extern unsigned int lrecord_type_count;
207 struct lrecord_implementation
211 /* `marker' is called at GC time, to make sure that all Lisp_Objects
212 pointed to by this object get properly marked. It should call
213 the mark_object function on all Lisp_Objects in the object. If
214 the return value is non-nil, it should be a Lisp_Object to be
215 marked (don't call the mark_object function explicitly on it,
216 because the GC routines will do this). Doing it this way reduces
217 recursion, so the object returned should preferably be the one
218 with the deepest level of Lisp_Object pointers. This function
219 can be NULL, meaning no GC marking is necessary. */
220 Lisp_Object (*marker) (Lisp_Object);
222 /* `printer' converts the object to a printed representation.
223 This can be NULL; in this case default_object_printer() will be
225 void (*printer) (Lisp_Object, Lisp_Object printcharfun, int escapeflag);
227 /* `finalizer' is called at GC time when the object is about to
228 be freed, and at dump time (FOR_DISKSAVE will be non-zero in this
229 case). It should perform any necessary cleanup (e.g. freeing
230 malloc()ed memory). This can be NULL, meaning no special
231 finalization is necessary.
233 WARNING: remember that `finalizer' is called at dump time even
234 though the object is not being freed. */
235 void (*finalizer) (void *header, int for_disksave);
237 /* This can be NULL, meaning compare objects with EQ(). */
238 int (*equal) (Lisp_Object obj1, Lisp_Object obj2, int depth);
240 /* `hash' generates hash values for use with hash tables that have
241 `equal' as their test function. This can be NULL, meaning use
242 the Lisp_Object itself as the hash. But, you must still satisfy
243 the constraint that if two objects are `equal', then they *must*
244 hash to the same value in order for hash tables to work properly.
245 This means that `hash' can be NULL only if the `equal' method is
247 unsigned long (*hash) (Lisp_Object, int);
249 /* External data layout description */
250 const struct lrecord_description *description;
252 /* These functions allow any object type to have builtin property
253 lists that can be manipulated from the lisp level with
254 `get', `put', `remprop', and `object-plist'. */
255 Lisp_Object (*getprop) (Lisp_Object obj, Lisp_Object prop);
256 int (*putprop) (Lisp_Object obj, Lisp_Object prop, Lisp_Object val);
257 int (*remprop) (Lisp_Object obj, Lisp_Object prop);
258 Lisp_Object (*plist) (Lisp_Object obj);
260 /* Only one of `static_size' and `size_in_bytes_method' is non-0.
261 If both are 0, this type is not instantiable by alloc_lcrecord(). */
263 size_t (*size_in_bytes_method) (const void *header);
265 /* The (constant) index into lrecord_implementations_table */
266 enum lrecord_type lrecord_type_index;
268 /* A "basic" lrecord is any lrecord that's not an lcrecord, i.e.
269 one that does not have an lcrecord_header at the front and which
270 is (usually) allocated in frob blocks. We only use this flag for
271 some consistency checking, and that only when error-checking is
273 unsigned int basic_p :1;
276 /* All the built-in lisp object types are enumerated in `enum record_type'.
277 Additional ones may be defined by a module (none yet). We leave some
278 room in `lrecord_implementations_table' for such new lisp object types. */
279 #define MODULE_DEFINABLE_TYPE_COUNT 32
281 extern const struct lrecord_implementation *lrecord_implementations_table[(unsigned int)lrecord_type_last_built_in_type + MODULE_DEFINABLE_TYPE_COUNT];
283 #define XRECORD_LHEADER_IMPLEMENTATION(obj) \
284 LHEADER_IMPLEMENTATION (XRECORD_LHEADER (obj))
285 #define LHEADER_IMPLEMENTATION(lh) lrecord_implementations_table[(lh)->type]
287 extern int gc_in_progress;
289 #define MARKED_RECORD_P(obj) (XRECORD_LHEADER (obj)->mark)
290 #define MARKED_RECORD_HEADER_P(lheader) ((lheader)->mark)
291 #define MARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 1))
292 #define UNMARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 0))
294 #define C_READONLY_RECORD_HEADER_P(lheader) ((lheader)->c_readonly)
295 #define LISP_READONLY_RECORD_HEADER_P(lheader) ((lheader)->lisp_readonly)
296 #define SET_C_READONLY_RECORD_HEADER(lheader) do { \
297 struct lrecord_header *SCRRH_lheader = (lheader); \
298 SCRRH_lheader->c_readonly = 1; \
299 SCRRH_lheader->lisp_readonly = 1; \
300 SCRRH_lheader->mark = 1; \
302 #define SET_LISP_READONLY_RECORD_HEADER(lheader) \
303 ((void) ((lheader)->lisp_readonly = 1))
304 #define RECORD_MARKER(lheader) lrecord_markers[(lheader)->type]
306 /* External description stuff
308 A lrecord external description is an array of values. The first
309 value of each line is a type, the second the offset in the lrecord
310 structure. Following values are parameters, their presence, type
311 and number is type-dependent.
313 The description ends with a "XD_END" or "XD_SPECIFIER_END" record.
315 Some example descriptions :
317 static const struct lrecord_description cons_description[] = {
318 { XD_LISP_OBJECT, offsetof (Lisp_Cons, car) },
319 { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr) },
323 Which means "two lisp objects starting at the 'car' and 'cdr' elements"
325 static const struct lrecord_description string_description[] = {
326 { XD_BYTECOUNT, offsetof (Lisp_String, size) },
327 { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
328 { XD_LISP_OBJECT, offsetof (Lisp_String, plist) },
331 "A pointer to string data at 'data', the size of the pointed array being the value
332 of the size variable plus 1, and one lisp object at 'plist'"
336 A Lisp object. This is also the type to use for pointers to other lrecords.
339 An array of Lisp objects or pointers to lrecords.
340 The third element is the count.
343 Link in a linked list of objects of the same type.
346 Pointer to undumpable data. Must be NULL when dumping.
349 Pointer to described struct. Parameters are number of structures and
353 Pointer to dumpable opaque data. Parameter is the size of the data.
354 Pointed data must be relocatable without changes.
357 Pointer to a C string.
360 Pointer to a doc string (C string if positive, opaque value if negative)
363 An integer which will be reset to a given value in the dump file.
367 size_t value. Used for counts.
370 int value. Used for counts.
373 long value. Used for counts.
376 bytecount value. Used for counts.
379 Special type indicating the end of the array.
382 Special type indicating the end of the array for a specifier. Extra
383 description is going to be fetched from the specifier methods.
387 XD_INDIRECT(line, delta)
388 Usable where a "count" or "size" is requested. Gives the value of
389 the element which is at line number 'line' in the description (count
390 starts at zero) and adds delta to it.
393 enum lrecord_description_type {
394 XD_LISP_OBJECT_ARRAY,
411 struct lrecord_description {
412 enum lrecord_description_type type;
415 const struct struct_description *data2;
418 struct struct_description {
420 const struct lrecord_description *description;
423 #define XD_INDIRECT(val, delta) (-1-((val)|(delta<<8)))
425 #define XD_IS_INDIRECT(code) (code<0)
426 #define XD_INDIRECT_VAL(code) ((-1-code) & 255)
427 #define XD_INDIRECT_DELTA(code) (((-1-code)>>8) & 255)
429 #define XD_DYNARR_DESC(base_type, sub_desc) \
430 { XD_STRUCT_PTR, offsetof (base_type, base), XD_INDIRECT(1, 0), sub_desc }, \
431 { XD_INT, offsetof (base_type, cur) }, \
432 { XD_INT_RESET, offsetof (base_type, max), XD_INDIRECT(1, 0) }
434 /* DEFINE_LRECORD_IMPLEMENTATION is for objects with constant size.
435 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION is for objects whose size varies.
438 #if defined (ERROR_CHECK_TYPECHECK)
439 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
441 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
444 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
445 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
447 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
448 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof(structtype),0,1,structtype)
450 #define DEFINE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
451 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
453 #define DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
454 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
456 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
457 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
459 #define DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
460 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,0,sizer,1,structtype)
462 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
463 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype) \
465 #define MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
466 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
467 const struct lrecord_implementation lrecord_##c_name = \
468 { name, marker, printer, nuker, equal, hash, desc, \
469 getprop, putprop, remprop, plist, size, sizer, \
470 lrecord_type_##c_name, basic_p }
472 #define DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
473 DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
475 #define DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
476 MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
478 #define DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
479 DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
481 #define DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
482 MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype)
484 #define MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
485 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype) \
486 unsigned int lrecord_type_##c_name; \
487 struct lrecord_implementation lrecord_##c_name = \
488 { name, marker, printer, nuker, equal, hash, desc, \
489 getprop, putprop, remprop, plist, size, sizer, \
490 lrecord_type_last_built_in_type, basic_p }
493 extern Lisp_Object (*lrecord_markers[]) (Lisp_Object);
495 #define INIT_LRECORD_IMPLEMENTATION(type) do { \
496 lrecord_implementations_table[lrecord_type_##type] = &lrecord_##type; \
497 lrecord_markers[lrecord_type_##type] = \
498 lrecord_implementations_table[lrecord_type_##type]->marker; \
501 #define INIT_EXTERNAL_LRECORD_IMPLEMENTATION(type) do { \
502 lrecord_type_##type = lrecord_type_count++; \
503 lrecord_##type.lrecord_type_index = (enum lrecord_type) lrecord_type_##type; \
504 INIT_LRECORD_IMPLEMENTATION(type); \
507 #define LRECORDP(a) (XTYPE (a) == Lisp_Type_Record)
508 #define XRECORD_LHEADER(a) ((struct lrecord_header *) XPNTR (a))
510 #define RECORD_TYPEP(x, ty) \
511 (LRECORDP (x) && (((unsigned int)(XRECORD_LHEADER (x)->type)) == ((unsigned int)(ty))))
513 /* Steps to create a new object:
515 1. Declare the struct for your object in a header file somewhere.
516 Remember that it must begin with
518 struct lcrecord_header header;
520 2. Put a DECLARE_LRECORD() for the object below the struct definition,
521 along with the standard XFOO/XSETFOO junk.
523 3. Add this header file to inline.c.
525 4. Create the methods for your object. Note that technically you don't
526 need any, but you will almost always want at least a mark method.
528 5. Define your object with DEFINE_LRECORD_IMPLEMENTATION() or some
531 6. Include the header file in the .c file where you defined the object.
533 7. Put a call to INIT_LRECORD_IMPLEMENTATION() for the object in the
534 .c file's syms_of_foo() function.
536 8. Add a type enum for the object to enum lrecord_type, earlier in this
541 ------------------------------ in toolbar.h -----------------------------
543 struct toolbar_button
545 struct lcrecord_header header;
550 Lisp_Object up_glyph;
551 Lisp_Object down_glyph;
552 Lisp_Object disabled_glyph;
554 Lisp_Object cap_up_glyph;
555 Lisp_Object cap_down_glyph;
556 Lisp_Object cap_disabled_glyph;
558 Lisp_Object callback;
559 Lisp_Object enabled_p;
560 Lisp_Object help_string;
574 DECLARE_LRECORD (toolbar_button, struct toolbar_button);
575 #define XTOOLBAR_BUTTON(x) XRECORD (x, toolbar_button, struct toolbar_button)
576 #define XSETTOOLBAR_BUTTON(x, p) XSETRECORD (x, p, toolbar_button)
577 #define TOOLBAR_BUTTONP(x) RECORDP (x, toolbar_button)
578 #define CHECK_TOOLBAR_BUTTON(x) CHECK_RECORD (x, toolbar_button)
579 #define CONCHECK_TOOLBAR_BUTTON(x) CONCHECK_RECORD (x, toolbar_button)
581 ------------------------------ in toolbar.c -----------------------------
588 mark_toolbar_button (Lisp_Object obj)
590 struct toolbar_button *data = XTOOLBAR_BUTTON (obj);
591 mark_object (data->next);
592 mark_object (data->frame);
593 mark_object (data->up_glyph);
594 mark_object (data->down_glyph);
595 mark_object (data->disabled_glyph);
596 mark_object (data->cap_up_glyph);
597 mark_object (data->cap_down_glyph);
598 mark_object (data->cap_disabled_glyph);
599 mark_object (data->callback);
600 mark_object (data->enabled_p);
601 return data->help_string;
604 DEFINE_LRECORD_IMPLEMENTATION ("toolbar-button", toolbar_button,
605 mark_toolbar_button, 0, 0, 0, 0, 0,
606 struct toolbar_button);
611 syms_of_toolbar (void)
613 INIT_LRECORD_IMPLEMENTATION (toolbar_button);
618 ------------------------------ in inline.c -----------------------------
624 ------------------------------ in lrecord.h -----------------------------
629 lrecord_type_toolbar_button,
637 Note: Object types defined in external dynamically-loaded modules (not
638 part of the XEmacs main source code) should use DECLARE_EXTERNAL_LRECORD
639 and DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION rather than DECLARE_LRECORD
640 and DEFINE_LRECORD_IMPLEMENTATION.
645 #ifdef ERROR_CHECK_TYPECHECK
647 # define DECLARE_LRECORD(c_name, structtype) \
648 extern const struct lrecord_implementation lrecord_##c_name; \
649 INLINE_HEADER structtype * \
650 error_check_##c_name (Lisp_Object obj); \
651 INLINE_HEADER structtype * \
652 error_check_##c_name (Lisp_Object obj) \
654 assert (RECORD_TYPEP (obj, lrecord_type_##c_name)); \
655 return (structtype *) XPNTR (obj); \
657 extern Lisp_Object Q##c_name##p
659 # define DECLARE_EXTERNAL_LRECORD(c_name, structtype) \
660 extern unsigned int lrecord_type_##c_name; \
661 extern struct lrecord_implementation lrecord_##c_name; \
662 INLINE_HEADER structtype * \
663 error_check_##c_name (Lisp_Object obj); \
664 INLINE_HEADER structtype * \
665 error_check_##c_name (Lisp_Object obj) \
667 assert (RECORD_TYPEP (obj, lrecord_type_##c_name)); \
668 return (structtype *) XPNTR (obj); \
670 extern Lisp_Object Q##c_name##p
672 # define DECLARE_NONRECORD(c_name, type_enum, structtype) \
673 INLINE_HEADER structtype * \
674 error_check_##c_name (Lisp_Object obj); \
675 INLINE_HEADER structtype * \
676 error_check_##c_name (Lisp_Object obj) \
678 assert (XTYPE (obj) == type_enum); \
679 return (structtype *) XPNTR (obj); \
681 extern Lisp_Object Q##c_name##p
683 # define XRECORD(x, c_name, structtype) error_check_##c_name (x)
684 # define XNONRECORD(x, c_name, type_enum, structtype) error_check_##c_name (x)
686 # define XSETRECORD(var, p, c_name) do \
689 assert (RECORD_TYPEP (var, lrecord_type_##c_name)); \
692 #else /* not ERROR_CHECK_TYPECHECK */
694 # define DECLARE_LRECORD(c_name, structtype) \
695 extern Lisp_Object Q##c_name##p; \
696 extern const struct lrecord_implementation lrecord_##c_name
697 # define DECLARE_EXTERNAL_LRECORD(c_name, structtype) \
698 extern Lisp_Object Q##c_name##p; \
699 extern unsigned int lrecord_type_##c_name; \
700 extern struct lrecord_implementation lrecord_##c_name
701 # define DECLARE_NONRECORD(c_name, type_enum, structtype) \
702 extern Lisp_Object Q##c_name##p
703 # define XRECORD(x, c_name, structtype) ((structtype *) XPNTR (x))
704 # define XNONRECORD(x, c_name, type_enum, structtype) \
705 ((structtype *) XPNTR (x))
706 # define XSETRECORD(var, p, c_name) XSETOBJ (var, p)
708 #endif /* not ERROR_CHECK_TYPECHECK */
710 #define RECORDP(x, c_name) RECORD_TYPEP (x, lrecord_type_##c_name)
712 /* Note: we now have two different kinds of type-checking macros.
713 The "old" kind has now been renamed CONCHECK_foo. The reason for
714 this is that the CONCHECK_foo macros signal a continuable error,
715 allowing the user (through debug-on-error) to substitute a different
716 value and return from the signal, which causes the lvalue argument
717 to get changed. Quite a lot of code would crash if that happened,
718 because it did things like
723 and later on did XSTRING (XCAR (list)), assuming that the type
724 is correct (when it might be wrong, if the user substituted a
725 correct value in the debugger).
727 To get around this, I made all the CHECK_foo macros signal a
728 non-continuable error. Places where a continuable error is OK
729 (generally only when called directly on the argument of a Lisp
730 primitive) should be changed to use CONCHECK().
732 FSF Emacs does not have this problem because RMS took the cheesy
733 way out and disabled returning from a signal entirely. */
735 #define CONCHECK_RECORD(x, c_name) do { \
736 if (!RECORD_TYPEP (x, lrecord_type_##c_name)) \
737 x = wrong_type_argument (Q##c_name##p, x); \
739 #define CONCHECK_NONRECORD(x, lisp_enum, predicate) do {\
740 if (XTYPE (x) != lisp_enum) \
741 x = wrong_type_argument (predicate, x); \
743 #define CHECK_RECORD(x, c_name) do { \
744 if (!RECORD_TYPEP (x, lrecord_type_##c_name)) \
745 dead_wrong_type_argument (Q##c_name##p, x); \
747 #define CHECK_NONRECORD(x, lisp_enum, predicate) do { \
748 if (XTYPE (x) != lisp_enum) \
749 dead_wrong_type_argument (predicate, x); \
752 void *alloc_lcrecord (size_t size, const struct lrecord_implementation *);
754 #define alloc_lcrecord_type(type, lrecord_implementation) \
755 ((type *) alloc_lcrecord (sizeof (type), lrecord_implementation))
757 /* Copy the data from one lcrecord structure into another, but don't
758 overwrite the header information. */
760 #define copy_lcrecord(dst, src) \
761 memcpy ((char *) (dst) + sizeof (struct lcrecord_header), \
762 (char *) (src) + sizeof (struct lcrecord_header), \
763 sizeof (*(dst)) - sizeof (struct lcrecord_header))
765 #define zero_lcrecord(lcr) \
766 memset ((char *) (lcr) + sizeof (struct lcrecord_header), 0, \
767 sizeof (*(lcr)) - sizeof (struct lcrecord_header))
769 #endif /* INCLUDED_lrecord_h_ */