-
Notifications
You must be signed in to change notification settings - Fork 19
/
shm_solaris.go
198 lines (168 loc) · 4.14 KB
/
shm_solaris.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
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
// +build solaris
package shm
// #include <sys/ipc.h>
// #include <sys/shm.h>
// #include <errno.h>
import "C"
import (
"unsafe"
)
// Perm is used to pass permission information to IPC operations.
type Perm struct {
// Owner's user ID.
Uid uint32
// Owner's group ID.
Gid uint32
// Creator's user ID.
Cuid uint32
// Creator's group ID.
Cgid uint32
// Read/write permission.
Mode uint32
// Sequence number.
Seq uint32
// Key.
Key int32
}
// IdDs describes shared memory segment.
type IdDs struct {
// Operation permission struct.
Perm Perm
// Padding.
PadCgo0 [4]byte
// Size of segment in bytes.
SegSz uint64
// Flags.
Flags uint64
// Internal.
Lkcnt uint16
// Padding.
PadCgo1 [2]byte
// Pid of last shmat/shmdt.
Lpid int32
// Pid of creator.
Cpid int32
// Padding.
PadCgo2 [4]byte
// Number of current attaches.
Nattch uint64
// Internal.
Cnattch uint64
// Last attach time.
Atime int64
// Last detach time.
Dtime int64
// Last change time.
Ctime int64
// Internal.
Amp *byte
// Internal.
Gransize uint64
// Internal.
Allocated uint64
// Padding.
Pad4 [1]int64
}
// Constants.
const (
// Mode bits for `shmget`.
// Create key if key does not exist.
IPC_CREAT = 01000
// Fail if key exists.
IPC_EXCL = 02000
// Return error on wait.
IPC_NOWAIT = 04000
// Special key values.
// Private key.
IPC_PRIVATE = 0
// Flags for `shmat`.
// Attach read-only access.
SHM_RDONLY = 010000
// Round attach address to SHMLBA.
SHM_RND = 020000
// Take-over region on attach.
SHM_REMAP = 040000
// Execution access.
SHM_EXEC = 0100000
// Commands for `shmctl`.
// Lock segment (root only).
SHM_LOCK = 1
// Unlock segment (root only).
SHM_UNLOCK = 12
// Control commands for `shmctl`.
// Remove identifier.
IPC_RMID = 10
// Set `ipc_perm` options.
IPC_SET = 11
// Get `ipc_perm' options.
IPC_STAT = 12
)
// Get allocates a shared memory segment.
//
// Get() returns the identifier of the shared memory segment associated with the value of the argument key.
// A new shared memory segment is created if key has the value IPC_PRIVATE or key isn't IPC_PRIVATE,
// no shared memory segment corresponding to key exists, and IPC_CREAT is specified in shmFlg.
//
// If shmFlg specifies both IPC_CREAT and IPC_EXCL and a shared memory segment already exists for key,
// then Get() fails with errno set to EEXIST.
func Get(key int, size int, shmFlg int) (shmId int, err error) {
id, errno := C.shmget(C.key_t(key), C.size_t(size), C.int(shmFlg))
if int(id) == -1 {
return -1, errno
}
return int(id), nil
}
// At attaches the shared memory segment identified by shmId.
//
// Using At() with shmAddr equal to NULL is the preferred, portable way of attaching a shared memory segment.
func At(shmId int, shmAddr uintptr, shmFlg int) (data []byte, err error) {
addr, errno := C.shmat(C.int(shmId), unsafe.Pointer(shmAddr), C.int(shmFlg))
if int(uintptr(addr)) == -1 {
return nil, errno
}
length, err := Size(shmId)
if err != nil {
return nil, err
}
var b = struct {
addr uintptr
len int
cap int
}{uintptr(addr), int(length), int(length)}
data = *(*[]byte)(unsafe.Pointer(&b))
return data, nil
}
// Dt detaches the shared memory segment.
//
// The to-be-detached segment must be currently attached with shmAddr equal to the value returned by the attaching At() call.
func Dt(data []byte) error {
result, errno := C.shmdt(unsafe.Pointer(&data[0]))
if int(result) == -1 {
return errno
}
return nil
}
// Ctl performs the control operation specified by cmd on the shared memory segment whose identifier is given in shmId.
//
// The buf argument is a pointer to a IdDs structure.
func Ctl(shmId int, cmd int, buf *IdDs) (int, error) {
result, errno := C.shmctl(C.int(shmId), C.int(cmd), (*C.struct_shmid_ds)(unsafe.Pointer(buf)))
if int(result) == -1 {
return -1, errno
}
return int(result), nil
}
// Rm removes the shared memory segment.
func Rm(shmId int) error {
_, err := Ctl(shmId, IPC_RMID, nil)
return err
}
// Size returns size of shared memory segment.
func Size(shmId int) (int64, error) {
var idDs IdDs
_, err := Ctl(shmId, IPC_STAT, &idDs)
if err != nil {
return 0, err
}
return int64(idDs.SegSz), nil
}