-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "AutoCloseFD.h" | ||
|
||
#include <unistd.h> | ||
|
||
namespace nixd { | ||
|
||
AutoCloseFD::~AutoCloseFD() { | ||
if (FD != ReleasedFD) [[likely]] | ||
close(FD); | ||
} | ||
|
||
AutoCloseFD::AutoCloseFD(AutoCloseFD &&That) noexcept : FD(That.get()) { | ||
That.release(); | ||
} | ||
|
||
AutoCloseFD::FDTy AutoCloseFD::get() const { return FD; } | ||
|
||
void AutoCloseFD::release() { FD = ReleasedFD; } | ||
|
||
bool AutoCloseFD::isReleased(FDTy FD) { return FD == ReleasedFD; } | ||
|
||
bool AutoCloseFD::isReleased() const { return isReleased(FD); } | ||
|
||
AutoCloseFD::AutoCloseFD(FDTy FD) : FD(FD) {} | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#pragma once | ||
|
||
#include <cerrno> | ||
#include <optional> | ||
|
||
namespace nixd { | ||
|
||
/// \brief File Descriptor RAII wrapper | ||
class AutoCloseFD { | ||
public: | ||
using FDTy = int; | ||
|
||
private: | ||
static constexpr FDTy ReleasedFD = -EBADF; | ||
FDTy FD; | ||
|
||
public: | ||
AutoCloseFD(FDTy FD); | ||
AutoCloseFD(const AutoCloseFD &) = delete; | ||
AutoCloseFD(AutoCloseFD &&That) noexcept; | ||
|
||
[[nodiscard]] bool isReleased() const; | ||
static bool isReleased(FDTy FD); | ||
|
||
~AutoCloseFD(); | ||
|
||
[[nodiscard]] FDTy get() const; | ||
void release(); | ||
}; | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "AutoRemoveShm.h" | ||
|
||
namespace nixd { | ||
|
||
AutoRemoveShm::AutoRemoveShm(std::string ShmName, | ||
boost::interprocess::offset_t Size) | ||
: ShmName(std::move(ShmName)) { | ||
Shm = boost::interprocess::shared_memory_object( | ||
boost::interprocess::open_or_create, this->ShmName.c_str(), | ||
boost::interprocess::read_write); | ||
Shm.truncate(Size); | ||
} | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include <boost/interprocess/shared_memory_object.hpp> | ||
|
||
namespace nixd { | ||
|
||
/// Shared memory object, the object will be removed in dtor. | ||
class AutoRemoveShm { | ||
boost::interprocess::shared_memory_object Shm; | ||
std::string ShmName; | ||
|
||
public: | ||
AutoRemoveShm(std::string ShmName, boost::interprocess::offset_t Size); | ||
|
||
~AutoRemoveShm() { Shm.remove(ShmName.c_str()); } | ||
|
||
[[nodiscard]] const std::string &shmName() const { return ShmName; } | ||
|
||
boost::interprocess::shared_memory_object &get() { return Shm; } | ||
}; | ||
|
||
} // namespace nixd |