]> code.delx.au - gnu-emacs/blobdiff - src/emacs-module.c
; Revert "Ensure undo-boundary after insert-file-contents."
[gnu-emacs] / src / emacs-module.c
index 22fee7e48607c395ea580ec9daff07009408f567..eca5af739b92846c6be69e7d83435159e8f8035b 100644 (file)
@@ -1,13 +1,13 @@
 /* emacs-module.c - Module loading and runtime implementation
 
-Copyright (C) 2015 Free Software Foundation, Inc.
+Copyright (C) 2015-2016 Free Software Foundation, Inc.
 
 This file is part of GNU Emacs.
 
 GNU Emacs is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 3 of the License, or
-(at your option) any later version.
+the Free Software Foundation, either version 3 of the License, or (at
+your option) any later version.
 
 GNU Emacs is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -35,8 +35,7 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 \f
 /* Feature tests.  */
 
-/* True if __attribute__ ((cleanup (...))) works, false otherwise.  */
-#ifdef HAVE_VAR_ATTRIBUTE_CLEANUP
+#if __has_attribute (cleanup)
 enum { module_has_cleanup = true };
 #else
 enum { module_has_cleanup = false };
@@ -44,10 +43,7 @@ enum { module_has_cleanup = false };
 
 /* Handle to the main thread.  Used to verify that modules call us in
    the right thread.  */
-#ifdef HAVE_THREADS_H
-# include <threads.h>
-static thrd_t main_thread;
-#elif defined HAVE_PTHREAD
+#ifdef HAVE_PTHREAD
 # include <pthread.h>
 static pthread_t main_thread;
 #elif defined WINDOWSNT
@@ -56,6 +52,24 @@ static pthread_t main_thread;
 static DWORD main_thread;
 #endif
 
+/* True if Lisp_Object and emacs_value have the same representation.
+   This is typically true unless WIDE_EMACS_INT.  In practice, having
+   the same sizes and alignments and maximums should be a good enough
+   proxy for equality of representation.  */
+enum
+  {
+    plain_values
+      = (sizeof (Lisp_Object) == sizeof (emacs_value)
+        && alignof (Lisp_Object) == alignof (emacs_value)
+        && INTPTR_MAX == EMACS_INT_MAX)
+  };
+
+/* Function prototype for module user-pointer finalizers.  These
+   should not throw C++ exceptions, so emacs-module.h declares the
+   corresponding interfaces with EMACS_NOEXCEPT.  There is only C code
+   in this module, though, so this constraint is not enforced here.  */
+typedef void (*emacs_finalizer_function) (void *);
+
 \f
 /* Private runtime and environment members.  */
 
@@ -77,7 +91,7 @@ struct emacs_env_private
 struct emacs_runtime_private
 {
   /* FIXME: Ideally, we would just define "struct emacs_runtime_private"
-   * as a synonym of "emacs_env", but I don't know how to do that in C.  */
+     as a synonym of "emacs_env", but I don't know how to do that in C.  */
   emacs_env pub;
 };
 \f
@@ -103,11 +117,17 @@ static void module_reset_handlerlist (const int *);
 static void module_wrong_type (emacs_env *, Lisp_Object, Lisp_Object);
 
 /* We used to return NULL when emacs_value was a different type from
-   Lisp_Object, but nowadays we just use Qnil instead.  */
-static emacs_value module_nil;
+   Lisp_Object, but nowadays we just use Qnil instead.  Although they
+   happen to be the same thing in the current implementation, module
+   code should not assume this.  */
+verify (NIL_IS_ZERO);
+static emacs_value const module_nil = 0;
 \f
 /* Convenience macros for non-local exit handling.  */
 
+/* FIXME: The following implementation for non-local exit handling
+   does not support recovery from stack overflow, see sysdep.c.  */
+
 /* Emacs uses setjmp and longjmp for non-local exits, but
    module frames cannot be skipped because they are in general
    not prepared for long jumps (e.g., the behavior in C++ is undefined
@@ -313,6 +333,7 @@ module_non_local_exit_get (emacs_env *env, emacs_value *sym, emacs_value *data)
   struct emacs_env_private *p = env->private_members;
   if (p->pending_non_local_exit != emacs_funcall_exit_return)
     {
+      /* FIXME: lisp_to_value can exit non-locally.  */
       *sym = lisp_to_value (p->non_local_exit_symbol);
       *data = lisp_to_value (p->non_local_exit_data);
     }
