src/share/vm/utilities/sizes.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 2314
f95d63e2154a
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_UTILITIES_SIZES_HPP
aoqi@0 26 #define SHARE_VM_UTILITIES_SIZES_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "utilities/globalDefinitions.hpp"
aoqi@0 30
aoqi@0 31 // The following two classes are used to represent 'sizes' and 'offsets' in the VM;
aoqi@0 32 // they serve as 'unit' types. ByteSize is used for sizes measured in bytes, while
aoqi@0 33 // WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words
aoqi@0 34 // depending on platform).
aoqi@0 35 //
aoqi@0 36 // The classes are defined with friend functions operating on them instead of member
aoqi@0 37 // functions so that they (the classes) can be re-#define'd to int types in optimized
aoqi@0 38 // mode. This allows full type checking and maximum safety in debug mode, and full
aoqi@0 39 // optimizations (constant folding) and zero overhead (time and space wise) in the
aoqi@0 40 // optimized build (some compilers do not optimize one-element value classes but
aoqi@0 41 // instead create an object in memory - thus the overhead may be significant).
aoqi@0 42 //
aoqi@0 43 // Note: 1) DO NOT add new overloaded friend functions that do not have a unique function
aoqi@0 44 // function name but require signature types for resolution. This will not work
aoqi@0 45 // in optimized mode as both, ByteSize and WordSize are mapped to the same type
aoqi@0 46 // and thus the distinction would not be possible anymore (=> compiler errors).
aoqi@0 47 //
aoqi@0 48 // 2) DO NOT add non-static member functions as they cannot be mapped so something
aoqi@0 49 // compilable in the optimized build. Static member functions could be added
aoqi@0 50 // but require a corresponding class definition in the optimized build.
aoqi@0 51 //
aoqi@0 52 // These classes should help doing a transition from (currently) word-size based offsets
aoqi@0 53 // to byte-size based offsets in the VM (this will be important if we desire to pack
aoqi@0 54 // objects more densely in the VM for 64bit machines). Such a transition should proceed
aoqi@0 55 // in two steps to minimize the risk of introducing hard-to-find bugs:
aoqi@0 56 //
aoqi@0 57 // a) first transition the whole VM into a form where all sizes are strongly typed
aoqi@0 58 // b) change all WordSize's to ByteSize's where desired and fix the compilation errors
aoqi@0 59
aoqi@0 60
aoqi@0 61 #ifdef ASSERT
aoqi@0 62
aoqi@0 63 class ByteSize VALUE_OBJ_CLASS_SPEC {
aoqi@0 64 private:
aoqi@0 65 int _size;
aoqi@0 66
aoqi@0 67 // Note: This constructor must be private to avoid implicit conversions!
aoqi@0 68 ByteSize(int size) { _size = size; }
aoqi@0 69
aoqi@0 70 public:
aoqi@0 71 // constructors
aoqi@0 72 inline friend ByteSize in_ByteSize(int size);
aoqi@0 73
aoqi@0 74 // accessors
aoqi@0 75 inline friend int in_bytes(ByteSize x);
aoqi@0 76
aoqi@0 77 // operators
aoqi@0 78 friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); }
aoqi@0 79 friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); }
aoqi@0 80 friend ByteSize operator * (ByteSize x, int y) { return ByteSize(in_bytes(x) * y ); }
aoqi@0 81
aoqi@0 82 // comparison
aoqi@0 83 friend bool operator == (ByteSize x, ByteSize y) { return in_bytes(x) == in_bytes(y); }
aoqi@0 84 friend bool operator != (ByteSize x, ByteSize y) { return in_bytes(x) != in_bytes(y); }
aoqi@0 85 friend bool operator < (ByteSize x, ByteSize y) { return in_bytes(x) < in_bytes(y); }
aoqi@0 86 friend bool operator <= (ByteSize x, ByteSize y) { return in_bytes(x) <= in_bytes(y); }
aoqi@0 87 friend bool operator > (ByteSize x, ByteSize y) { return in_bytes(x) > in_bytes(y); }
aoqi@0 88 friend bool operator >= (ByteSize x, ByteSize y) { return in_bytes(x) >= in_bytes(y); }
aoqi@0 89 };
aoqi@0 90
aoqi@0 91 inline ByteSize in_ByteSize(int size) { return ByteSize(size); }
aoqi@0 92 inline int in_bytes(ByteSize x) { return x._size; }
aoqi@0 93
aoqi@0 94
aoqi@0 95 class WordSize VALUE_OBJ_CLASS_SPEC {
aoqi@0 96 private:
aoqi@0 97 int _size;
aoqi@0 98
aoqi@0 99 // Note: This constructor must be private to avoid implicit conversions!
aoqi@0 100 WordSize(int size) { _size = size; }
aoqi@0 101
aoqi@0 102 public:
aoqi@0 103 // constructors
aoqi@0 104 inline friend WordSize in_WordSize(int size);
aoqi@0 105
aoqi@0 106 // accessors
aoqi@0 107 inline friend int in_words(WordSize x);
aoqi@0 108
aoqi@0 109 // operators
aoqi@0 110 friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); }
aoqi@0 111 friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); }
aoqi@0 112 friend WordSize operator * (WordSize x, int y) { return WordSize(in_words(x) * y ); }
aoqi@0 113
aoqi@0 114 // comparison
aoqi@0 115 friend bool operator == (WordSize x, WordSize y) { return in_words(x) == in_words(y); }
aoqi@0 116 friend bool operator != (WordSize x, WordSize y) { return in_words(x) != in_words(y); }
aoqi@0 117 friend bool operator < (WordSize x, WordSize y) { return in_words(x) < in_words(y); }
aoqi@0 118 friend bool operator <= (WordSize x, WordSize y) { return in_words(x) <= in_words(y); }
aoqi@0 119 friend bool operator > (WordSize x, WordSize y) { return in_words(x) > in_words(y); }
aoqi@0 120 friend bool operator >= (WordSize x, WordSize y) { return in_words(x) >= in_words(y); }
aoqi@0 121 };
aoqi@0 122
aoqi@0 123 inline WordSize in_WordSize(int size) { return WordSize(size); }
aoqi@0 124 inline int in_words(WordSize x) { return x._size; }
aoqi@0 125
aoqi@0 126
aoqi@0 127 #else // ASSERT
aoqi@0 128
aoqi@0 129 // The following definitions must match the corresponding friend declarations
aoqi@0 130 // in the Byte/WordSize classes if they are typedef'ed to be int. This will
aoqi@0 131 // be the case in optimized mode to ensure zero overhead for these types.
aoqi@0 132 //
aoqi@0 133 // Note: If a compiler does not inline these function calls away, one may
aoqi@0 134 // want to use #define's to make sure full optimization (constant
aoqi@0 135 // folding in particular) is possible.
aoqi@0 136
aoqi@0 137 typedef int ByteSize;
aoqi@0 138 inline ByteSize in_ByteSize(int size) { return size; }
aoqi@0 139 inline int in_bytes (ByteSize x) { return x; }
aoqi@0 140
aoqi@0 141 typedef int WordSize;
aoqi@0 142 inline WordSize in_WordSize(int size) { return size; }
aoqi@0 143 inline int in_words (WordSize x) { return x; }
aoqi@0 144
aoqi@0 145 #endif // ASSERT
aoqi@0 146
aoqi@0 147
aoqi@0 148 // Use the following #define to get C++ field member offsets
aoqi@0 149
aoqi@0 150 #define byte_offset_of(klass,field) in_ByteSize((int)offset_of(klass, field))
aoqi@0 151
aoqi@0 152 #endif // SHARE_VM_UTILITIES_SIZES_HPP

mercurial