-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnamed_link.go
More file actions
226 lines (203 loc) · 7.18 KB
/
Copy pathnamed_link.go
File metadata and controls
226 lines (203 loc) · 7.18 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package hqq
import (
"errors"
"os"
"unsafe"
"gosuda.org/hqq/internal/mmap"
"gosuda.org/hqq/internal/shm"
)
// NamedStandardLink owns a named shared-memory mapping and a StandardLink
// opened on that page-aligned mapping. Close unmaps and closes local resources;
// call Unlink, or use CreateStandardLink's unlink-on-close behavior, to remove
// the shared-memory name.
type NamedStandardLink struct {
*StandardLink
name string
sharedMemory *shm.SharedMemory
mapping []byte
unlinkOnClose bool
}
// NamedAdvancedLink owns a named shared-memory mapping and an AdvancedLink
// opened on that page-aligned mapping.
type NamedAdvancedLink struct {
*AdvancedLink
name string
sharedMemory *shm.SharedMemory
mapping []byte
unlinkOnClose bool
}
// CreateStandardLink creates a named, page-aligned shared-memory region sized
// for a StandardLink and opens the first endpoint on it.
func CreateStandardLink(name string, bufferCount int, bufferSize int, mode os.FileMode) (*NamedStandardLink, error) {
return openMappedNamedStandardLink(name, bufferCount, bufferSize, os.O_RDWR|os.O_CREATE|os.O_EXCL, mode, true)
}
// OpenNamedStandardLink opens an existing named shared-memory region and
// attaches a StandardLink endpoint to it.
func OpenNamedStandardLink(name string, bufferCount int, bufferSize int) (*NamedStandardLink, error) {
return openMappedNamedStandardLink(name, bufferCount, bufferSize, os.O_RDWR, 0, false)
}
func openMappedNamedStandardLink(name string, bufferCount int, bufferSize int, flags int, mode os.FileMode, unlinkOnClose bool) (*NamedStandardLink, error) {
size := SizeStandardLink(bufferCount, bufferSize)
smem, mapped, offset, err := mapNamedLink(name, size, flags, mode)
if err != nil {
return nil, err
}
link, err := OpenStandardLink(offset, bufferCount, bufferSize)
if err != nil {
cleanupMappedLink(smem, mapped, flags&os.O_CREATE != 0)
return nil, err
}
return &NamedStandardLink{
StandardLink: link,
name: name,
sharedMemory: smem,
mapping: mapped,
unlinkOnClose: unlinkOnClose,
}, nil
}
// CreateAdvancedLink creates a named shared-memory region and opens the first
// AdvancedLink endpoint with default AdvancedOptions.
func CreateAdvancedLink(name string, bufferCount int, bufferSize int, mode os.FileMode) (*NamedAdvancedLink, error) {
return CreateAdvancedLinkWithOptions(name, bufferCount, bufferSize, mode, AdvancedOptions{})
}
// CreateAdvancedLinkWithOptions is CreateAdvancedLink with explicit advanced
// protocol safety and dispatcher queue options.
func CreateAdvancedLinkWithOptions(name string, bufferCount int, bufferSize int, mode os.FileMode, opts AdvancedOptions) (*NamedAdvancedLink, error) {
return openMappedNamedAdvancedLink(name, bufferCount, bufferSize, os.O_RDWR|os.O_CREATE|os.O_EXCL, mode, true, opts)
}
// OpenNamedAdvancedLink opens an existing named shared-memory region and
// attaches an AdvancedLink endpoint with default AdvancedOptions.
func OpenNamedAdvancedLink(name string, bufferCount int, bufferSize int) (*NamedAdvancedLink, error) {
return OpenNamedAdvancedLinkWithOptions(name, bufferCount, bufferSize, AdvancedOptions{})
}
// OpenNamedAdvancedLinkWithOptions is OpenNamedAdvancedLink with explicit
// advanced protocol safety and dispatcher queue options.
func OpenNamedAdvancedLinkWithOptions(name string, bufferCount int, bufferSize int, opts AdvancedOptions) (*NamedAdvancedLink, error) {
return openMappedNamedAdvancedLink(name, bufferCount, bufferSize, os.O_RDWR, 0, false, opts)
}
func openMappedNamedAdvancedLink(name string, bufferCount int, bufferSize int, flags int, mode os.FileMode, unlinkOnClose bool, opts AdvancedOptions) (*NamedAdvancedLink, error) {
size := SizeStandardLink(bufferCount, bufferSize)
smem, mapped, offset, err := mapNamedLink(name, size, flags, mode)
if err != nil {
return nil, err
}
link, err := NewAdvancedLinkWithOptions(offset, bufferCount, bufferSize, opts)
if err != nil {
cleanupMappedLink(smem, mapped, flags&os.O_CREATE != 0)
return nil, err
}
return &NamedAdvancedLink{
AdvancedLink: link,
name: name,
sharedMemory: smem,
mapping: mapped,
unlinkOnClose: unlinkOnClose,
}, nil
}
// Name returns the shared-memory object name.
func (l *NamedStandardLink) Name() string {
if l == nil {
return ""
}
return l.name
}
// Name returns the shared-memory object name.
func (l *NamedAdvancedLink) Name() string {
if l == nil {
return ""
}
return l.name
}
// Close closes the link, unmaps the shared memory, closes the shared-memory
// handle, and unlinks the name for links created by CreateStandardLink.
func (l *NamedStandardLink) Close() error {
if l == nil {
return nil
}
var err error
if l.StandardLink != nil {
err = errors.Join(err, l.StandardLink.Close())
l.StandardLink = nil
}
err = errors.Join(err, closeMappedResources(l.sharedMemory, l.mapping, l.unlinkOnClose))
l.sharedMemory = nil
l.mapping = nil
return err
}
// Close closes the link, unmaps the shared memory, closes the shared-memory
// handle, and unlinks the name for links created by CreateAdvancedLink.
func (l *NamedAdvancedLink) Close() error {
if l == nil {
return nil
}
var err error
if l.AdvancedLink != nil {
err = errors.Join(err, l.AdvancedLink.Close())
l.AdvancedLink = nil
}
err = errors.Join(err, closeMappedResources(l.sharedMemory, l.mapping, l.unlinkOnClose))
l.sharedMemory = nil
l.mapping = nil
return err
}
// Unlink removes the shared-memory name. Existing mappings remain valid until
// closed by the operating system.
func (l *NamedStandardLink) Unlink() error {
if l == nil || l.sharedMemory == nil {
return nil
}
l.unlinkOnClose = false
return l.sharedMemory.Delete()
}
// Unlink removes the shared-memory name. Existing mappings remain valid until
// closed by the operating system.
func (l *NamedAdvancedLink) Unlink() error {
if l == nil || l.sharedMemory == nil {
return nil
}
l.unlinkOnClose = false
return l.sharedMemory.Delete()
}
func mapNamedLink(name string, size uintptr, flags int, mode os.FileMode) (*shm.SharedMemory, []byte, uintptr, error) {
if size == 0 || uintptr(int(size)) != size {
return nil, nil, 0, ErrInvalidSize
}
smem, err := shm.OpenSharedMemory(name, int(size), flags, mode)
if err != nil {
return nil, nil, 0, err
}
mapped, err := mmap.Map(smem.FD(), 0, int(size), mmap.PROT_READ|mmap.PROT_WRITE, mmap.MAP_SHARED)
if err != nil {
_ = smem.Close()
if flags&os.O_CREATE != 0 {
_ = smem.Delete()
}
return nil, nil, 0, err
}
if len(mapped) == 0 {
cleanupMappedLink(smem, mapped, flags&os.O_CREATE != 0)
return nil, nil, 0, ErrInvalidSize
}
offset := uintptr(unsafe.Pointer(unsafe.SliceData(mapped)))
if offset%pagesize != 0 {
cleanupMappedLink(smem, mapped, flags&os.O_CREATE != 0)
return nil, nil, 0, ErrMemoryAlign
}
return smem, mapped, offset, nil
}
func cleanupMappedLink(smem *shm.SharedMemory, mapped []byte, unlink bool) {
_ = closeMappedResources(smem, mapped, unlink)
}
func closeMappedResources(smem *shm.SharedMemory, mapped []byte, unlink bool) error {
var err error
if mapped != nil {
err = errors.Join(err, mmap.UnMap(mapped))
}
if smem != nil {
err = errors.Join(err, smem.Close())
if unlink {
err = errors.Join(err, smem.Delete())
}
}
return err
}