SHARE
TWEET

mmap go

a guest Feb 6th, 2012 408 Never
  1. package main
  2.  
  3. import (
  4.         "fmt"
  5.         "os"
  6.         "syscall"
  7.         "unsafe"
  8.         )
  9.  
  10. func main() {
  11.         n := 1000
  12.         t := int(unsafe.Sizeof(0)) * n
  13.  
  14.         map_file, _ := os.Create("/tmp/test.dat")
  15.         _, _ = map_file.Seek(int64(t - 1), 0)
  16.         map_file.Write([]byte(" "))
  17.  
  18.         mmap, _ := syscall.Mmap(map_file.Fd(), 0, int(t), syscall.PROT_READ | syscall.PROT_WRITE, syscall.MAP_SHARED)
  19.         map_array := (*[1000]int)(unsafe.Pointer(&mmap))
  20.  
  21.         for i:=0; i < n; i++ {
  22.                 map_array[i] = i*i
  23.         }
  24.  
  25.         fmt.Println(*map_array)
  26. }
RAW Paste Data
Top