| 1 | /* |
| 2 | * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #ifndef SHARE_JFR_WRITERS_JFRMEMORYWRITERHOST_HPP |
| 26 | #define SHARE_JFR_WRITERS_JFRMEMORYWRITERHOST_HPP |
| 27 | |
| 28 | #include "jfr/writers/jfrStorageHost.inline.hpp" |
| 29 | |
| 30 | #ifdef ASSERT |
| 31 | class ExclusiveAccessAssert { |
| 32 | private: |
| 33 | bool _acquired; |
| 34 | public: |
| 35 | ExclusiveAccessAssert() : _acquired(false) {} |
| 36 | void acquire() { assert_non_acquired(); _acquired = true; } |
| 37 | void release() { assert_acquired(); _acquired = false; } |
| 38 | bool is_acquired() const { return _acquired; } |
| 39 | void assert_acquired() const { assert(_acquired, "Not acquired!" ); } |
| 40 | void assert_non_acquired() const { assert(!_acquired, "Already acquired!" ); } |
| 41 | }; |
| 42 | #else |
| 43 | class ExclusiveAccessAssert {}; |
| 44 | #endif |
| 45 | |
| 46 | template <typename Adapter, typename AP, typename AccessAssert = ExclusiveAccessAssert> |
| 47 | class MemoryWriterHost : public StorageHost<Adapter, AP> { |
| 48 | debug_only(AccessAssert _access;) |
| 49 | public: |
| 50 | typedef typename Adapter::StorageType StorageType; |
| 51 | protected: |
| 52 | void bytes(void* dest, const void* buf, size_t len); |
| 53 | MemoryWriterHost(StorageType* storage, Thread* thread); |
| 54 | MemoryWriterHost(StorageType* storage, size_t size); |
| 55 | MemoryWriterHost(Thread* thread); |
| 56 | debug_only(bool is_acquired() const;) |
| 57 | public: |
| 58 | void acquire(); |
| 59 | void release(); |
| 60 | }; |
| 61 | |
| 62 | template <typename Adapter, typename AP> |
| 63 | class AcquireReleaseMemoryWriterHost : public MemoryWriterHost<Adapter, AP> { |
| 64 | public: |
| 65 | typedef typename Adapter::StorageType StorageType; |
| 66 | AcquireReleaseMemoryWriterHost(StorageType* storage, Thread* thread); |
| 67 | AcquireReleaseMemoryWriterHost(StorageType* storage, size_t size); |
| 68 | AcquireReleaseMemoryWriterHost(Thread* thread); |
| 69 | ~AcquireReleaseMemoryWriterHost(); |
| 70 | }; |
| 71 | |
| 72 | #endif // SHARE_JFR_WRITERS_JFRMEMORYWRITERHOST_HPP |
| 73 | |