[gimp] libgimp: move the shm code to new private files gimp-shm.[ch]



commit ca1cb056fc8dc2b30e29c6448a38258b6f8d5f07
Author: Michael Natterer <mitch gimp org>
Date:   Sun Aug 4 18:54:00 2019 +0200

    libgimp: move the shm code to new private files gimp-shm.[ch]
    
    and remove it from the public API, it should have never been
    there in the first place.

 libgimp/Makefile.am             |   2 +
 libgimp/gimp.c                  | 170 ++--------------------------------------
 libgimp/gimp.def                |   2 -
 libgimp/gimp.h                  |   2 -
 libgimp/gimptilebackendplugin.c |   5 +-
 5 files changed, 10 insertions(+), 171 deletions(-)
---
diff --git a/libgimp/Makefile.am b/libgimp/Makefile.am
index 9e6bd9407d..542ac903c6 100644
--- a/libgimp/Makefile.am
+++ b/libgimp/Makefile.am
@@ -116,6 +116,8 @@ libgimp_private_sources = \
        gimpplugin-private.c    \
        gimpplugin-private.h    \
        gimp-private.h          \
+       gimp-shm.c              \
+       gimp-shm.h              \
        gimpprocedure-private.c \
        gimpprocedure-private.h
 
diff --git a/libgimp/gimp.c b/libgimp/gimp.c
index f52724a034..0574071179 100644
--- a/libgimp/gimp.c
+++ b/libgimp/gimp.c
@@ -68,27 +68,6 @@
 #include <sys/select.h>
 #endif
 
-#if defined(USE_SYSV_SHM)
-
-#ifdef HAVE_IPC_H
-#include <sys/ipc.h>
-#endif
-
-#ifdef HAVE_SHM_H
-#include <sys/shm.h>
-#endif
-
-#elif defined(USE_POSIX_SHM)
-
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
-
-#include <fcntl.h>
-#include <sys/mman.h>
-
-#endif /* USE_POSIX_SHM */
-
 #ifdef GDK_WINDOWING_QUARTZ
 #include <Cocoa/Cocoa.h>
 #endif
@@ -107,7 +86,6 @@
 #  include <windows.h>
 #  include <tlhelp32.h>
 #  undef RGB
-#  define USE_WIN32_SHM 1
 #endif
 
 #include <locale.h>
@@ -118,8 +96,8 @@
 #include "libgimpbase/gimpprotocol.h"
 #include "libgimpbase/gimpwire.h"
 
-
 #include "gimp-private.h"
+#include "gimp-shm.h"
 #include "gimpgpcompat.h"
 #include "gimpgpparams.h"
 #include "gimpplugin-private.h"
@@ -128,10 +106,6 @@
 #include "libgimp-intl.h"
 
 
-#define TILE_MAP_SIZE (_tile_width * _tile_height * 32)
-
-#define ERRMSG_SHM_FAILED "Could not attach to gimp shared memory segment"
-
 /* Maybe this should go in a public header if we add other things to it */
 typedef enum
 {
@@ -191,14 +165,8 @@ static gchar                         *plug_in_backtrace_path = NULL;
 GIOChannel                          *_gimp_readchannel       = NULL;
 GIOChannel                          *_gimp_writechannel      = NULL;
 
-#ifdef USE_WIN32_SHM
-static HANDLE shm_handle;
-#endif
-
 static gint           _tile_width        = -1;
 static gint           _tile_height       = -1;
-static gint           _shm_ID            = -1;
-static guchar        *_shm_addr          = NULL;
 static gboolean       _show_help_button  = TRUE;
 static gboolean       _export_profile    = FALSE;
 static gboolean       _export_exif       = FALSE;
@@ -954,38 +922,6 @@ gimp_tile_height (void)
   return _tile_height;
 }
 
