Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/compiler/net/MessageBuffer.cpp
6000 views
1
/*******************************************************************************
2
* Copyright (c) 2020, 2021 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
23
#include "net/MessageBuffer.hpp"
24
#include <cstring>
25
26
namespace JITServer
27
{
28
MessageBuffer::MessageBuffer() :
29
_capacity(INITIAL_BUFFER_SIZE),
30
_allocator(TR::Compiler->persistentGlobalAllocator())
31
{
32
_storage = allocateMemory(_capacity);
33
if (!_storage)
34
throw std::bad_alloc();
35
_curPtr = _storage;
36
}
37
38
void
39
MessageBuffer::expandIfNeeded(uint32_t requiredSize)
40
{
41
// if the data size becomes larger than buffer capacity,
42
// expand the storage
43
if (requiredSize > _capacity)
44
{
45
expand(requiredSize, size());
46
}
47
}
48
49
void
50
MessageBuffer::expand(uint32_t requiredSize, uint32_t numBytesToCopy)
51
{
52
TR_ASSERT_FATAL((requiredSize > _capacity), "requiredSize %u has to be greater than _capacity %u", requiredSize, _capacity);
53
TR_ASSERT_FATAL((numBytesToCopy <= _capacity), "numBytesToCopy %u has to be less than _capacity %u", numBytesToCopy, _capacity);
54
55
_capacity = computeRequiredCapacity(requiredSize);
56
uint32_t curSize = size();
57
58
char *newStorage = allocateMemory(_capacity);
59
if (!newStorage)
60
throw std::bad_alloc();
61
62
memcpy(newStorage, _storage, numBytesToCopy);
63
64
freeMemory(_storage);
65
_storage = newStorage;
66
_curPtr = _storage + curSize;
67
}
68
69
uint32_t
70
MessageBuffer::writeData(const void *dataStart, uint32_t dataSize, uint8_t paddingSize)
71
{
72
// write dataSize bytes starting from dataStart address
73
// into the buffer. Might require buffer expansion.
74
// Return the offset into the buffer for the beginning of data
75
uint32_t requiredSize = size() + dataSize + paddingSize;
76
expandIfNeeded(requiredSize);
77
char *data = _curPtr;
78
memcpy(_curPtr, dataStart, dataSize);
79
_curPtr += dataSize + paddingSize;
80
return offset(data);
81
}
82
83
uint8_t
84
MessageBuffer::alignCurrentPositionOn64Bit()
85
{
86
// Compute the amount of padding required to align _curPtr on 64-bit boundary
87
uintptr_t alignedPtr = ((uintptr_t)_curPtr + 0x7) & (~((uintptr_t)0x7));
88
uint8_t padding = (uint8_t)(alignedPtr - (uintptr_t)_curPtr); // Guaranteed to fit on a byte
89
90
// Expand the buffer if it's too small to contain the padding
91
uint32_t requiredSize = size() + padding;
92
expandIfNeeded(requiredSize);
93
94
// Advance _curPtr to align it on 64-bit
95
_curPtr += padding;
96
97
return padding;
98
}
99
100
uint32_t
101
MessageBuffer::computeRequiredCapacity(uint32_t requiredSize)
102
{
103
// Compute the nearest power of 2 that can fit requiredSize bytes
104
if (requiredSize <= _capacity)
105
return _capacity;
106
uint32_t extendedCapacity = _capacity * 2;
107
while (requiredSize > extendedCapacity)
108
extendedCapacity *= 2;
109
return extendedCapacity;
110
}
111
};
112
113
114