--- /dev/null 2017-11-09 09:38:01.297999907 +0100 +++ new/src/hotspot/share/jfr/writers/jfrStreamWriterHost.inline.hpp 2018-04-09 15:31:19.641058407 +0200 @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#ifndef SHARE_VM_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP +#define SHARE_VM_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP + +#include "jfr/writers/jfrStreamWriterHost.hpp" +#include "runtime/os.hpp" + +template +StreamWriterHost::StreamWriterHost(typename Adapter::StorageType* storage, Thread* thread) : + MemoryWriterHost(storage, thread), _stream_pos(0), _fd(invalid_fd) { +} + +template +StreamWriterHost::StreamWriterHost(typename Adapter::StorageType* storage, size_t size) : + MemoryWriterHost(storage, size), _stream_pos(0), _fd(invalid_fd) { +} + +template +StreamWriterHost::StreamWriterHost(Thread* thread) : + MemoryWriterHost(thread), _stream_pos(0), _fd(invalid_fd) { +} + +template +inline intptr_t StreamWriterHost::current_stream_position() const { + return this->used_offset() + _stream_pos; +} + +template +inline bool StreamWriterHost::accommodate(size_t used, size_t requested) { + if (used > 0) { + this->flush(used); + } + assert(this->used_size() == 0, "invariant"); + if (this->available_size() >= requested) { + return true; + } + return StorageHost::accommodate(0, requested); +} + +template +inline void StreamWriterHost::bytes(void* dest, const void* buf, size_t len) { + if (len > this->available_size()) { + this->write_unbuffered(buf, len); + return; + } + MemoryWriterHost::bytes(dest, buf, len); +} + +template +inline void StreamWriterHost::flush(size_t size) { + assert(size > 0, "invariant"); + assert(this->is_valid(), "invariant"); + _stream_pos += os::write(_fd, this->start_pos(), (int)size); + StorageHost::reset(); + assert(0 == this->used_offset(), "invariant"); +} + +template +inline bool StreamWriterHost::has_valid_fd() const { + return invalid_fd != _fd; +} + +template +inline intptr_t StreamWriterHost::current_offset() const { + return current_stream_position(); +} + +template +void StreamWriterHost::seek(intptr_t offset) { + this->flush(); + assert(0 == this->used_offset(), "can only seek from beginning"); + _stream_pos = os::seek_to_file_offset(_fd, offset); +} + +template +void StreamWriterHost::flush() { + if (this->is_valid()) { + const size_t used = this->used_size(); + if (used > 0) { + this->flush(used); + } + } +} + +template +void StreamWriterHost::write_unbuffered(const void* buf, size_t len) { + this->flush(); + assert(0 == this->used_offset(), "can only seek from beginning"); + while (len > 0) { + const int n = MIN2((int)len, INT_MAX); + _stream_pos += os::write(_fd, buf, n); + len -= n; + } +} + +template +inline bool StreamWriterHost::is_valid() const { + return has_valid_fd(); +} + +template +inline void StreamWriterHost::close_fd() { + assert(this->has_valid_fd(), "closing invalid fd!"); + os::close(_fd); + _fd = invalid_fd; +} + +template +inline void StreamWriterHost::reset(fio_fd fd) { + assert(!this->has_valid_fd(), "invariant"); + _fd = fd; + _stream_pos = 0; + this->hard_reset(); +} + +#endif // SHARE_VM_JFR_WRITERS_JFRSTREAMWRITERHOST_INLINE_HPP