code format
This commit is contained in:
@@ -1,24 +1,25 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
File
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
File
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "pisharedmemory.h"
|
||||
|
||||
#include "piincludes_p.h"
|
||||
#include "pipropertystorage.h"
|
||||
#if defined(LINUX) || defined(MAC_OS)
|
||||
# define SHM_POSIX
|
||||
@@ -30,13 +31,13 @@
|
||||
|
||||
#endif
|
||||
#ifdef MAC_OS
|
||||
//# include <fcntl.h>
|
||||
// # include <fcntl.h>
|
||||
|
||||
#endif
|
||||
#ifdef SHM_POSIX
|
||||
# include <fcntl.h>
|
||||
# include <sys/stat.h>
|
||||
# include <sys/mman.h>
|
||||
# include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
|
||||
@@ -83,8 +84,7 @@ PISharedMemory::PISharedMemory(): PIIODevice() {
|
||||
PISharedMemory::PISharedMemory(const PIString & shm_name, int size, PIIODevice::DeviceMode mode): PIIODevice(shm_name, mode) {
|
||||
initPrivate();
|
||||
dsize = size;
|
||||
if (!shm_name.isEmpty())
|
||||
open();
|
||||
if (!shm_name.isEmpty()) open();
|
||||
}
|
||||
|
||||
|
||||
@@ -96,16 +96,16 @@ PISharedMemory::~PISharedMemory() {
|
||||
|
||||
bool PISharedMemory::openDevice() {
|
||||
close();
|
||||
//piCoutObj << "try open" << path() << dsize;
|
||||
// piCoutObj << "try open" << path() << dsize;
|
||||
#ifdef WINDOWS
|
||||
PRIVATE->name = ("PIP_SHM_" + path()).toByteArray();
|
||||
PRIVATE->name.push_back(0);
|
||||
const char * nm = (const char *)PRIVATE->name.data();
|
||||
PRIVATE->map = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, nm);
|
||||
//piCoutObj << "open map =" << ullong(PRIVATE->map);
|
||||
PRIVATE->map = OpenFileMappingA(FILE_MAP_ALL_ACCESS, FALSE, nm);
|
||||
// piCoutObj << "open map =" << ullong(PRIVATE->map);
|
||||
if (!PRIVATE->map) {
|
||||
PRIVATE->map = CreateFileMappingA(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, (DWORD)dsize, nm);
|
||||
//piCoutObj << "create map =" << ullong(PRIVATE->map);
|
||||
// piCoutObj << "create map =" << ullong(PRIVATE->map);
|
||||
if (!PRIVATE->map) {
|
||||
piCoutObj << "CreateFileMapping error," << errorString();
|
||||
return false;
|
||||
@@ -117,33 +117,32 @@ bool PISharedMemory::openDevice() {
|
||||
piCoutObj << "MapViewOfFile error," << errorString();
|
||||
return false;
|
||||
}
|
||||
//piCout << PRIVATE->map << PRIVATE->data;
|
||||
// piCout << PRIVATE->map << PRIVATE->data;
|
||||
#endif
|
||||
#ifdef SHM_POSIX
|
||||
PRIVATE->name = ("/pip_shm_" + path()).toByteArray();
|
||||
PRIVATE->name.push_back(0);
|
||||
int fd = shm_open((const char *)PRIVATE->name.data(), O_RDWR, 0777);
|
||||
//piCoutObj << "try open" << PICoutManipulators::Hex << fd;
|
||||
// piCoutObj << "try open" << PICoutManipulators::Hex << fd;
|
||||
if (fd < 0) {
|
||||
//piCoutObj << "shm_open error," << errorString();
|
||||
// piCoutObj << "shm_open error," << errorString();
|
||||
fd = shm_open((const char *)PRIVATE->name.data(), O_RDWR | O_CREAT, 0777);
|
||||
//piCoutObj << "try create" << PICoutManipulators::Hex << (m | O_CREAT) << fd;
|
||||
// piCoutObj << "try create" << PICoutManipulators::Hex << (m | O_CREAT) << fd;
|
||||
if (fd < 0) {
|
||||
piCoutObj << "shm_open error," << errorString();
|
||||
return false;
|
||||
}
|
||||
PRIVATE->owner = true;
|
||||
//piCoutObj << "created" << fd;
|
||||
// piCoutObj << "created" << fd;
|
||||
}
|
||||
if (fd >= 0)
|
||||
ftruncate(fd, dsize);
|
||||
if (fd >= 0) ftruncate(fd, dsize);
|
||||
PRIVATE->data = mmap(0, dsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
::close(fd);
|
||||
if (PRIVATE->data == MAP_FAILED) {
|
||||
piCoutObj << "mmap error," << errorString();
|
||||
return false;
|
||||
}
|
||||
//piCoutObj << "opened" << PRIVATE->data;
|
||||
// piCoutObj << "opened" << PRIVATE->data;
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
@@ -155,13 +154,13 @@ bool PISharedMemory::closeDevice() {
|
||||
if (PRIVATE->map) CloseHandle(PRIVATE->map);
|
||||
#endif
|
||||
#ifdef SHM_POSIX
|
||||
//piCoutObj << "close" << PIString(PRIVATE->name) << PRIVATE->data;
|
||||
// piCoutObj << "close" << PIString(PRIVATE->name) << PRIVATE->data;
|
||||
if (PRIVATE->data) munmap(PRIVATE->data, dsize);
|
||||
if (PRIVATE->owner) {
|
||||
//piCout << "unlink" << PIString(PRIVATE->name);
|
||||
// piCout << "unlink" << PIString(PRIVATE->name);
|
||||
shm_unlink((const char *)PRIVATE->name.data());
|
||||
}
|
||||
//if (fd > 0)
|
||||
// if (fd > 0)
|
||||
#endif
|
||||
initPrivate();
|
||||
return true;
|
||||
@@ -204,11 +203,11 @@ void PISharedMemory::configureFromVariantDevice(const PIPropertyStorage & d) {
|
||||
|
||||
void PISharedMemory::initPrivate() {
|
||||
#ifdef WINDOWS
|
||||
PRIVATE->map = 0;
|
||||
PRIVATE->map = 0;
|
||||
PRIVATE->data = 0;
|
||||
#endif
|
||||
#ifdef SHM_POSIX
|
||||
PRIVATE->data = 0;
|
||||
PRIVATE->data = 0;
|
||||
PRIVATE->owner = false;
|
||||
#endif
|
||||
}
|
||||
@@ -250,12 +249,12 @@ int PISharedMemory::read(void * read_to, int max_size) {
|
||||
int PISharedMemory::read(void * read_to, int max_size, int offset) {
|
||||
#ifdef WINDOWS
|
||||
if (!PRIVATE->data) return -1;
|
||||
CopyMemory(read_to, &(((char*)(PRIVATE->data))[offset]), max_size);
|
||||
CopyMemory(read_to, &(((char *)(PRIVATE->data))[offset]), max_size);
|
||||
return max_size;
|
||||
#endif
|
||||
#ifdef SHM_POSIX
|
||||
if (!PRIVATE->data) return -1;
|
||||
memcpy(read_to, &(((char*)(PRIVATE->data))[offset]), max_size);
|
||||
memcpy(read_to, &(((char *)(PRIVATE->data))[offset]), max_size);
|
||||
return max_size;
|
||||
#endif
|
||||
return -1;
|
||||
@@ -270,12 +269,12 @@ int PISharedMemory::write(const void * data, int max_size) {
|
||||
int PISharedMemory::write(const void * data, int max_size, int offset) {
|
||||
#ifdef WINDOWS
|
||||
if (!PRIVATE->data) return -1;
|
||||
CopyMemory(&(((char*)(PRIVATE->data))[offset]), data, max_size);
|
||||
CopyMemory(&(((char *)(PRIVATE->data))[offset]), data, max_size);
|
||||
return max_size;
|
||||
#endif
|
||||
#ifdef SHM_POSIX
|
||||
if (!PRIVATE->data) return -1;
|
||||
memcpy(&(((char*)(PRIVATE->data))[offset]), data, max_size);
|
||||
memcpy(&(((char *)(PRIVATE->data))[offset]), data, max_size);
|
||||
return max_size;
|
||||
#endif
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user