gopkg.in/ro-ag/posix.v1@v1.0.6/posix.go (about) 1 //go:build darwin || linux 2 3 // Package posix is a golang implementation for mmap, shm_open for MacOSx and Linux without CGO ! 4 // MacOSx has an emulation for memfd_create using shm_open. 5 package posix 6 7 import ( 8 "syscall" 9 "unsafe" 10 ) 11 12 //ShmOpen 13 //Create and open a new object, or open an existing object. 14 //This is analogous to open. The call returns a file 15 //descriptor for use by the other interfaces listed below. 16 func ShmOpen(name string, oflag int, mode uint32) (fd int, err error) { 17 return shmOpen(name, oflag, mode) 18 } 19 20 //Ftruncate 21 //Set the size of the shared memory object. (A newly 22 //created shared memory object has a length of zero.) 23 func Ftruncate(fd int, length int) error { 24 return ftruncate(fd, length) 25 } 26 27 //Madvise 28 //This system call is used to give advice or directions to 29 //the kernel about the address range beginning at address addr and 30 //with size length bytes In most cases, the goal of such advice is 31 //to improve system or application performance. 32 func Madvise(b []byte, behav int) error { 33 return madvise(b, behav) 34 } 35 36 //Mmap 37 //Map the shared memory object into the virtual address 38 //space of the calling process. 39 //MmapAt has the same parameters as the C mmap implementation where the address is exposed. 40 func Mmap(address unsafe.Pointer, length int, prot int, flags int, fd int, offset int64) (data []byte, add uintptr, err error) { 41 return mapper.Mmap(address, uintptr(length), prot, flags, fd, offset) 42 } 43 44 //Munmap 45 //Unmap the shared memory object from the virtual address 46 //space of the calling process. 47 func Munmap(b []byte) error { 48 return mapper.Munmap(b) 49 } 50 51 //Mprotect 52 //changes the access protections for the calling 53 //process's memory pages containing any part of the address range 54 //in the interval [addr, addr+len-1]. addr must be aligned to a 55 //page boundary. 56 func Mprotect(b []byte, prot int) error { 57 return mprotect(b, prot) 58 } 59 60 //Mlock 61 //lock part of the calling process's virtual address space into RAM, 62 //preventing that memory from being paged to the swap area. 63 func Mlock(b []byte, size int) error { 64 return mlock(b, size) 65 } 66 67 //Munlock 68 //perform the converse operation, 69 //unlocking part of the calling process's virtual address 70 //space, so that pages in the specified virtual address range may 71 //once more to be swapped out if required by the kernel memory 72 //manager. 73 func Munlock(b []byte, size int) error { 74 return munlock(b, size) 75 } 76 77 //Mlockall 78 //lock all calling process's virtual address space into RAM, 79 //preventing that memory from being paged to the swap area. 80 func Mlockall(flags int) error { 81 return mlockall(flags) 82 } 83 84 //Munlockall 85 //perform the converse operation, 86 //unlocking all calling process's virtual address 87 //space, so that pages in the specified virtual address range may 88 //once more to be swapped out if required by the kernel memory 89 //manager. 90 func Munlockall() error { 91 return munlockall() 92 } 93 94 //Msync flushes changes made to the in-core copy of a file that 95 //was mapped into memory using mmap(2) back to the filesystem. 96 //Without use of this call, there is no guarantee that changes are 97 //written back before munmap(2) is called. To be more precise, the 98 //part of the file that corresponds to the memory area starting at 99 //addr and having length 'length' is updated. 100 func Msync(b []byte, flags int) error { 101 return msync(b, flags) 102 } 103 104 //ShmUnlink 105 //Remove a shared memory object shmName. 106 func ShmUnlink(path string) (err error) { 107 return shmUnlink(path) 108 } 109 110 //Close 111 //the file descriptor allocated by shm_open(3) when it 112 //is no longer needed. 113 func Close(fd int) error { 114 return closeFd(fd) 115 } 116 117 //Fstat 118 //Obtain a stat structure that describes the shared memory 119 //object. Among the information returned by this call are 120 //the object's size (st_size), permissions (st_mode), owner 121 //(st_uid), and group (st_gid). 122 func Fstat(fd int, stat *Stat_t) error { 123 return fstat(fd, stat) 124 } 125 126 //Fchown 127 //To change the ownership of a shared memory object. 128 func Fchown(fd int, uid int, gid int) error { 129 return fchown(fd, uid, gid) 130 } 131 132 //Fchmod 133 //To change the permissions of a shared memory object. 134 func Fchmod(fd int, mode int) error { 135 return fchmod(fd, mode) 136 } 137 138 //Fcntl performs one of the operations described below on the 139 //open file descriptor fd. The operation is determined by cmd 140 func Fcntl(fd int, cmd int, arg int) (val int, err error) { 141 return fcntl(fd, cmd, arg) 142 } 143 144 //Getpagesize 145 //The function returns the number of bytes in a memory page, 146 //where "page" is a fixed-length block, the unit for 147 //memory allocation and file mapping performed by mmap 148 func Getpagesize() int { 149 return syscall.Getpagesize() 150 } 151 152 //MemfdCreate creates an anonymous file and returns a file 153 //descriptor that refers to it. The file behaves like a regular 154 //file, and so can be modified, truncated, memory-mapped, and so 155 //on. However, unlike a regular file, it lives in RAM and has a 156 //volatile backing storage. Once all references to the file are 157 //dropped, it is automatically released. 158 // 159 //NOTE: MacOSx is an Emulation of the original function in Linux 160 // is made for testing only 161 func MemfdCreate(name string, flags int) (fd int, err error) { 162 return memfdCreate(name, flags) 163 } 164 165 // Single-word zero for use when we need a valid pointer to 0 bytes. 166 var _zero uintptr