Extend QSharedMemory to work with qsizetype for sizes

This allows larger than 2G memory segments to be allocated.

Fixes: QTBUG-76995
Change-Id: I95309eeea511fadb28724c7592298c2fcc6f1d1a
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Lars Knoll 2020-03-15 12:33:35 +01:00 committed by Volker Hilsheimer
parent 9ad1c6d835
commit a2cef41a31
7 changed files with 24 additions and 18 deletions

View File

@ -340,7 +340,7 @@ QString QSharedMemory::nativeKey() const
\sa error() \sa error()
*/ */
bool QSharedMemory::create(int size, AccessMode mode) bool QSharedMemory::create(qsizetype size, AccessMode mode)
{ {
Q_D(QSharedMemory); Q_D(QSharedMemory);
@ -384,7 +384,7 @@ bool QSharedMemory::create(int size, AccessMode mode)
\sa create(), attach() \sa create(), attach()
*/ */
int QSharedMemory::size() const qsizetype QSharedMemory::size() const
{ {
Q_D(const QSharedMemory); Q_D(const QSharedMemory);
return d->size; return d->size;

View File

@ -103,8 +103,8 @@ public:
void setNativeKey(const QString &key); void setNativeKey(const QString &key);
QString nativeKey() const; QString nativeKey() const;
bool create(int size, AccessMode mode = ReadWrite); bool create(qsizetype size, AccessMode mode = ReadWrite);
int size() const; qsizetype size() const;
bool attach(AccessMode mode = ReadWrite); bool attach(AccessMode mode = ReadWrite);
bool isAttached() const; bool isAttached() const;

View File

@ -84,7 +84,7 @@ bool QSharedMemoryPrivate::cleanHandle()
return true; return true;
} }
bool QSharedMemoryPrivate::create(int size) bool QSharedMemoryPrivate::create(qsizetype size)
{ {
Q_UNUSED(size); Q_UNUSED(size);
Q_UNIMPLEMENTED(); Q_UNIMPLEMENTED();

View File

@ -123,7 +123,7 @@ public:
QSharedMemoryPrivate(); QSharedMemoryPrivate();
void *memory; void *memory;
int size; qsizetype size;
QString key; QString key;
QString nativeKey; QString nativeKey;
QSharedMemory::SharedMemoryError error; QSharedMemory::SharedMemoryError error;
@ -145,7 +145,7 @@ public:
#endif #endif
bool initKey(); bool initKey();
bool cleanHandle(); bool cleanHandle();
bool create(int size); bool create(qsizetype size);
bool attach(QSharedMemory::AccessMode mode); bool attach(QSharedMemory::AccessMode mode);
bool detach(); bool detach();

View File

@ -82,7 +82,7 @@ bool QSharedMemoryPrivate::cleanHandle()
return true; return true;
} }
bool QSharedMemoryPrivate::create(int size) bool QSharedMemoryPrivate::create(qsizetype size)
{ {
if (!handle()) if (!handle())
return false; return false;
@ -165,11 +165,11 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
cleanHandle(); cleanHandle();
return false; return false;
} }
size = st.st_size; size = qsizetype(st.st_size);
// grab the memory // grab the memory
const int mprot = (mode == QSharedMemory::ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE); const int mprot = (mode == QSharedMemory::ReadOnly ? PROT_READ : PROT_READ | PROT_WRITE);
memory = QT_MMAP(0, size, mprot, MAP_SHARED, hand, 0); memory = QT_MMAP(0, size_t(size), mprot, MAP_SHARED, hand, 0);
if (memory == MAP_FAILED || !memory) { if (memory == MAP_FAILED || !memory) {
setErrorString(QLatin1String("QSharedMemory::attach (mmap)")); setErrorString(QLatin1String("QSharedMemory::attach (mmap)"));
cleanHandle(); cleanHandle();
@ -191,7 +191,7 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
bool QSharedMemoryPrivate::detach() bool QSharedMemoryPrivate::detach()
{ {
// detach from the memory segment // detach from the memory segment
if (::munmap(memory, size) == -1) { if (::munmap(memory, size_t(size)) == -1) {
setErrorString(QLatin1String("QSharedMemory::detach (munmap)")); setErrorString(QLatin1String("QSharedMemory::detach (munmap)"));
return false; return false;
} }

View File

@ -132,7 +132,7 @@ bool QSharedMemoryPrivate::cleanHandle()
return true; return true;
} }
bool QSharedMemoryPrivate::create(int size) bool QSharedMemoryPrivate::create(qsizetype size)
{ {
// build file if needed // build file if needed
bool createdFile = false; bool createdFile = false;
@ -154,7 +154,7 @@ bool QSharedMemoryPrivate::create(int size)
} }
// create // create
if (-1 == shmget(unix_key, size, 0600 | IPC_CREAT | IPC_EXCL)) { if (-1 == shmget(unix_key, size_t(size), 0600 | IPC_CREAT | IPC_EXCL)) {
const QLatin1String function("QSharedMemory::create"); const QLatin1String function("QSharedMemory::create");
switch (errno) { switch (errno) {
case EINVAL: case EINVAL:
@ -192,7 +192,7 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
// grab the size // grab the size
shmid_ds shmid_ds; shmid_ds shmid_ds;
if (!shmctl(id, IPC_STAT, &shmid_ds)) { if (!shmctl(id, IPC_STAT, &shmid_ds)) {
size = (int)shmid_ds.shm_segsz; size = (qsizetype)shmid_ds.shm_segsz;
} else { } else {
setErrorString(QLatin1String("QSharedMemory::attach (shmctl)")); setErrorString(QLatin1String("QSharedMemory::attach (shmctl)"));
return false; return false;

View File

@ -122,7 +122,7 @@ bool QSharedMemoryPrivate::cleanHandle()
return true; return true;
} }
bool QSharedMemoryPrivate::create(int size) bool QSharedMemoryPrivate::create(qsizetype size)
{ {
const QLatin1String function("QSharedMemory::create"); const QLatin1String function("QSharedMemory::create");
if (nativeKey.isEmpty()) { if (nativeKey.isEmpty()) {
@ -132,7 +132,13 @@ bool QSharedMemoryPrivate::create(int size)
} }
// Create the file mapping. // Create the file mapping.
hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size, DWORD high, low;
if constexpr (sizeof(qsizetype) == 8)
high = DWORD(quint64(size) >> 32);
else
high = 0;
low = DWORD(size_t(size) & 0xffffffff);
hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, high, low,
reinterpret_cast<const wchar_t *>(nativeKey.utf16())); reinterpret_cast<const wchar_t *>(nativeKey.utf16()));
setErrorString(function); setErrorString(function);
@ -160,7 +166,7 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach: ")); errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach: "));
return false; return false;
} }
size = info.RegionSize; size = qsizetype(info.RegionSize);
return true; return true;
} }