src/gui/embedded/qwssharedmemory.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qwssharedmemory_p.h"
       
    43 
       
    44 #if !defined(QT_NO_QWS_MULTIPROCESS)
       
    45 
       
    46 #include <sys/shm.h>
       
    47 
       
    48 QT_BEGIN_NAMESPACE
       
    49 
       
    50 QWSSharedMemory::QWSSharedMemory()
       
    51     : shmBase(0), shmSize(0), character(0),  shmId(-1), key(-1)
       
    52 {
       
    53 }
       
    54 
       
    55 
       
    56 QWSSharedMemory::~QWSSharedMemory()
       
    57 {
       
    58     detach();
       
    59 }
       
    60 
       
    61 /*
       
    62   man page says:
       
    63     On  Linux,  it is possible to attach a shared memory segment even if it
       
    64     is already marked to be deleted.  However, POSIX.1-2001 does not  spec-
       
    65     ify this behaviour and many other implementations do not support it.
       
    66 */
       
    67 
       
    68 bool QWSSharedMemory::create(int size)
       
    69 {
       
    70     if (shmId != -1)
       
    71         detach();
       
    72     shmId = shmget(IPC_PRIVATE, size, IPC_CREAT|0600);
       
    73 
       
    74     if (shmId == -1) {
       
    75 #ifdef QT_SHM_DEBUG
       
    76         perror("QWSSharedMemory::create allocating shared memory");
       
    77         qWarning("Error allocating shared memory of size %d", size);
       
    78 #endif
       
    79         return false;
       
    80     }
       
    81     shmBase = shmat(shmId,0,0);
       
    82     shmctl(shmId, IPC_RMID, 0);
       
    83     if (shmBase == (void*)-1) {
       
    84 #ifdef QT_SHM_DEBUG
       
    85         perror("QWSSharedMemory::create attaching to shared memory");
       
    86         qWarning("Error attaching to shared memory id %d", shmId);
       
    87 #endif
       
    88         shmBase = 0;
       
    89         return false;
       
    90     }
       
    91     return true;
       
    92 }
       
    93 
       
    94 bool QWSSharedMemory::attach(int id)
       
    95 {
       
    96     if (shmId == id)
       
    97         return id != -1;
       
    98     if (shmId != -1)
       
    99         detach();
       
   100 
       
   101     shmBase = shmat(id,0,0);
       
   102     if (shmBase == (void*)-1) {
       
   103 #ifdef QT_SHM_DEBUG
       
   104         perror("QWSSharedMemory::attach attaching to shared memory");
       
   105         qWarning("Error attaching to shared memory 0x%x of size %d",
       
   106                  id, size());
       
   107 #endif
       
   108         shmBase = 0;
       
   109         return false;
       
   110     }
       
   111     shmId = id;
       
   112     return true;
       
   113 }
       
   114 
       
   115 
       
   116 void QWSSharedMemory::detach ()
       
   117 {
       
   118     if (!shmBase)
       
   119         return;
       
   120     shmdt (shmBase);
       
   121     shmBase = 0;
       
   122     shmSize = 0;
       
   123     shmId = -1;
       
   124 }
       
   125 
       
   126 void QWSSharedMemory::setPermissions (mode_t mode)
       
   127 {
       
   128   struct shmid_ds shm;
       
   129   shmctl (shmId, IPC_STAT, &shm);
       
   130   shm.shm_perm.mode = mode;
       
   131   shmctl (shmId, IPC_SET, &shm);
       
   132 }
       
   133 
       
   134 int QWSSharedMemory::size () const
       
   135 {
       
   136     struct shmid_ds shm;
       
   137     shmctl (shmId, IPC_STAT, &shm);
       
   138     return shm.shm_segsz;
       
   139 }
       
   140 
       
   141 
       
   142 // old API
       
   143 
       
   144 
       
   145 
       
   146 QWSSharedMemory::QWSSharedMemory (int size, const QString &filename, char c)
       
   147 {
       
   148   shmSize = size;
       
   149   shmFile = filename;
       
   150   shmBase = 0;
       
   151   shmId = -1;
       
   152   character = c;
       
   153   key = ftok (shmFile.toLatin1().constData(), c);
       
   154 }
       
   155 
       
   156 
       
   157 
       
   158 bool QWSSharedMemory::create ()
       
   159 {
       
   160   shmId = shmget (key, shmSize, IPC_CREAT | 0666);
       
   161   return (shmId != -1);
       
   162 }
       
   163 
       
   164 void QWSSharedMemory::destroy ()
       
   165 {
       
   166     if (shmId != -1)
       
   167         shmctl(shmId, IPC_RMID, 0);
       
   168 }
       
   169 
       
   170 bool QWSSharedMemory::attach ()
       
   171 {
       
   172   if (shmId == -1)
       
   173     shmId = shmget (key, shmSize, 0);
       
   174 
       
   175   shmBase = shmat (shmId, 0, 0);
       
   176   if ((long)shmBase == -1)
       
   177       shmBase = 0;
       
   178 
       
   179   return (long)shmBase != 0;
       
   180 }
       
   181 
       
   182 
       
   183 QT_END_NAMESPACE
       
   184 
       
   185 #endif // QT_NO_QWS_MULTIPROCESS