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()
*/
bool QSharedMemory::create(int size, AccessMode mode)
bool QSharedMemory::create(qsizetype size, AccessMode mode)
{
Q_D(QSharedMemory);
@ -384,7 +384,7 @@ bool QSharedMemory::create(int size, AccessMode mode)
\sa create(), attach()
*/
int QSharedMemory::size() const
qsizetype QSharedMemory::size() const
{
Q_D(const QSharedMemory);
return d->size;

View File

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

View File

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

View File

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

View File

@ -82,7 +82,7 @@ bool QSharedMemoryPrivate::cleanHandle()
return true;
}
bool QSharedMemoryPrivate::create(int size)
bool QSharedMemoryPrivate::create(qsizetype size)
{
if (!handle())
return false;
@ -165,11 +165,11 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
cleanHandle();
return false;
}
size = st.st_size;
size = qsizetype(st.st_size);
// grab the memory
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) {
setErrorString(QLatin1String("QSharedMemory::attach (mmap)"));
cleanHandle();
@ -191,7 +191,7 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
bool QSharedMemoryPrivate::detach()
{
// detach from the memory segment
if (::munmap(memory, size) == -1) {
if (::munmap(memory, size_t(size)) == -1) {
setErrorString(QLatin1String("QSharedMemory::detach (munmap)"));
return false;
}

View File

@ -132,7 +132,7 @@ bool QSharedMemoryPrivate::cleanHandle()
return true;
}
bool QSharedMemoryPrivate::create(int size)
bool QSharedMemoryPrivate::create(qsizetype size)
{
// build file if needed
bool createdFile = false;
@ -154,7 +154,7 @@ bool QSharedMemoryPrivate::create(int size)
}
// 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");
switch (errno) {
case EINVAL:
@ -192,7 +192,7 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
// grab the size
shmid_ds shmid_ds;
if (!shmctl(id, IPC_STAT, &shmid_ds)) {
size = (int)shmid_ds.shm_segsz;
size = (qsizetype)shmid_ds.shm_segsz;
} else {
setErrorString(QLatin1String("QSharedMemory::attach (shmctl)"));
return false;

View File

@ -122,7 +122,7 @@ bool QSharedMemoryPrivate::cleanHandle()
return true;
}
bool QSharedMemoryPrivate::create(int size)
bool QSharedMemoryPrivate::create(qsizetype size)
{
const QLatin1String function("QSharedMemory::create");
if (nativeKey.isEmpty()) {
@ -132,8 +132,14 @@ bool QSharedMemoryPrivate::create(int size)
}
// Create the file mapping.
hand = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, size,
reinterpret_cast<const wchar_t*>(nativeKey.utf16()));
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()));
setErrorString(function);
// hand is valid when it already exists unlike unix so explicitly check
@ -160,7 +166,7 @@ bool QSharedMemoryPrivate::attach(QSharedMemory::AccessMode mode)
errorString = QSharedMemory::tr("%1: size query failed").arg(QLatin1String("QSharedMemory::attach: "));
return false;
}
size = info.RegionSize;
size = qsizetype(info.RegionSize);
return true;
}