]> code.delx.au - gnu-emacs/commitdiff
Refactor pointer-to-integer conversion
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 9 Jan 2015 16:04:36 +0000 (08:04 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 9 Jan 2015 16:22:35 +0000 (08:22 -0800)
* gfilenotify.c (monitor_to_lisp, lisp_to_monitor):
Rename and move to lisp.h.  All uses changed.
* lisp.h (XINTPTR, make_pointer_integer): New inline functions,
which are renamed from gfilenotify.c's lisp_to_monitor and
monitor_to_lisp, and with more-generic void * signatures.

src/ChangeLog
src/gfilenotify.c
src/lisp.h

index c302f95d3fa11217300fe39450b5b09b1abd8577..c11ba11715b8849dc408a0834d3ae28105b24138 100644 (file)
@@ -1,3 +1,12 @@
+2015-01-09  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Refactor pointer-to-integer conversion
+       * gfilenotify.c (monitor_to_lisp, lisp_to_monitor):
+       Rename and move to lisp.h.  All uses changed.
+       * lisp.h (XINTPTR, make_pointer_integer): New inline functions,
+       which are renamed from gfilenotify.c's lisp_to_monitor and
+       monitor_to_lisp, and with more-generic void * signatures.
+
 2015-01-08  Eli Zaretskii  <eliz@gnu.org>
 
        * dispnew.c (buffer_posn_from_coords): Fix the value of the column
index 88222b5bf05f72aaa75686f590e47dae79df06b5..e03bec93541a4591b42af199ed9fc5a4c9d11310 100644 (file)
@@ -31,22 +31,6 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 \f
 static Lisp_Object watch_list;
 
-/* Convert a monitor to a Lisp integer and back.  On all known glib
-   platforms, converting the sum of MONITOR and Lisp_Int0 directly to
-   a Lisp_Object value results in a Lisp integer, which is safe.  */
-
-static Lisp_Object
-monitor_to_lisp (GFileMonitor *monitor)
-{
-  return XIL (TAG_PTR (Lisp_Int0, monitor));
-}
-
-static GFileMonitor *
-lisp_to_monitor (Lisp_Object watch_descriptor)
-{
-  return XUNTAG (watch_descriptor, Lisp_Int0);
-}
-
 /* This is the callback function for arriving signals from
    g_file_monitor.  It shall create a Lisp event, and put it into
    Emacs input queue.  */
@@ -93,7 +77,7 @@ dir_monitor_callback (GFileMonitor *monitor,
     }
 
   /* Determine callback function.  */
-  monitor_object = monitor_to_lisp (monitor);
+  monitor_object = make_pointer_integer (monitor);
   eassert (INTEGERP (monitor_object));
   watch_object = assq_no_quit (monitor_object, watch_list);
 
@@ -192,9 +176,9 @@ will be reported only in case of the 'moved' event.  */)
   if (! monitor)
     xsignal2 (Qfile_notify_error, build_string ("Cannot watch file"), file);
 
-  Lisp_Object watch_descriptor = monitor_to_lisp (monitor);
+  Lisp_Object watch_descriptor = make_pointer_integer (monitor);
 
-  /* Check the dicey assumption that monitor_to_lisp is safe.  */
+  /* Check the dicey assumption that make_pointer_integer is safe.  */
   if (! INTEGERP (watch_descriptor))
     {
       g_object_unref (monitor);
@@ -225,7 +209,7 @@ WATCH-DESCRIPTOR should be an object returned by `gfile-add-watch'.  */)
              watch_descriptor);
 
   eassert (INTEGERP (watch_descriptor));
-  GFileMonitor *monitor = lisp_to_monitor (watch_descriptor);
+  GFileMonitor *monitor = XINTPTR (watch_descriptor);
   if (!g_file_monitor_cancel (monitor))
     xsignal2 (Qfile_notify_error, build_string ("Could not rm watch"),
              watch_descriptor);
index 5a4198ef6831c787f6c3daa39ba11a8a40a6574e..4571c455a7b3bf2bdcb748f60fabf881162bc85f 100644 (file)
@@ -1112,6 +1112,25 @@ make_lisp_proc (struct Lisp_Process *p)
 #define XSETBOOL_VECTOR(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_BOOL_VECTOR))
 #define XSETSUB_CHAR_TABLE(a, b) (XSETPSEUDOVECTOR (a, b, PVEC_SUB_CHAR_TABLE))
 
+/* Efficiently convert a pointer to a Lisp object and back.  The
+   pointer is represented as a Lisp integer, so the garbage collector
+   does not know about it.  The pointer should not have both Lisp_Int1
+   bits set, which makes this conversion inherently unportable.  */
+
+INLINE void *
+XINTPTR (Lisp_Object a)
+{
+  return XUNTAG (a, Lisp_Int0);
+}
+
+INLINE Lisp_Object
+make_pointer_integer (void *p)
+{
+  Lisp_Object a = XIL (TAG_PTR (Lisp_Int0, p));
+  eassert (INTEGERP (a) && XINTPTR (a) == p);
+  return a;
+}
+
 /* Type checking.  */
 
 LISP_MACRO_DEFUN_VOID (CHECK_TYPE,