| 1 | /* |
| 2 | * Copyright (c) 1997, 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_RUNTIME_FLAGS_FLAGSETTING_HPP |
| 26 | #define SHARE_RUNTIME_FLAGS_FLAGSETTING_HPP |
| 27 | |
| 28 | #include "memory/allocation.hpp" |
| 29 | |
| 30 | // debug flags control various aspects of the VM and are global accessible |
| 31 | |
| 32 | // use FlagSetting to temporarily change some debug flag |
| 33 | // e.g. FlagSetting fs(DebugThisAndThat, true); |
| 34 | // restored to previous value upon leaving scope |
| 35 | class FlagSetting : public StackObj { |
| 36 | bool val; |
| 37 | bool* flag; |
| 38 | public: |
| 39 | FlagSetting(bool& fl, bool newValue) { flag = &fl; val = fl; fl = newValue; } |
| 40 | ~FlagSetting() { *flag = val; } |
| 41 | }; |
| 42 | |
| 43 | class UIntFlagSetting : public StackObj { |
| 44 | uint val; |
| 45 | uint* flag; |
| 46 | public: |
| 47 | UIntFlagSetting(uint& fl, uint newValue) { flag = &fl; val = fl; fl = newValue; } |
| 48 | ~UIntFlagSetting() { *flag = val; } |
| 49 | }; |
| 50 | |
| 51 | class SizeTFlagSetting : public StackObj { |
| 52 | size_t val; |
| 53 | size_t* flag; |
| 54 | public: |
| 55 | SizeTFlagSetting(size_t& fl, size_t newValue) { flag = &fl; val = fl; fl = newValue; } |
| 56 | ~SizeTFlagSetting() { *flag = val; } |
| 57 | }; |
| 58 | |
| 59 | // Helper class for temporarily saving the value of a flag during a scope. |
| 60 | template <size_t SIZE> |
| 61 | class FlagGuard { |
| 62 | unsigned char _value[SIZE]; |
| 63 | void* const _addr; |
| 64 | public: |
| 65 | FlagGuard(void* flag_addr) : _addr(flag_addr) { memcpy(_value, _addr, SIZE); } |
| 66 | ~FlagGuard() { memcpy(_addr, _value, SIZE); } |
| 67 | }; |
| 68 | |
| 69 | #define FLAG_GUARD(f) FlagGuard<sizeof(f)> f ## _guard(&f) |
| 70 | |
| 71 | #endif // SHARE_RUNTIME_FLAGS_FLAGSETTING_HPP |
| 72 | |