Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/runtime/bcutil/BufferManager.cpp
5985 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2014 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
* BufferManager.cpp
24
*
25
*/
26
#include "j9comp.h"
27
#include "j9.h"
28
#include "ut_j9bcu.h"
29
30
#include "BufferManager.hpp"
31
32
BufferManager::BufferManager(J9PortLibrary *portLibrary, UDATA bufferSize, U_8 **buffer) :
33
_portLibrary(portLibrary),
34
_bufferSize(bufferSize),
35
_buffer(buffer),
36
_pos(0),
37
_shouldFreeBuffer(false)
38
{
39
if ( NULL == *_buffer ) {
40
PORT_ACCESS_FROM_PORT(_portLibrary);
41
U_8 *ptr = (U_8*)j9mem_allocate_memory(_bufferSize, J9MEM_CATEGORY_CLASSES);
42
if ( NULL == ptr ) {
43
/*
44
* Not enough native memory to complete this ROMClass load.
45
*/
46
_bufferSize = 0;
47
} else {
48
/*
49
* Pass back the newly allocated buffer to the caller.
50
*/
51
*_buffer = ptr;
52
}
53
}
54
}
55
56
BufferManager::~BufferManager()
57
{
58
if ( _shouldFreeBuffer ) {
59
PORT_ACCESS_FROM_PORT(_portLibrary);
60
j9mem_free_memory(*_buffer);
61
*_buffer = NULL;
62
}
63
}
64
65
void *
66
BufferManager::alloc(UDATA size)
67
{
68
U_8 *memory = NULL;
69
if ((_pos + size) <= _bufferSize) {
70
memory = *_buffer + _pos;
71
_lastAllocation = memory;
72
_pos += size;
73
} else {
74
/*
75
* If an allocation failed, free the original buffer.
76
*/
77
_shouldFreeBuffer = true;
78
}
79
return memory;
80
}
81
82
void
83
BufferManager::reclaim(void *memory, UDATA actualSize)
84
{
85
if (memory == _lastAllocation) {
86
UDATA newPos = UDATA(_lastAllocation) - UDATA(*_buffer) + actualSize;
87
if (newPos <= _pos) {
88
_pos = newPos;
89
return;
90
}
91
}
92
93
Trc_BCU_Assert_ShouldNeverHappen();
94
}
95
96