update methods

This commit is contained in:
hslam 2020-12-01 16:05:01 +08:00
parent 2829f84507
commit ef08b8c480
3 changed files with 22 additions and 22 deletions

View File

@ -34,12 +34,12 @@ func main() {
if err != nil {
panic(err)
}
shmid, data, err := shm.GetAt(key, 128, shm.IPC_CREAT|0600)
shmid, data, err := shm.GetAttach(key, 128, shm.IPC_CREAT|0600)
if err != nil {
panic(err)
}
defer shm.Remove(shmid)
defer shm.Dt(data)
defer shm.Detach(data)
context := []byte("Hello World")
copy(data, context)
fmt.Println(string(data[:11]))
@ -61,11 +61,11 @@ func main() {
if err != nil {
panic(err)
}
_, data, err := shm.GetAt(key, 128, 0600)
_, data, err := shm.GetAttach(key, 128, 0600)
if err != nil {
panic(err)
}
defer shm.Dt(data)
defer shm.Detach(data)
fmt.Println(string(data[:11]))
}
```

View File

@ -47,8 +47,8 @@ func Get(key int, size int, shmFlg int) (int, error) {
return shmid, nil
}
// Attach calls the shmat system call.
func Attach(shmid int, shmFlg int) (uintptr, error) {
// At calls the shmat system call.
func At(shmid int, shmFlg int) (uintptr, error) {
shmaddr, _, errno := syscall.Syscall(SYS_SHMAT, uintptr(shmid), 0, uintptr(shmFlg))
if int(shmaddr) < 0 {
return shmaddr, syscall.Errno(errno)
@ -56,8 +56,8 @@ func Attach(shmid int, shmFlg int) (uintptr, error) {
return shmaddr, nil
}
// Detach calls the shmdt system call.
func Detach(addr uintptr) error {
// Dt calls the shmdt system call.
func Dt(addr uintptr) error {
r1, _, errno := syscall.Syscall(SYS_SHMDT, addr, 0, 0)
if int(r1) < 0 {
return syscall.Errno(errno)
@ -65,13 +65,13 @@ func Detach(addr uintptr) error {
return nil
}
// GetAt calls the shmget and shmat system call.
func GetAt(key int, size int, shmFlg int) (int, []byte, error) {
// GetAttach calls the shmget and shmat system call.
func GetAttach(key int, size int, shmFlg int) (int, []byte, error) {
shmid, err := Get(key, size, shmFlg)
if err != nil {
return shmid, nil, err
}
shmaddr, err := Attach(shmid, shmFlg)
shmaddr, err := At(shmid, shmFlg)
if err != nil {
Remove(shmid)
return shmid, nil, err
@ -85,9 +85,9 @@ func GetAt(key int, size int, shmFlg int) (int, []byte, error) {
return shmid, b, nil
}
// Dt calls the shmdt system call with []byte b.
func Dt(b []byte) error {
return Detach(uintptr(unsafe.Pointer(&b[0])))
// Detach calls the shmdt system call with []byte b.
func Detach(b []byte) error {
return Dt(uintptr(unsafe.Pointer(&b[0])))
}
// Remove removes the shm with the given id.

View File

@ -19,12 +19,12 @@ func TestGetAt(t *testing.T) {
if err != nil {
panic(err)
}
shmid, data, err := GetAt(key, 128, IPC_CREAT|0600)
shmid, data, err := GetAttach(key, 128, IPC_CREAT|0600)
if err != nil {
t.Error(err)
}
defer Remove(shmid)
defer Dt(data)
defer Detach(data)
copy(data, context)
time.Sleep(time.Millisecond * 200)
close(done)
@ -34,11 +34,11 @@ func TestGetAt(t *testing.T) {
if err != nil {
panic(err)
}
_, data, err := GetAt(key, 128, 0600)
_, data, err := GetAttach(key, 128, 0600)
if err != nil {
t.Error(err)
}
defer Dt(data)
defer Detach(data)
if context != string(data[:11]) {
t.Error(context, string(data[:11]))
}
@ -53,12 +53,12 @@ func TestGetAtZeroFlag(t *testing.T) {
if err != nil {
panic(err)
}
shmid, data, err := GetAt(key, 128, 0)
shmid, data, err := GetAttach(key, 128, 0)
if err != nil {
t.Error(err)
}
defer Remove(shmid)
defer Dt(data)
defer Detach(data)
copy(data, context)
time.Sleep(time.Millisecond * 200)
close(done)
@ -68,11 +68,11 @@ func TestGetAtZeroFlag(t *testing.T) {
if err != nil {
panic(err)
}
_, data, err := GetAt(key, 128, 0600)
_, data, err := GetAttach(key, 128, 0600)
if err != nil {
t.Error(err)
}
defer Dt(data)
defer Detach(data)
if context != string(data[:11]) {
t.Error(context, string(data[:11]))
}