Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(233)

Side by Side Diff: mojo/public/platform/native_cgo/system_cgo.go

Issue 1000213003: go/bindings/cgo: allocate C structs on C side (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/go/bindings/interface.go ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package native_cgo 5 package native_cgo
6 6
7 //#include "mojo/public/c/system/core.h" 7 //#include "mojo/public/c/system/core.h"
8 // // These functions are used to 8-byte align C structs.
9 // MojoResult CreateSharedBuffer(struct MojoCreateSharedBufferOptions* options,
10 // uint64_t num_bytes, MojoHandle* handle) {
11 // struct MojoCreateSharedBufferOptions aligned_options = *options;
12 // return MojoCreateSharedBuffer(&aligned_options, num_bytes, handle);
13 // }
14 //
15 // MojoResult DuplicateBufferHandle(MojoHandle handle,
16 // struct MojoDuplicateBufferHandleOptions* options, MojoHandle* duplicate) {
17 // struct MojoDuplicateBufferHandleOptions aligned_options = *options;
18 // return MojoDuplicateBufferHandle(handle, &aligned_options, duplicate);
19 // }
20 //
21 // MojoResult CreateDataPipe(struct MojoCreateDataPipeOptions* options,
22 // MojoHandle* producer, MojoHandle* consumer) {
23 // struct MojoCreateDataPipeOptions aligned_options = *options;
24 // return MojoCreateDataPipe(&aligned_options, producer, consumer);
25 // }
26 //
27 // MojoResult CreateMessagePipe(struct MojoCreateMessagePipeOptions* options,
28 // MojoHandle* handle0, MojoHandle* handle1) {
29 // struct MojoCreateMessagePipeOptions aligned_options = *options;
30 // return MojoCreateMessagePipe(&aligned_options, handle0, handle1);
31 // }
32 //
8 import "C" 33 import "C"
9 import ( 34 import (
10 "reflect" 35 "reflect"
11 "unsafe" 36 "unsafe"
12 ) 37 )
13 38
14 // CGoSystem provides an implementation of the system.System interface based on CGO 39 // CGoSystem provides an implementation of the system.System interface based on CGO
15 type CGoSystem struct{} 40 type CGoSystem struct{}
16 41
17 func (c *CGoSystem) CreateSharedBuffer(flags uint32, numBytes uint64) (int32, ui nt32) { 42 func (c *CGoSystem) CreateSharedBuffer(flags uint32, numBytes uint64) (int32, ui nt32) {
18 var opts *C.struct_MojoCreateSharedBufferOptions 43 var opts *C.struct_MojoCreateSharedBufferOptions
19 opts = &C.struct_MojoCreateSharedBufferOptions{ 44 opts = &C.struct_MojoCreateSharedBufferOptions{
20 C.uint32_t(unsafe.Sizeof(*opts)), 45 C.uint32_t(unsafe.Sizeof(*opts)),
21 C.MojoCreateSharedBufferOptionsFlags(flags), 46 C.MojoCreateSharedBufferOptionsFlags(flags),
22 } 47 }
23 var cHandle C.MojoHandle 48 var cHandle C.MojoHandle
24 » r := C.MojoCreateSharedBuffer(opts, C.uint64_t(numBytes), &cHandle) 49 » r := C.CreateSharedBuffer(opts, C.uint64_t(numBytes), &cHandle)
25 return int32(r), uint32(cHandle) 50 return int32(r), uint32(cHandle)
26 } 51 }
27 52
28 func (c *CGoSystem) DuplicateBufferHandle(handle uint32, flags uint32) (int32, u int32) { 53 func (c *CGoSystem) DuplicateBufferHandle(handle uint32, flags uint32) (int32, u int32) {
29 var opts *C.struct_MojoDuplicateBufferHandleOptions 54 var opts *C.struct_MojoDuplicateBufferHandleOptions
30 opts = &C.struct_MojoDuplicateBufferHandleOptions{ 55 opts = &C.struct_MojoDuplicateBufferHandleOptions{
31 C.uint32_t(unsafe.Sizeof(*opts)), 56 C.uint32_t(unsafe.Sizeof(*opts)),
32 C.MojoDuplicateBufferHandleOptionsFlags(flags), 57 C.MojoDuplicateBufferHandleOptionsFlags(flags),
33 } 58 }
34 var cDuplicateHandle C.MojoHandle 59 var cDuplicateHandle C.MojoHandle
35 » r := C.MojoDuplicateBufferHandle(C.MojoHandle(handle), opts, &cDuplicate Handle) 60 » r := C.DuplicateBufferHandle(C.MojoHandle(handle), opts, &cDuplicateHand le)
36 return int32(r), uint32(cDuplicateHandle) 61 return int32(r), uint32(cDuplicateHandle)
37 } 62 }
38 63
39 func (c *CGoSystem) MapBuffer(handle uint32, offset, numBytes uint64, flags uint 32) (result int32, buf []byte) { 64 func (c *CGoSystem) MapBuffer(handle uint32, offset, numBytes uint64, flags uint 32) (result int32, buf []byte) {
40 var bufPtr unsafe.Pointer 65 var bufPtr unsafe.Pointer
41 r := C.MojoMapBuffer(C.MojoHandle(handle), C.uint64_t(offset), C.uint64_ t(numBytes), &bufPtr, C.MojoMapBufferFlags(flags)) 66 r := C.MojoMapBuffer(C.MojoHandle(handle), C.uint64_t(offset), C.uint64_ t(numBytes), &bufPtr, C.MojoMapBufferFlags(flags))
42 if r != C.MOJO_RESULT_OK { 67 if r != C.MOJO_RESULT_OK {
43 return int32(r), nil 68 return int32(r), nil
44 } 69 }
45 return int32(r), unsafeByteSlice(bufPtr, int(numBytes)) 70 return int32(r), unsafeByteSlice(bufPtr, int(numBytes))
46 } 71 }
47 72
48 func (c *CGoSystem) UnmapBuffer(buf []byte) (result int32) { 73 func (c *CGoSystem) UnmapBuffer(buf []byte) (result int32) {
49 return int32(C.MojoUnmapBuffer(unsafe.Pointer(&buf[0]))) 74 return int32(C.MojoUnmapBuffer(unsafe.Pointer(&buf[0])))
50 } 75 }
51 76
52 func createDataPipeWithCOptions(opts *C.struct_MojoCreateDataPipeOptions) (resul t int32, producerHandle, consumerHandle uint32) { 77 func createDataPipeWithCOptions(opts *C.struct_MojoCreateDataPipeOptions) (resul t int32, producerHandle, consumerHandle uint32) {
53 var cProducerHandle, cConsumerHandle C.MojoHandle 78 var cProducerHandle, cConsumerHandle C.MojoHandle
54 » r := C.MojoCreateDataPipe(opts, &cProducerHandle, &cConsumerHandle) 79 » r := C.CreateDataPipe(opts, &cProducerHandle, &cConsumerHandle)
55 return int32(r), uint32(cProducerHandle), uint32(cConsumerHandle) 80 return int32(r), uint32(cProducerHandle), uint32(cConsumerHandle)
56 } 81 }
57 82
58 func (c *CGoSystem) CreateDataPipe(flags, elementNumBytes, capacityNumBytes uint 32) (result int32, producerHandle, consumerHandle uint32) { 83 func (c *CGoSystem) CreateDataPipe(flags, elementNumBytes, capacityNumBytes uint 32) (result int32, producerHandle, consumerHandle uint32) {
59 var opts *C.struct_MojoCreateDataPipeOptions 84 var opts *C.struct_MojoCreateDataPipeOptions
60 opts = &C.struct_MojoCreateDataPipeOptions{ 85 opts = &C.struct_MojoCreateDataPipeOptions{
61 C.uint32_t(unsafe.Sizeof(*opts)), 86 C.uint32_t(unsafe.Sizeof(*opts)),
62 C.MojoCreateDataPipeOptionsFlags(flags), 87 C.MojoCreateDataPipeOptionsFlags(flags),
63 C.uint32_t(elementNumBytes), 88 C.uint32_t(elementNumBytes),
64 C.uint32_t(capacityNumBytes), 89 C.uint32_t(capacityNumBytes),
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 return int32(r), int(int32(index)), satisfied, satisfiable 180 return int32(r), int(int32(index)), satisfied, satisfiable
156 } 181 }
157 182
158 func (c *CGoSystem) CreateMessagePipe(flags uint32) (int32, uint32, uint32) { 183 func (c *CGoSystem) CreateMessagePipe(flags uint32) (int32, uint32, uint32) {
159 var handle0, handle1 C.MojoHandle 184 var handle0, handle1 C.MojoHandle
160 var opts *C.struct_MojoCreateMessagePipeOptions 185 var opts *C.struct_MojoCreateMessagePipeOptions
161 opts = &C.struct_MojoCreateMessagePipeOptions{ 186 opts = &C.struct_MojoCreateMessagePipeOptions{
162 C.uint32_t(unsafe.Sizeof(*opts)), 187 C.uint32_t(unsafe.Sizeof(*opts)),
163 C.MojoCreateMessagePipeOptionsFlags(flags), 188 C.MojoCreateMessagePipeOptionsFlags(flags),
164 } 189 }
165 » r := C.MojoCreateMessagePipe(opts, &handle0, &handle1) 190 » r := C.CreateMessagePipe(opts, &handle0, &handle1)
166 return int32(r), uint32(handle0), uint32(handle1) 191 return int32(r), uint32(handle0), uint32(handle1)
167 } 192 }
168 193
169 func (c *CGoSystem) WriteMessage(handle uint32, bytes []byte, handles []uint32, flags uint32) (result int32) { 194 func (c *CGoSystem) WriteMessage(handle uint32, bytes []byte, handles []uint32, flags uint32) (result int32) {
170 var bytesPtr unsafe.Pointer 195 var bytesPtr unsafe.Pointer
171 if len(bytes) != 0 { 196 if len(bytes) != 0 {
172 bytesPtr = unsafe.Pointer(&bytes[0]) 197 bytesPtr = unsafe.Pointer(&bytes[0])
173 } 198 }
174 var handlesPtr *C.MojoHandle 199 var handlesPtr *C.MojoHandle
175 if len(handles) != 0 { 200 if len(handles) != 0 {
(...skipping 28 matching lines...) Expand all
204 Data: uintptr(ptr), 229 Data: uintptr(ptr),
205 Len: length, 230 Len: length,
206 Cap: length, 231 Cap: length,
207 } 232 }
208 return unsafe.Pointer(header) 233 return unsafe.Pointer(header)
209 } 234 }
210 235
211 func unsafeByteSlice(ptr unsafe.Pointer, length int) []byte { 236 func unsafeByteSlice(ptr unsafe.Pointer, length int) []byte {
212 return *(*[]byte)(newUnsafeSlice(ptr, length)) 237 return *(*[]byte)(newUnsafeSlice(ptr, length))
213 } 238 }
OLDNEW
« no previous file with comments | « mojo/public/go/bindings/interface.go ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698