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.
7 This file is part of SXEmacs
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.
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.
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/>. */
23 /* Synched up with: FSF 19.30. */
25 #ifndef INCLUDED_lisp_h_
26 #define INCLUDED_lisp_h_
28 /************************************************************************/
29 /* general definitions */
30 /************************************************************************/
32 /* ------------------------ include files ------------------- */
34 /* We include the following generally useful header files so that you
35 don't have to worry about prototypes when using the standard C
36 library functions and macros. These files shouldn't be excessively
37 large so they shouldn't cause that much of a slowdown. */
40 #include <string.h> /* primarily for memcpy, etc. */
41 #include <stdio.h> /* NULL, etc. */
44 #include <stddef.h> /* offsetof */
45 #include <sys/types.h>
47 #if defined HAVE_STDBOOL_H
53 #elif defined(__GNUC__)
54 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
55 #elif defined(__LCLINT__)
56 # define UNUSED(x) /*@unused@*/ x
62 #elif defined(__GNUC__)
63 # define WEAK_EXTERN extern __attribute__((weak))
65 # error "Grrr, bloody 'ell, can't figure out how to create weak symbols"
69 #elif defined(__GNUC__)
70 # define WEAK __attribute__((weak))
72 # error "Grrr, bloody 'ell, can't figure out how to create weak symbols"
78 #define LIKELY(_x) __builtin_expect(!!(_x), 1)
82 #define UNLIKELY(_x) __builtin_expect(!!(_x), 0)
85 #if defined SXE_STATIC_EXTERN_INLINE
86 # define extern_inline static inline
87 #elif !defined __GNUC_STDC_INLINE__ && !defined SXE_INLINE_NO_EXTERN
88 # define extern_inline extern inline
90 # define extern_inline inline
93 #ifdef ALL_DEBUG_FLAGS
95 #define SXE_DEBUG_FLAG
98 #define __SXE_DEBUG__(args...) fprintf(stderr, "SXE " args)
99 #ifndef SXE_DEBUG_FLAG
100 #define SXE_DEBUG(args...)
102 #define SXE_DEBUG(args...) __SXE_DEBUG__(args)
104 #define SXE_DEBUG_PT(args...) SXE_DEBUG("[pthread]: " args)
105 #define SXE_CRITICAL(args...) __SXE_DEBUG__("CRITICAL: " args)
109 /* We define assert iff USE_ASSERTIONS or DEBUG_SXEMACS is defined.
110 Otherwise we define it to be empty. Quantify has shown that the
111 time the assert checks take is measurable so let's not include them
112 in production binaries. */
114 #ifdef USE_ASSERTIONS
115 /* Highly dubious kludge */
116 /* (thanks, Jamie, I feel better now -- ben) */
117 void assert_failed(const char *, int, const char *);
118 # define abort() (assert_failed (__FILE__, __LINE__, "abort()"))
119 # define assert(x) ((x) ? (void) 0 : assert_failed (__FILE__, __LINE__, #x))
121 # ifdef DEBUG_SXEMACS
122 # define assert(x) ((x) ? (void) 0 : (void) abort ())
129 #ifndef BITS_PER_CHAR
130 #define BITS_PER_CHAR 8
132 #define SXE_SHORTBITS (SIZEOF_SHORT * BITS_PER_CHAR)
133 #define SXE_INTBITS (SIZEOF_INT * BITS_PER_CHAR)
134 #define SXE_LONGBITS (SIZEOF_LONG * BITS_PER_CHAR)
135 #define SXE_LONG_LONG_BITS (SIZEOF_LONG_LONG_INT * BITS_PER_CHAR)
136 #define SXE_VOID_P_BITS (SIZEOF_VOID_P * BITS_PER_CHAR)
139 /* ------------------------ dynamic arrays ------------------- */
141 #define Dynarr_declare(type) \
148 typedef struct dynarr {
149 Dynarr_declare(void);
152 void *Dynarr_newf(int elsize);
153 void Dynarr_resize(void *dy, int size);
154 void Dynarr_insert_many(void *d, const void *el, int len, int start);
155 void Dynarr_delete_many(void *d, int start, int len);
156 void Dynarr_free(void *d);
158 #define Dynarr_new(type) ((type##_dynarr *) Dynarr_newf (sizeof (type)))
159 #define Dynarr_new2(dynarr_type, type) \
160 ((dynarr_type *) Dynarr_newf (sizeof (type)))
161 #define Dynarr_at(d, pos) ((d)->base[pos])
162 #define Dynarr_atp(d, pos) (&Dynarr_at (d, pos))
163 #define Dynarr_begin(d) Dynarr_atp (d, 0)
164 #define Dynarr_end(d) Dynarr_atp (d, Dynarr_length (d) - 1)
165 #define Dynarr_sizeof(d) ((d)->cur * (d)->elsize)
166 #define Dynarr_length(d) ((d)->cur)
167 #define Dynarr_largest(d) ((d)->largest)
168 #define Dynarr_reset(d) ((d)->cur = 0)
169 #define Dynarr_add_many(d, el, len) Dynarr_insert_many (d, el, len, (d)->cur)
170 #define Dynarr_insert_many_at_start(d, el, len) \
171 Dynarr_insert_many (d, el, len, 0)
172 #define Dynarr_add_literal_string(d, s) Dynarr_add_many (d, s, sizeof (s) - 1)
173 #define Dynarr_add_lisp_string(d, s) do { \
174 Lisp_String *dyna_ls_s = XSTRING (s); \
175 Dynarr_add_many (d, (char *) string_data (dyna_ls_s), \
176 string_length (dyna_ls_s)); \
179 #define Dynarr_add(d, el) ( \
180 (d)->cur >= (d)->max ? Dynarr_resize ((d), (d)->cur+1) : (void) 0, \
181 ((d)->base)[(d)->cur++] = (el), \
182 (d)->cur > (d)->largest ? (d)->largest = (d)->cur : (int) 0)
184 /* The following defines will get you into real trouble if you aren't
185 careful. But they can save a lot of execution time when used wisely. */
186 #define Dynarr_increment(d) ((d)->cur++)
187 #define Dynarr_set_size(d, n) ((d)->cur = n)
189 #ifdef MEMORY_USAGE_STATS
190 struct overhead_stats;
191 size_t Dynarr_memory_usage(void *d, struct overhead_stats *stats);
194 /* Also define min() and max(). (Some compilers put them in strange
195 places that won't be referenced by the above include files, such
196 as 'macros.h' under Solaris.) */
199 #define min(a,b) (((a) <= (b)) ? (a) : (b))
202 #define max(a,b) (((a) > (b)) ? (a) : (b))
206 /************************************************************************/
208 /************************************************************************/
210 #ifdef ALL_DEBUG_FLAGS
212 #define GC_DEBUG_FLAG
215 #if !defined GC_DEBUG_FLAG
216 # define SXE_DEBUG_GC(args...)
218 # define SXE_DEBUG_GC(args...) __SXE_DEBUG__("[gc] " args)
220 #define SXE_DEBUG_GC_PT(args...) SXE_DEBUG_GC("[pthread]: " args)
221 #define SXE_CRITICAL_GC(args...) __SXE_DEBUG__("[gc] CRITICAL: " args)
223 void malloc_warning(const char *);
225 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
226 # if defined HAVE_THREADS
227 # if !defined GC_PTHREADS
228 # define GC_PTHREADS 1
229 # endif /* !GC_PTHREADS */
230 # if !defined GC_THREADS
231 # define GC_THREADS 1
232 # endif /* !GC_THREADS */
233 # endif /* HAVE_THREADS */
236 # if defined GC_DEBUG_FLAG
238 # endif /* GC_DEBUG_FLAG */
239 # if defined HAVE_GC_GC_H
241 # elif defined HAVE_GC_H
244 # error "Take less of those pills!"
247 # if defined GC_DEBUG_FLAG
248 # define zmalloc GC_MALLOC_IGNORE_OFF_PAGE
249 # define zcalloc(n, m) GC_MALLOC((n)*(m))
250 # define zmalloc_atomic GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE
251 # define zmalloc_and_zero GC_MALLOC
252 # define zrealloc GC_REALLOC
253 # define zstrdup GC_STRDUP
255 # define zfree(x) GC_FREE(x)
256 # else /* !GC_DEBUG_FLAG */
257 # define zmalloc GC_malloc_ignore_off_page
258 # define zcalloc(n, m) GC_malloc((n)*(m))
259 # define zmalloc_atomic GC_malloc_atomic_ignore_off_page
260 # define zmalloc_and_zero GC_malloc
261 # define zrealloc GC_realloc
262 # define zstrdup GC_strdup
265 # endif /* GC_DEBUG_FLAG */
270 #define zmalloc_atomic F&^!
271 #define zmalloc_and_zero F&^!
272 #define zrealloc F&^!
273 #define zstrdrup F&^!
276 /* also define libc mem funs */
277 #define ymalloc malloc
278 #define ycalloc(n, m) calloc(n, m)
279 #define ymalloc_atomic(n) ycalloc(1, n)
280 #define ymalloc_and_zero(x) ycalloc(1, x)
281 #define yrealloc realloc
282 #define ystrdup strdup
283 #define yfree(x) free(x)
284 /* and their convenience companions */
285 #define ynew(type) ((type*)ymalloc(sizeof(type)))
286 #define ynew_array(type, len) ((type*)ymalloc((len) * sizeof(type)))
287 #define ynew_and_zero(type) ((type*)ymalloc_and_zero(sizeof(type)))
288 #define ynew_array_and_zero(type, len) \
289 ((type*)ymalloc_and_zero((len) * sizeof(type)))
290 #define YREALLOC_ARRAY(ptr, type, len) \
291 ((void)(ptr = (type *)yrealloc(ptr, (len) * sizeof (type))))
293 #if defined HAVE_BDWGC && defined EF_USE_BDWGC
294 /* and now the x* series */
295 # define xmalloc zmalloc
296 # define xcalloc zcalloc
297 # define xmalloc_atomic zmalloc_atomic
298 # define xmalloc_and_zero zmalloc_and_zero
299 # define xrealloc zrealloc
300 # define xstrdup zstrdup
301 # if defined ERROR_CHECK_MALLOC
302 # define xfree(args...) zfree(args)
303 # else /* !ERROR_CHECK_MALLOC */
304 # define xfree(args...)
305 # endif /* ERROR_CHECK_MALLOC */
308 void *xmalloc(size_t size);
309 void *xmalloc_atomic(size_t size);
310 void *xmalloc_and_zero(size_t size);
311 void *xrealloc(void *, size_t size);
312 char *xstrdup(const char *);
313 # if defined ERROR_CHECK_MALLOC
314 # if SIZEOF_VOID_P == 4
315 # define xfree(lvalue) \
317 void *volatile *xfree_ptr = \
319 ((volatile void*)&(lvalue)); \
320 assert(*xfree_ptr != (void*)0xB00BB4BE); \
322 *xfree_ptr = (void*)0xB00BB4BE; \
324 # elif SIZEOF_VOID_P == 8
325 # define xfree(lvalue) \
327 void *volatile *xfree_ptr = \
329 ((volatile void*)&(lvalue)); \
330 assert(*xfree_ptr != (void*)0xCAFEBABEDEADBEEF); \
332 *xfree_ptr = (void*)0xCAFEBABEDEADBEEF; \
335 # error "Strange-arse system detected. Watch a movie, it\'s more fun!"
337 # else /* !ERROR_CHECK_MALLOC */
338 # define xfree(args...) yfree(args)
339 # endif /* ERROR_CHECK_MALLOC */
342 /* generally useful */
343 #define countof(x) ((int) (sizeof(x)/sizeof((x)[0])))
344 #define xnew(type) ((type *) xmalloc (sizeof (type)))
345 #define xnew_atomic(type) ((type *) xmalloc_atomic (sizeof (type)))
346 #define xnew_array(type, len) ((type *) xmalloc ((len) * sizeof (type)))
347 #define xnew_atomic_array(type, len) \
348 ((type*)xmalloc_atomic((len) * sizeof(type)))
349 #define xnew_and_zero(type) ((type *) xmalloc_and_zero (sizeof (type)))
350 #define xzero(lvalue) ((void) memset (&(lvalue), '\0', sizeof (lvalue)))
351 #define xnew_array_and_zero(type, len) \
352 ((type*)xmalloc_and_zero ((len) * sizeof (type)))
353 #define xrealloc_array(ptr, type, len) \
354 ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
355 #define XREALLOC_ARRAY xrealloc_array
356 #define alloca_array(type, len) ((type *) alloca ((len) * sizeof (type)))
358 #if !defined HAVE_DECL_STRDUP
359 extern char *strdup(const char *s);
360 #endif /* HAVE_DECL_STRDUP */
363 # if defined (__GNUC__) && (__GNUC__ >= 2)
364 # define PRINTF_ARGS(string_index,first_to_check) \
365 __attribute__ ((format (printf, string_index, first_to_check)))
367 # define PRINTF_ARGS(string_index,first_to_check)
371 #ifndef DOESNT_RETURN
372 # if defined __GNUC__
373 # if ((__GNUC__ > 2) || (__GNUC__ == 2) && (__GNUC_MINOR__ >= 5))
374 # define DOESNT_RETURN void
375 # define DECLARE_DOESNT_RETURN(decl) \
376 extern void decl __attribute__ ((noreturn))
377 # define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
378 /* Should be able to state multiple independent __attribute__s, but \
379 the losing syntax doesn't work that way, and screws losing cpp */ \
381 __attribute__ ((noreturn, format (printf, str, idx)))
383 # define DOESNT_RETURN void volatile
384 # define DECLARE_DOESNT_RETURN(decl) extern void volatile decl
385 # define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
386 extern void volatile decl PRINTF_ARGS(str,idx)
387 # endif /* GNUC 2.5 */
389 # define DOESNT_RETURN void
390 # define DECLARE_DOESNT_RETURN(decl) extern void decl
391 # define DECLARE_DOESNT_RETURN_GCC_ATTRIBUTE_SYNTAX_SUCKS(decl,str,idx) \
392 extern void decl PRINTF_ARGS(str,idx)
396 /* No type has a greater alignment requirement than max_align_t.
397 (except perhaps for types we don't use, like long double) */
414 # if defined (__GNUC__) && (__GNUC__ >= 2)
415 /* gcc has an extension that gives us exactly what we want. */
416 # define ALIGNOF(type) __alignof__ (type)
417 # elif ! defined (__cplusplus)
418 /* The following is mostly portable, except that:
419 - it doesn't work for inside out declarations like void (*) (void).
420 (so just call ALIGNOF with a typedef'ed name)
421 - it doesn't work with C++. The C++ committee has decided,
422 in its infinite wisdom, that:
423 "Types must be declared in declarations, not in expressions." */
424 # define ALIGNOF(type) offsetof (struct { char c; type member; }, member)
426 /* C++ is annoying, but it has a big bag of tricks.
427 The following doesn't have the "inside out" declaration bug C does. */
428 template < typename T > struct alignment_trick {
432 # define ALIGNOF(type) offsetof (alignment_trick<type>, member)
436 #define ALIGN_SIZE(len, unit) \
437 ((((len) + (unit) - 1) / (unit)) * (unit))
439 /* #### Yuck, this is kind of evil */
440 #define ALIGN_PTR(ptr, unit) \
441 ((void *) ALIGN_SIZE ((size_t) (ptr), unit))
444 #define DO_NOTHING do {} while (0)
447 #ifndef DECLARE_NOTHING
448 #define DECLARE_NOTHING struct nosuchstruct
451 /*#ifdef DEBUG_SXEMACS*/
455 /*#define REGISTER register*/
458 /* EMACS_INT is the underlying integral type into which a Lisp_Object must fit.
459 In particular, it must be large enough to contain a pointer.
460 config.h can override this, e.g. to use `long long' for bigger lisp ints.
462 #### In point of fact, it would NOT be a good idea for config.h to mess
463 with EMACS_INT. A lot of code makes the basic assumption that EMACS_INT
464 is the size of a pointer. */
466 #ifndef SIZEOF_EMACS_INT
467 # define SIZEOF_EMACS_INT SIZEOF_VOID_P
471 # if SIZEOF_EMACS_INT == SIZEOF_LONG
472 # define EMACS_INT long
473 # elif SIZEOF_EMACS_INT == SIZEOF_INT
474 # define EMACS_INT int
475 # elif SIZEOF_EMACS_INT == SIZEOF_LONG_LONG_INT
476 # define EMACS_INT long long
478 # error Unable to determine suitable type for EMACS_INT
483 # define EMACS_UINT unsigned EMACS_INT
486 #define BITS_PER_EMACS_INT (SIZEOF_EMACS_INT * BITS_PER_CHAR)
488 /************************************************************************/
490 /************************************************************************/
492 /* We put typedefs here so that prototype declarations don't choke.
493 Note that we don't actually declare the structures here (except
494 maybe for simple structures like Dynarrs); that keeps them private
495 to the routines that actually use them. */
497 /* ------------------------------- */
498 /* basic char/int typedefs */
499 /* ------------------------------- */
501 /* The definitions we put here use typedefs to attribute specific meaning
502 to types that by themselves are pretty general. Stuff pointed to by a
503 char * or unsigned char * will nearly always be one of four types:
504 a) pointer to internally-formatted text; b) pointer to text in some
505 external format, which can be defined as all formats other than the
506 internal one; c) pure ASCII text; d) binary data that is not meant to
507 be interpreted as text. [A fifth possible type "e) a general pointer
508 to memory" should be replaced with void *.] Using these more specific
509 types rather than the general ones helps avoid the confusions that
510 occur when the semantics of a char * argument being studied are unclear. */
512 typedef unsigned char UChar;
514 /* The data representing the text in a buffer is logically a set
515 of Bufbytes, declared as follows. */
517 typedef UChar Bufbyte;
519 /* Explicitly signed or unsigned versions: */
520 typedef UChar UBufbyte;
521 typedef char SBufbyte;
523 /* The data representing a string in "external" format (binary or any
524 external encoding) is logically a set of Extbytes, declared as
525 follows. Extbyte is guaranteed to be just a char, so for example
526 strlen (Extbyte *) is OK. Extbyte is only a documentation device
527 for referring to external text. */
529 typedef char Extbyte;
531 /* A byte in a string in binary format: */
532 typedef char Char_Binary;
533 typedef UChar UChar_Binary;
535 /* A byte in a string in entirely US-ASCII format: (Nothing outside
536 the range 00 - 7F) */
538 typedef char Char_ASCII;
539 typedef UChar UChar_ASCII;
541 /* To the user, a buffer is made up of characters, declared as follows.
542 In the non-Mule world, characters and Bufbytes are equivalent.
543 In the Mule world, a character requires (typically) 1 to 4
544 Bufbytes for its representation in a buffer. */
548 /* Different ways of referring to a position in a buffer. We use
549 the typedefs in preference to 'EMACS_INT' to make it clearer what
550 sort of position is being used. See extents.c for a description
551 of the different positions. We put them here instead of in
552 buffer.h (where they rightfully belong) to avoid syntax errors
553 in function prototypes. */
555 typedef EMACS_INT Bufpos;
556 typedef EMACS_INT Bytind;
557 typedef EMACS_INT Memind;
559 /* Counts of bytes or chars */
561 typedef EMACS_INT Bytecount;
562 typedef EMACS_INT Charcount;
564 /* Length in bytes of a string in external format */
565 typedef EMACS_INT Extcount;
567 /* ------------------------------- */
568 /* structure/other typedefs */
569 /* ------------------------------- */
571 /* Counts of bytes or array elements */
572 typedef EMACS_INT Memory_count;
573 typedef EMACS_INT Element_count;
575 /* is this right here? */
576 typedef struct lstream_s *lstream_t;
578 typedef struct lstream_s Lstream;
580 typedef unsigned int face_index;
583 Dynarr_declare(struct face_cachel);
584 } face_cachel_dynarr;
586 typedef unsigned int glyph_index;
588 /* This is shared by process.h, events.h and others in future.
589 See events.h for description */
590 typedef long unsigned int USID;
593 Dynarr_declare(struct glyph_cachel);
594 } glyph_cachel_dynarr;
596 struct buffer; /* "buffer.h" */
597 struct console; /* "console.h" */
598 struct device; /* "device.h" */
599 struct extent_fragment;
601 typedef struct extent *EXTENT;
602 struct frame; /* "frame.h" */
603 struct window; /* "window.h" */
604 typedef struct Lisp_Event Lisp_Event; /* "events.h" */
605 typedef struct Lisp_Face Lisp_Face; /* "faces.h" */
606 typedef struct Lisp_Process Lisp_Process; /* "procimpl.h" */
607 struct stat; /* <sys/stat.h> */
608 typedef struct Lisp_Color_Instance Lisp_Color_Instance;
609 typedef struct Lisp_Font_Instance Lisp_Font_Instance;
610 typedef struct Lisp_Image_Instance Lisp_Image_Instance;
611 typedef struct Lisp_Gui_Item Lisp_Gui_Item;
613 struct display_glyph_area;
615 struct redisplay_info;
616 struct window_mirror;
617 struct scrollbar_instance;
618 struct font_metric_info;
620 struct console_type_entry;
623 Dynarr_declare(Bufbyte);
627 Dynarr_declare(Extbyte);
631 Dynarr_declare(Emchar);
635 Dynarr_declare(char);
638 typedef unsigned char unsigned_char;
640 Dynarr_declare(unsigned char);
641 } unsigned_char_dynarr;
643 typedef unsigned long unsigned_long;
645 Dynarr_declare(unsigned long);
646 } unsigned_long_dynarr;
653 Dynarr_declare(Bufpos);
657 Dynarr_declare(Bytind);
661 Dynarr_declare(Charcount);
665 Dynarr_declare(Bytecount);
669 Dynarr_declare(struct console_type_entry);
670 } console_type_entry_dynarr;
672 enum run_hooks_condition {
673 RUN_HOOKS_TO_COMPLETION,
674 RUN_HOOKS_UNTIL_SUCCESS,
675 RUN_HOOKS_UNTIL_FAILURE