Example #1
0
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)
}
Example #2
0
File: db.go Project: jamesr/ggit
func (p pack) Close() {
	if p.p != nil {
		_ = syscall.Munmap(p.p.data)
		_ = p.pFile.Close()
	}
	_ = syscall.Munmap(p.idx.data)
	_ = p.idxFile.Close()
}
Example #3
0
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...))
}
Example #4
0
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())
	}
}
Example #5
0
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
}
Example #6
0
func (p *Program) Close() (first error) {
	if p.Text != nil {
		if err := syscall.Munmap(p.Text); err != nil && first == nil {
			first = err
		}
	}

	if p.ROData != nil {
		if err := syscall.Munmap(p.ROData); err != nil && first == nil {
			first = err
		}
	}

	return
}
Example #7
0
// 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()
}
Example #8
0
// 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()
}
Example #9
0
func unmmapFile(m *mmapData) error {
	if err := syscall.Munmap(m.o); err != nil {
		return err
	}

	return m.f.Close()
}
Example #10
0
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)
}
Example #11
0
File: shm.go Project: tcsc/squaddie
// 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
}
Example #12
0
// 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
}
Example #13
0
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
}
Example #14
0
func (this *Bitmap) Close() error {

	this.Sync()

	syscall.Munmap(this.Data)
	return nil
}
Example #15
0
// 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
}
Example #16
0
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)
	}
}
Example #17
0
// Ensure the file ahs room for more data.
func (file *File) Ensure(more uint64) (err error) {
	if file.Append+more <= file.Size {
		return
	}
	if file.Buf != nil {
		if err = syscall.Munmap(file.Buf); err != nil {
			return
		}
	}
	if _, err = file.Fh.Seek(0, os.SEEK_END); err != nil {
		return
	}
	if _, err = file.Fh.Write(make([]byte, file.Growth)); err != nil {
		return
	}
	if err = file.Fh.Sync(); err != nil {
		return
	}
	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 {
		return
	}
	file.Size += file.Growth
	log.Printf("File %s has grown %d bytes\n", file.Name, file.Growth)
	return file.Ensure(more)
}
Example #18
0
// Close closes framebuffer device and restores its contents.
func (fb *Framebuffer) Close() {
	for i := range fb.restData {
		fb.data[i] = fb.restData[i]
	}
	syscall.Munmap(fb.data)
	fb.dev.Close()
}
Example #19
0
func ReadLastNLines(f *os.File, lines int) (string, error) {
	if lines <= 0 {
		return "", fmt.Errorf("invalid line count")
	}

	stat, err := f.Stat()
	if err != nil {
		return "", err
	}

	data, err := syscall.Mmap(int(f.Fd()), 0, int(stat.Size()), syscall.PROT_READ, syscall.MAP_SHARED)
	if err != nil {
		return "", err
	}
	defer syscall.Munmap(data)

	for i := len(data) - 1; i >= 0; i-- {
		if data[i] == '\n' {
			lines--
		}

		if lines < 0 {
			return string(data[i+1 : len(data)]), nil
		}
	}

	return string(data), nil
}
Example #20
0
File: pal.go Project: remerge/gopal
func MMapPal(filename string) (*Pal, error) {
	p := &Pal{}

	fi, err := os.Stat(filename)
	if err != nil {
		return nil, err
	}
	if fi.IsDir() {
		return nil, errors.New(fmt.Sprintf("%s is a directory, file needed", filename))
	}

	file, err := os.OpenFile(filename, os.O_RDWR, 0777)
	if err != nil {
		return nil, err
	}
	defer file.Close()
	mmap, err := syscall.Mmap(int(file.Fd()), 0, int(fi.Size()), syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)
	if err != nil {
		return nil, err
	}
	err = p.From(mmap)
	if err != nil {
		syscall.Munmap(mmap)
		return nil, err
	}
	p.mmaped = true
	runtime.SetFinalizer(p, func(p *Pal) {
		p.Free()
	})
	return p, nil
}
Example #21
0
func unmmapFile(mm *mmapData) {
	len_equal_cap := mm.d[0:cap(mm.d)] // else get EINVAL
	err := syscall.Munmap(len_equal_cap)
	if err != nil {
		log.Println("unmmapFile:", err)
	}
}
Example #22
0
File: cgroup.go Project: vosst/csi
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
}
Example #23
0
func (mc *mmapCache) CloseFile(filename string) error {
	mc.Lock()
	defer mc.Unlock()

	// Make sure the file is open first
	md, ok := mc.files[filename]
	if !ok {
		mc.Unlock()
		return fmt.Errorf("File '%s' is not open", filename)
	}

	// Make sure it has a 0 count
	if md.count > 0 {
		return fmt.Errorf("File '%s' does not have a 0 reference count", filename)
	}

	// Unmap the file
	err := syscall.Munmap(md.data)
	if err != nil {
		return err
	}

	// Close the file
	err = md.file.Close()
	if err != nil {
		return err
	}

	// Remove the file from the map
	delete(mc.files, filename)

	return nil
}
Example #24
0
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

}
Example #25
0
func (fb *Framebuffer) Close() {
	// restore text mode
	ioctl(fb.tty.Fd(), KDSETMODE, unsafe.Pointer(uintptr(KD_TEXT)))
	fb.tty.Close()
	syscall.Munmap(fb.Mem)
	fb.dev.Close()
}
Example #26
0
File: pal.go Project: remerge/gopal
func (p *Pal) Free() {
	if p == nil || !p.mmaped || p.data == nil {
		return
	}
	syscall.Munmap(p.data)
	p.data = nil
	p.mmaped = false
}
Example #27
0
// Close releases resources associated with this CDB.
func (db *CDB) Close() error {
	var err error
	if db.data != nil {
		err = syscall.Munmap(db.data)
	}
	db.data = nil
	return err
}
Example #28
0
File: io.go Project: postfix/fslm
func (m *MappedFile) Close() error {
	err1 := syscall.Munmap(m.data)
	err2 := m.file.Close()
	if err1 != nil {
		return err1
	}
	return err2
}
Example #29
0
func (b *sharedMemory) Close() error {
	if err := syscall.Munmap(b.raw); err != nil {
		return err
	}
	b.blocks = nil
	b.raw = nil
	return nil
}
Example #30
0
// Unmap unmaps the buffer file from memory.
func (b *Buffer) Unmap() {
	if err := syscall.Munmap(b.data); err != nil {
		// Munmap should only fail if we pass it a bad pointer
		// which should not happen. If it does something has
		// gone terribly wrong and should not proceed further.
		panic(err)
	}
}