Add write API to shm objects
This commit is contained in:
parent
43bcb41a2a
commit
6b71b58997
@ -56,6 +56,8 @@ type MMap interface {
|
|||||||
Seek(offset int64, whence int) (int64, error)
|
Seek(offset int64, whence int) (int64, error)
|
||||||
Read(b []byte) (int, error)
|
Read(b []byte) (int, error)
|
||||||
ReadWithSize() ([]byte, error)
|
ReadWithSize() ([]byte, error)
|
||||||
|
Write(p []byte) (n int, err error)
|
||||||
|
WriteWithSize([]byte) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type AccessFlags int
|
type AccessFlags int
|
||||||
@ -132,6 +134,16 @@ func read_with_size(f *os.File) ([]byte, error) {
|
|||||||
return read_till_buf_full(f, make([]byte, size))
|
return read_till_buf_full(f, make([]byte, size))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func write_with_size(f *os.File, b []byte) error {
|
||||||
|
szbuf := []byte{0, 0, 0, 0}
|
||||||
|
binary.BigEndian.PutUint32(szbuf, uint32(len(b)))
|
||||||
|
_, err := f.Write(szbuf)
|
||||||
|
if err == nil {
|
||||||
|
_, err = f.Write(b)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
func test_integration_with_python(args []string) (rc int, err error) {
|
func test_integration_with_python(args []string) (rc int, err error) {
|
||||||
switch args[0] {
|
switch args[0] {
|
||||||
default:
|
default:
|
||||||
@ -154,9 +166,8 @@ func test_integration_with_python(args []string) (rc int, err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return 1, err
|
return 1, err
|
||||||
}
|
}
|
||||||
defer mmap.Close()
|
mmap.WriteWithSize(data)
|
||||||
binary.BigEndian.PutUint32(mmap.Slice(), uint32(len(data)))
|
mmap.Close()
|
||||||
copy(mmap.Slice()[4:], data)
|
|
||||||
fmt.Println(mmap.Name())
|
fmt.Println(mmap.Name())
|
||||||
}
|
}
|
||||||
return 0, nil
|
return 0, nil
|
||||||
|
|||||||
@ -65,6 +65,14 @@ func (self *file_based_mmap) Read(b []byte) (int, error) {
|
|||||||
return self.f.Read(b)
|
return self.f.Read(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *file_based_mmap) Write(b []byte) (int, error) {
|
||||||
|
return self.f.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *file_based_mmap) WriteWithSize(b []byte) error {
|
||||||
|
return write_with_size(self.f, b)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *file_based_mmap) ReadWithSize() ([]byte, error) {
|
func (self *file_based_mmap) ReadWithSize() ([]byte, error) {
|
||||||
return read_with_size(self.f)
|
return read_with_size(self.f)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -108,6 +108,14 @@ func (self *syscall_based_mmap) Read(b []byte) (int, error) {
|
|||||||
return self.f.Read(b)
|
return self.f.Read(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *syscall_based_mmap) Write(b []byte) (int, error) {
|
||||||
|
return self.f.Write(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self *syscall_based_mmap) WriteWithSize(b []byte) error {
|
||||||
|
return write_with_size(self.f, b)
|
||||||
|
}
|
||||||
|
|
||||||
func (self *syscall_based_mmap) ReadWithSize() ([]byte, error) {
|
func (self *syscall_based_mmap) ReadWithSize() ([]byte, error) {
|
||||||
return read_with_size(self.f)
|
return read_with_size(self.f)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user