OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package system | |
6 | |
7 // SharedBufferHandle is a handle for a buffer that can be shared between | |
8 // applications. | |
9 type SharedBufferHandle interface { | |
10 Handle | |
11 | |
12 // DuplicateBufferHandle duplicates the handle to a buffer. | |
13 DuplicateBufferHandle(opts *DuplicateBufferHandleOptions) (MojoResult, S
haredBufferHandle) | |
14 | |
15 // MapBuffer maps the requested part of the shared buffer given by handl
e | |
16 // into memory with specified flags. On success, it returns slice that | |
17 // points to the requested shared buffer. | |
18 MapBuffer(offset uint64, numBytes int, flags MojoMapBufferFlags) (MojoRe
sult, []byte) | |
19 | |
20 // UnmapBuffer unmaps a buffer that was returned by MapBuffer. | |
21 UnmapBuffer(buffer []byte) MojoResult | |
22 | |
23 // Gets the shared buffer information (buffer size, and the specified fl
ags). | |
24 GetBufferInformation() (MojoResult, MojoBufferInformation) | |
25 } | |
26 | |
27 type sharedBuffer struct { | |
28 // baseHandle should always be the first component of this struct, | |
29 // see |finalizeHandle()| for more details. | |
30 baseHandle | |
31 } | |
32 | |
33 func (h *sharedBuffer) DuplicateBufferHandle(opts *DuplicateBufferHandleOptions)
(MojoResult, SharedBufferHandle) { | |
34 var flags uint32 | |
35 if opts != nil { | |
36 flags = uint32(opts.Flags) | |
37 } | |
38 h.core.mu.Lock() | |
39 r, dup := sysImpl.DuplicateBufferHandle(uint32(h.mojoHandle), flags) | |
40 h.core.mu.Unlock() | |
41 return MojoResult(r), core.AcquireNativeHandle(MojoHandle(dup)).ToShared
BufferHandle() | |
42 } | |
43 | |
44 func (h *sharedBuffer) MapBuffer(offset uint64, numBytes int, flags MojoMapBuffe
rFlags) (MojoResult, []byte) { | |
45 h.core.mu.Lock() | |
46 r, buf := sysImpl.MapBuffer(uint32(h.mojoHandle), offset, uint64(numByte
s), uint32(flags)) | |
47 h.core.mu.Unlock() | |
48 if r != 0 { | |
49 return MojoResult(r), nil | |
50 } | |
51 | |
52 return MojoResult(r), buf | |
53 } | |
54 | |
55 func (h *sharedBuffer) UnmapBuffer(buffer []byte) MojoResult { | |
56 h.core.mu.Lock() | |
57 r := sysImpl.UnmapBuffer(buffer) | |
58 h.core.mu.Unlock() | |
59 return MojoResult(r) | |
60 } | |
61 | |
62 func (h *sharedBuffer) GetBufferInformation() (MojoResult, MojoBufferInformation
) { | |
63 h.core.mu.Lock() | |
64 r, flags, numBytes := sysImpl.GetBufferInformation(uint32(h.mojoHandle)) | |
65 h.core.mu.Unlock() | |
66 | |
67 if r != 0 { | |
68 return MojoResult(r), MojoBufferInformation{} | |
69 } | |
70 | |
71 return MojoResult(r), MojoBufferInformation{ | |
72 Flags: MojoBufferInformationFlags(flags), | |
73 NumBytes: numBytes} | |
74 } | |
OLD | NEW |