| | |
| | | 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) |
| | |
| | | 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) { |
| | |
| | | |
| | | // 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) |
| | | } |
| | | } |
| | |
| | | if err != nil { |
| | | return err |
| | | } else { |
| | | Detach(d) |
| | | return Detach(d) |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | 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 |
| | | } |
| | | } |