1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include <sys/types.h>
27
#if defined(USE_SYSV_SHM)
37
#elif defined(USE_POSIX_SHM)
46
#endif /* USE_POSIX_SHM */
48
#include <glib-object.h>
50
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
61
#define USE_WIN32_SHM 1
63
#endif /* G_OS_WIN32 || G_WITH_CYGWIN */
65
#include "plug-in-types.h"
67
#include "base/tile.h"
69
#include "gimppluginshm.h"
72
#define TILE_MAP_SIZE (TILE_WIDTH * TILE_HEIGHT * 4)
74
#define ERRMSG_SHM_DISABLE "Disabling shared memory tile transport"
82
#if defined(USE_WIN32_SHM)
89
gimp_plug_in_shm_new (void)
91
/* allocate a piece of shared memory for use in transporting tiles
92
* to plug-ins. if we can't allocate a piece of shared memory then
93
* we'll fall back on sending the data over the pipe.
96
GimpPlugInShm *shm = g_new0 (GimpPlugInShm, 1);
100
#if defined(USE_SYSV_SHM)
102
/* Use SysV shared memory mechanisms for transferring tile data. */
104
shm->shm_ID = shmget (IPC_PRIVATE, TILE_MAP_SIZE, IPC_CREAT | 0600);
106
if (shm->shm_ID != -1)
108
shm->shm_addr = (guchar *) shmat (shm->shm_ID, NULL, 0);
110
if (shm->shm_addr == (guchar *) -1)
112
g_printerr ("shmat() failed: %s\n" ERRMSG_SHM_DISABLE,
114
shmctl (shm->shm_ID, IPC_RMID, NULL);
118
#ifdef IPC_RMID_DEFERRED_RELEASE
119
if (shm->shm_addr != (guchar *) -1)
120
shmctl (shm->shm_ID, IPC_RMID, NULL);
125
g_printerr ("shmget() failed: %s\n" ERRMSG_SHM_DISABLE,
130
#elif defined(USE_WIN32_SHM)
132
/* Use Win32 shared memory mechanisms for transferring tile data. */
135
gchar fileMapName[MAX_PATH];
137
/* Our shared memory id will be our process ID */
138
pid = GetCurrentProcessId ();
140
/* From the id, derive the file map name */
141
g_snprintf (fileMapName, sizeof (fileMapName), "GIMP%d.SHM", pid);
143
/* Create the file mapping into paging space */
144
shm->shm_handle = CreateFileMapping ((HANDLE) 0xFFFFFFFF, NULL,
151
/* Map the shared memory into our address space for use */
152
shm->shm_addr = (guchar *) MapViewOfFile (shm->shm_handle,
154
0, 0, TILE_MAP_SIZE);
156
/* Verify that we mapped our view */
163
g_printerr ("MapViewOfFile error: %d... " ERRMSG_SHM_DISABLE,
169
g_printerr ("CreateFileMapping error: %d... " ERRMSG_SHM_DISABLE,
174
#elif defined(USE_POSIX_SHM)
176
/* Use POSIX shared memory mechanisms for transferring tile data. */
179
gchar shm_handle[32];
182
/* Our shared memory id will be our process ID */
185
/* From the id, derive the file map name */
186
g_snprintf (shm_handle, sizeof (shm_handle), "/gimp-shm-%d", pid);
188
/* Create the file mapping into paging space */
189
shm_fd = shm_open (shm_handle, O_RDWR | O_CREAT, 0600);
193
if (ftruncate (shm_fd, TILE_MAP_SIZE) != -1)
195
/* Map the shared memory into our address space for use */
196
shm->shm_addr = (guchar *) mmap (NULL, TILE_MAP_SIZE,
197
PROT_READ | PROT_WRITE, MAP_SHARED,
200
/* Verify that we mapped our view */
201
if (shm->shm_addr != MAP_FAILED)
207
g_printerr ("mmap() failed: %s\n" ERRMSG_SHM_DISABLE,
210
shm_unlink (shm_handle);
215
g_printerr ("ftruncate() failed: %s\n" ERRMSG_SHM_DISABLE,
218
shm_unlink (shm_handle);
225
g_printerr ("shm_open() failed: %s\n" ERRMSG_SHM_DISABLE,
232
if (shm->shm_ID == -1)
242
gimp_plug_in_shm_free (GimpPlugInShm *shm)
244
g_return_if_fail (shm != NULL);
246
if (shm->shm_ID != -1)
249
#if defined (USE_SYSV_SHM)
251
#ifndef IPC_RMID_DEFERRED_RELEASE
252
shmdt (shm->shm_addr);
253
shmctl (shm->shm_ID, IPC_RMID, NULL);
255
shmdt (shm->shm_addr);
256
#endif /* IPC_RMID_DEFERRED_RELEASE */
258
#elif defined(USE_WIN32_SHM)
261
CloseHandle (shm->shm_handle);
263
#elif defined(USE_POSIX_SHM)
265
gchar shm_handle[32];
267
munmap (shm->shm_addr, TILE_MAP_SIZE);
269
g_snprintf (shm_handle, sizeof (shm_handle), "/gimp-shm-%d",
272
shm_unlink (shm_handle);
282
gimp_plug_in_shm_get_ID (GimpPlugInShm *shm)
284
g_return_val_if_fail (shm != NULL, -1);
290
gimp_plug_in_shm_get_addr (GimpPlugInShm *shm)
292
g_return_val_if_fail (shm != NULL, NULL);
294
return shm->shm_addr;