-/**
- * gimp_shm_ID:
- *
- * Returns the shared memory ID used for passing tile data between the
- * GIMP core and the plug-in.
- *
- * This is a constant value given at plug-in configuration time.
- *
- * Returns: the shared memory ID
- **/
-gint
-gimp_shm_ID (void)
-{
-  return _shm_ID;
-}
-
-/**
- * gimp_shm_addr:
- *
- * Returns the address of the shared memory segment used for passing
- * tile data between the GIMP core and the plug-in.
- *
- * This is a constant value given at plug-in configuration time.
- *
- * Returns: the shared memory address
- **/
-guchar *
-gimp_shm_addr (void)
-{
-  return _shm_addr;
-}
-
 /**
  * gimp_show_help_button:
  *
@@ -1236,22 +1172,7 @@ gimp_close (void)
         PLUG_IN_INFO.quit_proc ();
     }
 
-#if defined(USE_SYSV_SHM)
-
-  if ((_shm_ID != -1) && _shm_addr)
-    shmdt ((char *) _shm_addr);
-
-#elif defined(USE_WIN32_SHM)
-
-  if (shm_handle)
-    CloseHandle (shm_handle);
-
-#elif defined(USE_POSIX_SHM)
-
-  if ((_shm_ID != -1) && (_shm_addr != MAP_FAILED))
-    munmap (_shm_addr, TILE_MAP_SIZE);
-
-#endif
+  _gimp_shm_close ();
 
   gp_quit_write (_gimp_writechannel, NULL);
 }
@@ -1573,10 +1494,11 @@ _gimp_config (GPConfig *config)
 {
   GFile *file;
   gchar *path;
+  gint   shm_ID;
 
   _tile_width       = config->tile_width;
   _tile_height      = config->tile_height;
-  _shm_ID           = config->shm_ID;
+  shm_ID            = config->shm_ID;
   _check_size       = config->check_size;
   _check_type       = config->check_type;
   _show_help_button = config->show_help_button ? TRUE : FALSE;
@@ -1610,89 +1532,7 @@ _gimp_config (GPConfig *config)
   g_free (path);
   g_object_unref (file);
 
-  if (_shm_ID != -1)
-    {
-#if defined(USE_SYSV_SHM)
-
-      /* Use SysV shared memory mechanisms for transferring tile data. */
-
-      _shm_addr = (guchar *) shmat (_shm_ID, NULL, 0);
-
-      if (_shm_addr == (guchar *) -1)
-        {
-          g_error ("shmat() failed: %s\n" ERRMSG_SHM_FAILED,
-                   g_strerror (errno));
-        }
-
-#elif defined(USE_WIN32_SHM)
-
-      /* Use Win32 shared memory mechanisms for transferring tile data. */
-
-      gchar fileMapName[128];
-
-      /* From the id, derive the file map name */
-      g_snprintf (fileMapName, sizeof (fileMapName), "GIMP%d.SHM", _shm_ID);
-
-      /* Open the file mapping */
-      shm_handle = OpenFileMapping (FILE_MAP_ALL_ACCESS,
-                                    0, fileMapName);
-      if (shm_handle)
-        {
-          /* Map the shared memory into our address space for use */
-          _shm_addr = (guchar *) MapViewOfFile (shm_handle,
-                                                FILE_MAP_ALL_ACCESS,
-                                                0, 0, TILE_MAP_SIZE);
-
-          /* Verify that we mapped our view */
-          if (!_shm_addr)
-            {
-              g_error ("MapViewOfFile error: %lu... " ERRMSG_SHM_FAILED,
-                       GetLastError ());
-            }
-        }
-      else
-        {
-          g_error ("OpenFileMapping error: %lu... " ERRMSG_SHM_FAILED,
-                   GetLastError ());
-        }
-
-#elif defined(USE_POSIX_SHM)
-
-      /* Use POSIX shared memory mechanisms for transferring tile data. */
-
-      gchar map_file[32];
-      gint  shm_fd;
-
-      /* From the id, derive the file map name */
-      g_snprintf (map_file, sizeof (map_file), "/gimp-shm-%d", _shm_ID);
-
-      /* Open the file mapping */
-      shm_fd = shm_open (map_file, O_RDWR, 0600);
-
-      if (shm_fd != -1)
-        {
-          /* Map the shared memory into our address space for use */
-          _shm_addr = (guchar *) mmap (NULL, TILE_MAP_SIZE,
-                                       PROT_READ | PROT_WRITE, MAP_SHARED,
-                                       shm_fd, 0);
-
-          /* Verify that we mapped our view */
-          if (_shm_addr == MAP_FAILED)
-            {
-              g_error ("mmap() failed: %s\n" ERRMSG_SHM_FAILED,
-                       g_strerror (errno));
-            }
-
-          close (shm_fd);
-        }
-      else
-        {
-          g_error ("shm_open() failed: %s\n" ERRMSG_SHM_FAILED,
-                   g_strerror (errno));
-        }
-
-#endif
-    }
+  _gimp_shm_open (shm_ID);
 }
 
 static void
diff --git a/libgimp/gimp.def b/libgimp/gimp.def
index ef418f84c5..e00448a110 100644
--- a/libgimp/gimp.def
+++ b/libgimp/gimp.def
@@ -716,8 +716,6 @@ EXPORTS
        gimp_selection_shrink
        gimp_selection_translate
        gimp_selection_value
-       gimp_shm_ID
-       gimp_shm_addr
        gimp_show_help_button
        gimp_smudge
        gimp_smudge_default
diff --git a/libgimp/gimp.h b/libgimp/gimp.h
index d47aa6ff2e..328bbb1edd 100644
--- a/libgimp/gimp.h
+++ b/libgimp/gimp.h
@@ -171,8 +171,6 @@ GimpPDBStatusType   gimp_get_pdb_status       (void);
  */
 guint               gimp_tile_width           (void) G_GNUC_CONST;
 guint               gimp_tile_height          (void) G_GNUC_CONST;
-gint                gimp_shm_ID               (void) G_GNUC_CONST;
-guchar            * gimp_shm_addr             (void) G_GNUC_CONST;
 gboolean            gimp_show_help_button     (void) G_GNUC_CONST;
 gboolean            gimp_export_color_profile (void) G_GNUC_CONST;
 gboolean            gimp_export_exif          (void) G_GNUC_CONST;
diff --git a/libgimp/gimptilebackendplugin.c b/libgimp/gimptilebackendplugin.c
index 68fa04c642..140fe0e707 100644
--- a/libgimp/gimptilebackendplugin.c
+++ b/libgimp/gimptilebackendplugin.c
@@ -30,6 +30,7 @@
 #include "libgimpbase/gimpwire.h"
 
 #include "gimp-private.h"
+#include "gimp-shm.h"
 #include "gimpplugin-private.h"
 #include "gimptilebackendplugin.h"
 
@@ -372,7 +373,7 @@ gimp_tile_get (GimpTileBackendPlugin *backend_plugin,
 
   if (tile_data->use_shm)
     {
-      tile->data = g_memdup (gimp_shm_addr (),
+      tile->data = g_memdup (_gimp_shm_addr (),
                              tile->ewidth * tile->eheight * priv->bpp);
     }
   else
@@ -425,7 +426,7 @@ gimp_tile_put (GimpTileBackendPlugin *backend_plugin,
 
   if (tile_info->use_shm)
     {
-      memcpy (gimp_shm_addr (),
+      memcpy (_gimp_shm_addr (),
               tile->data,
               tile->ewidth * tile->eheight * priv->bpp);
     }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]