Coverity fixes
[sxemacs] / src / lisp.h
1 /* Fundamental definitions for SXEmacs Lisp interpreter.
2    Copyright (C) 1985-1987, 1992-1995 Free Software Foundation, Inc.
3    Copyright (C) 1993-1996 Richard Mlynarik.
4    Copyright (C) 1995, 1996, 2000 Ben Wing.
5    Copyright (C) 2004 Steve Youngs.
6
7 This file is part of SXEmacs
8
9 SXEmacs is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 SXEmacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
21
22
23 /* Synched up with: FSF 19.30. */
24
25 #ifndef INCLUDED_lisp_h_
26 #define INCLUDED_lisp_h_
27
28 /************************************************************************/
29 /*                        general definitions                           */
30 /************************************************************************/
31
32 /* the old SXEmacs general includes and utility macros moved here: */
33 #include "sxe-utils.h"
34
35 /* ------------------------ dynamic arrays ------------------- */
36
37 #define Dynarr_declare(type)    \
38   type *base;                   \
39   int elsize;                   \
40   int cur;                      \
41   int largest;                  \
42   int max
43
44 typedef struct dynarr {
45         Dynarr_declare(void);
46 } Dynarr;
47
48 void *Dynarr_newf(int elsize);
49 void Dynarr_resize(void *dy, int size);
50 void Dynarr_insert_many(void *d, const void *el, int len, int start);
51 void Dynarr_delete_many(void *d, int start, int len);
52 void Dynarr_free(void *d);
53
54 #define Dynarr_new(type) ((type##_dynarr *) Dynarr_newf (sizeof (type)))
55 #define Dynarr_new2(dynarr_type, type) \
56   ((dynarr_type *) Dynarr_newf (sizeof (type)))
57 #define Dynarr_at(d, pos) ((d)->base[pos])
58 #define Dynarr_atp(d, pos) (&Dynarr_at (d, pos))
59 #define Dynarr_begin(d) Dynarr_atp (d, 0)
60 #define Dynarr_end(d) Dynarr_atp (d, Dynarr_length (d) - 1)
61 #define Dynarr_sizeof(d) ((d)->cur * (d)->elsize)
62 #define Dynarr_length(d) ((d)->cur)
63 #define Dynarr_largest(d) ((d)->largest)
64 #define Dynarr_reset(d) ((d)->cur = 0)
65 #define Dynarr_add_many(d, el, len) Dynarr_insert_many (d, el, len, (d)->cur)
66 #define Dynarr_insert_many_at_start(d, el, len) \
67   Dynarr_insert_many (d, el, len, 0)
68 #define Dynarr_add_literal_string(d, s) Dynarr_add_many (d, s, sizeof (s) - 1)
69 #define Dynarr_add_lisp_string(d, s) do {               \
70   Lisp_String *dyna_ls_s = XSTRING (s);                 \
71   Dynarr_add_many (d, (char *) string_data (dyna_ls_s), \
72                    string_length (dyna_ls_s));          \
73 } while (0)
74
75 #define Dynarr_add(d, el) (                                             \
76   (d)->cur >= (d)->max ? Dynarr_resize ((d), (d)->cur+1) : (void) 0,    \
77   ((d)->base)[(d)->cur++] = (el),                                       \
78   (d)->cur > (d)->largest ? (d)->largest = (d)->cur : (int) 0)
79
80 /* The following defines will get you into real trouble if you aren't
81    careful.  But they can save a lot of execution time when used wisely. */
82 #define Dynarr_increment(d) ((d)->cur++)
83 #define Dynarr_set_size(d, n) ((d)->cur = n)
84
85 #ifdef MEMORY_USAGE_STATS
86 struct overhead_stats;
87 size_t Dynarr_memory_usage(void *d, struct overhead_stats *stats);
88 #endif
89
90
91 \f
92
93
94
95 /*#ifdef DEBUG_SXEMACS*/
96 #define REGISTER
97 #define register
98 /*#else*/
99 /*#define REGISTER register*/
100 /*#endif*/
101
102 /* EMACS_INT is the underlying integral type into which a Lisp_Object must fit.
103    In particular, it must be large enough to contain a pointer.
104    config.h can override this, e.g. to use `long long' for bigger lisp ints.
105
106    #### In point of fact, it would NOT be a good idea for config.h to mess
107    with EMACS_INT.  A lot of code makes the basic assumption that EMACS_INT
108    is the size of a pointer. */
109
110 #ifndef SIZEOF_EMACS_INT
111 # define SIZEOF_EMACS_INT SIZEOF_VOID_P
112 #endif
113
114 #ifndef EMACS_INT
115 # if   SIZEOF_EMACS_INT == SIZEOF_LONG
116 #  define EMACS_INT long
117 # elif SIZEOF_EMACS_INT == SIZEOF_INT
118 #  define EMACS_INT int
119 # elif SIZEOF_EMACS_INT == SIZEOF_LONG_LONG_INT
120 #  define EMACS_INT long long
121 # else
122 #  error Unable to determine suitable type for EMACS_INT
123 # endif
124 #endif
125
126 #ifndef EMACS_UINT
127 # define EMACS_UINT unsigned EMACS_INT
128 #endif
129
130 #define BITS_PER_EMACS_INT (SIZEOF_EMACS_INT * BITS_PER_CHAR)
131 \f
132 /************************************************************************/
133 /*                                typedefs                              */
134 /************************************************************************/
135
136 /* We put typedefs here so that prototype declarations don't choke.
137    Note that we don't actually declare the structures here (except
138    maybe for simple structures like Dynarrs); that keeps them private
139    to the routines that actually use them. */
140
141 /* ------------------------------- */
142 /*     basic char/int typedefs     */
143 /* ------------------------------- */
144
145 /* The definitions we put here use typedefs to attribute specific meaning
146    to types that by themselves are pretty general.  Stuff pointed to by a
147    char * or unsigned char * will nearly always be one of four types:
148    a) pointer to internally-formatted text; b) pointer to text in some
149    external format, which can be defined as all formats other than the
150    internal one; c) pure ASCII text; d) binary data that is not meant to
151    be interpreted as text. [A fifth possible type "e) a general pointer
152    to memory" should be replaced with void *.]  Using these more specific
153    types rather than the general ones helps avoid the confusions that
154    occur when the semantics of a char * argument being studied are unclear. */
155
156 typedef unsigned char UChar;
157
158 /* The data representing the text in a buffer is logically a set
159    of Bufbytes, declared as follows. */
160
161 typedef UChar Bufbyte;
162
163 /* Explicitly signed or unsigned versions: */
164 typedef UChar UBufbyte;
165 typedef char SBufbyte;
166
167 /* The data representing a string in "external" format (binary or any
168    external encoding) is logically a set of Extbytes, declared as
169    follows.  Extbyte is guaranteed to be just a char, so for example
170    strlen (Extbyte *) is OK.  Extbyte is only a documentation device
171    for referring to external text. */
172
173 typedef char Extbyte;
174
175 /* A byte in a string in binary format: */
176 typedef char Char_Binary;
177 typedef UChar UChar_Binary;
178
179 /* A byte in a string in entirely US-ASCII format: (Nothing outside
180  the range 00 - 7F) */
181
182 typedef char Char_ASCII;
183 typedef UChar UChar_ASCII;
184
185 /* To the user, a buffer is made up of characters, declared as follows.
186    In the non-Mule world, characters and Bufbytes are equivalent.
187    In the Mule world, a character requires (typically) 1 to 4
188    Bufbytes for its representation in a buffer. */
189
190 typedef int Emchar;
191
192 /* Different ways of referring to a position in a buffer.  We use
193    the typedefs in preference to 'EMACS_INT' to make it clearer what
194    sort of position is being used.  See extents.c for a description
195    of the different positions.  We put them here instead of in
196    buffer.h (where they rightfully belong) to avoid syntax errors
197    in function prototypes. */
198
199 typedef EMACS_INT Bufpos;
200 typedef EMACS_INT Bytind;
201 typedef EMACS_INT Memind;
202
203 /* Counts of bytes or chars */
204
205 typedef EMACS_INT Bytecount;
206 typedef EMACS_INT Charcount;
207
208 /* Length in bytes of a string in external format */
209 typedef EMACS_INT Extcount;
210
211 /* ------------------------------- */
212 /*     structure/other typedefs    */
213 /* ------------------------------- */
214
215 /* Counts of bytes or array elements */
216 typedef EMACS_INT Memory_count;
217 typedef EMACS_INT Element_count;
218
219 /* is this right here? */
220 typedef struct lstream_s *lstream_t;
221 /* deprecated */
222 typedef struct lstream_s Lstream;
223
224 typedef unsigned int face_index;
225
226 typedef struct {
227         Dynarr_declare(struct face_cachel);
228 } face_cachel_dynarr;
229
230 typedef unsigned int glyph_index;
231
232 /* This is shared by process.h, events.h and others in future.
233    See events.h for description */
234 typedef long unsigned int USID;
235
236 typedef struct {
237         Dynarr_declare(struct glyph_cachel);
238 } glyph_cachel_dynarr;
239
240 struct buffer;                  /* "buffer.h" */
241 struct console;                 /* "console.h" */
242 struct device;                  /* "device.h" */
243 struct extent_fragment;
244 struct extent;
245 typedef struct extent *EXTENT;
246 struct frame;                   /* "frame.h" */
247 struct window;                  /* "window.h" */
248 typedef struct Lisp_Event Lisp_Event;   /* "events.h" */
249 typedef struct Lisp_Face Lisp_Face;     /* "faces.h" */
250 typedef struct Lisp_Process Lisp_Process;       /* "procimpl.h" */
251 struct stat;                    /* <sys/stat.h> */
252 typedef struct Lisp_Color_Instance Lisp_Color_Instance;
253 typedef struct Lisp_Font_Instance Lisp_Font_Instance;
254 typedef struct Lisp_Image_Instance Lisp_Image_Instance;
255 typedef struct Lisp_Gui_Item Lisp_Gui_Item;
256 struct display_line;
257 struct display_glyph_area;
258 struct display_box;
259 struct redisplay_info;
260 struct window_mirror;
261 struct scrollbar_instance;
262 struct font_metric_info;
263 struct face_cachel;
264 struct console_type_entry;
265
266 typedef struct {
267         Dynarr_declare(Bufbyte);
268 } Bufbyte_dynarr;
269
270 typedef struct {
271         Dynarr_declare(Extbyte);
272 } Extbyte_dynarr;
273
274 typedef struct {
275         Dynarr_declare(Emchar);
276 } Emchar_dynarr;
277
278 typedef struct {
279         Dynarr_declare(char);
280 } char_dynarr;
281
282 typedef unsigned char unsigned_char;
283 typedef struct {
284         Dynarr_declare(unsigned char);
285 } unsigned_char_dynarr;
286
287 typedef unsigned long unsigned_long;
288 typedef struct {
289         Dynarr_declare(unsigned long);
290 } unsigned_long_dynarr;
291
292 typedef struct {
293         Dynarr_declare(int);
294 } int_dynarr;
295
296 typedef struct {
297         Dynarr_declare(Bufpos);
298 } Bufpos_dynarr;
299
300 typedef struct {
301         Dynarr_declare(Bytind);
302 } Bytind_dynarr;
303
304 typedef struct {
305         Dynarr_declare(Charcount);
306 } Charcount_dynarr;
307
308 typedef struct {
309         Dynarr_declare(Bytecount);
310 } Bytecount_dynarr;
311
312 typedef struct {
313         Dynarr_declare(struct console_type_entry);
314 } console_type_entry_dynarr;
315
316 enum run_hooks_condition {
317         RUN_HOOKS_TO_COMPLETION,
318         RUN_HOOKS_UNTIL_SUCCESS,
319         RUN_HOOKS_UNTIL_FAILURE
320 };
321
322 #ifdef HAVE_TOOLBARS
323 enum toolbar_pos {
324         TOP_TOOLBAR,
325         BOTTOM_TOOLBAR,
326         LEFT_TOOLBAR,
327         RIGHT_TOOLBAR
328 };
329 #endif
330
331 enum edge_style {
332         EDGE_ETCHED_IN,
333         EDGE_ETCHED_OUT,
334         EDGE_BEVEL_IN,
335         EDGE_BEVEL_OUT
336 };
337
338 #ifndef ERROR_CHECK_TYPECHECK
339
340 typedef enum error_behavior {
341         ERROR_ME,
342         ERROR_ME_NOT,
343         ERROR_ME_WARN
344 } Error_behavior;
345
346 #define ERRB_EQ(a, b) ((a) == (b))
347
348 #else
349
350 /* By defining it like this, we provide strict type-checking
351    for code that lazily uses ints. */
352
353 typedef struct _error_behavior_struct_ {
354         int really_unlikely_name_to_have_accidentally_in_a_non_errb_structure;
355 } Error_behavior;
356
357 extern Error_behavior ERROR_ME;
358 extern Error_behavior ERROR_ME_NOT;
359 extern Error_behavior ERROR_ME_WARN;
360
361 #define ERRB_EQ(a, b)                                                      \
362  ((a).really_unlikely_name_to_have_accidentally_in_a_non_errb_structure == \
363   (b).really_unlikely_name_to_have_accidentally_in_a_non_errb_structure)
364
365 #endif
366
367 enum munge_me_out_the_door {
368         MUNGE_ME_FUNCTION_KEY,
369         MUNGE_ME_KEY_TRANSLATION
370 };
371
372 /* very cool convenience type */
373 typedef size_t sxe_index_t;
374 \f
375 /************************************************************************/
376 /*                   Definition of Lisp_Object data type                */
377 /************************************************************************/
378
379 /* Define the fundamental Lisp data structures */
380
381 /* This is the set of Lisp data types */
382
383 enum Lisp_Type {
384         Lisp_Type_Record,
385         Lisp_Type_Int_Even,
386         Lisp_Type_Char,
387         Lisp_Type_Int_Odd
388 };
389
390 #define POINTER_TYPE_P(type) ((type) == Lisp_Type_Record)
391
392 /* Overridden by m/next.h */
393 #ifndef ASSERT_VALID_POINTER
394 # define ASSERT_VALID_POINTER(pnt) assert((((EMACS_UINT) pnt) & 3) == 0)
395 #endif
396
397 #define GCMARKBITS  0
398 #define GCTYPEBITS  2
399 #define GCBITS      2
400 #define INT_GCBITS  1
401
402 #define INT_VALBITS (BITS_PER_EMACS_INT - INT_GCBITS)
403 #define VALBITS (BITS_PER_EMACS_INT - GCBITS)
404 #define EMACS_INT_MAX ((EMACS_INT) ((1UL << (INT_VALBITS - 1)) -1UL))
405 #define EMACS_INT_MIN (-(EMACS_INT_MAX) - 1)
406 #define NUMBER_FITS_IN_AN_EMACS_INT(num) \
407   ((num) <= EMACS_INT_MAX && (num) >= EMACS_INT_MIN)
408
409 #include "lisp-disunion.h"
410
411 #define XPNTR(x) ((void *) XPNTRVAL(x))
412
413 /* WARNING WARNING WARNING.  You must ensure on your own that proper
414    GC protection is provided for the elements in this array. */
415 typedef struct {
416         Dynarr_declare(Lisp_Object);
417 } Lisp_Object_dynarr;
418
419 typedef struct {
420         Dynarr_declare(Lisp_Object *);
421 } Lisp_Object_ptr_dynarr;
422
423 /* Close your eyes now lest you vomit or spontaneously combust ... */
424
425 #define HACKEQ_UNSAFE(obj1, obj2)                               \
426   (EQ (obj1, obj2) || (!POINTER_TYPE_P (XTYPE (obj1))           \
427                        && !POINTER_TYPE_P (XTYPE (obj2))        \
428                        && XCHAR_OR_INT (obj1) == XCHAR_OR_INT (obj2)))
429
430 #ifdef DEBUG_SXEMACS
431 extern int debug_issue_ebola_notices;
432 int eq_with_ebola_notice(Lisp_Object, Lisp_Object);
433 #define EQ_WITH_EBOLA_NOTICE(obj1, obj2)                                \
434   (debug_issue_ebola_notices ? eq_with_ebola_notice (obj1, obj2)        \
435    : EQ (obj1, obj2))
436 #else
437 #define EQ_WITH_EBOLA_NOTICE(obj1, obj2) EQ (obj1, obj2)
438 #endif
439
440 /* OK, you can open them again */
441 \f
442 /************************************************************************/
443 /**                  Definitions of basic Lisp objects                 **/
444 /************************************************************************/
445
446 #include "lrecord.h"
447
448 /*------------------------------ unbound -------------------------------*/
449
450 /* Qunbound is a special Lisp_Object (actually of type
451    symbol-value-forward), that can never be visible to
452    the Lisp caller and thus can be used in the C code
453    to mean "no such value". */
454
455 #define UNBOUNDP(val) EQ (val, Qunbound)
456
457 /*------------------------------- cons ---------------------------------*/
458
459 /* In a cons, the markbit of the car is the gc mark bit */
460
461 struct Lisp_Cons {
462         struct lrecord_header lheader;
463         /* for seq iterators */
464         void *si;
465         Lisp_Object car, cdr;
466 };
467 typedef struct Lisp_Cons Lisp_Cons;
468
469 DECLARE_LRECORD(cons, Lisp_Cons);
470 #define XCONS(x) XRECORD (x, cons, Lisp_Cons)
471 #define XSETCONS(x, p) XSETRECORD (x, p, cons)
472 #define CONSP(x) RECORDP (x, cons)
473 #define CHECK_CONS(x) CHECK_RECORD (x, cons)
474 #define CONCHECK_CONS(x) CONCHECK_RECORD (x, cons)
475
476 #define CONS_MARKED_P(c) MARKED_RECORD_HEADER_P(&((c)->lheader))
477 #define MARK_CONS(c) MARK_RECORD_HEADER (&((c)->lheader))
478
479 extern Lisp_Object Qnil;
480
481 #define NILP(x)  EQ (x, Qnil)
482 #define XCAR(a) (XCONS (a)->car)
483 #define XCDR(a) (XCONS (a)->cdr)
484 #define LISTP(x) (CONSP(x) || NILP(x))
485
486 #define CHECK_LIST(x) do {                      \
487   if (!LISTP (x))                               \
488     dead_wrong_type_argument (Qlistp, x);       \
489 } while (0)
490
491 #define CONCHECK_LIST(x) do {                   \
492   if (!LISTP (x))                               \
493     x = wrong_type_argument (Qlistp, x);        \
494 } while (0)
495
496 /*---------------------- list traversal macros -------------------------*/
497
498 /* Note: These macros are for traversing through a list in some format,
499    and executing code that you specify on each member of the list.
500
501    There are two kinds of macros, those requiring surrounding braces, and
502    those not requiring this.  Which type of macro will be indicated.
503    The general format for using a brace-requiring macro is
504
505    {
506      LIST_LOOP_3 (elt, list, tail)
507        execute_code_here;
508    }
509
510    or
511
512    {
513      LIST_LOOP_3 (elt, list, tail)
514        {
515          execute_code_here;
516        }
517    }
518
519    You can put variable declarations between the brace and beginning of
520    macro, but NOTHING ELSE.
521
522    The brace-requiring macros typically declare themselves any arguments
523    that are initialized and iterated by the macros.  If for some reason
524    you need to declare these arguments yourself (e.g. to do something on
525    them before the iteration starts, use the _NO_DECLARE versions of the
526    macros.)
527 */
528
529 /* There are two basic kinds of macros: those that handle "internal" lists
530    that are known to be correctly structured (i.e. first element is a cons
531    or nil, and the car of each cons is also a cons or nil, and there are
532    no circularities), and those that handle "external" lists, where the
533    list may have any sort of invalid formation.  This is reflected in
534    the names: those with "EXTERNAL_" work with external lists, and those
535    without this prefix work with internal lists.  The internal-list
536    macros will hit an assertion failure if the structure is ill-formed;
537    the external-list macros will signal an error in this case, either a
538    malformed-list error or a circular-list error.
539
540    Note also that the simplest external list iterator, EXTERNAL_LIST_LOOP,
541    does *NOT* check for circularities.  Therefore, make sure you call
542    QUIT each iteration or so.  However, it's probably easier just to use
543    EXTERNAL_LIST_LOOP_2, which is easier to use in any case.
544 */
545
546 /* LIST_LOOP and EXTERNAL_LIST_LOOP are the simplest macros.  They don't
547    require brace surrounding, and iterate through a list, which may or may
548    not known to be syntactically correct.  EXTERNAL_LIST_LOOP is for those
549    not known to be correct, and it detects and signals a malformed list
550    error when encountering a problem.  Circularities, however, are not
551    handled, and cause looping forever, so make sure to include a QUIT.
552    These functions also accept two args, TAIL (set progressively to each
553    cons starting with the first), and LIST, the list to iterate over.
554    TAIL needs to be defined by the program.
555
556    In each iteration, you can retrieve the current list item using XCAR
557    (tail), or destructively modify the list using XSETCAR (tail,
558    ...). */
559
560 #define LIST_LOOP(tail, list)           \
561         for (tail = list;               \
562              !NILP (tail);              \
563              tail = XCDR (tail))
564
565 #define EXTERNAL_LIST_LOOP(tail, list)                          \
566         for (tail = list; !NILP (tail); tail = XCDR (tail))     \
567                 if (!CONSP (tail)) {                            \
568                         signal_malformed_list_error (list);     \
569                 } else
570
571 /* The following macros are the "core" macros for list traversal.
572
573    *** ALL OF THESE MACROS MUST BE DECLARED INSIDE BRACES -- SEE ABOVE. ***
574
575    LIST_LOOP_2 and EXTERNAL_LIST_LOOP_2 are the standard, most-often used
576    macros.  They take two arguments, an element variable ELT and the list
577    LIST.  ELT is automatically declared, and set to each element in turn
578    from LIST.
579
580    LIST_LOOP_3 and EXTERNAL_LIST_LOOP_3 are the same, but they have a third
581    argument TAIL, another automatically-declared variable.  At each iteration,
582    this one points to the cons cell for which ELT is the car.
583
584    EXTERNAL_LIST_LOOP_4 is like EXTERNAL_LIST_LOOP_3 but takes an additional
585    LEN argument, again automatically declared, which counts the number of
586    iterations gone by.  It is 0 during the first iteration.
587
588    EXTERNAL_LIST_LOOP_4_NO_DECLARE is like EXTERNAL_LIST_LOOP_4 but none
589    of the variables are automatically declared, and so you need to declare
590    them yourself. (ELT and TAIL are Lisp_Objects, and LEN is an EMACS_INT.)
591 */
592
593 #define LIST_LOOP_2(elt, list)                          \
594         LIST_LOOP_3(elt, list, unused_tail_##elt)
595
596 #define LIST_LOOP_3(elt, list, tail)                            \
597         for (Lisp_Object elt, tail = list;                      \
598              NILP(tail) ? false : (elt = XCAR (tail), true);    \
599              tail = XCDR (tail))
600
601 /* The following macros are for traversing lisp lists.
602    Signal an error if LIST is not properly acyclic and nil-terminated.
603
604    Use tortoise/hare algorithm to check for cycles, but only if it
605    looks like the list is getting too long.  Not only is the hare
606    faster than the tortoise; it even gets a head start! */
607
608 /* Optimized and safe macros for looping over external lists.  */
609 #define CIRCULAR_LIST_SUSPICION_LENGTH 1024
610
611 #define EXTERNAL_LIST_LOOP_1(list)                                      \
612 Lisp_Object ELL1_elt, ELL1_hare, ELL1_tortoise;                         \
613 EMACS_INT ELL1_len;                                                     \
614 PRIVATE_EXTERNAL_LIST_LOOP_6 (ELL1_elt, list, ELL1_len, ELL1_hare,      \
615                       ELL1_tortoise, CIRCULAR_LIST_SUSPICION_LENGTH)
616
617 #define EXTERNAL_LIST_LOOP_2(elt, list)                                 \
618 Lisp_Object elt, hare_##elt, tortoise_##elt;                            \
619 EMACS_INT len_##elt;                                                    \
620 PRIVATE_EXTERNAL_LIST_LOOP_6 (elt, list, len_##elt, hare_##elt,         \
621                       tortoise_##elt, CIRCULAR_LIST_SUSPICION_LENGTH)
622
623 #define EXTERNAL_LIST_LOOP_3(elt, list, tail)                           \
624 Lisp_Object elt, tail, tortoise_##elt;                                  \
625 EMACS_INT len_##elt;                                                    \
626 PRIVATE_EXTERNAL_LIST_LOOP_6 (elt, list, len_##elt, tail,               \
627                       tortoise_##elt, CIRCULAR_LIST_SUSPICION_LENGTH)
628
629 #define EXTERNAL_LIST_LOOP_4_NO_DECLARE(elt, list, tail, len)           \
630 Lisp_Object tortoise_##elt;                                             \
631 PRIVATE_EXTERNAL_LIST_LOOP_6 (elt, list, len, tail,                     \
632                       tortoise_##elt, CIRCULAR_LIST_SUSPICION_LENGTH)
633
634 #define EXTERNAL_LIST_LOOP_4(elt, list, tail, len)                      \
635 Lisp_Object elt, tail, tortoise_##elt;                                  \
636 EMACS_INT len;                                                          \
637 PRIVATE_EXTERNAL_LIST_LOOP_6 (elt, list, len, tail,                     \
638                       tortoise_##elt, CIRCULAR_LIST_SUSPICION_LENGTH)
639
640 #define PRIVATE_EXTERNAL_LIST_LOOP_6(elt, list, len, hare,              \
641                                      tortoise, suspicion_length)        \
642   for (tortoise = hare = list, len = 0;                                 \
643                                                                         \
644        (CONSP (hare) ? ((elt = XCAR (hare)), 1) :                       \
645         (NILP (hare) ? 0 :                                              \
646          (signal_malformed_list_error (list), 0)));                     \
647                                                                         \
648        hare = XCDR (hare),                                              \
649          (void)                                                         \
650          ((++len > suspicion_length)                                    \
651           &&                                                            \
652           ((((len & 1) != 0) && (tortoise = XCDR (tortoise), 0)),       \
653            (EQ (hare, tortoise) && (signal_circular_list_error (list), 0)))))
654
655 /* GET_LIST_LENGTH and GET_EXTERNAL_LIST_LENGTH:
656
657    These two macros return the length of LIST (either an internal or external
658    list, according to which macro is used), stored into LEN (which must
659    be declared by the caller).  Circularities are trapped in external lists
660    (and cause errors).  Neither macro need be declared inside brackets. */
661
662 #define GET_LIST_LENGTH(list, len) do {         \
663   Lisp_Object GLL_tail;                         \
664   for (GLL_tail = list, len = 0;                \
665        !NILP (GLL_tail);                        \
666        GLL_tail = XCDR (GLL_tail), ++len)       \
667     DO_NOTHING;                                 \
668 } while (0)
669
670 #define GET_EXTERNAL_LIST_LENGTH(list, len)                             \
671 do {                                                                    \
672   Lisp_Object GELL_elt, GELL_tail;                                      \
673   EXTERNAL_LIST_LOOP_4_NO_DECLARE (GELL_elt, list, GELL_tail, len)      \
674     ;                                                                   \
675 } while (0)
676
677 /* For a list that's known to be in valid list format, where we may
678    be deleting the current element out of the list --
679    will abort() if the list is not in valid format */
680 #define LIST_LOOP_DELETING(consvar, nextconsvar, list)          \
681   for (consvar = list;                                          \
682        !NILP (consvar) ? (nextconsvar = XCDR (consvar), 1) :0;  \
683        consvar = nextconsvar)
684
685 /* LIST_LOOP_DELETE_IF and EXTERNAL_LIST_LOOP_DELETE_IF:
686
687    These two macros delete all elements of LIST (either an internal or
688    external list, according to which macro is used) satisfying
689    CONDITION, a C expression referring to variable ELT.  ELT is
690    automatically declared.  Circularities are trapped in external
691    lists (and cause errors).  Neither macro need be declared inside
692    brackets. */
693
694 #define LIST_LOOP_DELETE_IF(elt, list, condition) do {          \
695   /* Do not use ##list when creating new variables because      \
696      that may not be just a variable name. */                   \
697   Lisp_Object prev_tail_##elt = Qnil;                           \
698   LIST_LOOP_3 (elt, list, tail_##elt)                           \
699     {                                                           \
700       if (condition)                                            \
701         {                                                       \
702           if (NILP (prev_tail_##elt))                           \
703             list = XCDR (tail_##elt);                           \
704           else                                                  \
705             XCDR (prev_tail_##elt) = XCDR (tail_##elt); \
706         }                                                       \
707       else                                                      \
708         prev_tail_##elt = tail_##elt;                           \
709     }                                                           \
710 } while (0)
711
712 #define EXTERNAL_LIST_LOOP_DELETE_IF(elt, list, condition) do { \
713   Lisp_Object prev_tail_##elt = Qnil;                           \
714   EXTERNAL_LIST_LOOP_4 (elt, list, tail_##elt, len_##elt)       \
715     {                                                           \
716       if (condition)                                            \
717         {                                                       \
718           if (NILP (prev_tail_##elt))                           \
719             list = XCDR (tail_##elt);                           \
720           else                                                  \
721             XCDR (prev_tail_##elt) = XCDR (tail_##elt);         \
722           /* Keep tortoise from ever passing hare. */           \
723           len_##elt = 0;                                        \
724         }                                                       \
725       else                                                      \
726         prev_tail_##elt = tail_##elt;                           \
727     }                                                           \
728 } while (0)
729
730 /* Macros for looping over external alists.
731
732    *** ALL OF THESE MACROS MUST BE DECLARED INSIDE BRACES -- SEE ABOVE. ***
733
734    EXTERNAL_ALIST_LOOP_4 is similar to EXTERNAL_LIST_LOOP_2, but it
735    assumes the elements are aconses (the elements in an alist) and
736    sets two additional argument variables ELT_CAR and ELT_CDR to the
737    car and cdr of the acons.  All of the variables ELT, ELT_CAR and
738    ELT_CDR are automatically declared.
739
740    EXTERNAL_ALIST_LOOP_5 adds a TAIL argument to EXTERNAL_ALIST_LOOP_4,
741    just like EXTERNAL_LIST_LOOP_3 does, and again TAIL is automatically
742    declared.
743
744    EXTERNAL_ALIST_LOOP_6 adds a LEN argument to EXTERNAL_ALIST_LOOP_5,
745    just like EXTERNAL_LIST_LOOP_4 does, and again LEN is automatically
746    declared.
747
748    EXTERNAL_ALIST_LOOP_6_NO_DECLARE does not declare any of its arguments,
749    just like EXTERNAL_LIST_LOOP_4_NO_DECLARE, and so these must be declared
750    manually.
751  */
752
753 /* Optimized and safe macros for looping over external alists. */
754 #define EXTERNAL_ALIST_LOOP_4(elt, elt_car, elt_cdr, list)      \
755 Lisp_Object elt, elt_car, elt_cdr;                              \
756 Lisp_Object hare_##elt, tortoise_##elt;                         \
757 EMACS_INT len_##elt;                                            \
758 PRIVATE_EXTERNAL_ALIST_LOOP_8 (elt, elt_car, elt_cdr, list,     \
759                        len_##elt, hare_##elt, tortoise_##elt,   \
760                        CIRCULAR_LIST_SUSPICION_LENGTH)
761
762 #define EXTERNAL_ALIST_LOOP_5(elt, elt_car, elt_cdr, list, tail)        \
763 Lisp_Object elt, elt_car, elt_cdr, tail;                                \
764 Lisp_Object tortoise_##elt;                                             \
765 EMACS_INT len_##elt;                                                    \
766 PRIVATE_EXTERNAL_ALIST_LOOP_8 (elt, elt_car, elt_cdr, list,             \
767                        len_##elt, tail, tortoise_##elt,                 \
768                        CIRCULAR_LIST_SUSPICION_LENGTH)                  \
769
770 #define EXTERNAL_ALIST_LOOP_6(elt, elt_car, elt_cdr, list, tail, len)   \
771 Lisp_Object elt, elt_car, elt_cdr, tail;                                \
772 EMACS_INT len;                                                          \
773 Lisp_Object tortoise_##elt;                                             \
774 PRIVATE_EXTERNAL_ALIST_LOOP_8 (elt, elt_car, elt_cdr, list,             \
775                        len, tail, tortoise_##elt,                       \
776                        CIRCULAR_LIST_SUSPICION_LENGTH)
777
778 #define EXTERNAL_ALIST_LOOP_6_NO_DECLARE(elt, elt_car, elt_cdr, list,   \
779                                          tail, len)                     \
780 Lisp_Object tortoise_##elt;                                             \
781 PRIVATE_EXTERNAL_ALIST_LOOP_8 (elt, elt_car, elt_cdr, list,             \
782                        len, tail, tortoise_##elt,                       \
783                        CIRCULAR_LIST_SUSPICION_LENGTH)
784
785 #define PRIVATE_EXTERNAL_ALIST_LOOP_8(elt, elt_car, elt_cdr, list, len, \
786                                       hare, tortoise, suspicion_length) \
787 PRIVATE_EXTERNAL_LIST_LOOP_6 (elt, list, len, hare, tortoise,           \
788                               suspicion_length)                         \
789   if (CONSP (elt) ? (elt_car = XCAR (elt), elt_cdr = XCDR (elt), 0) :1) \
790     continue;                                                           \
791   else
792
793 /* Macros for looping over external property lists.
794
795    *** ALL OF THESE MACROS MUST BE DECLARED INSIDE BRACES -- SEE ABOVE. ***
796
797    EXTERNAL_PROPERTY_LIST_LOOP_3 maps over an external list assumed to
798    be a property list, consisting of alternating pairs of keys
799    (typically symbols or keywords) and values.  Each iteration
800    processes one such pair out of LIST, assigning the two elements to
801    KEY and VALUE respectively.  Malformed lists and circularities are
802    trapped as usual, and in addition, property lists with an odd number
803    of elements also signal an error.
804
805    EXTERNAL_PROPERTY_LIST_LOOP_4 adds a TAIL argument to
806    EXTERNAL_PROPERTY_LIST_LOOP_3, just like EXTERNAL_LIST_LOOP_3 does,
807    and again TAIL is automatically declared.
808
809    EXTERNAL_PROPERTY_LIST_LOOP_5 adds a LEN argument to
810    EXTERNAL_PROPERTY_LIST_LOOP_4, just like EXTERNAL_LIST_LOOP_4 does,
811    and again LEN is automatically declared.  Note that in this case,
812    LEN counts the iterations, NOT the total number of list elements
813    processed, which is 2 * LEN.
814
815    EXTERNAL_PROPERTY_LIST_LOOP_5_NO_DECLARE does not declare any of its
816    arguments, just like EXTERNAL_LIST_LOOP_4_NO_DECLARE, and so these
817    must be declared manually.  */
818
819 /* Optimized and safe macros for looping over external property lists. */
820 #define EXTERNAL_PROPERTY_LIST_LOOP_3(key, value, list)                 \
821 Lisp_Object key, value, hare_##key, tortoise_##key;                     \
822 EMACS_INT len_##key;                                                    \
823 EXTERNAL_PROPERTY_LIST_LOOP_7 (key, value, list, len_##key, hare_##key, \
824                      tortoise_##key, CIRCULAR_LIST_SUSPICION_LENGTH)
825
826 #define EXTERNAL_PROPERTY_LIST_LOOP_4(key, value, list, tail)           \
827 Lisp_Object key, value, tail, tortoise_##key;                           \
828 EMACS_INT len_##key;                                                    \
829 EXTERNAL_PROPERTY_LIST_LOOP_7 (key, value, list, len_##key, tail,       \
830                      tortoise_##key, CIRCULAR_LIST_SUSPICION_LENGTH)
831
832 #define EXTERNAL_PROPERTY_LIST_LOOP_5(key, value, list, tail, len)      \
833 Lisp_Object key, value, tail, tortoise_##key;                           \
834 EMACS_INT len;                                                          \
835 EXTERNAL_PROPERTY_LIST_LOOP_7 (key, value, list, len, tail,             \
836                      tortoise_##key, CIRCULAR_LIST_SUSPICION_LENGTH)
837
838 #define EXTERNAL_PROPERTY_LIST_LOOP_5_NO_DECLARE(key, value, list,      \
839                                                  tail, len)             \
840 Lisp_Object tortoise_##key;                                             \
841 EXTERNAL_PROPERTY_LIST_LOOP_7 (key, value, list, len, tail,             \
842                      tortoise_##key, CIRCULAR_LIST_SUSPICION_LENGTH)
843
844 #define EXTERNAL_PROPERTY_LIST_LOOP_7(key, value, list, len, hare,      \
845                              tortoise, suspicion_length)                \
846   for (tortoise = hare = list, len = 0;                                 \
847                                                                         \
848        ((CONSP (hare) &&                                                \
849          (key = XCAR (hare),                                            \
850           hare = XCDR (hare),                                           \
851           (CONSP (hare) ? 1 :                                           \
852            (signal_malformed_property_list_error (list), 0)))) ?        \
853         (value = XCAR (hare), 1) :                                      \
854         (NILP (hare) ? 0 :                                              \
855          (signal_malformed_property_list_error (list), 0)));            \
856                                                                         \
857        hare = XCDR (hare),                                              \
858          ((++len < suspicion_length) ?                                  \
859           ((void) 0) :                                                  \
860           (((len & 1) ?                                                 \
861             ((void) (tortoise = XCDR (XCDR (tortoise)))) :              \
862             ((void) 0))                                                 \
863            ,                                                            \
864            (EQ (hare, tortoise) ?                                       \
865             ((void) signal_circular_property_list_error (list)) :       \
866             ((void) 0)))))
867
868 /* For a property list (alternating keywords/values) that may not be
869    in valid list format -- will signal an error if the list is not in
870    valid format.  CONSVAR is used to keep track of the iterations
871    without modifying PLIST.
872
873    We have to be tricky to still keep the same C format.*/
874 #define EXTERNAL_PROPERTY_LIST_LOOP(tail, key, value, plist)    \
875   for (tail = plist;                                            \
876        (CONSP (tail) && CONSP (XCDR (tail)) ?                   \
877         (key = XCAR (tail), value = XCAR (XCDR (tail))) :       \
878         (key = Qunbound,    value = Qunbound)),                 \
879        !NILP (tail);                                            \
880        tail = XCDR (XCDR (tail)))                               \
881     if (UNBOUNDP (key))                                         \
882       Fsignal (Qmalformed_property_list, list1 (plist));        \
883     else
884
885 #define PROPERTY_LIST_LOOP(tail, key, value, plist)     \
886   for (tail = plist;                                    \
887        NILP (tail) ? 0 :                                \
888          (key   = XCAR (tail), tail = XCDR (tail),      \
889           value = XCAR (tail), tail = XCDR (tail), 1);  \
890        )
891
892 /* Return 1 if LIST is properly acyclic and nil-terminated, else 0. */
893 extern_inline int TRUE_LIST_P(Lisp_Object object);
894 extern_inline int TRUE_LIST_P(Lisp_Object object)
895 {
896         Lisp_Object hare, tortoise;
897         EMACS_INT len;
898
899         for (hare = tortoise = object, len = 0;
900              CONSP(hare); hare = XCDR(hare), len++) {
901                 if (len < CIRCULAR_LIST_SUSPICION_LENGTH)
902                         continue;
903
904                 if (len & 1)
905                         tortoise = XCDR(tortoise);
906                 else if (EQ(hare, tortoise))
907                         return 0;
908         }
909
910         return NILP(hare);
911 }
912
913 /* Signal an error if LIST is not properly acyclic and nil-terminated. */
914 #define CHECK_TRUE_LIST(list) do {                      \
915   Lisp_Object CTL_list = (list);                        \
916   Lisp_Object CTL_hare, CTL_tortoise;                   \
917   EMACS_INT CTL_len;                                    \
918                                                         \
919   for (CTL_hare = CTL_tortoise = CTL_list, CTL_len = 0; \
920        CONSP (CTL_hare);                                \
921        CTL_hare = XCDR (CTL_hare), CTL_len++)           \
922     {                                                   \
923       if (CTL_len < CIRCULAR_LIST_SUSPICION_LENGTH)     \
924         continue;                                       \
925                                                         \
926       if (CTL_len & 1)                                  \
927         CTL_tortoise = XCDR (CTL_tortoise);             \
928       else if (EQ (CTL_hare, CTL_tortoise))             \
929         Fsignal (Qcircular_list, list1 (CTL_list));     \
930     }                                                   \
931                                                         \
932   if (! NILP (CTL_hare))                                \
933     signal_malformed_list_error (CTL_list);             \
934 } while (0)
935
936 /*------------------------------ string --------------------------------*/
937
938 struct Lisp_String {
939         struct lrecord_header lheader;
940         Bytecount size;
941         Bufbyte *data;
942 #ifdef EF_USE_COMPRE
943         Lisp_Object compre;
944 #endif
945         Lisp_Object plist;
946 };
947 typedef struct Lisp_String Lisp_String;
948
949 DECLARE_LRECORD(string, Lisp_String);
950 #define XSTRING(x) XRECORD (x, string, Lisp_String)
951 #define XSETSTRING(x, p) XSETRECORD (x, p, string)
952 #define STRINGP(x) RECORDP (x, string)
953 #define CHECK_STRING(x) CHECK_RECORD (x, string)
954 #define CONCHECK_STRING(x) CONCHECK_RECORD (x, string)
955
956 #ifdef MULE
957
958 Charcount bytecount_to_charcount(const Bufbyte * ptr, Bytecount len);
959 Bytecount charcount_to_bytecount(const Bufbyte * ptr, Charcount len);
960
961 #else                           /* not MULE */
962
963 # define bytecount_to_charcount(ptr, len) (len)
964 # define charcount_to_bytecount(ptr, len) (len)
965
966 #endif                          /* not MULE */
967
968 #define string_length(s) ((s)->size)
969 #define XSTRING_LENGTH(s) string_length (XSTRING (s))
970 #define XSTRING_CHAR_LENGTH(s) string_char_length (XSTRING (s))
971 #define string_data(s) ((s)->data + 0)
972 #define XSTRING_DATA(s) string_data (XSTRING (s))
973 #define string_byte(s, i) ((s)->data[i] + 0)
974 #define XSTRING_BYTE(s, i) string_byte (XSTRING (s), i)
975 #define string_byte_addr(s, i) (&((s)->data[i]))
976 #define set_string_length(s, len) ((void) ((s)->size = (len)))
977 #define set_string_data(s, ptr) ((void) ((s)->data = (ptr)))
978 #define set_string_byte(s, i, b) ((void) ((s)->data[i] = (b)))
979
980 void resize_string(Lisp_String * s, Bytecount pos, Bytecount delta);
981
982 #ifdef MULE
983
984 extern_inline Charcount string_char_length(const Lisp_String *s);
985 extern_inline Charcount string_char_length(const Lisp_String *s)
986 {
987         return bytecount_to_charcount(string_data(s), string_length(s));
988 }
989
990 # define string_char(s, i) charptr_emchar_n (string_data (s), i)
991 # define string_char_addr(s, i) charptr_n_addr (string_data (s), i)
992 void set_string_char(Lisp_String * s, Charcount i, Emchar c);
993
994 #else                           /* not MULE */
995
996 # define string_char_length(s) string_length (s)
997 # define string_char(s, i) ((Emchar) string_byte (s, i))
998 # define string_char_addr(s, i) string_byte_addr (s, i)
999 # define set_string_char(s, i, c) set_string_byte (s, i, (Bufbyte)c)
1000
1001 #endif                          /* not MULE */
1002
1003 /* Return the true aligned size of a struct whose last member is a
1004    variable-length array field.  (this is known as the "struct hack") */
1005 /* Implementation: in practice, structtype and fieldtype usually have
1006    the same alignment, but we can't be sure.  We need to use
1007    ALIGN_SIZE to be absolutely sure of getting the correct alignment.
1008    To help the compiler's optimizer, we use a ternary expression that
1009    only a very stupid compiler would fail to correctly simplify. */
1010 #define FLEXIBLE_ARRAY_STRUCT_SIZEOF(structtype,        \
1011                                      fieldtype,         \
1012                                      fieldname,         \
1013                                      array_length)      \
1014 (ALIGNOF (structtype) == ALIGNOF (fieldtype)            \
1015  ? (offsetof (structtype, fieldname) +                  \
1016     (offsetof (structtype, fieldname[1]) -              \
1017      offsetof (structtype, fieldname[0])) *             \
1018     (array_length))                                     \
1019  : (ALIGN_SIZE                                          \
1020     ((offsetof (structtype, fieldname) +                \
1021       (offsetof (structtype, fieldname[1]) -            \
1022        offsetof (structtype, fieldname[0])) *           \
1023       (array_length)),                                  \
1024      ALIGNOF (structtype))))
1025
1026 /*------------------------------ vector --------------------------------*/
1027
1028 struct Lisp_Vector {
1029         struct lcrecord_header header;
1030         /* the sequence category */
1031         void *si;
1032         /* this vector's length */
1033         long int size;
1034         /* next is now chained through v->contents[size], terminated by Qzero.
1035            This means that pure vectors don't need a "next" */
1036         /* struct Lisp_Vector *next; */
1037         Lisp_Object contents[1];
1038 };
1039 typedef struct Lisp_Vector Lisp_Vector;
1040
1041 DECLARE_LRECORD(vector, Lisp_Vector);
1042 #define XVECTOR(x) XRECORD (x, vector, Lisp_Vector)
1043 #define XSETVECTOR(x, p) XSETRECORD (x, p, vector)
1044 #define VECTORP(x) RECORDP (x, vector)
1045 #define CHECK_VECTOR(x) CHECK_RECORD (x, vector)
1046 #define CONCHECK_VECTOR(x) CONCHECK_RECORD (x, vector)
1047
1048 #define vector_length(v) ((v)->size)
1049 #define XVECTOR_LENGTH(s) vector_length (XVECTOR (s))
1050 #define vector_data(v) ((v)->contents)
1051 #define XVECTOR_DATA(s) vector_data (XVECTOR (s))
1052
1053 /*---------------------------- bit vectors -----------------------------*/
1054
1055 #if (SXE_LONGBITS < 16)
1056 #error What the hell?!
1057 #elif (SXE_LONGBITS < 32)
1058 # define LONGBITS_LOG2 4
1059 # define LONGBITS_POWER_OF_2 16
1060 #elif (SXE_LONGBITS < 64)
1061 # define LONGBITS_LOG2 5
1062 # define LONGBITS_POWER_OF_2 32
1063 #elif (SXE_LONGBITS < 128)
1064 # define LONGBITS_LOG2 6
1065 # define LONGBITS_POWER_OF_2 64
1066 #else
1067 #error You really have 128-bit integers?!
1068 #endif
1069
1070 struct Lisp_Bit_Vector {
1071         struct lrecord_header lheader;
1072
1073         /* category subsystem */
1074         void *si;
1075
1076         Lisp_Object next;
1077         EMACS_INT size;
1078         unsigned long bits[1];
1079 };
1080 typedef struct Lisp_Bit_Vector Lisp_Bit_Vector;
1081
1082 DECLARE_LRECORD(bit_vector, Lisp_Bit_Vector);
1083 #define XBIT_VECTOR(x) XRECORD (x, bit_vector, Lisp_Bit_Vector)
1084 #define XSETBIT_VECTOR(x, p) XSETRECORD (x, p, bit_vector)
1085 #define BIT_VECTORP(x) RECORDP (x, bit_vector)
1086 #define CHECK_BIT_VECTOR(x) CHECK_RECORD (x, bit_vector)
1087 #define CONCHECK_BIT_VECTOR(x) CONCHECK_RECORD (x, bit_vector)
1088
1089 #define BITP(x) (INTP (x) && (XINT (x) == 0 || XINT (x) == 1))
1090
1091 #define CHECK_BIT(x) do {               \
1092   if (!BITP (x))                        \
1093     dead_wrong_type_argument (Qbitp, x);\
1094 } while (0)
1095
1096 #define CONCHECK_BIT(x) do {            \
1097   if (!BITP (x))                        \
1098     x = wrong_type_argument (Qbitp, x); \
1099 } while (0)
1100
1101 #define bit_vector_length(v) ((v)->size)
1102 #define bit_vector_next(v) ((v)->next)
1103
1104 extern_inline int bit_vector_bit(const Lisp_Bit_Vector *v, size_t n);
1105 extern_inline int bit_vector_bit(const Lisp_Bit_Vector *v, size_t n)
1106 {
1107         return ((v->bits[n >> LONGBITS_LOG2] >> (n & (LONGBITS_POWER_OF_2 - 1)))
1108                 & 1);
1109 }
1110
1111 extern_inline void set_bit_vector_bit(Lisp_Bit_Vector *v, size_t n, int value);
1112 extern_inline void set_bit_vector_bit(Lisp_Bit_Vector *v, size_t n, int value)
1113 {
1114         if (value)
1115                 v->bits[n >> LONGBITS_LOG2] |=
1116                     (1UL << (n & (LONGBITS_POWER_OF_2 - 1)));
1117         else
1118                 v->bits[n >> LONGBITS_LOG2] &=
1119                     ~(1UL << (n & (LONGBITS_POWER_OF_2 - 1)));
1120 }
1121
1122 /* Number of longs required to hold LEN bits */
1123 #define BIT_VECTOR_LONG_STORAGE(len) \
1124   (((len) + LONGBITS_POWER_OF_2 - 1) >> LONGBITS_LOG2)
1125
1126 /*------------------------------ symbol --------------------------------*/
1127
1128 typedef struct Lisp_Symbol Lisp_Symbol;
1129 struct Lisp_Symbol {
1130         struct lrecord_header lheader;
1131         /* next symbol in this obarray bucket */
1132         Lisp_Symbol *next;
1133         Lisp_String *name;
1134         Lisp_Object value;
1135         Lisp_Object function;
1136         Lisp_Object plist;
1137 };
1138
1139 #define SYMBOL_IS_KEYWORD(sym)                                          \
1140   ((string_byte (symbol_name (XSYMBOL (sym)), 0) == ':')                \
1141    && EQ (sym, oblookup (Vobarray,                                      \
1142                          string_data (symbol_name (XSYMBOL (sym))),     \
1143                          string_length (symbol_name (XSYMBOL (sym))))))
1144 #define KEYWORDP(obj) (SYMBOLP (obj) && SYMBOL_IS_KEYWORD (obj))
1145
1146 DECLARE_LRECORD(symbol, Lisp_Symbol);
1147 #define XSYMBOL(x) XRECORD (x, symbol, Lisp_Symbol)
1148 #define XSETSYMBOL(x, p) XSETRECORD (x, p, symbol)
1149 #define SYMBOLP(x) RECORDP (x, symbol)
1150 #define CHECK_SYMBOL(x) CHECK_RECORD (x, symbol)
1151 #define CONCHECK_SYMBOL(x) CONCHECK_RECORD (x, symbol)
1152
1153 #define symbol_next(s) ((s)->next)
1154 #define symbol_name(s) ((s)->name)
1155 #define symbol_value(s) ((s)->value)
1156 #define symbol_function(s) ((s)->function)
1157 #define symbol_plist(s) ((s)->plist)
1158
1159 /*------------------------------- subr ---------------------------------*/
1160
1161 typedef Lisp_Object(*lisp_fn_t) (void);
1162
1163 struct Lisp_Subr {
1164         struct lrecord_header lheader;
1165         short min_args;
1166         short max_args;
1167         const char *prompt;
1168         const char *doc;
1169         const char *name;
1170         lisp_fn_t subr_fn;
1171 };
1172 typedef struct Lisp_Subr Lisp_Subr;
1173
1174 DECLARE_LRECORD(subr, Lisp_Subr);
1175 #define XSUBR(x) XRECORD (x, subr, Lisp_Subr)
1176 #define XSETSUBR(x, p) XSETRECORD (x, p, subr)
1177 #define SUBRP(x) RECORDP (x, subr)
1178 #define CHECK_SUBR(x) CHECK_RECORD (x, subr)
1179 #define CONCHECK_SUBR(x) CONCHECK_RECORD (x, subr)
1180
1181 #define subr_function(subr) ((subr)->subr_fn)
1182 #define SUBR_FUNCTION(subr,max_args) \
1183   ((Lisp_Object (*) (EXFUN_##max_args)) (subr)->subr_fn)
1184 #define subr_name(subr) ((subr)->name)
1185
1186 /*------------------------------ marker --------------------------------*/
1187
1188 typedef struct Lisp_Marker Lisp_Marker;
1189 struct Lisp_Marker {
1190         struct lrecord_header lheader;
1191         Lisp_Marker *next;
1192         Lisp_Marker *prev;
1193         struct buffer *buffer;
1194         Memind memind;
1195         char insertion_type;
1196 };
1197
1198 DECLARE_LRECORD(marker, Lisp_Marker);
1199 #define XMARKER(x) XRECORD (x, marker, Lisp_Marker)
1200 #define XSETMARKER(x, p) XSETRECORD (x, p, marker)
1201 #define MARKERP(x) RECORDP (x, marker)
1202 #define CHECK_MARKER(x) CHECK_RECORD (x, marker)
1203 #define CONCHECK_MARKER(x) CONCHECK_RECORD (x, marker)
1204
1205 /* The second check was looking for GCed markers still in use */
1206 /* if (INTP (XMARKER (x)->lheader.next.v)) abort (); */
1207
1208 #define marker_next(m) ((m)->next)
1209 #define marker_prev(m) ((m)->prev)
1210
1211 /*------------------------------- char ---------------------------------*/
1212
1213 #define CHARP(x) (XTYPE (x) == Lisp_Type_Char)
1214
1215 #ifdef ERROR_CHECK_TYPECHECK
1216
1217 extern_inline Emchar XCHAR(Lisp_Object obj);
1218 extern_inline Emchar XCHAR(Lisp_Object obj)
1219 {
1220         assert(CHARP(obj));
1221         return XCHARVAL(obj);
1222 }
1223
1224 #else
1225
1226 #define XCHAR(x) ((Emchar)XCHARVAL (x))
1227
1228 #endif
1229
1230 #define CHECK_CHAR(x) CHECK_NONRECORD (x, Lisp_Type_Char, Qcharacterp)
1231 #define CONCHECK_CHAR(x) CONCHECK_NONRECORD (x, Lisp_Type_Char, Qcharacterp)
1232
1233 /*------------------------------ float ---------------------------------*/
1234
1235 /* moved to ent-float.h */
1236
1237 /*-------------------------------- int ---------------------------------*/
1238
1239 #define ZEROP(x) EQ (x, Qzero)
1240
1241 #ifdef ERROR_CHECK_TYPECHECK
1242
1243 extern_inline EMACS_INT XINT(Lisp_Object obj);
1244 extern_inline EMACS_INT XINT(Lisp_Object obj)
1245 {
1246         assert(INTP(obj));
1247         return XREALINT(obj);
1248 }
1249
1250 extern_inline EMACS_INT XCHAR_OR_INT(Lisp_Object obj);
1251 extern_inline EMACS_INT XCHAR_OR_INT(Lisp_Object obj)
1252 {
1253         assert(INTP(obj) || CHARP(obj));
1254         return CHARP(obj) ? XCHAR(obj) : XINT(obj);
1255 }
1256
1257 #else                           /* no error checking */
1258
1259 #define XINT(obj) XREALINT (obj)
1260 #define XCHAR_OR_INT(obj) (CHARP (obj) ? XCHAR (obj) : XINT (obj))
1261
1262 #endif                          /* no error checking */
1263
1264 #define CHECK_INT(x) do {                       \
1265   if (!INTP (x))                                \
1266     dead_wrong_type_argument (Qintegerp, x);    \
1267 } while (0)
1268
1269 #define CONCHECK_INT(x) do {                    \
1270   if (!INTP (x))                                \
1271     x = wrong_type_argument (Qintegerp, x);     \
1272 } while (0)
1273
1274 #define NATNUMP(x) (INTP (x) && XINT (x) >= 0)
1275
1276 #define CHECK_NATNUM(x) do {                    \
1277   if (!NATNUMP (x))                             \
1278     dead_wrong_type_argument (Qnatnump, x);     \
1279 } while (0)
1280
1281 #define CONCHECK_NATNUM(x) do {                 \
1282   if (!NATNUMP (x))                             \
1283     x = wrong_type_argument (Qnatnump, x);      \
1284 } while (0)
1285
1286 /* next three always continuable because they coerce their arguments. */
1287 #define CHECK_INT_COERCE_CHAR(x) do {                   \
1288   if (INTP (x))                                         \
1289     ;                                                   \
1290   else if (CHARP (x))                                   \
1291     x = make_int (XCHAR (x));                           \
1292   else                                                  \
1293     x = wrong_type_argument (Qinteger_or_char_p, x);    \
1294 } while (0)
1295
1296 #define CHECK_INT_COERCE_MARKER(x) do {                 \
1297   if (INTP (x))                                         \
1298     ;                                                   \
1299   else if (MARKERP (x))                                 \
1300     x = make_int (marker_position (x));                 \
1301   else                                                  \
1302     x = wrong_type_argument (Qinteger_or_marker_p, x);  \
1303 } while (0)
1304
1305 #define CHECK_INT_COERCE_CHAR_OR_MARKER(x) do {                 \
1306   if (INTP (x))                                                 \
1307     ;                                                           \
1308   else if (CHARP (x))                                           \
1309     x = make_int (XCHAR (x));                                   \
1310   else if (MARKERP (x))                                         \
1311     x = make_int (marker_position (x));                         \
1312   else                                                          \
1313     x = wrong_type_argument (Qinteger_char_or_marker_p, x);     \
1314 } while (0)
1315
1316 /*--------------------------- readonly objects -------------------------*/
1317
1318 #define CHECK_C_WRITEABLE(obj)                                  \
1319   do { if (c_readonly (obj)) c_write_error (obj); } while (0)
1320
1321 #define CHECK_LISP_WRITEABLE(obj)                                       \
1322   do { if (lisp_readonly (obj)) lisp_write_error (obj); } while (0)
1323
1324 #define C_READONLY(obj) (C_READONLY_RECORD_HEADER_P(XRECORD_LHEADER (obj)))
1325 #define LISP_READONLY(obj) (LISP_READONLY_RECORD_HEADER_P(XRECORD_LHEADER (obj)))
1326
1327 /*----------------------------- structures -----------------------------*/
1328
1329 typedef struct structure_keyword_entry structure_keyword_entry;
1330 struct structure_keyword_entry {
1331         Lisp_Object keyword;
1332         int (*validate) (Lisp_Object keyword, Lisp_Object value,
1333                          Error_behavior errb);
1334 };
1335
1336 typedef struct {
1337         Dynarr_declare(structure_keyword_entry);
1338 } structure_keyword_entry_dynarr;
1339
1340 typedef struct structure_type structure_type;
1341 struct structure_type {
1342         Lisp_Object type;
1343         structure_keyword_entry_dynarr *keywords;
1344         int (*validate) (Lisp_Object data, Error_behavior errb);
1345          Lisp_Object(*instantiate) (Lisp_Object data);
1346 };
1347
1348 typedef struct {
1349         Dynarr_declare(structure_type);
1350 } structure_type_dynarr;
1351
1352 struct structure_type *define_structure_type(Lisp_Object type, int (*validate)
1353                                               (Lisp_Object data,
1354                                                Error_behavior errb),
1355                                              Lisp_Object(*instantiate)
1356                                               (Lisp_Object data));
1357 void define_structure_type_keyword(struct structure_type *st,
1358                                    Lisp_Object keyword,
1359                                    int (*validate) (Lisp_Object keyword,
1360                                                     Lisp_Object value,
1361                                                     Error_behavior errb));
1362
1363 /*---------------------------- weak lists ------------------------------*/
1364
1365 enum weak_list_type {
1366         /* element disappears if it's unmarked. */
1367         WEAK_LIST_SIMPLE,
1368         /* element disappears if it's a cons and either its car or
1369            cdr is unmarked. */
1370         WEAK_LIST_ASSOC,
1371         /* element disappears if it's a cons and its car is unmarked. */
1372         WEAK_LIST_KEY_ASSOC,
1373         /* element disappears if it's a cons and its cdr is unmarked. */
1374         WEAK_LIST_VALUE_ASSOC,
1375         /* element disappears if it's a cons and neither its car nor
1376            its cdr is marked. */
1377         WEAK_LIST_FULL_ASSOC
1378 };
1379
1380 struct weak_list {
1381         struct lcrecord_header header;
1382         Lisp_Object list;       /* don't mark through this! */
1383         enum weak_list_type type;
1384         Lisp_Object next_weak;  /* don't mark through this! */
1385 };
1386
1387 DECLARE_LRECORD(weak_list, struct weak_list);
1388 #define XWEAK_LIST(x) XRECORD (x, weak_list, struct weak_list)
1389 #define XSETWEAK_LIST(x, p) XSETRECORD (x, p, weak_list)
1390 #define WEAK_LISTP(x) RECORDP (x, weak_list)
1391 #define CHECK_WEAK_LIST(x) CHECK_RECORD (x, weak_list)
1392 #define CONCHECK_WEAK_LIST(x) CONCHECK_RECORD (x, weak_list)
1393
1394 #define weak_list_list(w) ((w)->list)
1395 #define XWEAK_LIST_LIST(w) (XWEAK_LIST (w)->list)
1396
1397 Lisp_Object make_weak_list(enum weak_list_type type);
1398 /* The following two are only called by the garbage collector */
1399 int finish_marking_weak_lists(void);
1400 void prune_weak_lists(void);
1401
1402 /*-------------------------- lcrecord-list -----------------------------*/
1403
1404 struct lcrecord_list {
1405         struct lcrecord_header header;
1406         Lisp_Object free;
1407         size_t size;
1408         const struct lrecord_implementation *implementation;
1409 };
1410
1411 DECLARE_LRECORD(lcrecord_list, struct lcrecord_list);
1412 #define XLCRECORD_LIST(x) XRECORD (x, lcrecord_list, struct lcrecord_list)
1413 #define XSETLCRECORD_LIST(x, p) XSETRECORD (x, p, lcrecord_list)
1414 #define LCRECORD_LISTP(x) RECORDP (x, lcrecord_list)
1415 /* #define CHECK_LCRECORD_LIST(x) CHECK_RECORD (x, lcrecord_list)
1416    Lcrecord lists should never escape to the Lisp level, so
1417    functions should not be doing this. */
1418
1419 Lisp_Object make_lcrecord_list(size_t size, const struct lrecord_implementation
1420                                *implementation);
1421 Lisp_Object allocate_managed_lcrecord(Lisp_Object lcrecord_list);
1422 void free_managed_lcrecord(Lisp_Object lcrecord_list, Lisp_Object lcrecord);
1423 \f
1424 /************************************************************************/
1425 /*         Definitions of primitive Lisp functions and variables        */
1426 /************************************************************************/
1427
1428 /* DEFUN - Define a built-in Lisp-visible C function or `subr'.
1429  `lname' should be the name to give the function in Lisp,
1430     as a null-terminated C string.
1431  `Fname' should be the C equivalent of `lname', using only characters
1432     valid in a C identifier, with an "F" prepended.
1433     The name of the C constant structure that records information
1434     on this function for internal use is "S" concatenated with Fname.
1435  `min_args' should be a number, the minimum number of arguments allowed.
1436  `max_args' should be a number, the maximum number of arguments allowed,
1437     or else MANY or UNEVALLED.
1438     MANY means pass a vector of evaluated arguments,
1439          in the form of an integer number-of-arguments
1440          followed by the address of a vector of Lisp_Objects
1441          which contains the argument values.
1442     UNEVALLED means pass the list of unevaluated arguments.
1443  `prompt' says how to read arguments for an interactive call.
1444     See the doc string for `interactive'.
1445     A null string means call interactively with no arguments.
1446  `arglist' are the comma-separated arguments (always Lisp_Objects) for
1447     the function.
1448   The docstring for the function is placed as a "C" comment between
1449     the prompt and the `args' argument.  make-docfile reads the
1450     comment and creates the DOC file from it.
1451 */
1452
1453 #define EXFUN_0 void
1454 #define EXFUN_1 Lisp_Object
1455 #define EXFUN_2 Lisp_Object,Lisp_Object
1456 #define EXFUN_3 Lisp_Object,Lisp_Object,Lisp_Object
1457 #define EXFUN_4 Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object
1458 #define EXFUN_5 Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object
1459 #define EXFUN_6 Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object, \
1460 Lisp_Object
1461 #define EXFUN_7 Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object, \
1462 Lisp_Object,Lisp_Object
1463 #define EXFUN_8 Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object,Lisp_Object, \
1464 Lisp_Object,Lisp_Object,Lisp_Object
1465 #define EXFUN_MANY int, Lisp_Object*
1466 #define EXFUN_UNEVALLED Lisp_Object
1467 #define EXFUN(sym, max_args) Lisp_Object sym (EXFUN_##max_args)
1468
1469 #define SUBR_MAX_ARGS 8
1470 #define MANY -2
1471 #define UNEVALLED -1
1472
1473 /* Can't be const, because then subr->doc is read-only and
1474    Snarf_documentation chokes */
1475
1476 #define DEFUN(lname, Fname, min_args, max_args, prompt, arglist)        \
1477   Lisp_Object Fname (EXFUN_##max_args);                                 \
1478   static struct Lisp_Subr S##Fname =                                    \
1479   {                                                                     \
1480     { /* struct lrecord_header */                                       \
1481       lrecord_type_subr, /* lrecord_type_index */                       \
1482       1, /* mark bit */                                                 \
1483       1, /* c_readonly bit */                                           \
1484       1  /* lisp_readonly bit */                                        \
1485     },                                                                  \
1486     min_args,                                                           \
1487     max_args,                                                           \
1488     prompt,                                                             \
1489     0,  /* doc string */                                                \
1490     lname,                                                              \
1491     (lisp_fn_t) Fname                                                   \
1492   };                                                                    \
1493   Lisp_Object Fname (DEFUN_##max_args arglist)
1494
1495 /* Heavy ANSI C preprocessor hackery to get DEFUN to declare a
1496    prototype that matches max_args, and add the obligatory
1497    `Lisp_Object' type declaration to the formal C arguments.  */
1498
1499 #define DEFUN_MANY(named_int, named_Lisp_Object) named_int, named_Lisp_Object
1500 #define DEFUN_UNEVALLED(args) Lisp_Object args
1501 #define DEFUN_0() void
1502 #define DEFUN_1(a)                                      Lisp_Object a
1503 #define DEFUN_2(a,b)             DEFUN_1(a),            Lisp_Object b
1504 #define DEFUN_3(a,b,c)           DEFUN_2(a,b),          Lisp_Object c
1505 #define DEFUN_4(a,b,c,d)         DEFUN_3(a,b,c),        Lisp_Object d
1506 #define DEFUN_5(a,b,c,d,e)       DEFUN_4(a,b,c,d),      Lisp_Object e
1507 #define DEFUN_6(a,b,c,d,e,f)     DEFUN_5(a,b,c,d,e),    Lisp_Object f
1508 #define DEFUN_7(a,b,c,d,e,f,g)   DEFUN_6(a,b,c,d,e,f),  Lisp_Object g
1509 #define DEFUN_8(a,b,c,d,e,f,g,h) DEFUN_7(a,b,c,d,e,f,g),Lisp_Object h
1510
1511 /* WARNING: If you add defines here for higher values of max_args,
1512    make sure to also fix the clauses in PRIMITIVE_FUNCALL(),
1513    and change the define of SUBR_MAX_ARGS above.  */
1514
1515 #include "symeval.h"
1516
1517 /* `specpdl' is the special binding/unwind-protect stack.
1518
1519    Knuth says (see the Jargon File):
1520    At MIT, `pdl' [abbreviation for `Push Down List'] used to
1521    be a more common synonym for `stack'.
1522    Everywhere else `stack' seems to be the preferred term.
1523
1524    specpdl_depth is the current depth of `specpdl'.
1525    Save this for use later as arg to `unbind_to'.  */
1526 extern int specpdl_depth_counter;
1527 #define specpdl_depth() specpdl_depth_counter
1528
1529 #define CHECK_FUNCTION(fun) do {                \
1530  while (NILP (Ffunctionp (fun)))                \
1531    signal_invalid_function_error (fun);         \
1532  } while (0)
1533 \f
1534 /************************************************************************/
1535 /*                         Checking for QUIT                            */
1536 /************************************************************************/
1537
1538 /* Asynchronous events set something_happened, and then are processed
1539    within the QUIT macro.  At this point, we are guaranteed to not be in
1540    any sensitive code. */
1541
1542 extern volatile int something_happened;
1543 int check_what_happened(void);
1544
1545 extern volatile int quit_check_signal_happened;
1546 extern volatile int quit_check_signal_tick_count;
1547 int check_quit(void);
1548
1549 void signal_quit(void);
1550
1551 /* Nonzero if ought to quit now.  */
1552 #define QUITP                                                   \
1553   ((quit_check_signal_happened ? check_quit () : 0),            \
1554    (!NILP (Vquit_flag) && (NILP (Vinhibit_quit)                 \
1555                            || EQ (Vquit_flag, Qcritical))))
1556
1557 /* QUIT used to call QUITP, but there are some places where QUITP
1558    is called directly, and check_what_happened() should only be called
1559    when Emacs is actually ready to quit because it could do things
1560    like switch threads. */
1561 #define INTERNAL_QUITP                                          \
1562   ((something_happened ? check_what_happened () : 0),           \
1563    (!NILP (Vquit_flag) &&                                       \
1564     (NILP (Vinhibit_quit) || EQ (Vquit_flag, Qcritical))))
1565
1566 #define INTERNAL_REALLY_QUITP                                   \
1567   (check_what_happened (),                                      \
1568    (!NILP (Vquit_flag) &&                                       \
1569     (NILP (Vinhibit_quit) || EQ (Vquit_flag, Qcritical))))
1570
1571 /* Check quit-flag and quit if it is non-nil.  Also do any other things
1572    that might have gotten queued until it was safe. */
1573 #define QUIT do { if (INTERNAL_QUITP) signal_quit (); } while (0)
1574
1575 #define REALLY_QUIT do { if (INTERNAL_REALLY_QUITP) signal_quit (); } while (0)
1576 \f
1577 /************************************************************************/
1578 /*                               hashing                                */
1579 /************************************************************************/
1580 typedef long unsigned int hcode_t;
1581
1582 /* #### for a 64-bit machine, we should substitute a prime just over 2^32 */
1583 #define GOOD_HASH 65599         /* prime number just over 2^16; Dragon book, p. 435 */
1584 #define HASH2(a,b)               (GOOD_HASH * (a)                     + (b))
1585 #define HASH3(a,b,c)             (GOOD_HASH * HASH2 (a,b)             + (c))
1586 #define HASH4(a,b,c,d)           (GOOD_HASH * HASH3 (a,b,c)           + (d))
1587 #define HASH5(a,b,c,d,e)         (GOOD_HASH * HASH4 (a,b,c,d)         + (e))
1588 #define HASH6(a,b,c,d,e,f)       (GOOD_HASH * HASH5 (a,b,c,d,e)       + (f))
1589 #define HASH7(a,b,c,d,e,f,g)     (GOOD_HASH * HASH6 (a,b,c,d,e,f)     + (g))
1590 #define HASH8(a,b,c,d,e,f,g,h)   (GOOD_HASH * HASH7 (a,b,c,d,e,f,g)   + (h))
1591 #define HASH9(a,b,c,d,e,f,g,h,i) (GOOD_HASH * HASH8 (a,b,c,d,e,f,g,h) + (i))
1592
1593 #define LISP_HASH(obj) ((hcode_t)LISP_TO_VOID(obj))
1594 hcode_t string_hash(const char *xv);
1595 hcode_t memory_hash(const void *xv, size_t size);
1596 hcode_t internal_hash(const Lisp_Object obj, int depth);
1597 hcode_t internal_array_hash(const Lisp_Object *arr, size_t size, int depth);
1598 \f
1599 /************************************************************************/
1600 /*                       String translation                             */
1601 /************************************************************************/
1602
1603 #ifdef I18N3
1604 #ifdef HAVE_LIBINTL_H
1605 #include <libintl.h>
1606 #else
1607 char *dgettext(const char *, const char *);
1608 char *gettext(const char *);
1609 char *textdomain(const char *);
1610 char *bindtextdomain(const char *, const char *);
1611 #endif                          /* HAVE_LIBINTL_H */
1612
1613 #define GETTEXT(x)  gettext(x)
1614 #define LISP_GETTEXT(x)  Fgettext (x)
1615 #else                           /* !I18N3 */
1616 #define GETTEXT(x)  (x)
1617 #define LISP_GETTEXT(x)  (x)
1618 #endif                          /* !I18N3 */
1619
1620 /* DEFER_GETTEXT is used to identify strings which are translated when
1621    they are referenced instead of when they are defined.
1622    These include Qerror_messages and initialized arrays of strings.
1623 */
1624 #define DEFER_GETTEXT(x) (x)
1625 \f
1626 /************************************************************************/
1627 /*                   Garbage collection / GC-protection                 */
1628 /************************************************************************/
1629
1630 #include "dllist.h"
1631 #if (defined EF_USE_POM || defined EF_USE_ASYNEQ) &&    \
1632         !(defined HAVE_BDWGC && defined EF_USE_BDWGC)
1633 #include "semaphore.h"
1634 extern sxe_mutex_t cons_mutex;
1635
1636 extern_inline void
1637 lock_allocator(void)
1638         __attribute__((always_inline));
1639 extern_inline void
1640 lock_allocator(void)
1641 {
1642         SXE_DEBUG_GC_PT("locking cons mutex.\n");
1643         SXE_MUTEX_LOCK(&cons_mutex);
1644 }
1645
1646 extern_inline void
1647 unlock_allocator(void)
1648         __attribute__((always_inline));
1649 extern_inline void
1650 unlock_allocator(void)
1651 {
1652         SXE_DEBUG_GC_PT("unlocking cons mutex.\n");
1653         SXE_MUTEX_UNLOCK(&cons_mutex);
1654 }
1655
1656 #else  /* !EF_USE_POM || !BDWGC */
1657
1658 extern_inline void
1659 lock_allocator(void)
1660         __attribute__((always_inline));
1661 extern_inline void
1662 lock_allocator(void)
1663 {
1664 }
1665
1666 extern_inline void
1667 unlock_allocator(void)
1668         __attribute__((always_inline));
1669 extern_inline void
1670 unlock_allocator(void)
1671 {
1672 }
1673 #endif
1674
1675 /* number of bytes of structure consed since last GC */
1676
1677 extern EMACS_INT consing_since_gc;
1678
1679 /* threshold for doing another gc */
1680
1681 extern Fixnum gc_cons_threshold;
1682
1683 /* Structure for recording stack slots that need marking */
1684
1685 /* This is a chain of structures, each of which points at a Lisp_Object
1686    variable whose value should be marked in garbage collection.
1687    Normally every link of the chain is an automatic variable of a function,
1688    and its `val' points to some argument or local variable of the function.
1689    On exit to the function, the chain is set back to the value it had on
1690    entry.  This way, no link remains in the chain when the stack frame
1691    containing the link disappears.
1692
1693    Every function that can call Feval must protect in this fashion all
1694    Lisp_Object variables whose contents will be used again. */
1695
1696 extern struct gcpro *gcprolist;
1697
1698 struct gcpro {
1699         struct gcpro *next;
1700         Lisp_Object *var;       /* Address of first protected variable */
1701         int nvars;              /* Number of consecutive protected variables */
1702 };
1703
1704 #if defined(EF_USE_ASYNEQ)
1705 #include "events/workers.h"
1706
1707 extern void init_threads(int, sxe_thread_f);
1708 extern void fini_threads(int);
1709 extern dllist_t workers;
1710
1711 extern_inline struct gcpro *_get_gcprolist(void);
1712 extern_inline void _set_gcprolist(struct gcpro *provar);
1713
1714 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
1715 extern_inline struct gcpro*
1716 _get_gcprolist(void)
1717 {
1718         return NULL;
1719 }
1720
1721 extern_inline void
1722 _set_gcprolist(struct gcpro *provar)
1723 {
1724         return;
1725 }
1726
1727 #else  /* !BDWGC */
1728
1729 extern_inline struct gcpro*
1730 _get_gcprolist(void)
1731 {
1732         WITH_DLLIST_TRAVERSE(
1733                 workers,
1734                 eq_worker_t eqw = dllist_item;
1735                 sxe_thread_t me = pthread_self();
1736                 if (eq_worker_thread(eqw) == me) {
1737                         RETURN_FROM_DLLIST_TRAVERSE(
1738                                 workers, eq_worker_gcprolist(eqw));
1739                 });
1740         return NULL;
1741 }
1742
1743 extern_inline void
1744 _set_gcprolist(struct gcpro *provar)
1745 {
1746         WITH_DLLIST_TRAVERSE(
1747                 workers,
1748                 eq_worker_t eqw = dllist_item;
1749                 sxe_thread_t me = pthread_self();
1750                 if (eq_worker_thread(eqw) == me) {
1751                         eq_worker_gcprolist(eqw) = provar;
1752                         RETURN_FROM_DLLIST_TRAVERSE(workers, );
1753                 });
1754         return;
1755 }
1756 #endif  /* BDWGC */
1757
1758 #else  /* !EF_USE_ASYNEQ */
1759
1760 #define _get_gcprolist()        gcprolist
1761 #define _set_gcprolist(_var)    gcprolist = (_var)
1762
1763 #endif  /* EF_USE_ASYNEQ */
1764
1765 /* Normally, you declare variables gcpro1, gcpro2, ... and use the
1766    GCPROn() macros.  However, if you need to have nested gcpro's,
1767    declare ngcpro1, ngcpro2, ... and use NGCPROn().  If you need
1768    to nest another level, use nngcpro1, nngcpro2, ... and use
1769    NNGCPROn().  If you need to nest yet another level, create
1770    the appropriate macros. */
1771
1772 #if 1
1773 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
1774
1775 /* tricks to get over a myriad unused variable warnings */
1776 #define gcpro1          gcpro1 __attribute__((unused))
1777 #define gcpro2          gcpro2 __attribute__((unused))
1778 #define gcpro3          gcpro3 __attribute__((unused))
1779 #define gcpro4          gcpro4 __attribute__((unused))
1780 #define gcpro5          gcpro5 __attribute__((unused))
1781 #define gcpro6          gcpro6 __attribute__((unused))
1782 #define gcpro7          gcpro7 __attribute__((unused))
1783 #define gcpro8          gcpro8 __attribute__((unused))
1784
1785 #define ngcpro1         ngcpro1 __attribute__((unused))
1786 #define ngcpro2         ngcpro2 __attribute__((unused))
1787 #define ngcpro3         ngcpro3 __attribute__((unused))
1788 #define ngcpro4         ngcpro4 __attribute__((unused))
1789 #define ngcpro5         ngcpro5 __attribute__((unused))
1790 #define ngcpro6         ngcpro6 __attribute__((unused))
1791 #define ngcpro7         ngcpro7 __attribute__((unused))
1792 #define ngcpro8         ngcpro8 __attribute__((unused))
1793
1794 #define nngcpro1        nngcpro1 __attribute__((unused))
1795 #define nngcpro2        nngcpro2 __attribute__((unused))
1796 #define nngcpro3        nngcpro3 __attribute__((unused))
1797 #define nngcpro4        nngcpro4 __attribute__((unused))
1798 #define nngcpro5        nngcpro5 __attribute__((unused))
1799 #define nngcpro6        nngcpro6 __attribute__((unused))
1800 #define nngcpro7        nngcpro7 __attribute__((unused))
1801 #define nngcpro8        nngcpro8 __attribute__((unused))
1802
1803 #define GCPRO1(args...)
1804 #define GCPRO2(args...)
1805 #define GCPRO3(args...)
1806 #define GCPRO4(args...)
1807 #define GCPRO5(args...)
1808 #define GCPRO6(args...)
1809 #define GCPRO7(args...)
1810 #define GCPRO8(args...)
1811 #define GCPROn(args...)
1812 #define GCPRO1n(args...)
1813 #define GCPRO2n(args...)
1814 #define GCPRO3n(args...)
1815 #define GCPRO1nn(args...)
1816 #define UNGCPRO
1817
1818 #define NGCPRO1(args...)
1819 #define NGCPRO2(args...)
1820 #define NGCPRO3(args...)
1821 #define NGCPRO4(args...)
1822 #define NGCPRO5(args...)
1823 #define NGCPRO6(args...)
1824 #define NGCPRO7(args...)
1825 #define NGCPRO8(args...)
1826 #define NGCPROn(args...)
1827 #define NGCPRO1n(args...)
1828 #define NUNGCPRO
1829
1830 #define NNGCPRO1(args...)
1831 #define NNGCPRO2(args...)
1832 #define NNGCPRO3(args...)
1833 #define NNGCPRO4(args...)
1834 #define NNGCPRO5(args...)
1835 #define NNGCPRO6(args...)
1836 #define NNGCPRO7(args...)
1837 #define NNGCPRO8(args...)
1838 #define NNGCPROn(args...)
1839 #define NNUNGCPRO
1840
1841 #else  /* !BDWGC */
1842
1843 #define GCPRO1(var1)                                                    \
1844         ((void)(                                                        \
1845                 lock_allocator(),                                       \
1846                 gcpro1.next = _get_gcprolist(),                         \
1847                 gcpro1.var = &var1, gcpro1.nvars = 1,                   \
1848                 _set_gcprolist(&gcpro1),                                \
1849                 unlock_allocator()))
1850
1851 #define GCPRO2(var1, var2)                                              \
1852         ((void)(                                                        \
1853                 lock_allocator(),                                       \
1854                 gcpro1.next = _get_gcprolist(),                         \
1855                 gcpro1.var = &var1, gcpro1.nvars = 1,                   \
1856                 gcpro2.next = &gcpro1,                                  \
1857                 gcpro2.var = &var2, gcpro2.nvars = 1,                   \
1858                 _set_gcprolist(&gcpro2),                                \
1859                 unlock_allocator()))
1860
1861 #define GCPRO3(var1, var2, var3)                                        \
1862         ((void)(                                                        \
1863                 lock_allocator(),                                       \
1864                 gcpro1.next = _get_gcprolist(),                         \
1865                 gcpro1.var = &var1, gcpro1.nvars = 1,                   \
1866                 gcpro2.next = &gcpro1,                                  \
1867                 gcpro2.var = &var2, gcpro2.nvars = 1,                   \
1868                 gcpro3.next = &gcpro2,                                  \
1869                 gcpro3.var = &var3, gcpro3.nvars = 1,                   \
1870    &nbs