| 1 | // Copyright (c) 2012, 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/io_buffer.h" |
| 6 | |
| 7 | #include "platform/memory_sanitizer.h" |
| 8 | |
| 9 | namespace dart { |
| 10 | namespace bin { |
| 11 | |
| 12 | Dart_Handle IOBuffer::Allocate(intptr_t size, uint8_t** buffer) { |
| 13 | uint8_t* data = Allocate(size); |
| 14 | if (data == NULL) { |
| 15 | return Dart_Null(); |
| 16 | } |
| 17 | Dart_Handle result = Dart_NewExternalTypedDataWithFinalizer( |
| 18 | Dart_TypedData_kUint8, data, size, data, size, IOBuffer::Finalizer); |
| 19 | |
| 20 | if (Dart_IsError(result)) { |
| 21 | Free(data); |
| 22 | Dart_PropagateError(result); |
| 23 | } |
| 24 | if (buffer != NULL) { |
| 25 | *buffer = data; |
| 26 | } |
| 27 | return result; |
| 28 | } |
| 29 | |
| 30 | uint8_t* IOBuffer::Allocate(intptr_t size) { |
| 31 | return reinterpret_cast<uint8_t*>(calloc(size, sizeof(uint8_t))); |
| 32 | } |
| 33 | |
| 34 | } // namespace bin |
| 35 | } // namespace dart |
| 36 |