Unverified Commit 4d89b54e authored by Quan (Andy) Gan's avatar Quan (Andy) Gan Committed by GitHub
Browse files

[Bug] Fix munmap() using wrong parameter (#2519)



* fix munmap() using wrong parameter

* rename variables
Co-authored-by: default avatarMinjie Wang <wmjlyjemaine@gmail.com>
parent a6ec8480
......@@ -25,14 +25,14 @@ class SharedMemory {
* If shared memory is created in the object, it'll be owned by the object
* and will be responsible for deleting it when the object is destroyed.
*/
bool own;
bool own_;
/* \brief the file descripter of the shared memory. */
int fd;
int fd_;
/* \brief the address of the shared memory. */
void *ptr;
void *ptr_;
/* \brief the size of the shared memory. */
size_t size;
size_t size_;
/*
* \brief the name of the object.
......@@ -60,16 +60,16 @@ class SharedMemory {
/*
* \brief create shared memory.
* It creates the file and shared memory.
* \param size the size of the shared memory.
* \param sz the size of the shared memory.
* \return the address of the shared memory
*/
void *CreateNew(size_t size);
void *CreateNew(size_t sz);
/*
* \brief allocate shared memory that has been created.
* \param size the size of the shared memory.
* \param sz the size of the shared memory.
* \return the address of the shared memory
*/
void *Open(size_t size);
void *Open(size_t sz);
/*
* \brief check if the shared memory exist.
......
......@@ -41,10 +41,10 @@ class SharedMemoryResource: public Resource {
SharedMemory::SharedMemory(const std::string &name) {
#ifndef _WIN32
this->name = name;
this->own = false;
this->fd = -1;
this->ptr = nullptr;
this->size = 0;
this->own_ = false;
this->fd_ = -1;
this->ptr_ = nullptr;
this->size_ = 0;
#else
LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif // _WIN32
......@@ -52,11 +52,11 @@ SharedMemory::SharedMemory(const std::string &name) {
SharedMemory::~SharedMemory() {
#ifndef _WIN32
munmap(ptr, size);
close(fd);
if (own) {
CHECK(munmap(ptr_, size_) != -1) << strerror(errno);
close(fd_);
if (own_) {
LOG(INFO) << "remove " << name << " for shared memory";
shm_unlink(name.c_str());
CHECK(shm_unlink(name.c_str()) != -1) << strerror(errno);
// The resource has been deleted. We don't need to keep track of it any more.
DeleteResource(name);
}
......@@ -65,38 +65,40 @@ SharedMemory::~SharedMemory() {
#endif // _WIN32
}
void *SharedMemory::CreateNew(size_t size) {
void *SharedMemory::CreateNew(size_t sz) {
#ifndef _WIN32
this->own = true;
this->own_ = true;
// We need to create a shared-memory file.
// TODO(zhengda) we need to report error if the shared-memory file exists.
int flag = O_RDWR|O_CREAT;
fd = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
CHECK_NE(fd, -1) << "fail to open " << name << ": " << strerror(errno);
fd_ = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
CHECK_NE(fd_, -1) << "fail to open " << name << ": " << strerror(errno);
// Shared memory cannot be deleted if the process exits abnormally.
AddResource(name, std::shared_ptr<Resource>(new SharedMemoryResource(name)));
auto res = ftruncate(fd, size);
auto res = ftruncate(fd_, sz);
CHECK_NE(res, -1)
<< "Failed to truncate the file. " << strerror(errno);
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
CHECK_NE(ptr, MAP_FAILED)
ptr_ = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd_, 0);
CHECK_NE(ptr_, MAP_FAILED)
<< "Failed to map shared memory. mmap failed with error " << strerror(errno);
return ptr;
this->size_ = sz;
return ptr_;
#else
LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif // _WIN32
}
void *SharedMemory::Open(size_t size) {
void *SharedMemory::Open(size_t sz) {
#ifndef _WIN32
int flag = O_RDWR;
fd = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
CHECK_NE(fd, -1) << "fail to open " << name << ": " << strerror(errno);
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
CHECK_NE(ptr, MAP_FAILED)
fd_ = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
CHECK_NE(fd_, -1) << "fail to open " << name << ": " << strerror(errno);
ptr_ = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd_, 0);
CHECK_NE(ptr_, MAP_FAILED)
<< "Failed to map shared memory. mmap failed with error " << strerror(errno);
return ptr;
this->size_ = sz;
return ptr_;
#else
LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif // _WIN32
......@@ -104,9 +106,9 @@ void *SharedMemory::Open(size_t size) {
bool SharedMemory::Exist(const std::string &name) {
#ifndef _WIN32
int fd = shm_open(name.c_str(), O_RDONLY, S_IRUSR | S_IWUSR);
if (fd >= 0) {
close(fd);
int fd_ = shm_open(name.c_str(), O_RDONLY, S_IRUSR | S_IWUSR);
if (fd_ >= 0) {
close(fd_);
return true;
} else {
return false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment