Commit bc38458d authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent d27af7e8
......@@ -21,9 +21,13 @@
#include "wcfs_misc.h"
#include <golang/libgolang.h>
using namespace golang;
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/mman.h>
#include <memory>
......@@ -86,10 +90,10 @@ namespace mm {
// flags is MAP_* from mmap(2); MAP_FIXED is added to flags.
//
// XXX split from File?
error map_into(void *addr, size_t size, int prot, int flags, const File &f, off_t offset) {
error map_into(void *addr, size_t size, int prot, int flags, const os::File &f, off_t offset) {
void *addr2;
addr2 = mmap(addr, size, PROT_READ, MAP_FIXED | MAP_SHARED, f->fd(), offset);
addr2 = mmap(addr, size, prot, MAP_FIXED | flags, f.fd(), offset);
if (addr2 == MAP_FAILED)
return _pathError("mmap", f.name(), errno);
if (addr2 != addr)
......
......@@ -63,8 +63,8 @@ class File {
public:
friend tuple<File, error> open(const string &path, int flags, mode_t mode);
// XXX empty ctor -> fd=-1, path=?
int fd();
string name();
int fd() const;
string name() const;
error close();
error stat(struct stat *st);
......
......@@ -58,8 +58,10 @@ typedef uint64_t Oid; // XXX ok?
// TidHead is invalid Tid which is larged Tid value and means @head
const Tid TidHead = -1ULL;
string h(uint64_t v); // v -> 016x hex representation
static string h(uint64_t v); // v -> 016x hex representation
#define h_(v) (h(v).c_str())
static error mmap_zero_into_ro(void *addr, size_t size);
// XXX ok?
......@@ -275,11 +277,12 @@ error _Mapping::_remmapblk(int64_t blk, Tid at) {
// block is beyond file size - mmap with zeros - else access to memory
// after file.size will raise SIGBUS. (assumes head/f size ↑=)
if ((blk+1)*f->blksize > st.st_size) {
mm.map_zero_into_ro(blkmem); // XXX err
err = mmap_zero_into_ro(blkmem, 1*f->blksize);
// XXX err handle
}
// block is inside file - mmap file data
else {
err = mm::map_into(blkmem, f->blksize, PROT_READ, MAP_SHARED, fsfile, blk*f->blksize);
err = mm::map_into(blkmem, 1*f->blksize, PROT_READ, MAP_SHARED, fsfile, blk*f->blksize);
// XXX err handle
}
......@@ -306,6 +309,32 @@ tuple<os::File, error> WCFS::_open(const string &path, int flags) {
// ---- misc ----
string h(uint64_t v) {
static string h(uint64_t v) {
return fmt::sprintf("%016x", v);
}
// map_zero_ro mmaps read-only zeros into [addr +size) so that region as all zeros.
// created mapping, even after it is accessed, does not consume memory.
static error _mmap_zero_into_ro(void *addr, size_t size);
static error mmap_zero_into_ro(void *addr, size_t size) {
error err = _mmap_zero_into_ro(addr, size);
if (err != nil)
err = fmt::errorf("mmap zero"); // XXX -> xerr::contextf?
return err;
}
static error _mmap_zero_into_ro(void *addr, size_t size) {
// mmap /dev/zero with MAP_NORESERVE and MAP_SHARED
// this way the mapping will be able to be read, but no memory will be allocated to keep it.
os::File z;
error err;
tie(z, err) = os::open("/dev/zero");
if (err != nil)
return err;
defer([&]() {
z.close();
});
err = mm::map_into(addr, size, PROT_READ, MAP_SHARED | MAP_NORESERVE, z, 0);
if (err != nil)
return err;
return nil;
}
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