Add write API to shm objects

This commit is contained in:
Kovid Goyal 2023-02-23 20:18:17 +05:30
parent 43bcb41a2a
commit 6b71b58997
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 30 additions and 3 deletions

View File

@ -56,6 +56,8 @@ type MMap interface {
Seek(offset int64, whence int) (int64, error)
Read(b []byte) (int, error)
ReadWithSize() ([]byte, error)
Write(p []byte) (n int, err error)
WriteWithSize([]byte) error
}
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))
}
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) {
switch args[0] {
default:
@ -154,9 +166,8 @@ func test_integration_with_python(args []string) (rc int, err error) {
if err != nil {
return 1, err
}
defer mmap.Close()
binary.BigEndian.PutUint32(mmap.Slice(), uint32(len(data)))
copy(mmap.Slice()[4:], data)
mmap.WriteWithSize(data)
mmap.Close()
fmt.Println(mmap.Name())
}
return 0, nil

View File

@ -65,6 +65,14 @@ func (self *file_based_mmap) Read(b []byte) (int, error) {
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) {
return read_with_size(self.f)
}

View File

@ -108,6 +108,14 @@ func (self *syscall_based_mmap) Read(b []byte) (int, error) {
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) {
return read_with_size(self.f)
}