Improve documentation
[sxemacs] / 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 SXEmacs
6
7 SXEmacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 SXEmacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
19
20
21 /* Synched up with: Not in FSF. */
22
23 #ifndef INCLUDED_lrecord_h_
24 #define INCLUDED_lrecord_h_
25
26 /* The "lrecord" type of Lisp object is used for all object types
27    other than a few simple ones.  This allows many types to be
28    implemented but only a few bits required in a Lisp object for type
29    information. (The tradeoff is that each object has its type marked
30    in it, thereby increasing its size.) All lrecords begin with a
31    `struct lrecord_header', which identifies the lisp object type, by
32    providing an index into a table of `struct lrecord_implementation',
33    which describes the behavior of the lisp object.  It also contains
34    some other data bits.
35
36    Lrecords are of two types: straight lrecords, and lcrecords.
37    Straight lrecords are used for those types of objects that have
38    their own allocation routines (typically allocated out of 2K chunks
39    of memory called `frob blocks').  These objects have a `struct
40    lrecord_header' at the top, containing only the bits needed to find
41    the lrecord_implementation for the object.  There are special
42    routines in alloc.c to deal with each such object type.
43
44    Lcrecords are used for less common sorts of objects that don't do
45    their own allocation.  Each such object is malloc()ed individually,
46    and the objects are chained together through a `next' pointer.
47    Lcrecords have a `struct lcrecord_header' at the top, which
48    contains a `struct lrecord_header' and a `next' pointer, and are
49    allocated using alloc_lcrecord().
50
51    Creating a new lcrecord type is fairly easy; just follow the
52    lead of some existing type (e.g. hash tables).  Note that you
53    do not need to supply all the methods (see below); reasonable
54    defaults are provided for many of them.  Alternatively, if you're
55    just looking for a way of encapsulating data (which possibly
56    could contain Lisp_Objects in it), you may well be able to use
57    the opaque type. */
58
59 struct lrecord_header {
60         /* index into lrecord_implementations_table[] */
61         unsigned int type:8;
62
63         /* If `mark' is 0 after the GC mark phase, the object will be freed
64            during the GC sweep phase.  There are 2 ways that `mark' can be 1:
65            - by being referenced from other objects during the GC mark phase
66            - because it is permanently on, for c_readonly objects */
67         unsigned int mark:1;
68
69         /* 1 if the object resides in logically read-only space, and does not
70            reference other non-c_readonly objects.
71            Invariant: if (c_readonly == 1), then (mark == 1 && lisp_readonly == 1) */
72         unsigned int c_readonly:1;
73
74         /* 1 if the object is readonly from lisp */
75         unsigned int lisp_readonly:1;
76
77         unsigned int morphisms:/*number_of_cat_morphism_kinds => */6;
78
79         long unsigned int hash_cache;
80 };
81
82 struct lrecord_implementation;
83 int lrecord_type_index(const struct lrecord_implementation *implementation);
84
85 #define set_lheader_implementation(header,imp)                  \
86         do {                                                    \
87                 struct lrecord_header* SLI_header = (header);   \
88                 SLI_header->type = (imp)->lrecord_type_index;   \
89                 SLI_header->mark = 0;                           \
90                 SLI_header->c_readonly = 0;                     \
91                 SLI_header->lisp_readonly = 0;                  \
92                 SLI_header->morphisms = 0;                      \
93                 SLI_header->hash_cache = 0;                     \
94         } while (0)
95
96 struct lcrecord_header {
97         struct lrecord_header lheader;
98
99 #if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
100         /* The `next' field is normally used to chain all lcrecords together
101            so that the GC can find (and free) all of them.
102            `alloc_lcrecord' threads lcrecords together.
103
104            The `next' field may be used for other purposes as long as some
105            other mechanism is provided for letting the GC do its work.
106
107            For example, the event and marker object types allocate members
108            out of memory chunks, and are able to find all unmarked members
109            by sweeping through the elements of the list of chunks.  */
110         struct lcrecord_header *next;
111 #endif  /* !BDWGC */
112
113         /* The `uid' field is just for debugging/printing convenience.
114            Having this slot doesn't hurt us much spacewise, since an
115            lcrecord already has the above slots plus malloc overhead. */
116         unsigned int uid:31;
117
118         /* The `free' field is a flag that indicates whether this lcrecord
119            is on a "free list".  Free lists are used to minimize the number
120            of calls to malloc() when we're repeatedly allocating and freeing
121            a number of the same sort of lcrecord.  Lcrecords on a free list
122            always get marked in a different fashion, so we can use this flag
123            as a sanity check to make sure that free lists only have freed
124            lcrecords and there are no freed lcrecords elsewhere. */
125         unsigned int free:1;
126 };
127
128 /* Used for lcrecords in an lcrecord-list. */
129 struct free_lcrecord_header {
130         struct lcrecord_header lcheader;
131         Lisp_Object chain;
132 };
133
134 enum lrecord_type {
135         /* Symbol value magic types come first to make SYMBOL_VALUE_MAGIC_P fast.
136            #### This should be replaced by a symbol_value_magic_p flag
137            in the Lisp_Symbol lrecord_header. */
138         lrecord_type_symbol_value_forward, /* 0 */
139         lrecord_type_symbol_value_varalias,
140         lrecord_type_symbol_value_lisp_magic,
141         lrecord_type_symbol_value_buffer_local,
142         lrecord_type_max_symbol_value_magic = /* 3 */
143             lrecord_type_symbol_value_buffer_local,
144
145         lrecord_type_symbol,    /* 4 */
146         lrecord_type_subr,
147         lrecord_type_cons,
148         lrecord_type_vector,
149         lrecord_type_string,
150         lrecord_type_lcrecord_list,
151         lrecord_type_compiled_function, /* 10 */
152         lrecord_type_weak_list,
153         lrecord_type_bit_vector,
154         lrecord_type_hash_table,
155         lrecord_type_lstream,
156         lrecord_type_process,
157         lrecord_type_charset,
158         lrecord_type_coding_system,
159         lrecord_type_char_table,
160         lrecord_type_char_table_entry,
161         lrecord_type_range_table, /* 20 */
162         lrecord_type_opaque,
163         lrecord_type_opaque_ptr,
164         lrecord_type_dynacat,
165         lrecord_type_buffer,
166         lrecord_type_extent,
167         lrecord_type_extent_info,
168         lrecord_type_extent_auxiliary,
169         lrecord_type_event,
170         lrecord_type_event_queue,
171         lrecord_type_event_prique, /* 30 */
172         lrecord_type_worker_job,
173         lrecord_type_keymap,
174         lrecord_type_command_builder,
175         lrecord_type_timeout,
176         lrecord_type_specifier,
177         lrecord_type_console,
178         lrecord_type_device,
179         lrecord_type_frame,
180         lrecord_type_window,
181         lrecord_type_window_configuration, /* 40 */
182         lrecord_type_gui_item,
183         lrecord_type_popup_data,
184         lrecord_type_toolbar_button,
185         lrecord_type_color_instance,
186         lrecord_type_font_instance,
187         lrecord_type_image_instance,
188         lrecord_type_glyph,
189         lrecord_type_face,
190         lrecord_type_database,
191         lrecord_type_ldap,
192         lrecord_type_pgconn,
193         lrecord_type_pgresult,
194         lrecord_type_devmode,
195         lrecord_type_case_table,
196         lrecord_type_emacs_ffi,
197         lrecord_type_ffiobject, /* 60 */
198         lrecord_type_evp_pkey,
199         lrecord_type_ssl_conn,
200         lrecord_type_dllist,
201         lrecord_type_skiplist,
202         lrecord_type_bloom,
203         lrecord_type_audio_device,
204         lrecord_type_media_stream,
205         lrecord_type_media_thread,
206         /* ent types */
207         lrecord_type_marker,
208         lrecord_first_ent_type = lrecord_type_marker,
209         lrecord_type_float,     /* 70 */
210         lrecord_type_bigz,
211         lrecord_type_bigq,
212         lrecord_type_bigf,
213         lrecord_type_bigfr,
214         lrecord_type_bigc,
215         lrecord_type_bigg,
216         lrecord_type_quatern,
217         lrecord_type_indef,     /* 78 */
218         lrecord_type_free,      /* only used for "free" lrecords */
219         lrecord_type_undefined, /* only used for debugging */
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         const char *name;
227
228         /* `marker' is called at GC time, to make sure that all Lisp_Objects
229            pointed to by this object get properly marked.  It should call
230            the mark_object function on all Lisp_Objects in the object.  If
231            the return value is non-nil, it should be a Lisp_Object to be
232            marked (don't call the mark_object function explicitly on it,
233            because the GC routines will do this).  Doing it this way reduces
234            recursion, so the object returned should preferably be the one
235            with the deepest level of Lisp_Object pointers.  This function
236            can be NULL, meaning no GC marking is necessary. */
237          Lisp_Object(*marker) (Lisp_Object);
238
239         /* `printer' converts the object to a printed representation.
240            This can be NULL; in this case default_object_printer() will be
241            used instead. */
242         void (*printer) (Lisp_Object, Lisp_Object printcharfun, int escapeflag);
243
244         /* `finalizer' is called at GC time when the object is about to
245            be freed, and at dump time (FOR_DISKSAVE will be non-zero in this
246            case).  It should perform any necessary cleanup (e.g. freeing
247            malloc()ed memory).  This can be NULL, meaning no special
248            finalization is necessary.
249
250            WARNING: remember that `finalizer' is called at dump time even
251            though the object is not being freed. */
252         void (*finalizer) (void *header, int for_disksave);
253
254         /* This can be NULL, meaning compare objects with EQ(). */
255         int (*equal) (Lisp_Object obj1, Lisp_Object obj2, int depth);
256
257         /* `hash' generates hash values for use with hash tables that have
258            `equal' as their test function.  This can be NULL, meaning use
259            the Lisp_Object itself as the hash.  But, you must still satisfy
260            the constraint that if two objects are `equal', then they *must*
261            hash to the same value in order for hash tables to work properly.
262            This means that `hash' can be NULL only if the `equal' method is
263            also NULL. */
264         long unsigned int (*hash) (Lisp_Object, int);
265
266         /* External data layout description */
267         const struct lrecord_description *description;
268
269         /* These functions allow any object type to have builtin property
270            lists that can be manipulated from the lisp level with
271            `get', `put', `remprop', and `object-plist'. */
272         Lisp_Object(*getprop) (Lisp_Object obj, Lisp_Object prop);
273         int (*putprop) (Lisp_Object obj, Lisp_Object prop, Lisp_Object val);
274         int (*remprop) (Lisp_Object obj, Lisp_Object prop);
275         Lisp_Object(*plist) (Lisp_Object obj);
276
277         /* Only one of `static_size' and `size_in_bytes_method' is non-0.
278            If both are 0, this type is not instantiable by alloc_lcrecord(). */
279         size_t static_size;
280         size_t(*size_in_bytes_method) (const void *header);
281
282         /* The (constant) index into lrecord_implementations_table */
283         enum lrecord_type lrecord_type_index;
284
285         /* A "basic" lrecord is any lrecord that's not an lcrecord, i.e.
286            one that does not have an lcrecord_header at the front and which
287            is (usually) allocated in frob blocks.  We only use this flag for
288            some consistency checking, and that only when error-checking is
289            enabled. */
290         bool basic_p:1;
291 };
292
293 /* All the built-in lisp object types are enumerated in `enum record_type'.
294    Additional ones may be defined by a module (none yet).  We leave some
295    room in `lrecord_implementations_table' for such new lisp object types. */
296 #define MODULE_DEFINABLE_TYPE_COUNT 32
297
298 extern const struct lrecord_implementation
299 *lrecord_implementations_table[(unsigned int)lrecord_type_last_built_in_type
300                                + MODULE_DEFINABLE_TYPE_COUNT];
301
302 #define XRECORD_LHEADER_IMPLEMENTATION(obj) \
303         LHEADER_IMPLEMENTATION (XRECORD_LHEADER (obj))
304 #define LHEADER_IMPLEMENTATION(lh) lrecord_implementations_table[(lh)->type]
305
306 extern int gc_in_progress;
307
308 #define MARKED_RECORD_P(obj) (XRECORD_LHEADER (obj)->mark)
309 #define MARKED_RECORD_HEADER_P(lheader) ((lheader)->mark)
310 #define MARK_RECORD_HEADER(lheader)   ((void) ((lheader)->mark = 1))
311 #define UNMARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 0))
312
313 #define C_READONLY_RECORD_HEADER_P(lheader)  ((lheader)->c_readonly)
314 #define LISP_READONLY_RECORD_HEADER_P(lheader)  ((lheader)->lisp_readonly)
315 #define SET_C_READONLY_RECORD_HEADER(lheader)                           \
316         do {                                                            \
317                 struct lrecord_header *SCRRH_lheader = (lheader);       \
318                 SCRRH_lheader->c_readonly = 1;                          \
319                 SCRRH_lheader->lisp_readonly = 1;                       \
320                 SCRRH_lheader->mark = 1;                                \
321         } while (0)
322 #define SET_LISP_READONLY_RECORD_HEADER(lheader) \
323         ((void) ((lheader)->lisp_readonly = 1))
324 #define RECORD_MARKER(lheader) lrecord_markers[(lheader)->type]
325
326 /* External description stuff
327
328    A lrecord external description  is an array  of values.  The  first
329    value of each line is a type, the second  the offset in the lrecord
330    structure.  Following values  are parameters, their  presence, type
331    and number is type-dependent.
332
333    The description ends with a "XD_END" or "XD_SPECIFIER_END" record.
334
335    Some example descriptions :
336
337    static const struct lrecord_description cons_description[] = {
338      { XD_LISP_OBJECT, offsetof (Lisp_Cons, car) },
339      { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr) },
340      { XD_END }
341    };
342
343    Which means "two lisp objects starting at the 'car' and 'cdr' elements"
344
345   static const struct lrecord_description string_description[] = {
346     { XD_BYTECOUNT,       offsetof (Lisp_String, size) },
347     { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
348     { XD_LISP_OBJECT,     offsetof (Lisp_String, plist) },
349     { XD_END }
350   };
351   "A pointer to string data at 'data', the size of the pointed array being the value
352    of the size variable plus 1, and one lisp object at 'plist'"
353
354   The existing types :
355     XD_LISP_OBJECT
356   A Lisp object.  This is also the type to use for pointers to other lrecords.
357
358     XD_LISP_OBJECT_ARRAY
359   An array of Lisp objects or pointers to lrecords.
360   The third element is the count.
361
362     XD_LO_LINK
363   Link in a linked list of objects of the same type.
364
365     XD_OPAQUE_PTR
366   Pointer to undumpable data.  Must be NULL when dumping.
367
368     XD_STRUCT_PTR
369   Pointer to described struct.  Parameters are number of structures and
370   struct_description.
371
372     XD_OPAQUE_DATA_PTR
373   Pointer to dumpable opaque data.  Parameter is the size of the data.
374   Pointed data must be relocatable without changes.
375
376     XD_C_STRING
377   Pointer to a C string.
378
379     XD_DOC_STRING
380   Pointer to a doc string (C string if positive, opaque value if negative)
381
382     XD_INT_RESET
383   An integer which will be reset to a given value in the dump file.
384
385     XD_SIZE_T
386   size_t value.  Used for counts.
387
388     XD_INT
389   int value.  Used for counts.
390
391     XD_LONG
392   long value.  Used for counts.
393
394     XD_BYTECOUNT
395   bytecount value.  Used for counts.
396
397     XD_END
398   Special type indicating the end of the array.
399
400     XD_SPECIFIER_END
401   Special type indicating the end of the array for a specifier.  Extra
402   description is going to be fetched from the specifier methods.
403
404   Special macros:
405     XD_INDIRECT(line, delta)
406   Usable where  a "count" or "size"  is requested.  Gives the value of
407   the element which is at line number 'line' in the description (count
408   starts at zero) and adds delta to it.
409 */
410
411 enum lrecord_description_type {
412         XD_LISP_OBJECT_ARRAY,
413         XD_LISP_OBJECT,
414         XD_LO_LINK,
415         XD_OPAQUE_PTR,
416         XD_STRUCT_PTR,
417         XD_OPAQUE_DATA_PTR,
418         XD_C_STRING,
419         XD_DOC_STRING,
420         XD_INT_RESET,
421         XD_SIZE_T,
422         XD_INT,
423         XD_LONG,
424         XD_BYTECOUNT,
425         XD_END,
426         XD_SPECIFIER_END
427 };
428
429 struct lrecord_description {
430         enum lrecord_description_type type;
431         int offset;
432         EMACS_INT data1;
433         const struct struct_description *data2;
434 };
435
436 struct struct_description {
437         size_t size;
438         const struct lrecord_description *description;
439 };
440
441 #define XD_INDIRECT(val, delta) (-1-((val)|(delta<<8)))
442
443 #define XD_IS_INDIRECT(code) (code<0)
444 #define XD_INDIRECT_VAL(code) ((-1-code) & 255)
445 #define XD_INDIRECT_DELTA(code) (((-1-code)>>8) & 255)
446
447 #define XD_DYNARR_DESC(base_type, sub_desc) \
448   { XD_STRUCT_PTR, offsetof (base_type, base), XD_INDIRECT(1, 0), sub_desc }, \
449   { XD_INT,        offsetof (base_type, cur) }, \
450   { XD_INT_RESET,  offsetof (base_type, max), XD_INDIRECT(1, 0) }
451
452 /* DEFINE_LRECORD_IMPLEMENTATION is for objects with constant size.
453    DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION is for objects whose size varies.
454  */
455
456 #if defined (ERROR_CHECK_TYPECHECK)
457 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
458 #else
459 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
460 #endif
461
462 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
463 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
464
465 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
466 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof(structtype),0,1,structtype)
467
468 #define DEFINE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
469 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
470
471 #define DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
472 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
473
474 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
475 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
476
477 #define DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
478 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,0,sizer,1,structtype)
479
480 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
481 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype) \
482
483 #define MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
484 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)                       \
485 const struct lrecord_implementation lrecord_##c_name =                  \
486   { name, marker, printer, nuker, equal, hash, desc,                    \
487     getprop, putprop, remprop, plist, size, sizer,                      \
488     lrecord_type_##c_name, basic_p }
489
490 #define DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
491 DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
492
493 #define DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
494 MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
495
496 #define DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
497 DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
498
499 #define DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
500 MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype)
501
502 #define MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
503 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)                       \
504 unsigned int lrecord_type_##c_name;                                     \
505 struct lrecord_implementation lrecord_##c_name =                        \
506   { name, marker, printer, nuker, equal, hash, desc,                    \
507     getprop, putprop, remprop, plist, size, sizer,                      \
508     lrecord_type_last_built_in_type, basic_p }
509
510 extern Lisp_Object(*lrecord_markers[]) (Lisp_Object);
511
512 #define INIT_LRECORD_IMPLEMENTATION(type) do {                          \
513   lrecord_implementations_table[lrecord_type_##type] = &lrecord_##type; \
514   lrecord_markers[lrecord_type_##type] =                                \
515     lrecord_implementations_table[lrecord_type_##type]->marker;         \
516 } while (0)
517
518 #define INIT_EXTERNAL_LRECORD_IMPLEMENTATION(type) do {                 \
519   lrecord_type_##type = lrecord_type_count++;                           \
520   lrecord_##type.lrecord_type_index = (enum lrecord_type) lrecord_type_##type; \
521   INIT_LRECORD_IMPLEMENTATION(type);                                    \
522 } while (0)
523
524 #define LRECORDP(a) (XTYPE (a) == Lisp_Type_Record)
525 #define XRECORD_LHEADER(a) ((struct lrecord_header *) XPNTR (a))
526
527 #define RECORD_TYPEP(x, ty) \
528   (LRECORDP (x) && (((unsigned int)(XRECORD_LHEADER (x)->type)) == ((unsigned int)(ty))))
529
530 #if defined(EF_USE_POM)
531 #define XRECORD_MTX(x)          (XRECORD_LHEADER(x)->objmtx)
532 #define XLOCKRECORD(x)                          \
533         SXE_MUTEX_LOCK(&(XRECORD_MTX(x)))
534 #define XUNLOCKRECORD(x)                        \
535         SXE_MUTEX_UNLOCK(&(XRECORD_MTX(x)))
536 #else
537 #define XRECORD_MTX(x)
538 #define XLOCKRECORD(x)
539 #define XUNLOCKRECORD(x)
540 #endif
541
542 /* Steps to create a new object:
543
544    1. Declare the struct for your object in a header file somewhere.
545    Remember that it must begin with
546
547    struct lcrecord_header header;
548
549    2. Put a DECLARE_LRECORD() for the object below the struct definition,
550    along with the standard XFOO/XSETFOO junk.
551
552    3. Add this header file to inline.c.
553
554    4. Create the methods for your object.  Note that technically you don't
555    need any, but you will almost always want at least a mark method.
556
557    5. Define your object with DEFINE_LRECORD_IMPLEMENTATION() or some
558    variant.
559
560    6. Include the header file in the .c file where you defined the object.
561
562    7. Put a call to INIT_LRECORD_IMPLEMENTATION() for the object in the
563    .c file's syms_of_foo() function.
564
565    8. Add a type enum for the object to enum lrecord_type, earlier in this
566    file.
567
568 An example:
569
570 ------------------------------ in toolbar.h -----------------------------
571
572 struct toolbar_button
573 {
574   struct lcrecord_header header;
575
576   Lisp_Object next;
577   Lisp_Object frame;
578
579   Lisp_Object up_glyph;
580   Lisp_Object down_glyph;
581   Lisp_Object disabled_glyph;
582
583   Lisp_Object cap_up_glyph;
584   Lisp_Object cap_down_glyph;
585   Lisp_Object cap_disabled_glyph;
586
587   Lisp_Object callback;
588   Lisp_Object enabled_p;
589   Lisp_Object help_string;
590
591   char enabled;
592   char down;
593   char pushright;
594   char blank;
595
596   int x, y;
597   int width, height;
598   int dirty;
599   int vertical;
600   int border_width;
601 };
602
603 DECLARE_LRECORD (toolbar_button, struct toolbar_button);
604 #define XTOOLBAR_BUTTON(x) XRECORD (x, toolbar_button, struct toolbar_button)
605 #define XSETTOOLBAR_BUTTON(x, p) XSETRECORD (x, p, toolbar_button)
606 #define TOOLBAR_BUTTONP(x) RECORDP (x, toolbar_button)
607 #define CHECK_TOOLBAR_BUTTON(x) CHECK_RECORD (x, toolbar_button)
608 #define CONCHECK_TOOLBAR_BUTTON(x) CONCHECK_RECORD (x, toolbar_button)
609
610 ------------------------------ in toolbar.c -----------------------------
611
612 #include "ui/toolbar.h"
613
614 ...
615
616 static Lisp_Object
617 mark_toolbar_button (Lisp_Object obj)
618 {
619   struct toolbar_button *data = XTOOLBAR_BUTTON (obj);
620   mark_object (data->next);
621   mark_object (data->frame);
622   mark_object (data->up_glyph);
623   mark_object (data->down_glyph);
624   mark_object (data->disabled_glyph);
625   mark_object (data->cap_up_glyph);
626   mark_object (data->cap_down_glyph);
627   mark_object (data->cap_disabled_glyph);
628   mark_object (data->callback);
629   mark_object (data->enabled_p);
630   return data->help_string;
631 }
632
633 DEFINE_LRECORD_IMPLEMENTATION ("toolbar-button", toolbar_button,
634                                mark_toolbar_button, 0, 0, 0, 0, 0,
635                                struct toolbar_button);
636
637 ...
638
639 void
640 syms_of_toolbar (void)
641 {
642   INIT_LRECORD_IMPLEMENTATION (toolbar_button);
643
644   ...;
645 }
646
647 ------------------------------ in inline.c -----------------------------
648
649 #ifdef HAVE_TOOLBARS
650 #include "ui/toolbar.h"
651 #endif
652
653 ------------------------------ in lrecord.h -----------------------------
654
655 enum lrecord_type
656 {
657   ...
658   lrecord_type_toolbar_button,
659   ...
660 };
661
662 */
663
664 /*
665
666 Note: Object types defined in external dynamically-loaded modules (not
667 part of the XEmacs main source code) should use DECLARE_EXTERNAL_LRECORD
668 and DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION rather than DECLARE_LRECORD
669 and DEFINE_LRECORD_IMPLEMENTATION.
670
671 */
672
673 #ifdef ERROR_CHECK_TYPECHECK
674
675 # define DECLARE_LRECORD(c_name, structtype)                    \
676 extern const struct lrecord_implementation lrecord_##c_name;    \
677 extern_inline structtype *                                      \
678 error_check_##c_name (Lisp_Object obj);                         \
679 extern_inline structtype *                                      \
680 error_check_##c_name (Lisp_Object obj)                          \
681 {                                                               \
682   assert (RECORD_TYPEP (obj, lrecord_type_##c_name));           \
683   return (structtype *) XPNTR (obj);                            \
684 }                                                               \
685 extern Lisp_Object Q##c_name##p
686
687 # define DECLARE_EXTERNAL_LRECORD(c_name, structtype)           \
688 extern unsigned int lrecord_type_##c_name;                      \
689 extern struct lrecord_implementation lrecord_##c_name;          \
690 extern_inline structtype *                                      \
691 error_check_##c_name (Lisp_Object obj);                         \
692 extern_inline structtype *                                      \
693 error_check_##c_name (Lisp_Object obj)                          \
694 {                                                               \
695   assert (RECORD_TYPEP (obj, lrecord_type_##c_name));           \
696   return (structtype *) XPNTR (obj);                            \
697 }                                                               \
698 extern Lisp_Object Q##c_name##p
699
700 # define DECLARE_NONRECORD(c_name, type_enum, structtype)       \
701 extern_inline structtype *                                      \
702 error_check_##c_name (Lisp_Object obj);                         \
703 extern_inline structtype *                                      \
704 error_check_##c_name (Lisp_Object obj)                          \
705 {                                                               \
706   assert (XTYPE (obj) == type_enum);                            \
707   return (structtype *) XPNTR (obj);                            \
708 }                                                               \
709 extern Lisp_Object Q##c_name##p
710
711 # define XRECORD(x, c_name, structtype) error_check_##c_name (x)
712 # define XNONRECORD(x, c_name, type_enum, structtype) error_check_##c_name (x)
713
714 # define XSETRECORD(var, p, c_name) do                          \
715 {                                                               \
716   XSETOBJ (var, p);                                             \
717   assert (RECORD_TYPEP (var, lrecord_type_##c_name));           \
718 } while (0)
719
720 #else                           /* not ERROR_CHECK_TYPECHECK */
721
722 # define DECLARE_LRECORD(c_name, structtype)                    \
723 extern Lisp_Object Q##c_name##p;                                \
724 extern const struct lrecord_implementation lrecord_##c_name
725 # define DECLARE_EXTERNAL_LRECORD(c_name, structtype)           \
726 extern Lisp_Object Q##c_name##p;                                \
727 extern unsigned int lrecord_type_##c_name;                      \
728 extern struct lrecord_implementation lrecord_##c_name
729 # define DECLARE_NONRECORD(c_name, type_enum, structtype)       \
730 extern Lisp_Object Q##c_name##p
731 # define XRECORD(x, c_name, structtype) ((structtype *) XPNTR (x))
732 # define XNONRECORD(x, c_name, type_enum, structtype)           \
733   ((structtype *) XPNTR (x))
734 # define XSETRECORD(var, p, c_name) XSETOBJ (var, p)
735
736 #endif                          /* not ERROR_CHECK_TYPECHECK */
737
738 #define RECORDP(x, c_name) RECORD_TYPEP (x, lrecord_type_##c_name)
739
740 /* Note: we now have two different kinds of type-checking macros.
741    The "old" kind has now been renamed CONCHECK_foo.  The reason for
742    this is that the CONCHECK_foo macros signal a continuable error,
743    allowing the user (through debug-on-error) to substitute a different
744    value and return from the signal, which causes the lvalue argument
745    to get changed.  Quite a lot of code would crash if that happened,
746    because it did things like
747
748    foo = XCAR (list);
749    CHECK_STRING (foo);
750
751    and later on did XSTRING (XCAR (list)), assuming that the type
752    is correct (when it might be wrong, if the user substituted a
753    correct value in the debugger).
754
755    To get around this, I made all the CHECK_foo macros signal a
756    non-continuable error.  Places where a continuable error is OK
757    (generally only when called directly on the argument of a Lisp
758    primitive) should be changed to use CONCHECK().
759
760    FSF Emacs does not have this problem because RMS took the cheesy
761    way out and disabled returning from a signal entirely. */
762
763 #define CONCHECK_RECORD(x, c_name) do {                 \
764  if (!RECORD_TYPEP (x, lrecord_type_##c_name))          \
765    x = wrong_type_argument (Q##c_name##p, x);           \
766 }  while (0)
767 #define CONCHECK_NONRECORD(x, lisp_enum, predicate) do {\
768  if (XTYPE (x) != lisp_enum)                            \
769    x = wrong_type_argument (predicate, x);              \
770  } while (0)
771 #define CHECK_RECORD(x, c_name) do {                    \
772  if (!RECORD_TYPEP (x, lrecord_type_##c_name))          \
773    dead_wrong_type_argument (Q##c_name##p, x);          \
774  } while (0)
775 #define CHECK_NONRECORD(x, lisp_enum, predicate) do {   \
776  if (XTYPE (x) != lisp_enum)                            \
777    dead_wrong_type_argument (predicate, x);             \
778  } while (0)
779
780 void *alloc_lcrecord(size_t size, const struct lrecord_implementation *);
781
782 #define alloc_lcrecord_type(type, lrecord_implementation) \
783   ((type *) alloc_lcrecord (sizeof (type), lrecord_implementation))
784
785 /* Copy the data from one lcrecord structure into another, but don't
786    overwrite the header information. */
787
788 #define copy_lcrecord(dst, src)                                 \
789   memcpy ((char *) (dst) + sizeof (struct lcrecord_header),     \
790           (char *) (src) + sizeof (struct lcrecord_header),     \
791           sizeof (*(dst)) - sizeof (struct lcrecord_header))
792
793 #define zero_lcrecord(lcr)                                      \
794    memset ((char *) (lcr) + sizeof (struct lcrecord_header), 0, \
795            sizeof (*(lcr)) - sizeof (struct lcrecord_header))
796
797 #endif                          /* INCLUDED_lrecord_h_ */