@@ -559,7 +580,7 @@ module_get_user_ptr (emacs_env *env, emacs_value uptr)
 static void
 module_set_user_ptr (emacs_env *env, emacs_value uptr, void *ptr)
 {
-  // FIXME: This function should return bool because it can fail.
+  /* FIXME: This function should return bool because it can fail.  */
   MODULE_FUNCTION_BEGIN ();
   check_main_thread ();
   if (module_non_local_exit_check (env) != emacs_funcall_exit_return)
@@ -587,7 +608,7 @@ static void
 module_set_user_finalizer (emacs_env *env, emacs_value uptr,
                           emacs_finalizer_function fin)
 {
-  // FIXME: This function should return bool because it can fail.
+  /* FIXME: This function should return bool because it can fail.  */
   MODULE_FUNCTION_BEGIN ();
   Lisp_Object lisp = value_to_lisp (uptr);
   if (! USER_PTRP (lisp))
@@ -598,7 +619,7 @@ module_set_user_finalizer (emacs_env *env, emacs_value uptr,
 static void
 module_vec_set (emacs_env *env, emacs_value vec, ptrdiff_t i, emacs_value val)
 {
-  // FIXME: This function should return bool because it can fail.
+  /* FIXME: This function should return bool because it can fail.  */
   MODULE_FUNCTION_BEGIN ();
   Lisp_Object lvec = value_to_lisp (vec);
   if (! VECTORP (lvec))
@@ -641,7 +662,7 @@ module_vec_get (emacs_env *env, emacs_value vec, ptrdiff_t i)
 static ptrdiff_t
 module_vec_size (emacs_env *env, emacs_value vec)
 {
-  // FIXME: Return a sentinel value (e.g., -1) on error.
+  /* FIXME: Return a sentinel value (e.g., -1) on error.  */
   MODULE_FUNCTION_BEGIN (0);
   Lisp_Object lvec = value_to_lisp (vec);
   if (! VECTORP (lvec))
@@ -729,19 +750,18 @@ usage: (module-call ENVOBJ &rest ARGLIST)   */)
   initialize_environment (&pub, &priv);
 
   USE_SAFE_ALLOCA;
-#ifdef WIDE_EMACS_INT
-  emacs_value *args = SAFE_ALLOCA (len * sizeof *args);
-
-  for (ptrdiff_t i = 0; i < len; i++)
-    args[i] = lisp_to_value (arglist[i + 1]);
-#else
-  /* BEWARE!  Here, we assume that Lisp_Object and
-   * emacs_value have the exact same representation.  */
-  emacs_value *args = (emacs_value*) arglist + 1;
-#endif
+  emacs_value *args;
+  if (plain_values)
+    args = (emacs_value *) arglist + 1;
+  else
+    {
+      args = SAFE_ALLOCA (len * sizeof *args);
+      for (ptrdiff_t i = 0; i < len; i++)
+       args[i] = lisp_to_value (arglist[i + 1]);
+    }
 
   emacs_value ret = envptr->subr (&pub, len, args, envptr->data);
-  SAFE_FREE();
+  SAFE_FREE ();
 
   eassert (&priv == pub.private_members);
 
@@ -775,9 +795,7 @@ usage: (module-call ENVOBJ &rest ARGLIST)   */)
 static void
 check_main_thread (void)
 {
-#ifdef HAVE_THREADS_H
-  eassert (thrd_equal (thdr_current (), main_thread));
-#elif defined HAVE_PTHREAD
+#ifdef HAVE_PTHREAD
   eassert (pthread_equal (pthread_self (), main_thread));
 #elif defined WINDOWSNT
   eassert (GetCurrentThreadId () == main_thread);
@@ -838,106 +856,107 @@ module_args_out_of_range (emacs_env *env, Lisp_Object a1, Lisp_Object a2)
 \f
 /* Value conversion.  */
 
-#ifdef WIDE_EMACS_INT
 /* Unique Lisp_Object used to mark those emacs_values which are really
-   just containers holding a Lisp_Object that's too large for emacs_value.  */
+   just containers holding a Lisp_Object that does not fit as an emacs_value,
+   either because it is an integer out of range, or is not properly aligned.
+   Used only if !plain_values.  */
 static Lisp_Object ltv_mark;
-#endif
 
-/* Convert an `emacs_value' to the corresponding internal object.
-   Never fails.  */
+/* Convert V to the corresponding internal object O, such that
+   V == lisp_to_value_bits (O).  Never fails.  */
 static Lisp_Object
-value_to_lisp (emacs_value v)
+value_to_lisp_bits (emacs_value v)
 {
-#ifdef WIDE_EMACS_INT
-  uintptr_t tmp = (uintptr_t)v;
-  unsigned tag = tmp & ((1 << GCTYPEBITS) - 1);
-  Lisp_Object o;
+  intptr_t i = (intptr_t) v;
+  if (plain_values || USE_LSB_TAG)
+    return XIL (i);
+
+  /* With wide EMACS_INT and when tag bits are the most significant,
+     reassembling integers differs from reassembling pointers in two
+     ways.  First, save and restore the least-significant bits of the
+     integer, not the most-significant bits.  Second, sign-extend the
+     integer when restoring, but zero-extend pointers because that
+     makes TAG_PTR faster.  */
+
+  EMACS_UINT tag = i & (GCALIGNMENT - 1);
+  EMACS_UINT untagged = i - tag;
   switch (tag)
     {
     case_Lisp_Int:
-      o = make_lisp_ptr ((void *)((tmp - tag) >> GCTYPEBITS), tag); break;
-    default:
-      o = make_lisp_ptr ((void *)(tmp - tag), tag);
+      {
+       bool negative = tag & 1;
+       EMACS_UINT sign_extension
+         = negative ? VALMASK & ~(INTPTR_MAX >> INTTYPEBITS): 0;
+       uintptr_t u = i;
+       intptr_t all_but_sign = u >> GCTYPEBITS;
+       untagged = sign_extension + all_but_sign;
+       break;
+      }
     }
-  /* eassert (lisp_to_value (o) == v); */
-  if (CONSP (o) && EQ (XCDR (o), ltv_mark))
-    return XCAR (o);
-  else
-    return o;
-#else
-  Lisp_Object o = XIL ((EMACS_INT) v);
-  /* Check the assumption made elsewhere that Lisp_Object and emacs_value
-     share the same underlying bit representation.  */
-  eassert (EQ (o, *(Lisp_Object*)&v));
-  /* eassert (lisp_to_value (o) == v); */
+
+  return XIL ((tag << VALBITS) + untagged);
+}
+
+/* If V was computed from lisp_to_value (O), then return O.
+   Exits non-locally only if the stack overflows.  */
+static Lisp_Object
+value_to_lisp (emacs_value v)
+{
+  Lisp_Object o = value_to_lisp_bits (v);
+  if (! plain_values && CONSP (o) && EQ (XCDR (o), ltv_mark))
+    o = XCAR (o);
   return o;
-#endif
 }
 
-/* Convert an internal object to an `emacs_value'.  Allocate storage
-   from the environment; return NULL if allocation fails.  */
+/* Attempt to convert O to an emacs_value.  Do not do any checking or
+   or allocate any storage; the caller should prevent or detect
+   any resulting bit pattern that is not a valid emacs_value.  */
 static emacs_value
-lisp_to_value (Lisp_Object o)
+lisp_to_value_bits (Lisp_Object o)
 {
-#ifdef WIDE_EMACS_INT
-  /* We need to compress the EMACS_INT into the space of a pointer.
-     For most objects, this is just a question of shuffling the tags around.
-     But in some cases (e.g. large integers) this can't be done, so we
-     should allocate a special object to hold the extra data.  */
-  Lisp_Object orig = o;
-  int tag = XTYPE (o);
-  switch (tag)
-    {
-    case_Lisp_Int:
-      {
-        EMACS_UINT ui = (EMACS_UINT) XINT (o);
-        if (ui <= (SIZE_MAX >> GCTYPEBITS))
-          {
-            uintptr_t uv = (uintptr_t) ui;
-            emacs_value v = (emacs_value) ((uv << GCTYPEBITS) | tag);
-            eassert (EQ (value_to_lisp (v), o));
-            return v;
-          }
-        else
-          {
-            o = Fcons (o, ltv_mark);
-            tag = Lisp_Cons;
-          }
-      } /* FALLTHROUGH */
-    default:
-      {
-        void *ptr = XUNTAG (o, tag);
-        if (((uintptr_t)ptr) & ((1 << GCTYPEBITS) - 1))
-          { /* Pointer is not properly aligned!  */
-            eassert (!CONSP (o)); /* Cons cells have to always be aligned!  */
-            o = Fcons (o, ltv_mark);
-            ptr = XUNTAG (o, tag);
-          }
-        emacs_value v = (emacs_value) (((uintptr_t) ptr) | tag);
-        eassert (EQ (value_to_lisp (v), orig));
-        return v;
-      }
-    }
-#else
-  emacs_value v = (emacs_value) XLI (o);
+  EMACS_UINT u = XLI (o);
 
-  /* Check the assumption made elsewhere that Lisp_Object and emacs_value
-     share the same underlying bit representation.  */
-  eassert (v == *(emacs_value*)&o);
-  eassert (EQ (value_to_lisp (v), o));
-  return v;
-#endif
+  /* Compress U into the space of a pointer, possibly losing information.  */
+  uintptr_t p = (plain_values || USE_LSB_TAG
+                ? u
+                : (INTEGERP (o) ? u << VALBITS : u & VALMASK) + XTYPE (o));
+  return (emacs_value) p;
 }
 
-\f
-/* Memory management.  */
+#ifndef HAVE_STRUCT_ATTRIBUTE_ALIGNED
+enum { HAVE_STRUCT_ATTRIBUTE_ALIGNED = 0 };
+#endif
 
-/* Mark all objects allocated from local environments so that they
-   don't get garbage-collected.  */
-void
-mark_modules (void)
+/* Convert O to an emacs_value.  Allocate storage if needed; this can
+   signal if memory is exhausted.  Must be an injective function.  */
+static emacs_value
+lisp_to_value (Lisp_Object o)
 {
+  emacs_value v = lisp_to_value_bits (o);
+
+  if (! EQ (o, value_to_lisp_bits (v)))
+    {
+      /* Package the incompressible object pointer inside a pair
+        that is compressible.  */
+      Lisp_Object pair = Fcons (o, ltv_mark);
+
+      if (! HAVE_STRUCT_ATTRIBUTE_ALIGNED)
+       {
+         /* Keep calling Fcons until it returns a compressible pair.
+            This shouldn't take long.  */
+         while ((intptr_t) XCONS (pair) & (GCALIGNMENT - 1))
+           pair = Fcons (o, pair);
+
+         /* Plant the mark.  The garbage collector will eventually
+            reclaim any just-allocated incompressible pairs.  */
+         XSETCDR (pair, ltv_mark);
+       }
+
+      v = (emacs_value) ((intptr_t) XCONS (pair) + Lisp_Cons);
+    }
+
+  eassert (EQ (o, value_to_lisp (v)));
+  return v;
 }
 
 \f
@@ -1048,10 +1067,9 @@ module_format_fun_env (const struct module_fun_env *env)
 void
 syms_of_module (void)
 {
-  module_nil = lisp_to_value (Qnil);
-#ifdef WIDE_EMACS_INT
-  ltv_mark = Fcons (Qnil, Qnil);
-#endif
+  if (!plain_values)
+    ltv_mark = Fcons (Qnil, Qnil);
+  eassert (NILP (value_to_lisp (module_nil)));
 
   DEFSYM (Qmodule_refs_hash, "module-refs-hash");
   DEFVAR_LISP ("module-refs-hash", Vmodule_refs_hash,
@@ -1111,9 +1129,7 @@ module_init (void)
 {
   /* It is not guaranteed that dynamic initializers run in the main thread,
      therefore detect the main thread here.  */
-#ifdef HAVE_THREADS_H
-  main_thread = thrd_current ();
-#elif defined HAVE_PTHREAD
+#ifdef HAVE_PTHREAD
   main_thread = pthread_self ();
 #elif defined WINDOWSNT
   /* The 'main' function already recorded the main thread's thread ID,