diff options
| author | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-13 17:29:05 +1100 |
|---|---|---|
| committer | Nicolas James <Eele1Ephe7uZahRie@tutanota.com> | 2025-02-13 17:29:05 +1100 |
| commit | 048d28b28dcaba3b0773c129d8dd63084420b01e (patch) | |
| tree | 513fc594ef6026fcfd02bc3abf743e01055fcccd /src/memory/memory.hh | |
initial commit
Diffstat (limited to 'src/memory/memory.hh')
| -rw-r--r-- | src/memory/memory.hh | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/memory/memory.hh b/src/memory/memory.hh new file mode 100644 index 0000000..c35feea --- /dev/null +++ b/src/memory/memory.hh @@ -0,0 +1,43 @@ +#ifndef MEMORY_MEMORY_HH_ +#define MEMORY_MEMORY_HH_ + +#include <cstddef> +#include <cstring> +#include <sys/uio.h> +#include <vector> +#include <stdexcept> + +namespace memory { + +using buffer_t = std::vector<std::byte>; + +buffer_t read_buffer(const std::size_t num_bytes, const pid_t& pid, + const void* const address); +void write_buffer(const buffer_t& buffer, const pid_t& pid, + const void* const address); + +template <typename T> +T read(const pid_t& pid, const void* const address) { + static_assert(std::is_trivially_copyable<T>::value); + + const buffer_t data = read_buffer(sizeof(T), pid, address); + T ret; + std::memcpy(&ret, std::data(data), sizeof(T)); + return std::move(ret); +} + +template <typename T> +void write(const T& data, const pid_t& pid, const void* const address) { + static_assert(std::is_trivially_copyable<T>::value); + + const buffer_t buffer = [&]() { + buffer_t buffer{sizeof(T)}; + std::memcpy(std::data(buffer), &data, sizeof(T)); + return buffer; + }(); + write_buffer(buffer, pid, address); +} + +} // namespace memory + +#endif |
