-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshmi_linux.go
156 lines (130 loc) · 2.66 KB
/
shmi_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//go:build linux && cgo
// +build linux,cgo
package jumpboot
/*
#cgo LDFLAGS: -lrt
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int _create(const char* name, int size, int flag) {
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
int fd = shm_open(name, flag, mode);
if (fd < 0) {
return -1;
}
if (ftruncate(fd, size) != 0) {
close(fd);
return -2;
}
return fd;
}
int Create(const char* name, int size) {
int flag = O_RDWR | O_CREAT;
return _create(name, size, flag);
}
int Open(const char* name, int size) {
int flag = O_RDWR;
return _create(name, size, flag);
}
void* Map(int fd, int size) {
void* p = mmap(
NULL, size,
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
if (p == MAP_FAILED) {
return NULL;
}
return p;
}
void Close(int fd, void* p, int size) {
if (p != NULL) {
munmap(p, size);
}
if (fd != 0) {
close(fd);
}
}
void Delete(const char* name) {
shm_unlink(name);
}
*/
import "C"
import (
"fmt"
"io"
"unsafe"
)
type shmi struct {
name string
fd C.int
v unsafe.Pointer
size int
parent bool
}
func (o *shmi) getSize() int {
return o.size
}
func (o *shmi) getPtr() unsafe.Pointer {
return o.v
}
// create shared memory. return shmi object.
func create(name string, size int) (*shmi, error) {
name = "/" + name
fd := C.Create(C.CString(name), C.int(size))
if fd < 0 {
return nil, fmt.Errorf("create")
}
v := C.Map(fd, C.int(size))
if v == nil {
C.Close(fd, nil, C.int(size))
C.Delete(C.CString(name))
}
return &shmi{name, fd, v, size, true}, nil
}
// open shared memory. return shmi object.
func open(name string, size int) (*shmi, error) {
name = "/" + name
fd := C.Open(C.CString(name), C.int(size))
if fd < 0 {
return nil, fmt.Errorf("open")
}
v := C.Map(fd, C.int(size))
if v == nil {
C.Close(fd, nil, C.int(size))
C.Delete(C.CString(name))
}
return &shmi{name, fd, v, size, false}, nil
}
func (o *shmi) close() error {
if o.v != nil {
C.Close(o.fd, o.v, C.int(o.size))
o.v = nil
}
if o.parent {
C.Delete(C.CString(o.name))
}
return nil
}
// read shared memory. return read size.
func (o *shmi) readAt(p []byte, off int64) (n int, err error) {
if off >= int64(o.size) {
return 0, io.EOF
}
if max := int64(o.size) - off; int64(len(p)) > max {
p = p[:max]
}
return copyPtr2Slice(uintptr(o.v), p, off, o.size), nil
}
// write shared memory. return write size.
func (o *shmi) writeAt(p []byte, off int64) (n int, err error) {
if off >= int64(o.size) {
return 0, io.EOF
}
if max := int64(o.size) - off; int64(len(p)) > max {
p = p[:max]
}
return copySlice2Ptr(p, uintptr(o.v), off, o.size), nil
}