Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
io_buffer.cc
Go to the documentation of this file.
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
8
9namespace dart {
10namespace bin {
11
12Dart_Handle IOBuffer::Allocate(intptr_t size, uint8_t** buffer) {
13 uint8_t* data = Allocate(size);
14 if (data == nullptr) {
15 return Dart_Null();
16 }
19
20 if (Dart_IsError(result)) {
21 Free(data);
23 }
24 if (buffer != nullptr) {
25 *buffer = data;
26 }
27 return result;
28}
29
30uint8_t* IOBuffer::Allocate(intptr_t size) {
31 return static_cast<uint8_t*>(calloc(size, sizeof(uint8_t)));
32}
33
34uint8_t* IOBuffer::Reallocate(uint8_t* buffer, intptr_t new_size) {
35 // It seems windows realloc() and glibc relloc() don't free memory when
36 // shrinking, so we'll manually allocate a new buffer, copy the data and free
37 // the old buffer. This also avoids a corner case if the new size is 0:
38 // It can return `nullptr` in that case even though `malloc(0)` would
39 // return a unique non-`nullptr` value.
40 auto new_buffer = IOBuffer::Allocate(new_size);
41 if (new_buffer != nullptr) {
42 memmove(new_buffer, buffer, new_size);
43 free(buffer);
44 return static_cast<uint8_t*>(new_buffer);
45 }
46 return static_cast<uint8_t*>(realloc(buffer, new_size));
47}
48
49} // namespace bin
50} // namespace dart
static void Free(void *buffer)
Definition io_buffer.h:28
static void Finalizer(void *isolate_callback_data, void *buffer)
Definition io_buffer.h:31
static Dart_Handle Allocate(intptr_t size, uint8_t **buffer)
Definition io_buffer.cc:12
static uint8_t * Reallocate(uint8_t *buffer, intptr_t new_size)
Definition io_buffer.cc:34
struct _Dart_Handle * Dart_Handle
Definition dart_api.h:258
@ Dart_TypedData_kUint8
Definition dart_api.h:2606
static const uint8_t buffer[]
GAsyncResult * result
DART_EXPORT Dart_Handle Dart_NewExternalTypedDataWithFinalizer(Dart_TypedData_Type type, void *data, intptr_t length, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
void * calloc(size_t n, size_t size)
Definition allocation.cc:11
void * realloc(void *ptr, size_t size)
Definition allocation.cc:27
DART_EXPORT Dart_Handle Dart_Null()
static int8_t data[kExtLength]