本文整理汇总了Golang中syscall.Mmap函数的典型用法代码示例。如果您正苦于以下问题:Golang Mmap函数的具体用法?Golang Mmap怎么用?Golang Mmap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Mmap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: newMap
func newMap(f *os.File, opts ...Option) (*mmap, error) {
m := &mmap{
flags: -1,
prot: -1,
len: -1,
}
for _, opt := range opts {
opt(m)
}
if m.flags == -1 || m.prot == -1 {
return nil, fmt.Errorf("must pass options to set the flag or prot values")
}
if f == nil && !m.anon {
return nil, fmt.Errorf("f arg cannot be nil, anon was %v", m.anon)
}
var fd uintptr
if m.anon {
fd = ^uintptr(0)
m.flags = m.flags | syscall.MAP_ANON
if m.len <= 0 {
return nil, fmt.Errorf("must use Length() if using Anon() option")
}
} else {
fd = f.Fd()
s, err := f.Stat()
if err != nil {
return nil, err
}
if s.Size() == 0 {
return nil, fmt.Errorf("cannot mmap 0 length file")
}
if m.len == -1 {
m.len = int(s.Size())
}
}
var err error
if m.anon {
m.data, err = syscall.Mmap(-1, 0, m.len, m.prot, m.flags)
} else {
m.data, err = syscall.Mmap(int(fd), m.offset, m.len, m.prot, m.flags)
}
if err != nil {
return nil, fmt.Errorf("problem with mmap system call: %q", err)
}
return m, nil
}
示例2: Open
// Open returns a pisearch object that references the two files
// name.4.idx and name.4.bin, or error if the files could not
// be opened and memory mapped.
func Open(name string) (pisearch *Pisearch, err error) {
file, err := os.Open(name + ".4.bin")
if err != nil {
log.Println("open of .4.bin failed")
return nil, err
}
fi, err := file.Stat()
if err != nil {
log.Println("stat failed")
file.Close()
return nil, err
}
numdigits := fi.Size() * 2
idxfile, err := os.Open(name + ".4.idx")
if err != nil {
log.Println("open of .4.idx failed")
file.Close()
return nil, err
}
idxfi, err := idxfile.Stat()
if err != nil {
log.Println("stat of idx failed")
idxfile.Close()
file.Close()
return nil, err
}
filemap, err := syscall.Mmap(int(file.Fd()), 0, int(fi.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
log.Println("mmap of file failed")
file.Close()
idxfile.Close()
return nil, err
}
idxmap, err := syscall.Mmap(int(idxfile.Fd()), 0, int(idxfi.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
log.Println("mmap of idx file failed")
syscall.Munmap(filemap)
file.Close()
idxfile.Close()
return nil, err
}
return &Pisearch{file, filemap, int(numdigits), idxfile, idxmap}, nil
}
示例3: seqMmapRead
// seqMmapRead does n sequential reads of records of size rsize using mmap and copy.
func seqMmapRead(fname string, rsize int, n int) (int, error) {
f, err := os.Open(fname)
if err != nil {
return 0, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return 0, err
}
fsize := int(fi.Size())
data, err := syscall.Mmap(int(f.Fd()), 0, fsize, syscall.PROT_READ, syscall.MAP_FILE|syscall.MAP_SHARED)
// syscall.Madvise(data, syscall.MADV_SEQUENTIAL)
defer syscall.Munmap(data)
buf := make([]byte, rsize)
if n <= 0 || n > fsize/rsize {
n = fsize / rsize
}
var i int
for i = 0; i < n; i++ {
offset := i * rsize
copy(buf, data[offset:offset+len(buf)])
}
return i, nil
}
示例4: newInputPreloader
func newInputPreloader(filePath string) (*inputPreloader, error) {
if filePath == "/dev/null" {
return nil, nil
}
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
info, err := file.Stat()
if err != nil {
return nil, err
}
preloader := &inputPreloader{
file: file,
fileSize: info.Size(),
}
mapping, err := syscall.Mmap(
int(preloader.file.Fd()),
0,
int(preloader.fileSize),
syscall.PROT_READ,
syscall.MAP_SHARED,
)
if err == nil {
pageSize := os.Getpagesize()
preloader.mapping = mapping
for i := 0; i < int(preloader.fileSize); i += pageSize {
preloader.checksum += preloader.mapping[i]
}
} else {
// mmap failed, so just read all the file.
io.Copy(ioutil.Discard, preloader.file)
}
return preloader, nil
}
示例5: FindValue
func (this *ByteProfile) FindValue(doc_id int64) ([]byte, error) {
if doc_id >= this.Len || doc_id < 1 {
return nil, errors.New("docid is wrong")
}
if this.ProfileList[doc_id].InMomory == true {
return this.ProfileList[doc_id].Data, nil
}
f, _ := os.Open(fmt.Sprintf("./index/%v_pfl.dat", this.Name))
defer f.Close()
fi, err := f.Stat()
if err != nil {
fmt.Printf("ERR:%v", err)
}
MmapBytes, err := syscall.Mmap(int(f.Fd()), 0, int(fi.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
fmt.Printf("MAPPING ERROR %v \n", err)
return nil, err
}
defer syscall.Munmap(MmapBytes)
StartPos := int(this.ProfileList[doc_id].Start)
EndPos := this.ProfileList[doc_id].DataLen + StartPos
this.ProfileList[doc_id].Data = make([]byte, this.ProfileList[doc_id].DataLen)
copy(this.ProfileList[doc_id].Data, MmapBytes[StartPos:EndPos])
//fmt.Printf("Cost Time : %v \n",functime("MmapBytes"))
this.ProfileList[doc_id].InMomory = true
//fmt.Printf("list : %v\n", string(this.ProfileList[doc_id].Data))
return this.ProfileList[doc_id].Data, nil
}
示例6: NewCGroup
func NewCGroup(pid int) (*Cgroup, error) {
fn := filepath.Join(Dir(pid), "cgroup")
f, err := os.Open(fn)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to read %s [%s]", fn, err))
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to stat %s [%s]", fn, err))
}
b, err := syscall.Mmap(int(f.Fd()), 0, int(stat.Size()), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to mmap %s [%s]", fn, err))
}
defer syscall.Munmap(b)
return NewCgroupFromReader(bytes.NewReader(b)), nil
}
示例7: helperMmap
func helperMmap() {
f, err := os.Create("child")
if err != nil {
log.Fatalf("Create: %v", err)
}
defer f.Close()
data, err := syscall.Mmap(int(f.Fd()), 0, mmapSize, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
log.Fatalf("Mmap: %v", err)
}
for i, b := range mmapWrites {
data[i] = b
}
if err := syscallx.Msync(data, syscall.MS_SYNC); err != nil {
log.Fatalf("Msync: %v", err)
}
if err := syscall.Munmap(data); err != nil {
log.Fatalf("Munmap: %v", err)
}
if err := f.Sync(); err != nil {
log.Fatalf("Fsync = %v", err)
}
err = f.Close()
if err != nil {
log.Fatalf("Close: %v", err)
}
}
示例8: Msync
func (t *FsyncErrorTest) Msync() {
var err error
// On OS X, msync does not cause SyncFile.
if isDarwin {
return
}
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// mmap the file.
data, err := syscall.Mmap(
int(t.f1.Fd()), 0, 4,
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
AssertEq(nil, err)
defer syscall.Munmap(data)
// msync the mapping.
err = msync(data)
ExpectThat(err, Error(HasSubstr("no such file")))
// Unmap.
err = syscall.Munmap(data)
AssertEq(nil, err)
}
示例9: initGPIO
func initGPIO(memfd int) {
buf, err := syscall.Mmap(memfd, BCM2835_GPIO_BASE, BCM2835_BLOCK_SIZE, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
if err != nil {
log.Fatalf("rpi: unable to mmap GPIO page: %v", err)
}
gpfsel = []*uint32{
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL0])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL1])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL2])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL3])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL4])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPFSEL5])),
}
gpset = []*uint32{
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPSET0])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPSET1])),
}
gpclr = []*uint32{
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPCLR0])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPCLR1])),
}
gplev = []*uint32{
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPLEV0])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPLEV1])),
}
}
示例10: Open
// Open takes a string path to a MaxMind DB file and returns a Reader
// structure or an error. The database file is opened using a memory map. Use
// the Close method on the Reader object to return the resources to the
// system.
func Open(file string) (*Reader, error) {
mapFile, err := os.Open(file)
if err != nil {
return nil, err
}
stats, err := mapFile.Stat()
if err != nil {
return nil, err
}
fileSize := int(stats.Size())
mmap, err := syscall.Mmap(int(mapFile.Fd()), 0, fileSize, syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
return nil, err
}
reader, err := FromBytes(mmap)
if err != nil {
syscall.Munmap(mmap)
mapFile.Close()
return nil, err
}
reader.file = mapFile
return reader, nil
}
示例11: NodeStoreWatch
func NodeStoreWatch(neopath string, ch chan string) {
file, err := os.Open(neopath + "/neostore.nodestore.db")
fd := int(file.Fd())
mmap, err := syscall.Mmap(fd, 0, 100*NODE_SIZE, syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
node := NodeRec{}
mmap_slice := make([]byte, len(mmap))
copy(mmap_slice, mmap)
go func() {
for {
for i := 0; i < 100; i++ {
if !bytes.Equal(mmap[i*NODE_SIZE:i*NODE_SIZE+NODE_SIZE], mmap_slice[i*NODE_SIZE:i*NODE_SIZE+NODE_SIZE]) {
node.Read(uint64(i), mmap)
fmt.Printf("updated node idx %d: \n", i)
fmt.Println(node)
}
}
copy(mmap_slice, mmap)
time.Sleep(time.Millisecond * time.Duration(rand.Int()%100))
}
}()
}
示例12: main
func main() {
const n = 4096 * 50 // size of shared memory
t := int(1) * n
fmt.Println("Total size of shared memory : ", t)
// open device file
mmap_file, err := os.OpenFile(device_file, os.O_RDWR, 0666)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer mmap_file.Close()
// mmap memory
mmap, err := syscall.Mmap(int(mmap_file.Fd()), 0, int(t),
syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer syscall.Munmap(mmap)
mmap_array := (*[n]byte)(unsafe.Pointer(&mmap[0]))
fmt.Println(*mmap_array)
// using memcpy to copy mmap memory buffer into program buffer
mmap_buf := make([]byte, t)
copy(mmap_buf, mmap_array[:])
fmt.Println(mmap_buf)
}
示例13: makeMemory
func makeMemory(size int, extraProt, extraFlags int) (mem []byte, err error) {
if size == 0 {
return
}
return syscall.Mmap(-1, 0, size, syscall.PROT_READ|syscall.PROT_WRITE|extraProt, syscall.MAP_PRIVATE|syscall.MAP_ANONYMOUS|extraFlags)
}
示例14: mmap
// mmap memory maps a DB's data file.
func mmap(db *DB, sz int) error {
// Truncate and fsync to ensure file size metadata is flushed.
// https://github.com/boltdb/bolt/issues/284
if !db.NoGrowSync && !db.readOnly {
if err := db.file.Truncate(int64(sz)); err != nil {
return fmt.Errorf("file resize error: %s", err)
}
if err := db.file.Sync(); err != nil {
return fmt.Errorf("file sync error: %s", err)
}
}
// Map the data file to memory.
b, err := syscall.Mmap(int(db.file.Fd()), 0, sz, syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
return err
}
// Advise the kernel that the mmap is accessed randomly.
if err := madvise(b, syscall.MADV_RANDOM); err != nil {
return fmt.Errorf("madvise: %s", err)
}
// Save the original byte slice and convert to a byte array pointer.
db.dataref = b
db.data = (*[maxMapSize]byte)(unsafe.Pointer(&b[0]))
db.datasz = sz
return nil
}
示例15: NewBufferMemoryFs
// NewBufferMemoryFs maps a mounted file system into shared memory and returns a handle to the shared memory buffer
func NewBufferMemoryFs(dir string, size, prot int) (Buffer, error) {
files, err := createBuffer(dir, size)
if err != nil {
return nil, err
}
file, localFile := files[0], files[1]
// MAP_NORESERVE is specifically not used because it allows mmap
// to succeed even when there are no huge pages reserved.
const flags = syscall.MAP_SHARED | syscall.MAP_POPULATE
b, err := syscall.Mmap(int(file.Fd()), 0, size, prot, flags)
if err != nil {
checkClose(file)
checkClose(localFile)
return nil, os.NewSyscallError("NewBufferMemoryFs: mmap", err)
}
buf := &sharedBuffer{file, localFile, b, stackToKeep()}
// Lock everything to avoid SIGBUS when trying to use memory
// mapped on a tmpfs that becomes full after the Statfs check in
// createBuffer.
if err := buf.LockAll(); err != nil {
checkClose(buf)
return nil, err
}
runtime.SetFinalizer(buf, (*sharedBuffer).finalize)
return buf, nil
}