Qt 4.8
Public Functions | Properties | List of all members
QWSSharedMemory Class Reference

#include <qwssharedmemory_p.h>

Public Functions

void * address () const
 
bool attach (int id)
 
bool create (int size)
 
void detach ()
 
int id () const
 
 QWSSharedMemory ()
 
int size () const
 
 ~QWSSharedMemory ()
 

Properties

void * shmBase
 
int shmId
 
int shmSize
 

Detailed Description

Definition at line 62 of file qwssharedmemory_p.h.

Constructors and Destructors

◆ QWSSharedMemory()

QWSSharedMemory::QWSSharedMemory ( )

Definition at line 75 of file qwssharedmemory.cpp.

76  : shmId(-1), shmBase(0), shmSize(0)
77 #ifdef QT_POSIX_IPC
78  , hand(-1)
79 #endif
80 {
81 }

◆ ~QWSSharedMemory()

QWSSharedMemory::~QWSSharedMemory ( )

Definition at line 83 of file qwssharedmemory.cpp.

84 {
85  detach();
86 }

Functions

◆ address()

void* QWSSharedMemory::address ( ) const
inline

◆ attach()

bool QWSSharedMemory::attach ( int  id)

Definition at line 146 of file qwssharedmemory.cpp.

Referenced by QWSSharedMemSurface::setMemory().

147 {
148  if (shmId == id)
149  return id != -1;
150 
151  detach();
152 
153  if (id == -1)
154  return false;
155 
156  shmId = id;
157 #ifndef QT_POSIX_IPC
158  shmBase = shmat(shmId, 0, 0);
159 #else
160  QByteArray shmName = makeKey(shmId);
161  EINTR_LOOP(hand, shm_open(shmName.constData(), O_RDWR, 0660));
162  if (hand != -1) {
163  // grab the size
164  QT_STATBUF st;
165  if (QT_FSTAT(hand, &st) != -1) {
166  shmSize = st.st_size;
167  // grab the memory
168  shmBase = mmap(0, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, hand, 0);
169  }
170  }
171 #endif
172  if (shmBase == (void*)-1 || !shmBase) {
173 #ifdef QT_SHM_DEBUG
174  perror("QWSSharedMemory::attach():");
175  qWarning("Error attaching to shared memory id %d", shmId);
176 #endif
177  detach();
178  return false;
179  }
180 
181  return true;
182 }
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
Q_CORE_EXPORT void qWarning(const char *,...)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
#define st(var, type, card)
#define O_RDWR

◆ create()

bool QWSSharedMemory::create ( int  size)

Definition at line 88 of file qwssharedmemory.cpp.

Referenced by QWSSharedMemSurface::setGeometry().

89 {
90  if (shmId != -1)
91  detach();
92 
93 #ifndef QT_POSIX_IPC
94  shmId = shmget(IPC_PRIVATE, size, IPC_CREAT | 0600);
95 #else
96  // ### generate really unique IDs
97  shmId = (getpid() << 16) + (localUniqueId.fetchAndAddRelaxed(1) % ushort(-1));
98  QByteArray shmName = makeKey(shmId);
99  EINTR_LOOP(hand, shm_open(shmName.constData(), O_RDWR | O_CREAT, 0660));
100  if (hand != -1) {
101  // the size may only be set once; ignore errors
102  int ret;
103  EINTR_LOOP(ret, ftruncate(hand, size));
104  if (ret == -1)
105  shmId = -1;
106  } else {
107  shmId = -1;
108  }
109 #endif
110  if (shmId == -1) {
111 #ifdef QT_SHM_DEBUG
112  perror("QWSSharedMemory::create():");
113  qWarning("Error allocating shared memory of size %d", size);
114 #endif
115  detach();
116  return false;
117  }
118 
119 #ifndef QT_POSIX_IPC
120  shmBase = shmat(shmId, 0, 0);
121  // On Linux, it is possible to attach a shared memory segment even if it
122  // is already marked to be deleted. However, POSIX.1-2001 does not specify
123  // this behaviour and many other implementations do not support it.
124  shmctl(shmId, IPC_RMID, 0);
125 #else
126  // grab the size
127  QT_STATBUF st;
128  if (QT_FSTAT(hand, &st) != -1) {
129  shmSize = st.st_size;
130  // grab the memory
131  shmBase = mmap(0, shmSize, PROT_READ | PROT_WRITE, MAP_SHARED, hand, 0);
132  }
133 #endif
134  if (shmBase == (void*)-1 || !shmBase) {
135 #ifdef QT_SHM_DEBUG
136  perror("QWSSharedMemory::create():");
137  qWarning("Error attaching to shared memory id %d", shmId);
138 #endif
139  detach();
140  return false;
141  }
142 
143  return true;
144 }
#define EINTR_LOOP(var, cmd)
Definition: qcore_unix_p.h:96
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define O_CREAT
Q_CORE_EXPORT void qWarning(const char *,...)
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
unsigned short ushort
Definition: qglobal.h:995
#define st(var, type, card)
#define O_RDWR

◆ detach()

void QWSSharedMemory::detach ( )

Definition at line 184 of file qwssharedmemory.cpp.

Referenced by attach(), create(), QWSSharedMemSurface::releaseSurface(), QWSSharedMemSurface::setGeometry(), QWSSharedMemSurface::setMemory(), and ~QWSSharedMemory().

185 {
186 #ifndef QT_POSIX_IPC
187  if (shmBase && shmBase != (void*)-1)
188  shmdt(shmBase);
189 #else
190  if (shmBase && shmBase != (void*)-1)
191  munmap(shmBase, shmSize);
192  if (hand > 0) {
193  // get the number of current attachments
194  int shm_nattch = 0;
195  QT_STATBUF st;
196  if (QT_FSTAT(hand, &st) == 0) {
197  // subtract 2 from linkcount: one for our own open and one for the dir entry
198  shm_nattch = st.st_nlink - 2;
199  }
200  qt_safe_close(hand);
201  // if there are no attachments then unlink the shared memory
202  if (shm_nattch == 0) {
203  QByteArray shmName = makeKey(shmId);
204  shm_unlink(shmName.constData());
205  }
206  }
207 #endif
208  shmBase = 0;
209  shmSize = 0;
210  shmId = -1;
211 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static int qt_safe_close(int fd)
Definition: qcore_unix_p.h:297
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
#define st(var, type, card)

◆ id()

int QWSSharedMemory::id ( ) const
inline

◆ size()

int QWSSharedMemory::size ( ) const

Definition at line 213 of file qwssharedmemory.cpp.

Referenced by address(), and QWSSharedMemSurface::setGeometry().

214 {
215  if (shmId == -1)
216  return 0;
217 
218 #ifndef QT_POSIX_IPC
219  if (!shmSize) {
220  struct shmid_ds shm;
221  shmctl(shmId, IPC_STAT, &shm);
222  shmSize = shm.shm_segsz;
223  }
224 #endif
225 
226  return shmSize;
227 }

Properties

◆ shmBase

void* QWSSharedMemory::shmBase
private

Definition at line 79 of file qwssharedmemory_p.h.

Referenced by address(), attach(), create(), and detach().

◆ shmId

int QWSSharedMemory::shmId
private

Definition at line 78 of file qwssharedmemory_p.h.

Referenced by attach(), create(), detach(), id(), and size().

◆ shmSize

int QWSSharedMemory::shmSize
mutableprivate

Definition at line 80 of file qwssharedmemory_p.h.

Referenced by attach(), create(), detach(), and size().


The documentation for this class was generated from the following files: