Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No Windows??? BAD CODER!!! #7

Closed
ericol opened this issue Jun 28, 2013 · 2 comments
Closed

No Windows??? BAD CODER!!! #7

ericol opened this issue Jun 28, 2013 · 2 comments
Assignees
Labels

Comments

@ericol
Copy link

ericol commented Jun 28, 2013

Hi there,
I tried to run this on window... to no avail. Basically, Go for windows does not support mmap and its siblings.

Not being the type that backs up that easily, I set to try to solve it (Bear in mind, I had Go installed locally because I found a nice tutorial few days ago, but haven't actually used it and have zero experience with the language).

I found online a Go package that is supposed to make mmap work in windows:

https://github.com/edsrzf/mmap-go

It didn't work out right out of the oven, so I tinkered with it and - several hours later - I had a working server running under windows :)

Still haven't used it so can't tell it it really works (I went as far as creating a collection, and the benchmark doesn't work). I haven't got any ide of how the prot flags work in Windows compared to *nix, and same for the flags, so I just put there what I thought was correct. Find below the patch for file.go

P.S.: HN brought me here.

diff --git a/src/loveoneanother.at/tiedot/file/file.go b/src/loveoneanother.at/tiedot/file/file.go
index ccb5a83..3170314 100644
--- a/src/loveoneanother.at/tiedot/file/file.go
+++ b/src/loveoneanother.at/tiedot/file/file.go
@@ -7,17 +7,24 @@ import (
    "log"
    "os"
    "sync"
-   "syscall"
+    "loveoneanother.at/tiedot/mmap"
 )

 type File struct {
    Name                 string
    Fh                   *os.File
    Append, Size, Growth uint64
-   Buf                  []byte
+   Buf                  mmap.MMap
    Sync                 *sync.RWMutex
 }

+//  void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
+
+// func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error)
+
+// func mmap(len int, prot, flags, hfile uintptr, off int64)
+
+//mmap.Mmap(int(file.Size), mmap.RDWR, 0, int(file.Fh.Fd()), 0 )
 // Open (create if non-exist) the file.
 func Open(name string, growth uint64) (file *File, err error) {
    if growth < 1 {
@@ -38,7 +45,7 @@ func Open(name string, growth uint64) (file *File, err error) {
    if file.Size == 0 {
        return file, file.Ensure(file.Growth)
    }
-   if file.Buf, err = syscall.Mmap(int(file.Fh.Fd()), 0, int(file.Size), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED); err != nil {
+   if file.Buf, err = mmap.MapoRegion(file.Fh, int(file.Size), mmap.RDWR, 0, 0); err != nil {
        return
    }
    // find append position
@@ -72,7 +79,7 @@ func (file *File) Ensure(more uint64) (err error) {
        return
    }
    if file.Buf != nil {
-       if err = syscall.Munmap(file.Buf); err != nil {
+       if err = file.Buf.Unmap(); err != nil {
            return
        }
    }
@@ -85,9 +92,9 @@ func (file *File) Ensure(more uint64) (err error) {
    if err = file.Fh.Sync(); err != nil {
        return
    }
-   if newSize := int(file.Size + file.Growth); newSize < 0 {
+    if newSize := int(file.Size + file.Growth); newSize < 0 {
        log.Panicf("File %s is getting too large", file.Name)
-   } else if file.Buf, err = syscall.Mmap(int(file.Fh.Fd()), 0, newSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED); err != nil {
+   } else if file.Buf, err = mmap.MapoRegion(file.Fh, newSize, mmap.RDWR, 0, 0); err != nil {
        return
    }
    file.Size += file.Growth
@@ -97,7 +104,7 @@ func (file *File) Ensure(more uint64) (err error) {

 // Close the file.
 func (file *File) Close() (err error) {
-   if err = syscall.Munmap(file.Buf); err != nil {
+   if err = file.Buf.Unmap(); err != nil {
        return
    }
    return file.Fh.Close()
@HouzuoGuo
Copy link
Owner

Oops, I should have mentioned that this has only been tested in Linux, sorry!

My laptop runs Windows 7 (as well as Linux), I should give it a try and see what the other problems are.

I will keep you updated.

@ghost ghost assigned HouzuoGuo Jun 28, 2013
@HouzuoGuo
Copy link
Owner

I made several fixes to mmap-go library and used it in tiedot beta, now it has been fully tested on Windows operating system (I tested it in Windows Server 2003 32-bit and Windows 7 64-bit).

Please check out beta branch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants