1 | // Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file |
---|---|
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "bin/typed_data_utils.h" |
6 | #include "platform/assert.h" |
7 | |
8 | namespace dart { |
9 | namespace bin { |
10 | |
11 | TypedDataScope::TypedDataScope(Dart_Handle data) : data_handle_(data) { |
12 | Dart_Handle result; |
13 | result = Dart_TypedDataAcquireData(data, &type_, &data_, &length_); |
14 | if (Dart_IsError(result)) { |
15 | Dart_PropagateError(result); |
16 | } |
17 | } |
18 | |
19 | void TypedDataScope::Release() { |
20 | if (data_handle_ == NULL) { |
21 | return; |
22 | } |
23 | Dart_Handle result = Dart_TypedDataReleaseData(data_handle_); |
24 | if (Dart_IsError(result)) { |
25 | Dart_PropagateError(result); |
26 | } |
27 | data_handle_ = NULL; |
28 | data_ = NULL; |
29 | length_ = 0; |
30 | type_ = Dart_TypedData_kInvalid; |
31 | } |
32 | |
33 | intptr_t TypedDataScope::size_in_bytes() const { |
34 | switch (type_) { |
35 | case Dart_TypedData_kByteData: |
36 | case Dart_TypedData_kInt8: |
37 | case Dart_TypedData_kUint8: |
38 | case Dart_TypedData_kUint8Clamped: |
39 | return length_; |
40 | case Dart_TypedData_kInt16: |
41 | case Dart_TypedData_kUint16: |
42 | return length_ * 2; |
43 | case Dart_TypedData_kInt32: |
44 | case Dart_TypedData_kUint32: |
45 | case Dart_TypedData_kFloat32: |
46 | return length_ * 4; |
47 | case Dart_TypedData_kInt64: |
48 | case Dart_TypedData_kUint64: |
49 | case Dart_TypedData_kFloat64: |
50 | return length_ * 8; |
51 | case Dart_TypedData_kFloat32x4: |
52 | return length_ * 16; |
53 | default: |
54 | UNREACHABLE(); |
55 | } |
56 | } |
57 | |
58 | const char* TypedDataScope::GetScopedCString() const { |
59 | char* buf = reinterpret_cast<char*>(Dart_ScopeAllocate(size_in_bytes())); |
60 | strncpy(buf, GetCString(), size_in_bytes()); |
61 | return buf; |
62 | } |
63 | |
64 | } // namespace bin |
65 | } // namespace dart |
66 |