XEmacs 21.2.28 "Hermes".
[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
30    type information. (The tradeoff is that each object has its
31    type marked in it, thereby increasing its size.) The first
32    four bytes of all lrecords is either a pointer to a struct
33    lrecord_implementation, which contains methods describing how
34    to process this object, or an index into an array of pointers
35    to struct lrecord_implementations plus 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
46    do their own allocation.  Each such object is malloc()ed
47    individually, and the objects are chained together through
48    a `next' pointer.  Lcrecords have a `struct lcrecord_header'
49    at the top, which contains a `struct lrecord_header' and
50    a `next' pointer, and are 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 type :8;
64   /* 1 if the object is marked during GC. */
65   unsigned mark :1;
66   /* 1 if the object resides in read-only space */
67   unsigned c_readonly : 1;
68   /* 1 if the object is readonly from lisp */
69   unsigned lisp_readonly : 1;
70 };
71
72 struct lrecord_implementation;
73 int lrecord_type_index (CONST struct lrecord_implementation *implementation);
74
75 #define set_lheader_implementation(header,imp) do {     \
76   struct lrecord_header* SLI_header = (header);         \
77   SLI_header->type = lrecord_type_index (imp);          \
78   SLI_header->mark = 0;                                 \
79   SLI_header->c_readonly = 0;                           \
80   SLI_header->lisp_readonly = 0;                        \
81 } while (0)
82
83 struct lcrecord_header
84 {
85   struct lrecord_header lheader;
86
87   /* The `next' field is normally used to chain all lrecords together
88      so that the GC can find (and free) all of them.
89      `alloc_lcrecord' threads records together.
90
91      The `next' field may be used for other purposes as long as some
92      other mechanism is provided for letting the GC do its work.
93
94      For example, the event and marker object types allocate members
95      out of memory chunks, and are able to find all unmarked members
96      by sweeping through the elements of the list of chunks.  */
97   struct lcrecord_header *next;
98
99   /* The `uid' field is just for debugging/printing convenience.
100      Having this slot doesn't hurt us much spacewise, since an
101      lcrecord already has the above slots plus malloc overhead. */
102   unsigned int uid :31;
103
104   /* The `free' field is a flag that indicates whether this lcrecord
105      is on a "free list".  Free lists are used to minimize the number
106      of calls to malloc() when we're repeatedly allocating and freeing
107      a number of the same sort of lcrecord.  Lcrecords on a free list
108      always get marked in a different fashion, so we can use this flag
109      as a sanity check to make sure that free lists only have freed
110      lcrecords and there are no freed lcrecords elsewhere. */
111   unsigned int free :1;
112 };
113
114 /* Used for lcrecords in an lcrecord-list. */
115 struct free_lcrecord_header
116 {
117   struct lcrecord_header lcheader;
118   Lisp_Object chain;
119 };
120
121 /* see alloc.c for an explanation */
122 Lisp_Object this_one_is_unmarkable (Lisp_Object obj);
123
124 struct lrecord_implementation
125 {
126   CONST char *name;
127   /* This function is called at GC time, to make sure that all Lisp_Objects
128      pointed to by this object get properly marked.  It should call
129      the mark_object function on all Lisp_Objects in the object.  If
130      the return value is non-nil, it should be a Lisp_Object to be
131      marked (don't call the mark_object function explicitly on it,
132      because the GC routines will do this).  Doing it this way reduces
133      recursion, so the object returned should preferably be the one
134      with the deepest level of Lisp_Object pointers.  This function
135      can be NULL, meaning no GC marking is necessary. */
136   Lisp_Object (*marker) (Lisp_Object);
137   /* This can be NULL if the object is an lcrecord; the
138      default_object_printer() in print.c will be used. */
139   void (*printer) (Lisp_Object, Lisp_Object printcharfun, int escapeflag);
140   /* This function is called at GC time when the object is about to
141      be freed, and at dump time (FOR_DISKSAVE will be non-zero in this
142      case).  It should perform any necessary cleanup (e.g. freeing
143      malloc()ed memory.  This can be NULL, meaning no special
144      finalization is necessary.
145
146      WARNING: remember that the finalizer is called at dump time even
147      though the object is not being freed. */
148   void (*finalizer) (void *header, int for_disksave);
149   /* This can be NULL, meaning compare objects with EQ(). */
150   int (*equal) (Lisp_Object obj1, Lisp_Object obj2, int depth);
151   /* This can be NULL, meaning use the Lisp_Object itself as the hash;
152      but *only* if the `equal' function is EQ (if two objects are
153      `equal', they *must* hash to the same value or the hashing won't
154      work). */
155   unsigned long (*hash) (Lisp_Object, int);
156
157   /* External data layout description */
158   const struct lrecord_description *description;
159
160   Lisp_Object (*getprop) (Lisp_Object obj, Lisp_Object prop);
161   int (*putprop) (Lisp_Object obj, Lisp_Object prop, Lisp_Object val);
162   int (*remprop) (Lisp_Object obj, Lisp_Object prop);
163   Lisp_Object (*plist) (Lisp_Object obj);
164
165   /* Only one of these is non-0.  If both are 0, it means that this type
166      is not instantiable by alloc_lcrecord(). */
167   size_t static_size;
168   size_t (*size_in_bytes_method) (CONST void *header);
169   /* A unique subtag-code (dynamically) assigned to this datatype. */
170   /* (This is a pointer so the rest of this structure can be read-only.) */
171   int *lrecord_type_index;
172   /* A "basic" lrecord is any lrecord that's not an lcrecord, i.e.
173      one that does not have an lcrecord_header at the front and which
174      is (usually) allocated in frob blocks.  We only use this flag for
175      some consistency checking, and that only when error-checking is
176      enabled. */
177   int basic_p;
178 };
179
180 extern CONST struct lrecord_implementation *lrecord_implementations_table[];
181
182 #define XRECORD_LHEADER_IMPLEMENTATION(obj) \
183    (lrecord_implementations_table[XRECORD_LHEADER (obj)->type])
184 #define LHEADER_IMPLEMENTATION(lh) (lrecord_implementations_table[(lh)->type])
185
186 extern int gc_in_progress;
187
188 #define MARKED_RECORD_P(obj) (gc_in_progress && XRECORD_LHEADER (obj)->mark)
189 #define MARKED_RECORD_HEADER_P(lheader) ((lheader)->mark)
190 #define MARK_RECORD_HEADER(lheader)   ((void) ((lheader)->mark = 1))
191 #define UNMARK_RECORD_HEADER(lheader) ((void) ((lheader)->mark = 0))
192
193 #define UNMARKABLE_RECORD_HEADER_P(lheader) \
194   (LHEADER_IMPLEMENTATION (lheader)->marker == this_one_is_unmarkable)
195
196 #define C_READONLY_RECORD_HEADER_P(lheader)  ((lheader)->c_readonly)
197 #define LISP_READONLY_RECORD_HEADER_P(lheader)  ((lheader)->lisp_readonly)
198 #define SET_C_READONLY_RECORD_HEADER(lheader) \
199   ((void) ((lheader)->c_readonly = (lheader)->lisp_readonly = 1))
200 #define SET_LISP_READONLY_RECORD_HEADER(lheader) \
201   ((void) ((lheader)->lisp_readonly = 1))
202
203 /* External description stuff
204
205    A lrecord external description  is an array  of values.  The  first
206    value of each line is a type, the second  the offset in the lrecord
207    structure.  Following values  are parameters, their  presence, type
208    and number is type-dependant.
209
210    The description ends with a "XD_END" or "XD_SPECIFIER_END" record.
211
212    Some example descriptions :
213
214    static const struct lrecord_description cons_description[] = {
215      { XD_LISP_OBJECT, offsetof (Lisp_Cons, car) },
216      { XD_LISP_OBJECT, offsetof (Lisp_Cons, cdr) },
217      { XD_END }
218    };
219
220    Which means "two lisp objects starting at the 'car' and 'cdr' elements"
221
222   static const struct lrecord_description string_description[] = {
223     { XD_BYTECOUNT,       offsetof (Lisp_String, size) },
224     { XD_OPAQUE_DATA_PTR, offsetof (Lisp_String, data), XD_INDIRECT(0, 1) },
225     { XD_LISP_OBJECT,     offsetof (Lisp_String, plist) },
226     { XD_END }
227   };
228   "A pointer to string data at 'data', the size of the pointed array being the value
229    of the size variable plus 1, and one lisp object at 'plist'"
230
231   The existing types :
232     XD_LISP_OBJECT
233   A Lisp object.  This is also the type to use for pointers to other lrecords.
234
235     XD_LISP_OBJECT_ARRAY
236   An array of Lisp objects or pointers to lrecords.
237   The third element is the count.
238
239   XD_LO_RESET_NIL
240   Lisp objects which will be reset to Qnil when dumping.  Useful for cleaning
241   up caches.
242
243     XD_LO_LINK
244   Link in a linked list of objects of the same type.
245
246     XD_OPAQUE_PTR
247   Pointer to undumpable data.  Must be NULL when dumping.
248
249     XD_STRUCT_PTR
250   Pointer to described struct.  Parameters are number of structures and
251   struct_description.
252
253     XD_OPAQUE_DATA_PTR
254   Pointer to dumpable opaque data.  Parameter is the size of the data.
255   Pointed data must be relocatable without changes.
256
257     XD_C_STRING
258   Pointer to a C string.
259
260     XD_DOC_STRING
261   Pointer to a doc string (C string if positive, opaque value if negative)
262
263     XD_INT_RESET
264   An integer which will be reset to a given value in the dump file.
265
266
267     XD_SIZE_T
268   size_t value.  Used for counts.
269
270     XD_INT
271   int value.  Used for counts.
272
273     XD_LONG
274   long value.  Used for counts.
275
276     XD_BYTECOUNT
277   bytecount value.  Used for counts.
278
279     XD_END
280   Special type indicating the end of the array.
281
282     XD_SPECIFIER_END
283   Special type indicating the end of the array for a specifier.  Extra
284   description is going to be fetched from the specifier methods.
285
286
287   Special macros:
288     XD_INDIRECT(line, delta)
289   Usable where  a "count" or "size"  is requested.  Gives the value of
290   the element which is at line number 'line' in the description (count
291   starts at zero) and adds delta to it.
292 */
293
294 enum lrecord_description_type {
295   XD_LISP_OBJECT_ARRAY,
296   XD_LISP_OBJECT,
297   XD_LO_RESET_NIL,
298   XD_LO_LINK,
299   XD_OPAQUE_PTR,
300   XD_STRUCT_PTR,
301   XD_OPAQUE_DATA_PTR,
302   XD_C_STRING,
303   XD_DOC_STRING,
304   XD_INT_RESET,
305   XD_SIZE_T,
306   XD_INT,
307   XD_LONG,
308   XD_BYTECOUNT,
309   XD_END,
310   XD_SPECIFIER_END
311 };
312
313 struct lrecord_description {
314   enum lrecord_description_type type;
315   int offset;
316   EMACS_INT data1;
317   const struct struct_description *data2;
318 };
319
320 struct struct_description {
321   size_t size;
322   const struct lrecord_description *description;
323 };
324
325 #define XD_INDIRECT(val, delta) (-1-((val)|(delta<<8)))
326
327 #define XD_IS_INDIRECT(code) (code<0)
328 #define XD_INDIRECT_VAL(code) ((-1-code) & 255)
329 #define XD_INDIRECT_DELTA(code) (((-1-code)>>8) & 255)
330
331 #define XD_DYNARR_DESC(base_type, sub_desc) \
332   { XD_STRUCT_PTR, offsetof (base_type, base), XD_INDIRECT(1, 0), sub_desc }, \
333   { XD_INT,        offsetof (base_type, cur) }, \
334   { XD_INT_RESET,  offsetof (base_type, max), XD_INDIRECT(1, 0) }
335
336 /* Declaring the following structures as const puts them in the
337    text (read-only) segment, which makes debugging inconvenient
338    because this segment is not mapped when processing a core-
339    dump file */
340
341 #ifdef DEBUG_XEMACS
342 #define CONST_IF_NOT_DEBUG
343 #else
344 #define CONST_IF_NOT_DEBUG CONST
345 #endif
346
347 /* DEFINE_LRECORD_IMPLEMENTATION is for objects with constant size.
348    DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION is for objects whose size varies.
349  */
350
351 #if defined (ERROR_CHECK_TYPECHECK)
352 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
353 #else
354 # define DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)
355 #endif
356
357 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
358 DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
359
360 #define DEFINE_BASIC_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,props,structtype) \
361 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,props,sizeof(structtype),0,1,structtype)
362
363 #define DEFINE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,structtype) \
364 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,structtype)
365
366 #define DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,props,structtype) \
367 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,props,sizeof (structtype),0,0,structtype)
368
369 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,sizer,structtype) \
370 DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,0,0,0,0,sizer,structtype)
371
372 #define DEFINE_LRECORD_SEQUENCE_IMPLEMENTATION_WITH_PROPS(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,props,sizer,structtype) \
373 MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,props,0,sizer,0,structtype) \
374
375 #define MAKE_LRECORD_IMPLEMENTATION(name,c_name,marker,printer,nuker,equal,hash,desc,getprop,putprop,remprop,props,size,sizer,basic_p,structtype) \
376 DECLARE_ERROR_CHECK_TYPECHECK(c_name, structtype)                       \
377 static int lrecord_##c_name##_lrecord_type_index;                       \
378 CONST_IF_NOT_DEBUG struct lrecord_implementation lrecord_##c_name =     \
379   { name, marker, printer, nuker, equal, hash, desc,                    \
380     getprop, putprop, remprop, props, size, sizer,                      \
381     &(lrecord_##c_name##_lrecord_type_index), basic_p }                 \
382
383 #define LRECORDP(a) (XTYPE (a) == Lisp_Type_Record)
384 #define XRECORD_LHEADER(a) ((struct lrecord_header *) XPNTR (a))
385
386 #define RECORD_TYPEP(x, ty) \
387   (LRECORDP (x) && \
388    lrecord_implementations_table[XRECORD_LHEADER (x)->type] == (ty))
389
390 /* NOTE: the DECLARE_LRECORD() must come before the associated
391    DEFINE_LRECORD_*() or you will get compile errors.
392
393    Furthermore, you always need to put the DECLARE_LRECORD() in a header
394    file, and make sure the header file is included in inline.c, even
395    if the type is private to a particular file.  Otherwise, you will
396    get undefined references for the error_check_foo() inline function
397    under GCC. */
398
399 #ifdef ERROR_CHECK_TYPECHECK
400
401 # define DECLARE_LRECORD(c_name, structtype)                    \
402 extern CONST_IF_NOT_DEBUG struct lrecord_implementation         \
403   lrecord_##c_name;                                             \
404 INLINE structtype *error_check_##c_name (Lisp_Object obj);      \
405 INLINE structtype *                                             \
406 error_check_##c_name (Lisp_Object obj)                          \
407 {                                                               \
408   assert (RECORD_TYPEP (obj, &lrecord_##c_name));               \
409   return (structtype *) XPNTR (obj);                            \
410 }                                                               \
411 extern Lisp_Object Q##c_name##p
412
413 # define DECLARE_NONRECORD(c_name, type_enum, structtype)       \
414 INLINE structtype *error_check_##c_name (Lisp_Object obj);      \
415 INLINE structtype *                                             \
416 error_check_##c_name (Lisp_Object obj)                          \
417 {                                                               \
418   assert (XTYPE (obj) == type_enum);                            \
419   return (structtype *) XPNTR (obj);                            \
420 }                                                               \
421 extern Lisp_Object Q##c_name##p
422
423 # define XRECORD(x, c_name, structtype) error_check_##c_name (x)
424 # define XNONRECORD(x, c_name, type_enum, structtype) error_check_##c_name (x)
425
426 # define XSETRECORD(var, p, c_name) do                          \
427 {                                                               \
428   XSETOBJ (var, Lisp_Type_Record, p);                           \
429   assert (RECORD_TYPEP (var, &lrecord_##c_name));               \
430 } while (0)
431
432 #else /* not ERROR_CHECK_TYPECHECK */
433
434 # define DECLARE_LRECORD(c_name, structtype)                    \
435 extern Lisp_Object Q##c_name##p;                                \
436 extern CONST_IF_NOT_DEBUG struct lrecord_implementation         \
437   lrecord_##c_name
438 # define DECLARE_NONRECORD(c_name, type_enum, structtype)       \
439 extern Lisp_Object Q##c_name##p
440 # define XRECORD(x, c_name, structtype) ((structtype *) XPNTR (x))
441 # define XNONRECORD(x, c_name, type_enum, structtype)           \
442   ((structtype *) XPNTR (x))
443 # define XSETRECORD(var, p, c_name) XSETOBJ (var, Lisp_Type_Record, p)
444
445 #endif /* not ERROR_CHECK_TYPECHECK */
446
447 #define RECORDP(x, c_name) RECORD_TYPEP (x, &lrecord_##c_name)
448
449 /* Note: we now have two different kinds of type-checking macros.
450    The "old" kind has now been renamed CONCHECK_foo.  The reason for
451    this is that the CONCHECK_foo macros signal a continuable error,
452    allowing the user (through debug-on-error) to substitute a different
453    value and return from the signal, which causes the lvalue argument
454    to get changed.  Quite a lot of code would crash if that happened,
455    because it did things like
456
457    foo = XCAR (list);
458    CHECK_STRING (foo);
459
460    and later on did XSTRING (XCAR (list)), assuming that the type
461    is correct (when it might be wrong, if the user substituted a
462    correct value in the debugger).
463
464    To get around this, I made all the CHECK_foo macros signal a
465    non-continuable error.  Places where a continuable error is OK
466    (generally only when called directly on the argument of a Lisp
467    primitive) should be changed to use CONCHECK().
468
469    FSF Emacs does not have this problem because RMS took the cheesy
470    way out and disabled returning from a signal entirely. */
471
472 #define CONCHECK_RECORD(x, c_name) do {                 \
473  if (!RECORD_TYPEP (x, &lrecord_##c_name))              \
474    x = wrong_type_argument (Q##c_name##p, x);           \
475 }  while (0)
476 #define CONCHECK_NONRECORD(x, lisp_enum, predicate) do {\
477  if (XTYPE (x) != lisp_enum)                            \
478    x = wrong_type_argument (predicate, x);              \
479  } while (0)
480 #define CHECK_RECORD(x, c_name) do {                    \
481  if (!RECORD_TYPEP (x, &lrecord_##c_name))              \
482    dead_wrong_type_argument (Q##c_name##p, x);          \
483  } while (0)
484 #define CHECK_NONRECORD(x, lisp_enum, predicate) do {   \
485  if (XTYPE (x) != lisp_enum)                            \
486    dead_wrong_type_argument (predicate, x);             \
487  } while (0)
488
489 void *alloc_lcrecord (size_t size, CONST struct lrecord_implementation *);
490
491 #define alloc_lcrecord_type(type, lrecord_implementation) \
492   ((type *) alloc_lcrecord (sizeof (type), lrecord_implementation))
493
494 /* Copy the data from one lcrecord structure into another, but don't
495    overwrite the header information. */
496
497 #define copy_lcrecord(dst, src)                                 \
498   memcpy ((char *) (dst) + sizeof (struct lcrecord_header),     \
499           (char *) (src) + sizeof (struct lcrecord_header),     \
500           sizeof (*(dst)) - sizeof (struct lcrecord_header))
501
502 #define zero_lcrecord(lcr)                                      \
503    memset ((char *) (lcr) + sizeof (struct lcrecord_header), 0, \
504            sizeof (*(lcr)) - sizeof (struct lcrecord_header))
505
506 #endif /* INCLUDED_lrecord_h_ */