本文整理汇总了Golang中syscall.Munmap函数的典型用法代码示例。如果您正苦于以下问题:Golang Munmap函数的具体用法?Golang Munmap怎么用?Golang Munmap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Munmap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
示例2: Close
func (p pack) Close() {
if p.p != nil {
_ = syscall.Munmap(p.p.data)
_ = p.pFile.Close()
}
_ = syscall.Munmap(p.idx.data)
_ = p.idxFile.Close()
}
示例3: Mmap_WithMsync_MunmapBeforeClose
func (t *NoErrorsTest) Mmap_WithMsync_MunmapBeforeClose() {
var n int
var err error
var expectedFsyncs []interface{}
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// 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)
AssertEq("taco", string(data))
// Modify the contents.
data[0] = 'p'
// msync. This causes an fsync, except on OS X (cf.
// https://github.com/osxfuse/osxfuse/issues/202).
err = msync(data)
ExpectEq(nil, err)
if !isDarwin {
expectedFsyncs = append(expectedFsyncs, "paco")
}
ExpectThat(t.getFlushes(), ElementsAre())
ExpectThat(t.getFsyncs(), ElementsAre(expectedFsyncs...))
// Unmap. This does not cause anything.
err = syscall.Munmap(data)
AssertEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre())
ExpectThat(t.getFsyncs(), ElementsAre(expectedFsyncs...))
// Close the file. We should now see a flush with the modified contents, even
// on OS X.
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
ExpectThat(t.getFlushes(), ElementsAre("paco"))
ExpectThat(t.getFsyncs(), ElementsAre(expectedFsyncs...))
}
示例4: Mmap_NoMsync_MunmapBeforeClose
func (t *NoErrorsTest) Mmap_NoMsync_MunmapBeforeClose() {
var n int
var err error
// Open the file.
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_RDWR, 0)
AssertEq(nil, err)
// Write some contents to the file.
n, err = t.f1.Write([]byte("taco"))
AssertEq(nil, err)
AssertEq(4, n)
// 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)
AssertEq("taco", string(data))
// Modify the contents.
data[0] = 'p'
// Unmap.
err = syscall.Munmap(data)
AssertEq(nil, err)
// munmap does not cause a flush.
ExpectThat(t.getFlushes(), ElementsAre())
ExpectThat(t.getFsyncs(), ElementsAre())
// Close the file. We should see a flush. On Darwin, this will contain out of
// date contents (cf. https://github.com/osxfuse/osxfuse/issues/202).
err = t.f1.Close()
t.f1 = nil
AssertEq(nil, err)
if isDarwin {
ExpectThat(t.getFlushes(), ElementsAre("taco"))
ExpectThat(t.getFsyncs(), ElementsAre())
} else {
ExpectThat(t.getFlushes(), ElementsAre("paco"))
ExpectThat(t.getFsyncs(), ElementsAre())
}
}
示例5: Close
func (r *Runner) Close() (first error) {
if r.stack != nil {
if err := syscall.Munmap(r.stack); err != nil && first == nil {
first = err
}
}
if r.globalsMemory != nil {
if err := syscall.Munmap(r.globalsMemory); err != nil && first == nil {
first = err
}
}
return
}
示例6: Close
// Close closes the pisearch object. Note: This code is not thread-safe.
// The caller must guarantee that no other threads are accessing the object.
func (p *Pisearch) Close() {
// I'm writing the code this way
// as a reminder to my future-self that, if you really want
// to have threads playing willy-nilly, you'll need to guard
// piMap and idxMap.
p.numDigits = 0
tmp := p.piMap
p.piMap = nil
_ = syscall.Munmap(tmp)
p.piFile.Close()
tmp = p.idxMap
p.idxMap = nil
_ = syscall.Munmap(tmp)
p.idxFile.Close()
}
示例7: Close
// Close closes the pisearch object. Note: This code is not thread-safe.
// The caller must guarantee that no other threads are accessing the object.
func (pisearch *Pisearch) Close() {
// I'm writing the code this way
// as a reminder to my future-self that, if you really want
// to have threads playing willy-nilly, you'll need to guard
// filemap_ and idxmap_.
pisearch.numDigits = 0
tmp := pisearch.filemap_
pisearch.filemap_ = nil
_ = syscall.Munmap(tmp)
pisearch.pifile_.Close()
tmp = pisearch.idxmap_
pisearch.idxmap_ = nil
_ = syscall.Munmap(tmp)
pisearch.idxfile_.Close()
}
示例8: unmmapFile
func unmmapFile(m *mmapData) error {
if err := syscall.Munmap(m.o); err != nil {
return err
}
return m.f.Close()
}
示例9: 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)
}
示例10: Close
// Unmaps the shared memory, closes the region and - if the region owns the
// underlying shm object - signals that the shm object can be unlinked
// when its unmapped by all processes.
func (r *region) Close() error {
err := syscall.Munmap(r.bytes)
if err != nil {
rpcLog.Error("Failed to unmap memory: %s", err.Error())
return err
}
err = r.fd.Close()
if err != nil {
rpcLog.Error("Failed to close shared memory handle: %s", err.Error())
return err
}
if r.unlink {
cname := C.CString(r.name)
defer C.free(unsafe.Pointer(cname))
_, err = C.shm_unlink(cname)
if err != nil {
rpcLog.Error("Failed to mark shm block for deletion: %s", err.Error())
return err
}
}
return nil
}
示例11: 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
}
示例12: findModifiedAndUntracked
func findModifiedAndUntracked() (err error) {
_, entries, _, data, err := ggit.MapIndexFile(".git/index")
if err != nil {
return err
}
defer syscall.Munmap(data)
parseIgnored(".git/info/exclude")
modified = make([]string, 0)
untracked = make([]string, 0)
ignorePatterns = make([]string, 0)
err = walkDir(".", entries)
/*
err = filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
return statusWalk(path, info, err, entries)
})
*/
if err != nil {
return err
}
return nil
}
示例13: Close
func (this *Bitmap) Close() error {
this.Sync()
syscall.Munmap(this.Data)
return nil
}
示例14: 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
}
示例15: 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)
}
}