GTK eradication -- the build chain.
[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         long unsigned int hash_cache;
78         unsigned int morphisms:/*number_of_cat_morphism_kinds => */6;
79 };
80
81 struct lrecord_implementation;
82 int lrecord_type_index(const struct lrecord_implementation *implementation);
83
84 #define set_lheader_implementation(header,imp)                  \
85         do {                                                    \
86                 struct lrecord_header* SLI_header = (header);   \
87                 SLI_header->type = (imp)->lrecord_type_index;   \
88                 SLI_header->mark = 0;                           \
89                 SLI_header->c_readonly = 0;                     \
90                 SLI_header->lisp_readonly = 0;                  \
91         } while (0)
92
93 struct lcrecord_header {
94         struct lrecord_header lheader;
95
96 #if !defined HAVE_BDWGC || !defined EF_USE_BDWGC
97         /* The `next' field is normally used to chain all lcrecords together
98            so that the GC can find (and free) all of them.
99            `alloc_lcrecord' threads lcrecords together.
100
101            The `next' field may be used for other purposes as long as some
102            other mechanism is provided for letting the GC do its work.
103
104            For example, the event and marker object types allocate members
105            out of memory chunks, and are able to find all unmarked members
106            by sweeping through the elements of the list of chunks.  */
107         struct lcrecord_header *next;
108 #endif  /* !BDWGC */
109
110         /* The `uid' field is just for debugging/printing convenience.
111            Having this slot doesn't hurt us much spacewise, since an
112            lcrecord already has the above slots plus malloc overhead. */
113         unsigned int uid:31;
114
115         /* The `free' field is a flag that indicates whether this lcrecord
116            is on a "free list".  Free lists are used to minimize the number
117            of calls to malloc() when we're repeatedly allocating and freeing
118            a number of the same sort of lcrecord.  Lcrecords on a free list
119            always get marked in a different fashion, so we can use this flag
120            as a sanity check to make sure that free lists only have freed
121            lcrecords and there are no freed lcrecords elsewhere. */
122         unsigned int free:1;
123 };
124
125 /* Used for lcrecords in an lcrecord-list. */
126 struct free_lcrecord_header {
127         struct lcrecord_header lcheader;
128         Lisp_Object chain;
129 };
130
131 enum lrecord_type {
132         /* Symbol value magic types come first to make SYMBOL_VALUE_MAGIC_P fast.
133            #### This should be replaced by a symbol_value_magic_p flag
134            in the Lisp_Symbol lrecord_header. */
135         lrecord_type_symbol_value_forward, /* 0 */
136         lrecord_type_symbol_value_varalias,
137         lrecord_type_symbol_value_lisp_magic,
138         lrecord_type_symbol_value_buffer_local,
139         lrecord_type_max_symbol_value_magic = /* 3 */
140             lrecord_type_symbol_value_buffer_local,
141
142         lrecord_type_symbol,    /* 4 */
143         lrecord_type_subr,
144         lrecord_type_cons,
145         lrecord_type_vector,
146         lrecord_type_string,
147         lrecord_type_lcrecord_list,
148         lrecord_type_compiled_function, /* 10 */
149         lrecord_type_weak_list,
150         lrecord_type_bit_vector,
151         lrecord_type_hash_table,
152         lrecord_type_lstream,
153         lrecord_type_process,
154         lrecord_type_charset,
155         lrecord_type_coding_system,
156         lrecord_type_char_table,
157         lrecord_type_char_table_entry,
158         lrecord_type_range_table, /* 20 */
159         lrecord_type_opaque,
160         lrecord_type_opaque_ptr,
161         lrecord_type_dynacat,
162         lrecord_type_buffer,
163         lrecord_type_extent,
164         lrecord_type_extent_info,
165         lrecord_type_extent_auxiliary,
166         lrecord_type_event,
167         lrecord_type_event_queue,
168         lrecord_type_event_prique, /* 30 */
169         lrecord_type_worker_job,
170         lrecord_type_keymap,
171         lrecord_type_command_builder,
172         lrecord_type_timeout,
173         lrecord_type_specifier,
174         lrecord_type_console,
175         lrecord_type_device,
176         lrecord_type_frame,
177         lrecord_type_window,
178         lrecord_type_window_configuration, /* 40 */
179         lrecord_type_gui_item,
180         lrecord_type_popup_data,
181         lrecord_type_toolbar_button,
182         lrecord_type_color_instance,
183         lrecord_type_font_instance,
184         lrecord_type_image_instance,
185         lrecord_type_glyph,
186         lrecord_type_face,
187         lrecord_type_database,
188         lrecord_type_ldap,
189         lrecord_type_pgconn,
190         lrecord_type_pgresult,
191         lrecord_type_devmode,
192         lrecord_type_case_table,
193         lrecord_type_emacs_ffi,
194         lrecord_type_emacs_gtk_object,
195         lrecord_type_emacs_gtk_boxed,
196         lrecord_type_ffiobject, /* 60 */
197         lrecord_type_evp_pkey,
198         lrecord_type_ssl_conn,
199         lrecord_type_dllist,
200         lrecord_type_skiplist,
201         lrecord_type_bloom,
202         lrecord_type_audio_device,
203         lrecord_type_media_stream,
204         lrecord_type_media_thread,
205         /* ent types */
206         lrecord_type_marker,
207         lrecord_first_ent_type = lrecord_type_marker,
208         lrecord_type_float,     /* 70 */
209         lrecord_type_bigz,
210         lrecord_type_bigq,
211         lrecord_type_bigf,
212         lrecord_type_bigfr,
213         lrecord_type_bigc,
214         lrecord_type_bigg,
215         lrecord_type_quatern,
216         lrecord_type_indef,     /* 78 */
217         lrecord_type_free,      /* only used for "free" lrecords */
218         lrecord_type_undefined, /* only used for debugging */
219         lrecord_type_last_built_in_type /* must be last */
220 };
221
222 extern unsigned int lrecord_type_count;
223
224 struct lrecord_implementation {
225         const char *name;
226
227         /* `marker' is called at GC time, to make sure that all Lisp_Objects
228            pointed to by this object get properly marked.  It should call
229            the mark_object function on all Lisp_Objects in the object.  If
230            the return value is non-nil, it should be a Lisp_Object to be
231            marked (don't call the mark_object function explicitly on it,
232            because the GC routines will do this).  Doing it this way reduces
233            recursion, so the object returned should preferably be the one
234            with the deepest level of Lisp_Object pointers.  This function
235            can be NULL, meaning no GC marking is necessary. */
236          Lisp_Object(*marker) (Lisp_Object);
237
238         /* `printer' converts the object to a printed representation.
239            This can be NULL; in this case default_object_printer() will be
240            used instead. */
241         void (*printer) (Lisp_Object, Lisp_Object printcharfun, int escapeflag);
242
243         /* `finalizer' is called at GC time when the object is about to
244            be freed, and at dump time (FOR_DISKSAVE will be non-zero in this
245            case).  It should perform any necessary cleanup (e.g. freeing
246            malloc()ed memory).  This can be NULL, meaning no special
247            finalization is necessary.
248
249            WARNING: remember that `finalizer' is called at dump time even
250            though the object is not being freed. */
251         void (*finalizer) (void *header, int for_disksave);
252
253         /* This can be NULL, meaning compare objects with EQ(). */
254         int (*equal) (Lisp_Object obj1, Lisp_Object obj2, int depth);
255
256         /* `hash' generates hash values for use with hash tables that have
257            `equal' as their test function.  This can be NULL, meaning use
258            the Lisp_Object itself as the hash.  But, you must still satisfy
259            the constraint that if two objects are `equal', then they *must*
260            hash to the same value in order for hash tables to work properly.
261            This means that `hash' can be NULL only if the `equal' method is
262            also NULL. */
263         long unsigned int (*hash) (Lisp_Object, int);
264
265         /* External data layout description */
266         const struct lrecord_description *description;
267
268         /* These functions allow any object type to have builtin property
269            lists that can be manipulated from the lisp level with
270            `get', `put', `remprop', and `object-plist'. */
271         Lisp_Object(*getprop) (Lisp_Object obj, Lisp_Object prop);
272         int (*putprop) (Lisp_Object obj, Lisp_Object prop, Lisp_Object val);
273         int (*remprop) (Lisp_Object obj, Lisp_Object prop);
274         Lisp_Object(*plist) (Lisp_Object obj);
275
276         /* Only one of `static_size' and `size_in_bytes_method' is non-0.
277            If both are 0, this type is not instantiable by alloc_lcrecord(). */
278         size_t static_size;
279         size_t(*size_in_bytes_method) (const void *header);
280
281         /* The (constant) index into lrecord_implementations_table */
282         enum lrecord_type lrecord_type_index;
283
284         /* A "basic" lrecord is any lrecord that's not an lcrecord, i.e.
285            one that does not have an lcrecord_header at the front and which
286            is (usually) allocated in frob blocks.  We only use this flag for
287            some consistency checking, and that only when error-checking is
288            enabled. */
289         bool basic_p:1;
290 };
291
292 /* All the built-in lisp object types are enumerated in `enum record_type'.
293    Additional ones may be defined by a module (none yet).  We leave some
294    room in `lrecord_implementations_table' for such new lisp object types. */
295 #define MODULE_DEFINABLE_TYPE_COUNT 32
296
297 extern const struct lrecord_implementation
298 *lrecord_implementations_table[(unsigned int)lrecord_type_last_built_in_type
299                                + 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 C_READONLY_RECORD_HEADER_P(lheader)  ((lheader)->c_readonly)
313 #define LISP_READONLY_RECORD_HEADER_P(lheader)  ((lheader)->lisp_readonly)
314 #define SET_C_READONLY_RECORD_HEADER(lheader)                           \
315         do {                                                            \
316                 struct lrecord_header *SCRRH_lheader = (lheader);       \
317                 SCRRH_lheader->c_readonly = 1;                          \
318                 SCRRH_lheader->lisp_readonly = 1;                       \
319                 SCRRH_lheader->mark = 1;                                \
320         } while (0)
321 #define SET_LISP_READONLY_RECORD_HEADER(lheader) \
322         ((void) ((lheader)->lisp_readonly = 1))
323 #define RECORD_MARKER(lheader) lrecord_markers[(lheader)->type]
324
325 /* External description stuff
326
327    A lrecord external description  is an array  of values.  The  first
328    value of each line is a type, the second  the offset in the lrecord
329    structure.  Following values  are parameters, their  presence, type
330    and number is type-dependent.
331
332    The description ends with a "XD_END" or "XD_SPECIFIER_END" record.
333
334    Some example descriptions :
335
336    static const struct lrecord_description cons_description[] = {
337      { XD_LISP_OBJECT, offsetof (Lisp_Cons, car) },
338      { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr) },
339      { XD_END }
340    };
341
342    Which means "two lisp objects starting at the 'car' and 'cdr' elements"
343
344   static const struct lrecord_description string_description[] = {
345     { XD_BYTECOUNT,       offsetof (Lisp_String, size) },
346     { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
347     { XD_LISP_OBJECT,     offsetof (Lisp_String, plist) },
348     { XD_END }
349   };
350   "A pointer to string data at 'data', the size of the pointed array being the value
351    of the size variable plus 1, and one lisp object at 'plist'"
352
353   The existing types :
354     XD_LISP_OBJECT
355   A Lisp object.  This is also the type to use for pointers to other lrecords.
356
357     XD_LISP_OBJECT_ARRAY
358   An array of Lisp objects or pointers to lrecords.
359   The third element is the count.
360
361     XD_LO_LINK
362   Link in a linked list of objects of the same type.
363
364     XD_OPAQUE_PTR
365   Pointer to undumpable data.  Must be NULL when dumping.
366
367     XD_STRUCT_PTR
368   Pointer to described struct.  Parameters are number of structures and
369   struct_description.
370
371     XD_OPAQUE_DATA_PTR
372   Pointer to dumpable opaque data.  Parameter is the size of the data.
373   Pointed data must be relocatable without changes.
374
375     XD_C_STRING
376   Pointer to a C string.
377
378     XD_DOC_STRING
379   Pointer to a doc string (C string if positive, opaque value if negative)
380
381     XD_INT_RESET
382   An integer which will be reset to a given value in the dump file.
383
384     XD_SIZE_T
385   size_t value.  Used for counts.
386
387     XD_INT
388   int value.  Used for counts.
389
390     XD_LONG
391   long value.  Used for counts.
392
393     XD_BYTECOUNT
394   bytecount value.  Used for counts.
395
396     XD_END
397   Special type indicating the end of the array.
398
399     XD_SPECIFIER_END
400   Special type indicating the end of the array for a specifier.  Extra
401   description is going to be fetched from the specifier methods.
402
403   Special macros:
404     XD_INDIRECT(line, delta)
405   Usable where  a "count" or "size"  is requested.  Gives the value of
406   the element which is at line number 'line' in the description (count
407   starts at zero) and adds delta to it.
408 */
409
410 enum lrecord_description_type {
411         XD_LISP_OBJECT_ARRAY,
412         XD_LISP_OBJECT,
413         XD_LO_LINK,
414         XD_OPAQUE_PTR,
415         XD_STRUCT_PTR,
416         XD_OPAQUE_DATA_PTR,
417         XD_C_STRING,
418         XD_DOC_STRING,
419         XD_INT_RESET,
420         XD_SIZE_T,
421         XD_INT,
422         XD_LONG,
423         XD_BYTECOUNT,
424         XD_END,
425         XD_SPECIFIER_END
426 };
427
428 struct lrecord_description {
429         enum lrecord_description_type type;
430         int offset;
431         EMACS_INT data1;
432         const struct struct_description *data2;
433 };
434
435 struct struct_description {
436         size_t size;
437         const struct lrecord_description *description;
438 };
439
440 #define XD_INDIRECT(val, delta) (-1-((val)|(delta<<8)))
441
442 #define XD_IS_INDIRECT(code) (code<0)
443 #define XD_INDIRECT_VAL(code) ((-1-code) & 255)
444 #define XD_INDIRECT_DELTA(code) (((-1-code)>>8) & 255)
445
446 #define XD_DYNARR_DESC(base_type, sub_desc) \
447   { XD_STRUCT_PTR, offsetof (base_type, base), XD_INDIRECT(1, 0), sub_desc }, \
448   { XD_INT,        offsetof (base_type, cur) }, \
449   { XD_INT_RESET,  offsetof (base_type, max), XD_INDIRECT(1, 0) }
450
451 /* DEFINE_LRECORD_IMPLEMENTATION is for objects with constant size.
452    DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION is for objects whose size varies.
453  */
454
455 #if defined (ERROR_CHECK_TYPECHECK)
456 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
457 #else
458 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
459 #endif
460
461 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
462 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
463
464 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
465 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof(structtype),0,1,structtype)
466
467 #define DEFINE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
468 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
469
470 #define DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
471 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
472
473 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
474 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
475
476 #define DEFINE_BASIC_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
477 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,0,sizer,1,structtype)
478
479 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
480 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype) \
481
482 #define MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
483 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)                       \
484 const struct lrecord_implementation lrecord_##c_name =                  \
485   { name, marker, printer, nuker, equal, hash, desc,                    \
486     getprop, putprop, remprop, plist, size, sizer,                      \
487     lrecord_type_##c_name, basic_p }
488
489 #define DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
490 DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
491
492 #define DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,structtype) \
493 MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizeof (structtype),0,0,structtype)
494
495 #define DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
496 DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
497
498 #define DEFINE_EXTERNAL_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,sizer,structtype) \
499 MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,0,sizer,0,structtype)
500
501 #define MAKE_EXTERNAL_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,plist,size,sizer,basic_p,structtype) \
502 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)                       \
503 unsigned int lrecord_type_##c_name;                                     \
504 struct lrecord_implementation lrecord_##c_name =                        \
505   { name, marker, printer, nuker, equal, hash, desc,                    \
506     getprop, putprop, remprop, plist, size, sizer,                      \
507     lrecord_type_last_built_in_type, basic_p }
508
509 extern Lisp_Object(*lrecord_markers[]) (Lisp_Object);
510
511 #define INIT_LRECORD_IMPLEMENTATION(type) do {                          \
512   lrecord_implementations_table[lrecord_type_##type] = &lrecord_##type; \
513   lrecord_markers[lrecord_type_##type] =                                \
514     lrecord_implementations_table[lrecord_type_##type]->marker;         \
515 } while (0)
516
517 #define INIT_EXTERNAL_LRECORD_IMPLEMENTATION(type) do {                 \
518   lrecord_type_##type = lrecord_type_count++;                           \
519   lrecord_##type.lrecord_type_index = (enum lrecord_type) lrecord_type_##type; \
520   INIT_LRECORD_IMPLEMENTATION(type);                                    \
521 } while (0)
522
523 #define LRECORDP(a) (XTYPE (a) == Lisp_Type_Record)
524 #define XRECORD_LHEADER(a) ((struct lrecord_header *) XPNTR (a))
525
526 #define RECORD_TYPEP(x, ty) \
527   (LRECORDP (x) && (((unsigned int)(XRECORD_LHEADER (x)->type)) == ((unsigned int)(ty))))
528
529 #if defined(EF_USE_POM)
530 #define XRECORD_MTX(x)          (XRECORD_LHEADER(x)->objmtx)
531 #define XLOCKRECORD(x)                          \
532         SXE_MUTEX_LOCK(&(XRECORD_MTX(x)))
533 #define XUNLOCKRECORD(x)                        \
534         SXE_MUTEX_UNLOCK(&(XRECORD_MTX(x)))
535 #else
536 #define XRECORD_MTX(x)
537 #define XLOCKRECORD(x)
538 #define XUNLOCKRECORD(x)
539 #endif
540
541 /* Steps to create a new object:
542
543    1. Declare the struct for your object in a header file somewhere.
544    Remember that it must begin with
545
546    struct lcrecord_header header;
547
548    2. Put a DECLARE_LRECORD() for the object below the struct definition,
549    along with the standard XFOO/XSETFOO junk.
550
551    3. Add this header file to inline.c.
552
553    4. Create the methods for your object.  Note that technically you don't
554    need any, but you will almost always want at least a mark method.
555
556    5. Define your object with DEFINE_LRECORD_IMPLEMENTATION() or some
557    variant.
558
559    6. Include the header file in the .c file where you defined the object.
560
561    7. Put a call to INIT_LRECORD_IMPLEMENTATION() for the object in the
562    .c file's syms_of_foo() function.
563
564    8. Add a type enum for the object to enum lrecord_type, earlier in this
565    file.
566
567 An example:
568
569 ------------------------------ in toolbar.h -----------------------------
570
571 struct toolbar_button
572 {
573   struct lcrecord_header header;
574
575   Lisp_Object next;
576   Lisp_Object frame;
577
578   Lisp_Object up_glyph;
579   Lisp_Object down_glyph;
580   Lisp_Object disabled_glyph;
581
582   Lisp_Object cap_up_glyph;
583   Lisp_Object cap_down_glyph;
584   Lisp_Object cap_disabled_glyph;
585
586   Lisp_Object callback;
587   Lisp_Object enabled_p;
588   Lisp_Object help_string;
589
590   char enabled;
591   char down;
592   char pushright;
593   char blank;
594
595   int x, y;
596   int width, height;
597   int dirty;
598   int vertical;
599   int border_width;
600 };
601
602 DECLARE_LRECORD (toolbar_button, struct toolbar_button);
603 #define XTOOLBAR_BUTTON(x) XRECORD (x, toolbar_button, struct toolbar_button)
604 #define XSETTOOLBAR_BUTTON(x, p) XSETRECORD (x, p, toolbar_button)
605 #define TOOLBAR_BUTTONP(x) RECORDP (x, toolbar_button)
606 #define CHECK_TOOLBAR_BUTTON(x) CHECK_RECORD (x, toolbar_button)
607 #define CONCHECK_TOOLBAR_BUTTON(x) CONCHECK_RECORD (x, toolbar_button)
608
609 ------------------------------ in toolbar.c -----------------------------
610
611 #include "ui/toolbar.h"
612
613 ...
614
615 static Lisp_Object
616 mark_toolbar_button (Lisp_Object obj)
617 {
618   struct toolbar_button *data = XTOOLBAR_BUTTON (obj);
619   mark_object (data->next);
620   mark_object (data->frame);
621   mark_object (data->up_glyph);
622   mark_object (data->down_glyph);
623   mark_object (data->disabled_glyph);
624   mark_object (data->cap_up_glyph);
625   mark_object (data->cap_down_glyph);
626   mark_object (data->cap_disabled_glyph);
627   mark_object (data->callback);
628   mark_object (data->enabled_p);
629   return data->help_string;
630 }
631
632 DEFINE_LRECORD_IMPLEMENTATION ("toolbar-button", toolbar_button,
633                                mark_toolbar_button, 0, 0, 0, 0, 0,
634                                struct toolbar_button);
635
636 ...
637
638 void
639 syms_of_toolbar (void)
640 {
641   INIT_LRECORD_IMPLEMENTATION (toolbar_button);
642
643   ...;
644 }
645
646 ------------------------------ in inline.c -----------------------------
647
648 #ifdef HAVE_TOOLBARS
649 #include "ui/toolbar.h"
650 #endif
651
652 ------------------------------ in lrecord.h -----------------------------
653
654 enum lrecord_type
655 {
656   ...
657   lrecord_type_toolbar_button,
658   ...
659 };
660
661 */
662
663 /*
664
665 Note: Object types defined in external dynamically-loaded modules (not
666 part of the XEmacs main source code) should use DECLARE_EXTERNAL_LRECORD
667 and DEFINE_EXTERNAL_LRECORD_IMPLEMENTATION rather than DECLARE_LRECORD
668 and DEFINE_LRECORD_IMPLEMENTATION.
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 extern_inline structtype *                                      \
677 error_check_##c_name (Lisp_Object obj);                         \
678 extern_inline 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 extern_inline structtype *                                      \
690 error_check_##c_name (Lisp_Object obj);                         \
691 extern_inline 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 extern_inline structtype *                                      \
701 error_check_##c_name (Lisp_Object obj);                         \
702 extern_inline 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 /* Copy the data from one lcrecord structure into another, but don't
785    overwrite the header information. */
786
787 #define copy_lcrecord(dst, src)                                 \
788   memcpy ((char *) (dst) + sizeof (struct lcrecord_header),     \
789           (char *) (src) + sizeof (struct lcrecord_header),     \
790           sizeof (*(dst)) - sizeof (struct lcrecord_header))
791
792 #define zero_lcrecord(lcr)                                      \
793    memset ((char *) (lcr) + sizeof (struct lcrecord_header), 0, \
794            sizeof (*(lcr)) - sizeof (struct lcrecord_header))
795
796 #endif                          /* INCLUDED_lrecord_h_ */