From edf8a5b86f66a5b956ce17b2ba9852680dfc9c86 Mon Sep 17 00:00:00 2001
From: chenshijun <csj_sky@126.com>
Date: 星期三, 01 四月 2020 11:23:48 +0800
Subject: [PATCH] 增加只創建共享内存的接口,删除强制删除的接口

---
 shmwrap.go |   51 ++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/shmwrap.go b/shmwrap.go
index 7975d61..e855c8e 100644
--- a/shmwrap.go
+++ b/shmwrap.go
@@ -24,6 +24,16 @@
     return data, id, nil
 }
 
+// NewBlockOnly shm block with size, no attach
+func NewBlockOnly(size int, key int) (int, error) {
+    id, err := shm.Get(key, size, shm.IPC_CREAT|0666)
+    if err != nil || id == -1 {
+        fmt.Printf("NewBlock Get:%x, %v\n", key, err)
+        return -1, err
+    }
+    return id, nil
+}
+
 // AttachBlock attach exist shm
 func AttachBlock(key int) ([]byte, int, error) {
     id, err := shm.Get(key, 0, 0)
@@ -91,6 +101,28 @@
     return data, id
 }
 
+// CreateShmOnly create shm block with size, only space, no padding, return id(int)
+// context for quit
+func CreateShmOnly(ctx context.Context, size int, key int) int {
+    id, err := NewBlockOnly(size, key)
+    if err != nil {
+    loopB:
+        for {
+            select {
+            case <-ctx.Done():
+                return -1
+            default:
+                if err == nil {
+                    break loopB
+                }
+                time.Sleep(time.Millisecond)
+                id, err = NewBlockOnly(size, key)
+            }
+        }
+    }
+    return id
+}
+
 // CreatePaddingShm create padding shm block with size, return data-with-padding([]byte), id(int)
 // context for quit, padding for fill raw shm block
 func CreatePaddingShm(ctx context.Context, size int, key int, padding []byte) ([]byte, int) {
@@ -139,10 +171,11 @@
 
 // RemoveShmKey Remove shmkey
 func RemoveShmKey(shmkey int) error {
-    _, id, err := AttachBlock(shmkey)
+    d, id, err := AttachBlock(shmkey)
     if err != nil {
         return err
     } else {
+        Detach(d)
         return shm.Rm(id)
     }
 }
@@ -153,6 +186,7 @@
     if err != nil {
         return err
     } else {
+        Detach(d)
         return Detach(d)
     }
 }
@@ -177,17 +211,4 @@
     }
 
     return int(idDs.Nattch), nil
-}
-
-func ForceRemoveShm(data []byte, shmId int) error {
-    count, err := ShmAttachNum(shmId)
-    if err != nil{
-        return err
-    }else{
-        for i:=0; i < count; i++{
-            Detach(data)
-        }
-    }
-    RemoveShmID(shmId)
-    return nil
-}
+}
\ No newline at end of file

--
Gitblit v1.8.0