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