Index: third_party/mojo/src/mojo/public/go/system/message_pipe.go |
diff --git a/third_party/mojo/src/mojo/public/go/system/message_pipe.go b/third_party/mojo/src/mojo/public/go/system/message_pipe.go |
index 79f0ae962439a358cd3c1a69004bb1d9f8b49cf2..21b81ef94c939a5c8a31c93209e14f84717d6746 100644 |
--- a/third_party/mojo/src/mojo/public/go/system/message_pipe.go |
+++ b/third_party/mojo/src/mojo/public/go/system/message_pipe.go |
@@ -4,11 +4,6 @@ |
package system |
-//#include "c_allocators.h" |
-//#include "mojo/public/c/system/core.h" |
-import "C" |
-import "unsafe" |
- |
// MessagePipeHandle is a handle for a bidirectional communication channel for |
// framed data (i.e., messages). Messages can contain plain data and/or Mojo |
// handles. |
@@ -28,50 +23,37 @@ type MessagePipeHandle interface { |
} |
type messagePipe struct { |
+ // baseHandle should always be the first component of this struct, |
+ // see |finalizeHandle()| for more details. |
baseHandle |
} |
func (h *messagePipe) ReadMessage(flags MojoReadMessageFlags) (MojoResult, []byte, []UntypedHandle) { |
h.core.mu.Lock() |
- defer h.core.mu.Unlock() |
- |
- cParams := C.MallocReadMessageParams() |
- defer C.FreeReadMessageParams(cParams) |
- *cParams.num_bytes = 0 |
- *cParams.num_handles = 0 |
- if result := C.MojoReadMessage(h.mojoHandle.cValue(), nil, cParams.num_bytes, nil, cParams.num_handles, flags.cValue()); result != C.MOJO_RESULT_RESOURCE_EXHAUSTED { |
- return MojoResult(result), nil, nil |
+ r, buf, rawHandles := sysImpl.ReadMessage(uint32(h.mojoHandle), uint32(flags)) |
+ h.core.mu.Unlock() |
+ if r != 0 { |
+ return MojoResult(r), nil, nil |
} |
- cArrays := C.MallocMessageArrays(*cParams.num_bytes, *cParams.num_handles) |
- defer C.FreeMessageArrays(cArrays) |
- result := C.MojoReadMessage(h.mojoHandle.cValue(), cArrays.bytes, cParams.num_bytes, cArrays.handles, cParams.num_handles, C.MojoReadMessageFlags(flags)) |
- bytesLen := int(*cParams.num_bytes) |
- cBytes := *(*[]byte)(newUnsafeSlice(unsafe.Pointer(cArrays.bytes), bytesLen)) |
- bytes := make([]byte, bytesLen) |
- copy(bytes, cBytes) |
- |
- handlesLen := int(*cParams.num_handles) |
- cHandles := *(*[]MojoHandle)(newUnsafeSlice(unsafe.Pointer(cArrays.handles), handlesLen)) |
- handles := []UntypedHandle{} |
- for i := 0; i < handlesLen; i++ { |
- handles = append(handles, h.core.AcquireNativeHandle(cHandles[i])) |
+ handles := make([]UntypedHandle, len(rawHandles)) |
+ for i := 0; i < len(handles); i++ { |
+ handles[i] = h.core.AcquireNativeHandle(MojoHandle(rawHandles[i])) |
} |
- return MojoResult(result), bytes, handles |
+ return MojoResult(r), buf, handles |
} |
func (h *messagePipe) WriteMessage(bytes []byte, handles []UntypedHandle, flags MojoWriteMessageFlags) MojoResult { |
- h.core.mu.Lock() |
- defer h.core.mu.Unlock() |
- |
- cArrays := C.MallocMessageArrays(C.uint32_t(len(bytes)), C.uint32_t(len(handles))) |
- defer C.FreeMessageArrays(cArrays) |
- cBytes := *(*[]byte)(newUnsafeSlice(unsafe.Pointer(cArrays.bytes), len(bytes))) |
- copy(cBytes, bytes) |
- cHandles := *(*[]MojoHandle)(newUnsafeSlice(unsafe.Pointer(cArrays.handles), len(handles))) |
- for i := 0; i < len(handles); i++ { |
- cHandles[i] = handles[i].ReleaseNativeHandle() |
+ var rawHandles []uint32 |
+ if len(handles) != 0 { |
+ rawHandles = make([]uint32, len(handles)) |
+ for i := 0; i < len(handles); i++ { |
+ rawHandles[i] = uint32(handles[i].NativeHandle()) |
+ } |
} |
- return MojoResult(C.MojoWriteMessage(h.mojoHandle.cValue(), cArrays.bytes, C.uint32_t(len(bytes)), cArrays.handles, C.uint32_t(len(handles)), flags.cValue())) |
+ h.core.mu.Lock() |
+ r := sysImpl.WriteMessage(uint32(h.mojoHandle), bytes, rawHandles, uint32(flags)) |
+ h.core.mu.Unlock() |
+ return MojoResult(r) |
} |