Path: blob/master/runtime/bcutil/BufferManager.cpp
5985 views
/*******************************************************************************1* Copyright (c) 2001, 2014 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21/*22* BufferManager.cpp23*24*/25#include "j9comp.h"26#include "j9.h"27#include "ut_j9bcu.h"2829#include "BufferManager.hpp"3031BufferManager::BufferManager(J9PortLibrary *portLibrary, UDATA bufferSize, U_8 **buffer) :32_portLibrary(portLibrary),33_bufferSize(bufferSize),34_buffer(buffer),35_pos(0),36_shouldFreeBuffer(false)37{38if ( NULL == *_buffer ) {39PORT_ACCESS_FROM_PORT(_portLibrary);40U_8 *ptr = (U_8*)j9mem_allocate_memory(_bufferSize, J9MEM_CATEGORY_CLASSES);41if ( NULL == ptr ) {42/*43* Not enough native memory to complete this ROMClass load.44*/45_bufferSize = 0;46} else {47/*48* Pass back the newly allocated buffer to the caller.49*/50*_buffer = ptr;51}52}53}5455BufferManager::~BufferManager()56{57if ( _shouldFreeBuffer ) {58PORT_ACCESS_FROM_PORT(_portLibrary);59j9mem_free_memory(*_buffer);60*_buffer = NULL;61}62}6364void *65BufferManager::alloc(UDATA size)66{67U_8 *memory = NULL;68if ((_pos + size) <= _bufferSize) {69memory = *_buffer + _pos;70_lastAllocation = memory;71_pos += size;72} else {73/*74* If an allocation failed, free the original buffer.75*/76_shouldFreeBuffer = true;77}78return memory;79}8081void82BufferManager::reclaim(void *memory, UDATA actualSize)83{84if (memory == _lastAllocation) {85UDATA newPos = UDATA(_lastAllocation) - UDATA(*_buffer) + actualSize;86if (newPos <= _pos) {87_pos = newPos;88return;89}90}9192Trc_BCU_Assert_ShouldNeverHappen();93}949596