Skip to content

Commit 3af7643

Browse files
guptaskvinser52
authored andcommitted
Moved common segment code for posix and file shm segments into ShmCommon
1 parent dff2296 commit 3af7643

File tree

4 files changed

+173
-293
lines changed

4 files changed

+173
-293
lines changed

cachelib/shm/FileShmSegment.cpp

Lines changed: 7 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -27,149 +27,6 @@
2727
namespace facebook {
2828
namespace cachelib {
2929

30-
constexpr static mode_t kRWMode = 0666;
31-
typedef struct stat stat_t;
32-
33-
namespace detail {
34-
35-
// TODO(SHM_FILE): move those *Impl functions to common file, there are copied
36-
// from PosixShmSegment.cpp
37-
static int openImpl(const char* name, int flags) {
38-
const int fd = open(name, flags);
39-
40-
if (fd != -1) {
41-
return fd;
42-
}
43-
44-
switch (errno) {
45-
case EEXIST:
46-
case EMFILE:
47-
case ENFILE:
48-
case EACCES:
49-
util::throwSystemError(errno);
50-
break;
51-
case ENAMETOOLONG:
52-
case EINVAL:
53-
util::throwSystemError(errno, "Invalid segment name");
54-
break;
55-
case ENOENT:
56-
if (!(flags & O_CREAT)) {
57-
util::throwSystemError(errno);
58-
} else {
59-
XDCHECK(false);
60-
// FIXME: posix says that ENOENT is thrown only when O_CREAT
61-
// is not set. However, it seems to be set even when O_CREAT
62-
// was set and the parent of path name does not exist.
63-
util::throwSystemError(errno, "Invalid errno");
64-
}
65-
break;
66-
default:
67-
XDCHECK(false);
68-
util::throwSystemError(errno, "Invalid errno");
69-
}
70-
return kInvalidFD;
71-
}
72-
73-
static void unlinkImpl(const char* const name) {
74-
const int ret = unlink(name);
75-
if (ret == 0) {
76-
return;
77-
}
78-
79-
switch (errno) {
80-
case ENOENT:
81-
case EACCES:
82-
util::throwSystemError(errno);
83-
break;
84-
case ENAMETOOLONG:
85-
case EINVAL:
86-
util::throwSystemError(errno, "Invalid segment name");
87-
break;
88-
default:
89-
XDCHECK(false);
90-
util::throwSystemError(errno, "Invalid errno");
91-
}
92-
}
93-
94-
static void ftruncateImpl(int fd, size_t size) {
95-
const int ret = ftruncate(fd, size);
96-
if (ret == 0) {
97-
return;
98-
}
99-
switch (errno) {
100-
case EBADF:
101-
case EINVAL:
102-
util::throwSystemError(errno);
103-
break;
104-
default:
105-
XDCHECK(false);
106-
util::throwSystemError(errno, "Invalid errno");
107-
}
108-
}
109-
110-
static void fstatImpl(int fd, stat_t* buf) {
111-
const int ret = fstat(fd, buf);
112-
if (ret == 0) {
113-
return;
114-
}
115-
switch (errno) {
116-
case EBADF:
117-
case ENOMEM:
118-
case EOVERFLOW:
119-
util::throwSystemError(errno);
120-
break;
121-
default:
122-
XDCHECK(false);
123-
util::throwSystemError(errno, "Invalid errno");
124-
}
125-
}
126-
127-
static void* mmapImpl(
128-
void* addr, size_t length, int prot, int flags, int fd, off_t offset) {
129-
void* ret = mmap(addr, length, prot, flags, fd, offset);
130-
if (ret != MAP_FAILED) {
131-
return ret;
132-
}
133-
134-
switch (errno) {
135-
case EACCES:
136-
case EAGAIN:
137-
if (flags & MAP_LOCKED) {
138-
util::throwSystemError(ENOMEM);
139-
break;
140-
}
141-
case EBADF:
142-
case EINVAL:
143-
case ENFILE:
144-
case ENODEV:
145-
case ENOMEM:
146-
case EPERM:
147-
case ETXTBSY:
148-
case EOVERFLOW:
149-
util::throwSystemError(errno);
150-
break;
151-
default:
152-
XDCHECK(false);
153-
util::throwSystemError(errno, "Invalid errno");
154-
}
155-
return nullptr;
156-
}
157-
158-
static void munmapImpl(void* addr, size_t length) {
159-
const int ret = munmap(addr, length);
160-
161-
if (ret == 0) {
162-
return;
163-
} else if (errno == EINVAL) {
164-
util::throwSystemError(errno);
165-
} else {
166-
XDCHECK(false);
167-
util::throwSystemError(EINVAL, "Invalid errno");
168-
}
169-
}
170-
171-
} // namespace detail
172-
17330
FileShmSegment::FileShmSegment(ShmAttachT,
17431
const std::string& name,
17532
ShmSegmentOpts opts)
@@ -217,13 +74,15 @@ FileShmSegment::~FileShmSegment() {
21774

21875
int FileShmSegment::createNewSegment(const std::string& name) {
21976
constexpr static int createFlags = O_RDWR | O_CREAT | O_EXCL;
220-
return detail::openImpl(name.c_str(), createFlags);
77+
detail::open_func_t open_func = std::bind(open, name.c_str(), createFlags);
78+
return detail::openImpl(open_func, createFlags);
22179
}
22280

22381
int FileShmSegment::getExisting(const std::string& name,
22482
const ShmSegmentOpts& opts) {
22583
int flags = opts.readOnly ? O_RDONLY : O_RDWR;
226-
return detail::openImpl(name.c_str(), flags);
84+
detail::open_func_t open_func = std::bind(open, name.c_str(), flags);
85+
return detail::openImpl(open_func, flags);
22786
}
22887

22988
void FileShmSegment::markForRemoval() {
@@ -240,7 +99,8 @@ void FileShmSegment::markForRemoval() {
24099

241100
bool FileShmSegment::removeByPath(const std::string& path) {
242101
try {
243-
detail::unlinkImpl(path.c_str());
102+
detail::unlink_func_t unlink_func = std::bind(unlink, path.c_str());
103+
detail::unlinkImpl(unlink_func);
244104
return true;
245105
} catch (const std::system_error& e) {
246106
// unlink is opaque unlike sys-V api where its through the shmid. Hence
@@ -263,7 +123,7 @@ size_t FileShmSegment::getSize() const {
263123
return buf.st_size;
264124
} else {
265125
throw std::runtime_error(folly::sformat(
266-
"Trying to get size of segment with name {} in an invalid state",
126+
"Trying to get size of segment with name {} in an invalid state",
267127
getName()));
268128
}
269129
return 0;

cachelib/shm/PosixShmSegment.cpp

Lines changed: 8 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -27,146 +27,7 @@
2727
namespace facebook {
2828
namespace cachelib {
2929

30-
constexpr static mode_t kRWMode = 0666;
31-
typedef struct stat stat_t;
32-
33-
namespace detail {
34-
35-
static int shmOpenImpl(const char* name, int flags) {
36-
const int fd = shm_open(name, flags, kRWMode);
37-
38-
if (fd != -1) {
39-
return fd;
40-
}
41-
42-
switch (errno) {
43-
case EEXIST:
44-
case EMFILE:
45-
case ENFILE:
46-
case EACCES:
47-
util::throwSystemError(errno);
48-
break;
49-
case ENAMETOOLONG:
50-
case EINVAL:
51-
util::throwSystemError(errno, "Invalid segment name");
52-
break;
53-
case ENOENT:
54-
if (!(flags & O_CREAT)) {
55-
util::throwSystemError(errno);
56-
} else {
57-
XDCHECK(false);
58-
// FIXME: posix says that ENOENT is thrown only when O_CREAT
59-
// is not set. However, it seems to be set even when O_CREAT
60-
// was set and the parent of path name does not exist.
61-
util::throwSystemError(errno, "Invalid errno");
62-
}
63-
break;
64-
default:
65-
XDCHECK(false);
66-
util::throwSystemError(errno, "Invalid errno");
67-
}
68-
return kInvalidFD;
69-
}
70-
71-
static void shmUnlinkImpl(const char* const name) {
72-
const int ret = shm_unlink(name);
73-
if (ret == 0) {
74-
return;
75-
}
76-
77-
switch (errno) {
78-
case ENOENT:
79-
case EACCES:
80-
util::throwSystemError(errno);
81-
break;
82-
case ENAMETOOLONG:
83-
case EINVAL:
84-
util::throwSystemError(errno, "Invalid segment name");
85-
break;
86-
default:
87-
XDCHECK(false);
88-
util::throwSystemError(errno, "Invalid errno");
89-
}
90-
}
91-
92-
static void ftruncateImpl(int fd, size_t size) {
93-
const int ret = ftruncate(fd, size);
94-
if (ret == 0) {
95-
return;
96-
}
97-
switch (errno) {
98-
case EBADF:
99-
case EINVAL:
100-
util::throwSystemError(errno);
101-
break;
102-
default:
103-
XDCHECK(false);
104-
util::throwSystemError(errno, "Invalid errno");
105-
}
106-
}
107-
108-
static void fstatImpl(int fd, stat_t* buf) {
109-
const int ret = fstat(fd, buf);
110-
if (ret == 0) {
111-
return;
112-
}
113-
switch (errno) {
114-
case EBADF:
115-
case ENOMEM:
116-
case EOVERFLOW:
117-
util::throwSystemError(errno);
118-
break;
119-
default:
120-
XDCHECK(false);
121-
util::throwSystemError(errno, "Invalid errno");
122-
}
123-
}
124-
125-
static void* mmapImpl(
126-
void* addr, size_t length, int prot, int flags, int fd, off_t offset) {
127-
void* ret = mmap(addr, length, prot, flags, fd, offset);
128-
if (ret != MAP_FAILED) {
129-
return ret;
130-
}
131-
132-
switch (errno) {
133-
case EACCES:
134-
case EAGAIN:
135-
if (flags & MAP_LOCKED) {
136-
util::throwSystemError(ENOMEM);
137-
break;
138-
}
139-
case EBADF:
140-
case EINVAL:
141-
case ENFILE:
142-
case ENODEV:
143-
case ENOMEM:
144-
case EPERM:
145-
case ETXTBSY:
146-
case EOVERFLOW:
147-
util::throwSystemError(errno);
148-
break;
149-
default:
150-
XDCHECK(false);
151-
util::throwSystemError(errno, "Invalid errno");
152-
}
153-
return nullptr;
154-
}
155-
156-
static void munmapImpl(void* addr, size_t length) {
157-
const int ret = munmap(addr, length);
158-
159-
if (ret == 0) {
160-
return;
161-
} else if (errno == EINVAL) {
162-
util::throwSystemError(errno);
163-
} else {
164-
XDCHECK(false);
165-
util::throwSystemError(EINVAL, "Invalid errno");
166-
}
167-
}
168-
169-
} // namespace detail
30+
constexpr mode_t kRWMode = 0666;
17031

17132
PosixShmSegment::PosixShmSegment(ShmAttachT,
17233
const std::string& name,
@@ -215,13 +76,15 @@ PosixShmSegment::~PosixShmSegment() {
21576

21677
int PosixShmSegment::createNewSegment(const std::string& name) {
21778
constexpr static int createFlags = O_RDWR | O_CREAT | O_EXCL;
218-
return detail::shmOpenImpl(name.c_str(), createFlags);
79+
detail::open_func_t open_func = std::bind(shm_open, name.c_str(), createFlags, kRWMode);
80+
return detail::openImpl(open_func, createFlags);
21981
}
22082

22183
int PosixShmSegment::getExisting(const std::string& name,
22284
const ShmSegmentOpts& opts) {
22385
int flags = opts.readOnly ? O_RDONLY : O_RDWR;
224-
return detail::shmOpenImpl(name.c_str(), flags);
86+
detail::open_func_t open_func = std::bind(shm_open, name.c_str(), flags, kRWMode);
87+
return detail::openImpl(open_func, flags);
22588
}
22689

22790
void PosixShmSegment::markForRemoval() {
@@ -239,7 +102,8 @@ void PosixShmSegment::markForRemoval() {
239102
bool PosixShmSegment::removeByName(const std::string& segmentName) {
240103
try {
241104
auto key = createKeyForName(segmentName);
242-
detail::shmUnlinkImpl(key.c_str());
105+
detail::unlink_func_t unlink_func = std::bind(shm_unlink, key.c_str());
106+
detail::unlinkImpl(unlink_func);
243107
return true;
244108
} catch (const std::system_error& e) {
245109
// unlink is opaque unlike sys-V api where its through the shmid. Hence
@@ -258,7 +122,7 @@ size_t PosixShmSegment::getSize() const {
258122
return buf.st_size;
259123
} else {
260124
throw std::runtime_error(folly::sformat(
261-
"Trying to get size of segment with name {} in an invalid state",
125+
"Trying to get size of segment with name {} in an invalid state",
262126
getName()));
263127
}
264128
return 0;

0 commit comments

Comments
 (0)