1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program 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 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <sys/types.h>
#include <errno.h>
#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 */
#include <glib-object.h>
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
#define STRICT
#include <windows.h>
#include <process.h>
#ifdef G_OS_WIN32
#include <fcntl.h>
#include <io.h>
#endif
#define USE_WIN32_SHM 1
#endif /* G_OS_WIN32 || G_WITH_CYGWIN */
#include "plug-in-types.h"
#include "base/tile.h"
#include "core/gimp.h"
#include "plug-in-shm.h"
struct _PlugInShm
{
gint shm_ID;
guchar *shm_addr;
#if defined(USE_WIN32_SHM)
HANDLE shm_handle;
#endif
};
#define TILE_MAP_SIZE (TILE_WIDTH * TILE_HEIGHT * 4)
#define ERRMSG_SHM_DISABLE "Disabling shared memory tile transport"
void
plug_in_shm_init (Gimp *gimp)
{
/* allocate a piece of shared memory for use in transporting tiles
* to plug-ins. if we can't allocate a piece of shared memory then
* we'll fall back on sending the data over the pipe.
*/
#if defined(USE_SYSV_SHM)
/* Use SysV shared memory mechanisms for transferring tile data. */
g_return_if_fail (GIMP_IS_GIMP (gimp));
gimp->plug_in_shm = g_new0 (PlugInShm, 1);
gimp->plug_in_shm->shm_ID = -1;
gimp->plug_in_shm->shm_ID = shmget (IPC_PRIVATE, TILE_MAP_SIZE,
IPC_CREAT | 0600);
if (gimp->plug_in_shm->shm_ID != -1)
{
gimp->plug_in_shm->shm_addr = (guchar *)
shmat (gimp->plug_in_shm->shm_ID, NULL, 0);
if (gimp->plug_in_shm->shm_addr == (guchar *) -1)
{
g_warning ("shmat() failed: %s\n" ERRMSG_SHM_DISABLE,
g_strerror (errno));
shmctl (gimp->plug_in_shm->shm_ID, IPC_RMID, NULL);
gimp->plug_in_shm->shm_ID = -1;
}
#ifdef IPC_RMID_DEFERRED_RELEASE
if (gimp->plug_in_shm->shm_addr != (guchar *) -1)
shmctl (gimp->plug_in_shm->shm_ID, IPC_RMID, NULL);
#endif
}
else
{
g_warning ("shmget() failed: %s\n" ERRMSG_SHM_DISABLE,
g_strerror (errno));
}
#elif defined(USE_WIN32_SHM)
/* Use Win32 shared memory mechanisms for transferring tile data. */
gint pid;
gchar fileMapName[MAX_PATH];
g_return_if_fail (GIMP_IS_GIMP (gimp));
gimp->plug_in_shm = g_new0 (PlugInShm, 1);
gimp->plug_in_shm->shm_ID = -1;
/* Our shared memory id will be our process ID */
pid = GetCurrentProcessId ();
/* From the id, derive the file map name */
g_snprintf (fileMapName, sizeof (fileMapName), "GIMP%d.SHM", pid);
/* Create the file mapping into paging space */
gimp->plug_in_shm->shm_handle = CreateFileMapping ((HANDLE) 0xFFFFFFFF, NULL,
PAGE_READWRITE, 0,
TILE_MAP_SIZE,
fileMapName);
if (gimp->plug_in_shm->shm_handle)
{
/* Map the shared memory into our address space for use */
gimp->plug_in_shm->shm_addr = (guchar *)
MapViewOfFile (gimp->plug_in_shm->shm_handle,
FILE_MAP_ALL_ACCESS,
0, 0, TILE_MAP_SIZE);
/* Verify that we mapped our view */
if (gimp->plug_in_shm->shm_addr)
gimp->plug_in_shm->shm_ID = pid;
else
g_warning ("MapViewOfFile error: %d... " ERRMSG_SHM_DISABLE,
GetLastError ());
}
else
{
g_warning ("CreateFileMapping error: %d... " ERRMSG_SHM_DISABLE,
GetLastError ());
}
#elif defined(USE_POSIX_SHM)
/* Use POSIX shared memory mechanisms for transferring tile data. */
gint pid;
gchar shm_handle[32];
gint shm_fd;
g_return_if_fail (GIMP_IS_GIMP (gimp));
gimp->plug_in_shm = g_new0 (PlugInShm, 1);
gimp->plug_in_shm->shm_ID = -1;
/* Our shared memory id will be our process ID */
pid = getpid ();
/* From the id, derive the file map name */
g_snprintf (shm_handle, sizeof (shm_handle), "/gimp-shm-%d", pid);
/* Create the file mapping into paging space */
shm_fd = shm_open (shm_handle, O_RDWR | O_CREAT, 0600);
if (shm_fd != -1)
{
if (ftruncate (shm_fd, TILE_MAP_SIZE) != -1)
{
/* Map the shared memory into our address space for use */
gimp->plug_in_shm->shm_addr = (guchar *)
mmap (NULL, TILE_MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
shm_fd, 0);
/* Verify that we mapped our view */
if (gimp->plug_in_shm->shm_addr != MAP_FAILED)
{
gimp->plug_in_shm->shm_ID = pid;
}
else
{
g_warning ("mmap() failed: %s\n" ERRMSG_SHM_DISABLE,
g_strerror (errno));
shm_unlink (shm_handle);
}
}
else
{
g_warning ("ftruncate() failed: %s\n" ERRMSG_SHM_DISABLE,
g_strerror (errno));
shm_unlink (shm_handle);
}
close (shm_fd);
}
else
{
g_warning ("shm_open() failed: %s\n" ERRMSG_SHM_DISABLE,
g_strerror (errno));
}
#endif
}
void
plug_in_shm_exit (Gimp *gimp)
{
g_return_if_fail (GIMP_IS_GIMP (gimp));
g_return_if_fail (gimp->plug_in_shm != NULL);
#if defined (USE_SYSV_SHM)
#ifndef IPC_RMID_DEFERRED_RELEASE
if (gimp->plug_in_shm->shm_ID != -1)
{
shmdt (gimp->plug_in_shm->shm_addr);
shmctl (gimp->plug_in_shm->shm_ID, IPC_RMID, NULL);
}
#else /* IPC_RMID_DEFERRED_RELEASE */
if (gimp->plug_in_shm->shm_ID != -1)
{
shmdt (gimp->plug_in_shm->shm_addr);
}
#endif
#elif defined(USE_WIN32_SHM)
if (gimp->plug_in_shm->shm_handle)
{
CloseHandle (gimp->plug_in_shm->shm_handle);
}
#elif defined(USE_POSIX_SHM)
if (gimp->plug_in_shm->shm_ID != -1)
{
gchar shm_handle[32];
munmap (gimp->plug_in_shm->shm_addr, TILE_MAP_SIZE);
g_snprintf (shm_handle, sizeof (shm_handle), "/gimp-shm-%d",
gimp->plug_in_shm->shm_ID);
shm_unlink (shm_handle);
}
#endif
g_free (gimp->plug_in_shm);
gimp->plug_in_shm = NULL;
}
gint
plug_in_shm_get_ID (Gimp *gimp)
{
g_return_val_if_fail (GIMP_IS_GIMP (gimp), -1);
return gimp->plug_in_shm ? gimp->plug_in_shm->shm_ID : -1;
}
guchar *
plug_in_shm_get_addr (Gimp *gimp)
{
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
g_return_val_if_fail (gimp->plug_in_shm != NULL, NULL);
return gimp->plug_in_shm->shm_addr;
}
|