forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.cc
108 lines (80 loc) · 2.01 KB
/
message.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2018 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/fml/message.h"
#include "flutter/fml/logging.h"
namespace fml {
Message::Message() = default;
Message::~Message() = default;
static uint32_t NextPowerOfTwoSize(uint32_t x) {
if (x == 0) {
return 1;
}
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x + 1;
}
const uint8_t* Message::GetBuffer() const {
return buffer_;
}
size_t Message::GetBufferSize() const {
return buffer_length_;
}
size_t Message::GetDataLength() const {
return data_length_;
}
size_t Message::GetSizeRead() const {
return size_read_;
}
bool Message::Reserve(size_t size) {
if (buffer_length_ >= size) {
return true;
}
return Resize(NextPowerOfTwoSize(size));
}
bool Message::Resize(size_t size) {
if (buffer_ == nullptr) {
// This is the initial resize where we have no previous buffer.
FML_DCHECK(buffer_length_ == 0);
void* buffer = ::malloc(size);
const bool success = buffer != nullptr;
if (success) {
buffer_ = static_cast<uint8_t*>(buffer);
buffer_length_ = size;
}
return success;
}
FML_DCHECK(size > buffer_length_);
void* resized = ::realloc(buffer_, size);
const bool success = resized != nullptr;
// In case of failure, the input buffer to realloc is still valid.
if (success) {
buffer_ = static_cast<uint8_t*>(resized);
buffer_length_ = size;
}
return success;
}
uint8_t* Message::PrepareEncode(size_t size) {
if (!Reserve(data_length_ + size)) {
return nullptr;
}
auto old_length = data_length_;
data_length_ += size;
return buffer_ + old_length;
}
uint8_t* Message::PrepareDecode(size_t size) {
if ((size + size_read_) > buffer_length_) {
return nullptr;
}
auto buffer = buffer_ + size_read_;
size_read_ += size;
return buffer;
}
void Message::ResetRead() {
size_read_ = 0;
}
} // namespace fml