mmap

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 16, 2021 License: MIT Imports: 3 Imported by: 3

README

mmap

PkgGoDev Build Status codecov Go Report Card LICENSE

Package mmap provides a way to memory-map a file.

Get started

Install
go get github.com/hslam/mmap
Import
import "github.com/hslam/mmap"
Usage
Example
package main

import (
	"fmt"
	"github.com/hslam/mmap"
	"os"
)

func main() {
	name := "mmap"
	file, err := os.Create(name)
	if err != nil {
		panic(err)
	}
	defer os.Remove(name)
	defer file.Close()
	str := "Hello world"
	file.Truncate(int64(len(str)))
	b, err := mmap.Open(int(file.Fd()), 0, len(str), mmap.READ|mmap.WRITE)
	if err != nil {
		panic(err)
	}
	defer mmap.Munmap(b)
	copy(b, []byte(str))
	mmap.Msync(b)
	buf := make([]byte, len(str))
	if n, err := file.Read(buf); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(string(buf[:n]))
	}
}
Output
Hello world
License

This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)

Author

mmap was written by Meng Huang.

Documentation

Overview

Package mmap provides a way to memory-map a file.

Index

Constants

View Source
const (
	PROT_READ  = syscall.PROT_READ
	PROT_WRITE = syscall.PROT_WRITE
	PROT_EXEC  = syscall.PROT_EXEC

	MAP_SHARED  = syscall.MAP_SHARED
	MAP_PRIVATE = syscall.MAP_PRIVATE
	MAP_COPY    = MAP_PRIVATE
)

Variables

This section is empty.

Functions

func Fd

func Fd(f *os.File) int

Fd returns the integer file descriptor referencing the open file. The file descriptor is valid only until f.Close is called or f is garbage collected.

func Fsize

func Fsize(f *os.File) int

Fsize returns the file size.

func Mmap

func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, err error)

Mmap calls the mmap system call.

func Msync

func Msync(b []byte) (err error)

Msync calls the msync system call.

func Munmap

func Munmap(b []byte) (err error)

Munmap calls the munmap system call.

func Offset

func Offset(offset int64) int64

Offset returns the valid offset.

func Open

func Open(fd int, offset int64, length int, p Prot) (data []byte, err error)

Open opens a mmap

func ProtFlags

func ProtFlags(p Prot) (prot int, flags int)

ProtFlags returns prot and flags by Prot p.

Types

type Prot

type Prot int

Prot is the protection flag.

const (
	// READ represents the read prot
	READ Prot = 1 << iota
	// WRITE represents the write prot
	WRITE
	// COPY represents the copy prot
	COPY
	// EXEC represents the exec prot
	EXEC
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL