1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qsharedmemory_p.h"
31
#if !defined(QT_QWS_NO_SHM)
33
#if defined(QT_POSIX_QSHM)
36
QSharedMemory::QSharedMemory (int size, const QString &filename, char c)
44
bool QSharedMemory::create ()
46
shmFD = shm_open (shmFile.latin1 (), O_RDWR | O_EXCL | O_CREAT, 0666);
49
else if (ftruncate (shmFD, shmSize) == -1)
58
void QSharedMemory::destroy ()
60
shm_unlink (shmFile.latin1 ());
63
bool QSharedMemory::attach ()
65
shmBase = mmap (0, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, shmFD, 0);
67
if (shmBase == MAP_FAILED)
74
void QSharedMemory::detach ()
76
munmap (shmBase, shmSize);
79
void QSharedMemory::setPermissions (mode_t mode)
81
mprotect (shmBase, shmSize, mode); // Provide defines to make prot work properly
84
int QSharedMemory::size()
87
int rc = fstat (shmFD, &buf);
94
#else // Assume SysV for backwards compat
97
QSharedMemory::QSharedMemory (int size, const QString &filename, char c)
102
key = ftok (shmFile.toLatin1().constData(), c);
106
bool QSharedMemory::create ()
108
shmId = shmget (key, shmSize, IPC_CREAT | 0666);
115
void QSharedMemory::destroy ()
118
shmctl (shmId, IPC_RMID, &shm);
121
bool QSharedMemory::attach ()
124
shmId = shmget (key, shmSize, 0);
126
shmBase = shmat (shmId, 0, 0);
127
if ((long) shmBase == -1 || shmBase == 0)
133
void QSharedMemory::detach ()
138
void QSharedMemory::setPermissions (mode_t mode)
141
shmctl (shmId, IPC_STAT, &shm);
142
shm.shm_perm.mode = mode;
143
shmctl (shmId, IPC_SET, &shm);
146
int QSharedMemory::size ()
149
shmctl (shmId, IPC_STAT, &shm);
150
return shm.shm_segsz;