sync to xemacs-21.2.37 but STILL BUGGY
[chise/xemacs-chise.git-] / src / lrecord.h
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 INCLUDED_lrecord_h_
25 #define INCLUDED_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 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
35    some other data bits.
36
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.
44
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().
51
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
58    the opaque type. */
59
60 struct lrecord_header
61 {
62   /* index into lrecord_implementations_table[] */
63   unsigned int type :8;
64
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 */
69   unsigned int mark :1;
70
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;
75
76   /* 1 if the object is readonly from lisp */
77   unsigned int lisp_readonly :1;
78
79 #ifdef UTF2000
80   /* The `older field is a flag that indicates whether this lcrecord
81      is on a "older storage". */
82   unsigned int older :1;
83 #endif
84 };
85
86 struct lrecord_implementation;
87 int lrecord_type_index (const struct lrecord_implementation *implementation);
88
89 #ifdef UTF2000
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;                        \
97 } while (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;                                \
105 } while (0)
106 #else
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;                        \
113 } while (0)
114 #endif
115
116 struct lcrecord_header
117 {
118   struct lrecord_header lheader;
119
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.
123
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.
126
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;
131
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;
136
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;
145 };
146
147 /* Used for lcrecords in an lcrecord-list. */
148 struct free_lcrecord_header
149 {
150   struct lcrecord_header lcheader;
151   Lisp_Object chain;
152 };
153
154 enum lrecord_type
155 {
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,
164
165   lrecord_type_symbol,
166   lrecord_type_subr,
167   lrecord_type_cons,
168   lrecord_type_vector,
169   lrecord_type_string,
170   lrecord_type_lcrecord_list,
171   lrecord_type_compiled_function,
172   lrecord_type_weak_list,
173   lrecord_type_bit_vector,
174   lrecord_type_float,
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_uint16_byte_table,
185   lrecord_type_uint8_byte_table,
186   lrecord_type_range_table,
187   lrecord_type_opaque,
188   lrecord_type_opaque_ptr,
189   lrecord_type_buffer,
190   lrecord_type_extent,
191   lrecord_type_extent_info,
192   lrecord_type_extent_auxiliary,
193   lrecord_type_marker,
194   lrecord_type_event,
195   lrecord_type_keymap,
196   lrecord_type_command_builder,
197   lrecord_type_timeout,
198   lrecord_type_specifier,
199   lrecord_type_console,
200   lrecord_type_device,
201   lrecord_type_frame,
202   lrecord_type_window,
203   lrecord_type_window_configuration,
204   lrecord_type_gui_item,
205   lrecord_type_popup_data,
206   lrecord_type_toolbar_button,
207   lrecord_type_color_instance,
208   lrecord_type_font_instance,
209   lrecord_type_image_instance,
210   lrecord_type_glyph,
211   lrecord_type_face,
212   lrecord_type_database,
213   lrecord_type_tooltalk_message,
214   lrecord_type_tooltalk_pattern,
215   lrecord_type_ldap,
216   lrecord_type_pgconn,
217   lrecord_type_pgresult,
218   lrecord_type_devmode,
219   lrecord_type_mswindows_dialog_id,
220   lrecord_type_last_built_in_type /* must be last */
221 };
222
223 extern unsigned int lrecord_type_count;
224
225 struct lrecord_implementation
226 {
227   const char *name;
228
229   /* `marker' is called at GC time, to make sure that all Lisp_Objects
230      pointed to by this object get properly marked.  It should call
231      the mark_object function on all Lisp_Objects in the object.  If
232      the return value is non-nil, it should be a Lisp_Object to be
233      marked (don't call the mark_object function explicitly on it,
234      because the GC routines will do this).  Doing it this way reduces
235      recursion, so the object returned should preferably be the one
236      with the deepest level of Lisp_Object pointers.  This function
237      can be NULL, meaning no GC marking is necessary. */
238   Lisp_Object (*marker) (Lisp_Object);
239
240   /* `printer' converts the object to a printed representation.
241      This can be NULL; in this case default_object_printer() will be
242      used instead. */
243   void (*printer) (Lisp_Object, Lisp_Object printcharfun, int escapeflag);
244
245   /* `finalizer' is called at GC time when the object is about to
246      be freed, and at dump time (FOR_DISKSAVE will be non-zero in this
247      case).  It should perform any necessary cleanup (e.g. freeing
248      malloc()ed memory).  This can be NULL, meaning no special
249      finalization is necessary.
250
251      WARNING: remember that `finalizer' is called at dump time even
252      though the object is not being freed. */
253   void (*finalizer) (void *header, int for_disksave);
254
255   /* This can be NULL, meaning compare objects with EQ(). */
256   int (*equal) (Lisp_Object obj1, Lisp_Object obj2, int depth);
257
258   /* `hash' generates hash values for use with hash tables that have
259      `equal' as their test function.  This can be NULL, meaning use
260      the Lisp_Object itself as the hash.  But, you must still satisfy
261      the constraint that if two objects are `equal', then they *must*
262      hash to the same value in order for hash tables to work properly.
263      This means that `hash' can be NULL only if the `equal' method is
264      also NULL. */
265   unsigned long (*hash) (Lisp_Object, int);
266
267   /* External data layout description */
268   const struct lrecord_description *description;
269
270   /* These functions allow any object type to have builtin property
271      lists that can be manipulated from the lisp level with
272      `get', `put', `remprop', and `object-plist'. */
273   Lisp_Object (*getprop) (Lisp_Object obj, Lisp_Object prop);
274   int (*putprop) (Lisp_Object obj, Lisp_Object prop, Lisp_Object val);
275   int (*remprop) (Lisp_Object obj, Lisp_Object prop);
276   Lisp_Object (*plist) (Lisp_Object obj);
277
278   /* Only one of `static_size' and `size_in_bytes_method' is non-0.
279      If both are 0, this type is not instantiable by alloc_lcrecord(). */
280   size_t static_size;
281   size_t (*size_in_bytes_method) (const void *header);
282
283   /* The (constant) index into lrecord_implementations_table */
284   enum lrecord_type lrecord_type_index;
285
286   /* A "basic" lrecord is any lrecord that's not an lcrecord, i.e.
287      one that does not have an lcrecord_header at the front and which
288      is (usually) allocated in frob blocks.  We only use this flag for
289      some consistency checking, and that only when error-checking is
290      enabled. */
291   unsigned int basic_p :1;
292 };
293
294 /* All the built-in lisp object types are enumerated in `enum record_type'.
295    Additional ones may be defined by a module (none yet).  We leave some
296    room in `lrecord_implementations_table' for such new lisp object types. */
297 #define MODULE_DEFINABLE_TYPE_COUNT 32
298
299 extern const struct lrecord_implementation *lrecord_implementations_table[(unsigned int)lrecord_type_last_built_in_type + MODULE_DEFINABLE_TYPE_COUNT];
300
301 #define XRECORD_LHEADER_IMPLEMENTATION(obj) \
302    LHEADER_IMPLEMENTATION (XRECORD_LHEADER (obj))
303 #define LHEADER_IMPLEMENTATION(lh) lrecord_implementations_table[(lh)->type]
304
305 extern int gc_in_progress;
306
307 #define MARKED_RECORD_P(obj) (XRECORD_LHEADER (obj)->mark)
308 #define MARKED_RECORD_HEADER_P(lheader) ((lheader)->mark)
309 #define MARK_RECORD_HEADER(lheader)   ((void) ((lheader)->mark = 1))
310 #define UNMARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 0))
311
312 #define OLDER_RECORD_P(obj) (XRECORD_LHEADER (obj)->older)
313 #define OLDER_RECORD_HEADER_P(lheader) ((lheader)->older)
314
315
316 #define C_READONLY_RECORD_HEADER_P(lheader)  ((lheader)->c_readonly)
317 #define LISP_READONLY_RECORD_HEADER_P(lheader)  ((lheader)->lisp_readonly)
318 #define SET_C_READONLY_RECORD_HEADER(lheader) do {      \
319   struct lrecord_header *SCRRH_lheader = (lheader);     \
320   SCRRH_lheader->c_readonly = 1;                        \
321   SCRRH_lheader->lisp_readonly = 1;                     \
322   SCRRH_lheader->mark = 1;                              \
323 } while (0)
324 #define SET_LISP_READONLY_RECORD_HEADER(lheader) \
325   ((void) ((lheader)->lisp_readonly = 1))
326 #define RECORD_MARKER(lheader) lrecord_markers[(lheader)->type]
327
328 /* External description stuff
329
330    A lrecord external description  is an array  of values.  The  first
331    value of each line is a type, the second  the offset in the lrecord
332    structure.  Following values  are parameters, their  presence, type
333    and number is type-dependent.
334
335    The description ends with a "XD_END" or "XD_SPECIFIER_END" record.
336
337    Some example descriptions :
338
339    static const struct lrecord_description cons_description[] = {
340      { XD_LISP_OBJECT, offsetof (Lisp_Cons, car) },
341      { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr) },
342      { XD_END }
343    };
344
345    Which means "two lisp objects starting at the 'car' and 'cdr' elements"
346
347   static const struct lrecord_description string_description[] = {
348     { XD_BYTECOUNT,       offsetof (Lisp_String, size) },
349     { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
350     { XD_LISP_OBJECT,     offsetof (Lisp_String, plist) },
351     { XD_END }
352   };
353   "A pointer to string data at 'data', the size of the pointed array being the value
354    of the size variable plus 1, and one lisp object at 'plist'"
355
356   The existing types :
357     XD_LISP_OBJECT
358   A Lisp object.  This is also the type to use for pointers to other lrecords.
359
360     XD_LISP_OBJECT_ARRAY
361   An array of Lisp objects or pointers to lrecords.
362   The third element is the count.
363
364     XD_LO_RESET_NIL
365   Lisp objects which will be reset to Qnil when dumping.  Useful for cleaning
366   up caches.
367
368     XD_LO_LINK
369   Link in a linked list of objects of the same type.
370
371     XD_OPAQUE_PTR
372   Pointer to undumpable data.  Must be NULL when dumping.
373
374     XD_STRUCT_PTR
375   Pointer to described struct.  Parameters are number of structures and
376   struct_description.
377
378     XD_OPAQUE_DATA_PTR
379   Pointer to dumpable opaque data.  Parameter is the size of the data.
380   Pointed data must be relocatable without changes.
381
382     XD_C_STRING
383   Pointer to a C string.
384
385     XD_DOC_STRING
386   Pointer to a doc string (C string if positive, opaque value if negative)
387
388     XD_INT_RESET
389   An integer which will be reset to a given value in the dump file.
390
391
392     XD_SIZE_T
393   size_t value.  Used for counts.
394
395     XD_INT
396   int value.  Used for counts.
397
398     XD_LONG
399   long value.  Used for counts.
400
401     XD_BYTECOUNT
402   bytecount value.  Used for counts.
403
404     XD_END
405   Special type indicating the end of the array.
406
407     XD_SPECIFIER_END
408   Special type indicating the end of the array for a specifier.  Extra
409   description is going to be fetched from the specifier methods.
410
411
412   Special macros:
413     XD_INDIRECT(line, delta)
414   Usable where  a "count" or "size"  is requested.  Gives the value of
415   the element which is at line number 'line' in the description (count
416   starts at zero) and adds delta to it.
417 */
418
419 enum lrecord_description_type {
420   XD_LISP_OBJECT_ARRAY,
421   XD_LISP_OBJECT,
422   XD_LO_RESET_NIL,
423   XD_LO_LINK,
424   XD_OPAQUE_PTR,
425   XD_STRUCT_PTR,
426   XD_OPAQUE_DATA_PTR,
427   XD_C_STRING,
428   XD_DOC_STRING,
429   XD_INT_RESET,
430   XD_SIZE_T,
431   XD_INT,
432   XD_LONG,
433   XD_BYTECOUNT,
434   XD_END,
435   XD_SPECIFIER_END
436 };
437
438 struct lrecord_description {
439   enum lrecord_description_type type;
440   int offset;
441   EMACS_INT data1;
442   const struct struct_description *data2;
443 };
444
445 struct struct_description {
446   size_t size;
447   const struct lrecord_description *description;
448 };
449
450 #define XD_INDIRECT(val, delta) (-1-((val)|(delta<<8)))
451
452 #define XD_IS_INDIRECT(code) (code<0)
453 #define XD_INDIRECT_VAL(code) ((-1-code) & 255)
454 #define XD_INDIRECT_DELTA(code) (((-1-code)>>8) & 255)
455
456 #define XD_DYNARR_DESC(base_type, sub_desc) \
457   { XD_STRUCT_PTR, offsetof (base_type, base), XD_INDIRECT(1, 0), sub_desc }, \
458   { XD_INT,        offsetof (base_type, cur) }, \
459   { XD_INT_RESET,  offsetof (base_type, max), XD_INDIRECT(1, 0) }
460
461 /* DEFINE_LRECORD_IMPLEMENTATION is for objects with constant size.
462    DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION is for objects whose size varies.
463  */
464
465 #if defined (ERROR_CHECK_TYPECHECK)
466 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
467 #else
468 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
469 #endif
470
471 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
472 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
473
474 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
475 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof(structtype),0,1,structtype)
476
477 #define DEFINE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
478 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
479
480 #define DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
481 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
482
483 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
484 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
485
486 #define DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
487 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,0,sizer,1,structtype)
488
489 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
490 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype) \
491
492 #define MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
493 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)                       \
494 const struct lrecord_implementation lrecord_##c_name =                  \
495   { name, marker, printer, nuker, equal, hash, desc,                    \
496     getprop, putprop, remprop, plist, size, sizer,                      \
497     lrecord_type_##c_name, basic_p }
498
499 #define DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
500 DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
501
502 #define DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
503 MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
504
505 #define DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
506 DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
507
508 #define DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
509 MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype)
510
511 #define MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
512 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)                       \
513 unsigned int lrecord_type_##c_name;                                     \
514 struct lrecord_implementation lrecord_##c_name =                        \
515   { name, marker, printer, nuker, equal, hash, desc,                    \
516     getprop, putprop, remprop, plist, size, sizer,                      \
517     lrecord_type_last_built_in_type, basic_p }
518
519
520 extern Lisp_Object (*lrecord_markers[]) (Lisp_Object);
521
522 #define INIT_LRECORD_IMPLEMENTATION(type) do {                          \
523   lrecord_implementations_table[lrecord_type_##type] = &lrecord_##type; \
524   lrecord_markers[lrecord_type_##type] =                                \
525     lrecord_implementations_table[lrecord_type_##type]->marker;         \
526 } while (0)
527
528 #define INIT_EXTERNAL_LRECORD_IMPLEMENTATION(type) do {                 \
529   lrecord_type_##type = lrecord_type_count++;                           \
530   lrecord_##type.lrecord_type_index = lrecord_type_##type;              \
531   INIT_LRECORD_IMPLEMENTATION(type);                                    \
532 } while (0)
533
534 #define LRECORDP(a) (XTYPE (a) == Lisp_Type_Record)
535 #define XRECORD_LHEADER(a) ((struct lrecord_header *) XPNTR (a))
536
537 #define RECORD_TYPEP(x, ty) \
538   (LRECORDP (x) && (((unsigned int)(XRECORD_LHEADER (x)->type)) == ((unsigned int)(ty))))
539
540 /* Steps to create a new object:
541
542    1. Declare the struct for your object in a header file somewhere.
543    Remember that it must begin with
544
545    struct lcrecord_header header;
546
547    2. Put a DECLARE_LRECORD() for the object below the struct definition,
548    along with the standard XFOO/XSETFOO junk.
549
550    3. Add this header file to inline.c.
551
552    4. Create the methods for your object.  Note that technically you don't
553    need any, but you will almost always want at least a mark method.
554
555    5. Define your object with DEFINE_LRECORD_IMPLEMENTATION() or some
556    variant.
557
558    6. Include the header file in the .c file where you defined the object.
559
560    7. Put a call to INIT_LRECORD_IMPLEMENTATION() for the object in the
561    .c file's syms_of_foo() function.
562
563    8. Add a type enum for the object to enum lrecord_type, earlier in this
564    file.
565
566 An example:
567
568 ------------------------------ in toolbar.h -----------------------------
569
570 struct toolbar_button
571 {
572   struct lcrecord_header header;
573
574   Lisp_Object next;
575   Lisp_Object frame;
576
577   Lisp_Object up_glyph;
578   Lisp_Object down_glyph;
579   Lisp_Object disabled_glyph;
580
581   Lisp_Object cap_up_glyph;
582   Lisp_Object cap_down_glyph;
583   Lisp_Object cap_disabled_glyph;
584
585   Lisp_Object callback;
586   Lisp_Object enabled_p;
587   Lisp_Object help_string;
588
589   char enabled;
590   char down;
591   char pushright;
592   char blank;
593
594   int x, y;
595   int width, height;
596   int dirty;
597   int vertical;
598   int border_width;
599 };
600
601 DECLARE_LRECORD (toolbar_button, struct toolbar_button);
602 #define XTOOLBAR_BUTTON(x) XRECORD (x, toolbar_button, struct toolbar_button)
603 #define XSETTOOLBAR_BUTTON(x, p) XSETRECORD (x, p, toolbar_button)
604 #define TOOLBAR_BUTTONP(x) RECORDP (x, toolbar_button)
605 #define CHECK_TOOLBAR_BUTTON(x) CHECK_RECORD (x, toolbar_button)
606 #define CONCHECK_TOOLBAR_BUTTON(x) CONCHECK_RECORD (x, toolbar_button)
607
608 ------------------------------ in toolbar.c -----------------------------
609
610 #include "toolbar.h"
611
612 ...
613
614 static Lisp_Object
615 mark_toolbar_button (Lisp_Object obj)
616 {
617   struct toolbar_button *data = XTOOLBAR_BUTTON (obj);
618   mark_object (data->next);
619   mark_object (data->frame);
620   mark_object (data->up_glyph);
621   mark_object (data->down_glyph);
622   mark_object (data->disabled_glyph);
623   mark_object (data->cap_up_glyph);
624   mark_object (data->cap_down_glyph);
625   mark_object (data->cap_disabled_glyph);
626   mark_object (data->callback);
627   mark_object (data->enabled_p);
628   return data->help_string;
629 }
630
631 DEFINE_LRECORD_IMPLEMENTATION ("toolbar-button", toolbar_button,
632                                mark_toolbar_button, 0, 0, 0, 0, 0,
633                                struct toolbar_button);
634
635 ...
636
637 void
638 syms_of_toolbar (void)
639 {
640   INIT_LRECORD_IMPLEMENTATION (toolbar_button);
641
642   ...;
643 }
644
645 ------------------------------ in inline.c -----------------------------
646
647 #ifdef HAVE_TOOLBARS
648 #include "toolbar.h"
649 #endif
650
651 ------------------------------ in lrecord.h -----------------------------
652
653 enum lrecord_type
654 {
655   ...
656   lrecord_type_toolbar_button,
657   ...
658 };
659
660 */
661
662 /*
663
664 Note: Object types defined in external dynamically-loaded modules (not
665 part of the XEmacs main source code) should use DECLARE_EXTERNAL_LRECORD
666 and DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION rather than DECLARE_LRECORD
667 and DEFINE_LRECORD_IMPLEMENTATION.
668
669 */
670
671
672 #ifdef ERROR_CHECK_TYPECHECK
673
674 # define DECLARE_LRECORD(c_name, structtype)                    \
675 extern const struct lrecord_implementation lrecord_##c_name;    \
676 INLINE_HEADER structtype *                                      \
677 error_check_##c_name (Lisp_Object obj);                         \
678 INLINE_HEADER structtype *                                      \
679 error_check_##c_name (Lisp_Object obj)                          \
680 {                                                               \
681   assert (RECORD_TYPEP (obj, lrecord_type_##c_name));           \
682   return (structtype *) XPNTR (obj);                            \
683 }                                                               \
684 extern Lisp_Object Q##c_name##p
685
686 # define DECLARE_EXTERNAL_LRECORD(c_name, structtype)           \
687 extern unsigned int lrecord_type_##c_name;                      \
688 extern struct lrecord_implementation lrecord_##c_name;          \
689 INLINE_HEADER structtype *                                      \
690 error_check_##c_name (Lisp_Object obj);                         \
691 INLINE_HEADER structtype *                                      \
692 error_check_##c_name (Lisp_Object obj)                          \
693 {                                                               \
694   assert (RECORD_TYPEP (obj, lrecord_type_##c_name));           \
695   return (structtype *) XPNTR (obj);                            \
696 }                                                               \
697 extern Lisp_Object Q##c_name##p
698
699 # define DECLARE_NONRECORD(c_name, type_enum, structtype)       \
700 INLINE_HEADER structtype *                                      \
701 error_check_##c_name (Lisp_Object obj);                         \
702 INLINE_HEADER structtype *                                      \
703 error_check_##c_name (Lisp_Object obj)                          \
704 {                                                               \
705   assert (XTYPE (obj) == type_enum);                            \
706   return (structtype *) XPNTR (obj);                            \
707 }                                                               \
708 extern Lisp_Object Q##c_name##p
709
710 # define XRECORD(x, c_name, structtype) error_check_##c_name (x)
711 # define XNONRECORD(x, c_name, type_enum, structtype) error_check_##c_name (x)
712
713 # define XSETRECORD(var, p, c_name) do                          \
714 {                                                               \
715   XSETOBJ (var, p);                                             \
716   assert (RECORD_TYPEP (var, lrecord_type_##c_name));           \
717 } while (0)
718
719 #else /* not ERROR_CHECK_TYPECHECK */
720
721 # define DECLARE_LRECORD(c_name, structtype)                    \
722 extern Lisp_Object Q##c_name##p;                                \
723 extern const struct lrecord_implementation lrecord_##c_name
724 # define DECLARE_EXTERNAL_LRECORD(c_name, structtype)           \
725 extern Lisp_Object Q##c_name##p;                                \
726 extern unsigned int lrecord_type_##c_name;                      \
727 extern struct lrecord_implementation lrecord_##c_name
728 # define DECLARE_NONRECORD(c_name, type_enum, structtype)       \
729 extern Lisp_Object Q##c_name##p
730 # define XRECORD(x, c_name, structtype) ((structtype *) XPNTR (x))
731 # define XNONRECORD(x, c_name, type_enum, structtype)           \
732   ((structtype *) XPNTR (x))
733 # define XSETRECORD(var, p, c_name) XSETOBJ (var, p)
734
735 #endif /* not ERROR_CHECK_TYPECHECK */
736
737 #define RECORDP(x, c_name) RECORD_TYPEP (x, lrecord_type_##c_name)
738
739 /* Note: we now have two different kinds of type-checking macros.
740    The "old" kind has now been renamed CONCHECK_foo.  The reason for
741    this is that the CONCHECK_foo macros signal a continuable error,
742    allowing the user (through debug-on-error) to substitute a different
743    value and return from the signal, which causes the lvalue argument
744    to get changed.  Quite a lot of code would crash if that happened,
745    because it did things like
746
747    foo = XCAR (list);
748    CHECK_STRING (foo);
749
750    and later on did XSTRING (XCAR (list)), assuming that the type
751    is correct (when it might be wrong, if the user substituted a
752    correct value in the debugger).
753
754    To get around this, I made all the CHECK_foo macros signal a
755    non-continuable error.  Places where a continuable error is OK
756    (generally only when called directly on the argument of a Lisp
757    primitive) should be changed to use CONCHECK().
758
759    FSF Emacs does not have this problem because RMS took the cheesy
760    way out and disabled returning from a signal entirely. */
761
762 #define CONCHECK_RECORD(x, c_name) do {                 \
763  if (!RECORD_TYPEP (x, lrecord_type_##c_name))          \
764    x = wrong_type_argument (Q##c_name##p, x);           \
765 }  while (0)
766 #define CONCHECK_NONRECORD(x, lisp_enum, predicate) do {\
767  if (XTYPE (x) != lisp_enum)                            \
768    x = wrong_type_argument (predicate, x);              \
769  } while (0)
770 #define CHECK_RECORD(x, c_name) do {                    \
771  if (!RECORD_TYPEP (x, lrecord_type_##c_name))          \
772    dead_wrong_type_argument (Q##c_name##p, x);          \
773  } while (0)
774 #define CHECK_NONRECORD(x, lisp_enum, predicate) do {   \
775  if (XTYPE (x) != lisp_enum)                            \
776    dead_wrong_type_argument (predicate, x);             \
777  } while (0)
778
779 void *alloc_lcrecord (size_t size, const struct lrecord_implementation *);
780
781 #define alloc_lcrecord_type(type, lrecord_implementation) \
782   ((type *) alloc_lcrecord (sizeof (type), lrecord_implementation))
783
784 #ifdef UTF2000
785 void *
786 alloc_older_lcrecord (size_t size, const struct lrecord_implementation *);
787
788 #define alloc_older_lcrecord_type(type, lrecord_implementation) \
789   ((type *) alloc_older_lcrecord (sizeof (type), lrecord_implementation))
790 #endif
791
792 /* Copy the data from one lcrecord structure into another, but don't
793    overwrite the header information. */
794
795 #define copy_lcrecord(dst, src)                                 \
796   memcpy ((char *) (dst) + sizeof (struct lcrecord_header),     \
797           (char *) (src) + sizeof (struct lcrecord_header),     \
798           sizeof (*(dst)) - sizeof (struct lcrecord_header))
799
800 #define zero_lcrecord(lcr)                                      \
801    memset ((char *) (lcr) + sizeof (struct lcrecord_header), 0, \
802            sizeof (*(lcr)) - sizeof (struct lcrecord_header))
803
804 #endif /* INCLUDED_lrecord_h_ */