glycerine / offheap

an off-heap hash-table in Go. Used to be called go-offheap-hashtable, but we shortened it.
http://godoc.org/github.com/glycerine/offheap
MIT License
368 stars 36 forks source link

Support for Windows. #3

Open cshanjib opened 5 years ago

cshanjib commented 5 years ago

Worked well with Unix and Darwin. Need a support for windows as well.

rosunshrestha commented 5 years ago

Similar issue with me. I think this is an issue with dependency offheap uses. https://github.com/tysonmote/gommap/pull/3/files This might be the case what you are looking for. But it need to be tested,

glycerine commented 5 years ago

I welcome PRs to improve portability.

cshanjib commented 5 years ago

I went through the pull request https://github.com/tysonmote/gommap/pull/3/files which claims the support for the windows of gommap. I tried it and again got stucked in the offheap/malloc.go. Here is the log if anybody could help,

..\..\..\..\github.com\glycerine\offheap\malloc.go:41:30: cannot use int(mm.File.Fd()) (type int) as type syscall.Handle in argument to syscall.Ftruncate
..\..\..\..\github.com\glycerine\offheap\malloc.go:81:11: undefined: syscall.MAP_SHARED
..\..\..\..\github.com\glycerine\offheap\malloc.go:83:11: undefined: syscall.MAP_ANON
..\..\..\..\github.com\glycerine\offheap\malloc.go:83:30: undefined: syscall.MAP_PRIVATE
..\..\..\..\github.com\glycerine\offheap\malloc.go:117:13: undefined: syscall.Stat_t
..\..\..\..\github.com\glycerine\offheap\malloc.go:118:14: undefined: syscall.Fstat
..\..\..\..\github.com\glycerine\offheap\malloc.go:125:31: cannot use mm.Fd (type int) as type syscall.Handle in argument to syscall.Ftruncate
..\..\..\..\github.com\glycerine\offheap\malloc.go:136:10: undefined: syscall.PROT_READ
..\..\..\..\github.com\glycerine\offheap\malloc.go:136:30: undefined: syscall.PROT_WRITE
..\..\..\..\github.com\glycerine\offheap\malloc.go:144:11: undefined: syscall.MAP_ANON
..\..\..\..\github.com\glycerine\offheap\malloc.go:144:11: too many errors
glycerine commented 5 years ago

consts_win.go defines the MAP_SHARED, etc; so we just need a consts_posix.go that does the same and then have malloc.go use those constants instead of the syscall ones that vary between OS.

The int vs syscall.Handle looks like a conversion problem that should be easy to solve by looking at the expected signature of Ftruncate, and converting to the correct type.

exapsy commented 10 months ago

syscall is not specific to Windows. So, in malloc.go you would need a malloc_unix and a malloc_windows most likely, each with their corresponding

//go:build windows

or

//go:build darwin || dragonfly || freebsd || linux || nacl || netbsd || openbsd || solaris

You would have to have different implementations as well, since POSIX systems use flags such as these

        syscall.PROT_READ|syscall.PROT_WRITE,
        syscall.MAP_ANONYMOUS|syscall.MAP_PRIVATE,

while windows uses more like these

    kernel32 := windows.NewLazySystemDLL("kernel32.dll")
    virtualAlloc := kernel32.NewProc("VirtualAlloc")

    r1, _, err := virtualAlloc.Call(
        0,
        size,
        uintptr(0x1000), // MEM_COMMIT
        uintptr(0x04),   // PAGE_READWRITE
    )

In general, that's how I'm implementing, my incomplete for now, memory allocator. Separate implementations for the syscalls for separate systems, as mmap is specific to POSIX.