OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 part of core; |
| 6 |
| 7 class _MojoSharedBufferNatives { |
| 8 static List Create(int num_bytes, int flags) |
| 9 native "MojoSharedBuffer_Create"; |
| 10 |
| 11 static List Duplicate(int buffer_handle, int flags) |
| 12 native "MojoSharedBuffer_Duplicate"; |
| 13 |
| 14 static List Map(int buffer_handle, int offset, int num_bytes, int flags) |
| 15 native "MojoSharedBuffer_Map"; |
| 16 |
| 17 static int Unmap(ByteData buffer) |
| 18 native "MojoSharedBuffer_Unmap"; |
| 19 } |
| 20 |
| 21 |
| 22 class MojoSharedBuffer { |
| 23 static final int CREATE_FLAG_NONE = 0; |
| 24 static final int DUPLICATE_FLAG_NONE = 0; |
| 25 static final int MAP_FLAG_NONE = 0; |
| 26 |
| 27 RawMojoHandle handle; |
| 28 int status; |
| 29 ByteData mapping; |
| 30 |
| 31 MojoSharedBuffer._internal() { |
| 32 handle = null; |
| 33 status = MojoResult.OK; |
| 34 mapping = null; |
| 35 } |
| 36 |
| 37 factory MojoSharedBuffer.create(int num_bytes, [int flags = 0]) { |
| 38 List result = _MojoSharedBufferNatives.Create(num_bytes, flags); |
| 39 if (result == null) { |
| 40 return null; |
| 41 } |
| 42 assert((result is List) && (result.length == 2)); |
| 43 if (!MojoResult.isOk(result[0])) { |
| 44 return null; |
| 45 } |
| 46 |
| 47 MojoSharedBuffer buf = new MojoSharedBuffer._internal(); |
| 48 buf.status = result[0]; |
| 49 buf.handle = new RawMojoHandle(result[1]); |
| 50 buf.mapping = null; |
| 51 return buf; |
| 52 } |
| 53 |
| 54 factory MojoSharedBuffer.duplicate( |
| 55 MojoSharedBuffer msb, [int flags = 0]) { |
| 56 List result = _MojoSharedBufferNatives.Duplicate(msb.handle.h, flags); |
| 57 if (result == null) { |
| 58 return null; |
| 59 } |
| 60 assert((result is List) && (result.length == 2)); |
| 61 if (!MojoResult.isOk(result[0])) { |
| 62 return null; |
| 63 } |
| 64 |
| 65 MojoSharedBuffer dupe = new MojoSharedBuffer._internal(); |
| 66 dupe.status = result[0]; |
| 67 dupe.handle = new RawMojoHandle(result[1]); |
| 68 dupe.mapping = msb.mapping; |
| 69 return dupe; |
| 70 } |
| 71 |
| 72 int close() { |
| 73 int r = handle.close(); |
| 74 status = r; |
| 75 mapping = null; |
| 76 return r; |
| 77 } |
| 78 |
| 79 int map(int offset, int num_bytes, [int flags = 0]) { |
| 80 List result = _MojoSharedBufferNatives.Map( |
| 81 handle.h, offset, num_bytes, flags); |
| 82 if (result == null) { |
| 83 status = MojoResult.INVALID_ARGUMENT; |
| 84 return status; |
| 85 } |
| 86 assert((result is List) && (result.length == 2)); |
| 87 status = result[0]; |
| 88 mapping = result[1]; |
| 89 return status; |
| 90 } |
| 91 |
| 92 int unmap() { |
| 93 int r = _MojoSharedBufferNatives.Unmap(mapping); |
| 94 status = r; |
| 95 mapping = null; |
| 96 return r; |
| 97 } |
| 98 } |
OLD | NEW |