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