|
|
|
@ -18,8 +18,52 @@ typedef struct mmap {
|
|
|
|
|
int open_and_map(const char* file, mmap_t* restrict ptr);
|
|
|
|
|
int unmap_and_close(mmap_t map);
|
|
|
|
|
|
|
|
|
|
#ifdef _cplusplus
|
|
|
|
|
typedef void* (*map_cb)(mmap_t map);
|
|
|
|
|
void* map_and_then(const char* file, map_cb callback);
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
}
|
|
|
|
|
#include <panic.h>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
namespace mm {
|
|
|
|
|
struct mmap {
|
|
|
|
|
inline static mmap_t create_raw(const char* file)
|
|
|
|
|
{
|
|
|
|
|
mmap_t map;
|
|
|
|
|
if (!open_and_map(file, &map)) panic("Failed to map file");
|
|
|
|
|
return map;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline mmap(mmap_t raw) :inner(raw){}
|
|
|
|
|
inline mmap(const char* file)
|
|
|
|
|
: inner(create_raw(file)) {}
|
|
|
|
|
|
|
|
|
|
inline mmap(mmap&& move) : inner(move.inner)
|
|
|
|
|
{
|
|
|
|
|
auto other = const_cast<mmap_t*>(&move.inner);
|
|
|
|
|
other->ptr = nullptr;
|
|
|
|
|
}
|
|
|
|
|
inline mmap(const mmap& copt) = delete;
|
|
|
|
|
|
|
|
|
|
inline ~mmap()
|
|
|
|
|
{
|
|
|
|
|
if (inner.ptr) {
|
|
|
|
|
::unmap_and_close(inner);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline const std::uint8_t* as_ptr() const { return (const std::uint8_t*)inner.ptr; }
|
|
|
|
|
inline std::uint8_t* as_ptr() { return (std::uint8_t*)inner.ptr; }
|
|
|
|
|
|
|
|
|
|
inline std::size_t size() const { return inner.len; }
|
|
|
|
|
|
|
|
|
|
inline int as_fd() const { return inner.fd; }
|
|
|
|
|
inline const mmap_t& as_raw() const { return inner; }
|
|
|
|
|
private:
|
|
|
|
|
const mmap_t inner;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#undef restrict
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|