~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/corelib/kernel/qsharedmemory_p.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the core module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qsharedmemory_p.h"
 
30
 
 
31
#if !defined(QT_QWS_NO_SHM)
 
32
 
 
33
#if defined(QT_POSIX_QSHM)
 
34
#include <sys/mman.h>
 
35
 
 
36
QSharedMemory::QSharedMemory (int size, const QString &filename, char c)
 
37
{
 
38
  shmSize = size;
 
39
  shmFile = filename;
 
40
  character = c;
 
41
  shmFile.append(c);
 
42
}
 
43
 
 
44
bool QSharedMemory::create ()
 
45
{
 
46
  shmFD = shm_open (shmFile.latin1 (), O_RDWR | O_EXCL | O_CREAT, 0666);
 
47
  if (shmFD == -1)
 
48
    return false;
 
49
  else if (ftruncate (shmFD, shmSize) == -1)
 
50
    {
 
51
      close (shmFD);
 
52
      return false;
 
53
    }
 
54
 
 
55
  return true;
 
56
}
 
57
 
 
58
void QSharedMemory::destroy ()
 
59
{
 
60
  shm_unlink (shmFile.latin1 ());
 
61
}
 
62
 
 
63
bool QSharedMemory::attach ()
 
64
{
 
65
  shmBase = mmap (0, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, shmFD, 0);
 
66
 
 
67
  if (shmBase == MAP_FAILED)
 
68
    return false;
 
69
 
 
70
  close (shmFD);
 
71
  return true;
 
72
}
 
73
 
 
74
void QSharedMemory::detach ()
 
75
{
 
76
  munmap (shmBase, shmSize);
 
77
}
 
78
 
 
79
void QSharedMemory::setPermissions (mode_t mode)
 
80
{
 
81
  mprotect (shmBase, shmSize, mode);        // Provide defines to make prot work properly
 
82
}
 
83
 
 
84
int QSharedMemory::size()
 
85
{
 
86
    struct stat buf;
 
87
    int rc = fstat (shmFD, &buf);
 
88
    if (rc != -1)
 
89
        return buf.st_size;
 
90
    else
 
91
        return rc;
 
92
}
 
93
 
 
94
#else // Assume SysV for backwards compat
 
95
#include <sys/shm.h>
 
96
 
 
97
QSharedMemory::QSharedMemory (int size, const QString &filename, char c)
 
98
{
 
99
  shmSize = size;
 
100
  shmFile = filename;
 
101
  character = c;
 
102
  key = ftok (shmFile.toLatin1().constData(), c);
 
103
  idInitted = false;
 
104
}
 
105
 
 
106
bool QSharedMemory::create ()
 
107
{
 
108
  shmId = shmget (key, shmSize, IPC_CREAT | 0666);
 
109
  if (shmId == -1)
 
110
    return false;
 
111
  else
 
112
    return true;
 
113
}
 
114
 
 
115
void QSharedMemory::destroy ()
 
116
{
 
117
  struct shmid_ds shm;
 
118
  shmctl (shmId, IPC_RMID, &shm);
 
119
}
 
120
 
 
121
bool QSharedMemory::attach ()
 
122
{
 
123
  if (shmId == -1)
 
124
    shmId = shmget (key, shmSize, 0);
 
125
 
 
126
  shmBase = shmat (shmId, 0, 0);
 
127
  if ((long) shmBase == -1 || shmBase == 0)
 
128
    return false;
 
129
  else
 
130
    return true;
 
131
}
 
132
 
 
133
void QSharedMemory::detach ()
 
134
{
 
135
  shmdt (shmBase);
 
136
}
 
137
 
 
138
void QSharedMemory::setPermissions (mode_t mode)
 
139
{
 
140
  struct shmid_ds shm;
 
141
  shmctl (shmId, IPC_STAT, &shm);
 
142
  shm.shm_perm.mode = mode;
 
143
  shmctl (shmId, IPC_SET, &shm);
 
144
}
 
145
 
 
146
int QSharedMemory::size ()
 
147
{
 
148
    struct shmid_ds shm;
 
149
    shmctl (shmId, IPC_STAT, &shm);
 
150
    return shm.shm_segsz;
 
151
}
 
152
 
 
153
#endif
 
154
 
 
155
#endif