src/share/vm/utilities/sizes.hpp

changeset 435
a61af66fc99e
child 1907
c18cbe5936b8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/utilities/sizes.hpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,144 @@
     1.4 +/*
     1.5 + * Copyright 2000-2004 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +// The following two classes are used to represent 'sizes' and 'offsets' in the VM;
    1.29 +// they serve as 'unit' types. ByteSize is used for sizes measured in bytes, while
    1.30 +// WordSize is used for sizes measured in machine words (i.e., 32bit or 64bit words
    1.31 +// depending on platform).
    1.32 +//
    1.33 +// The classes are defined with friend functions operating on them instead of member
    1.34 +// functions so that they (the classes) can be re-#define'd to int types in optimized
    1.35 +// mode. This allows full type checking and maximum safety in debug mode, and full
    1.36 +// optimizations (constant folding) and zero overhead (time and space wise) in the
    1.37 +// optimized build (some compilers do not optimize one-element value classes but
    1.38 +// instead create an object in memory - thus the overhead may be significant).
    1.39 +//
    1.40 +// Note: 1) DO NOT add new overloaded friend functions that do not have a unique function
    1.41 +//          function name but require signature types for resolution. This will not work
    1.42 +//          in optimized mode as both, ByteSize and WordSize are mapped to the same type
    1.43 +//          and thus the distinction would not be possible anymore (=> compiler errors).
    1.44 +//
    1.45 +//       2) DO NOT add non-static member functions as they cannot be mapped so something
    1.46 +//          compilable in the optimized build. Static member functions could be added
    1.47 +//          but require a corresponding class definition in the optimized build.
    1.48 +//
    1.49 +// These classes should help doing a transition from (currently) word-size based offsets
    1.50 +// to byte-size based offsets in the VM (this will be important if we desire to pack
    1.51 +// objects more densely in the VM for 64bit machines). Such a transition should proceed
    1.52 +// in two steps to minimize the risk of introducing hard-to-find bugs:
    1.53 +//
    1.54 +// a) first transition the whole VM into a form where all sizes are strongly typed
    1.55 +// b) change all WordSize's to ByteSize's where desired and fix the compilation errors
    1.56 +
    1.57 +
    1.58 +#ifdef ASSERT
    1.59 +
    1.60 +class ByteSize VALUE_OBJ_CLASS_SPEC {
    1.61 + private:
    1.62 +  int _size;
    1.63 +
    1.64 +  // Note: This constructor must be private to avoid implicit conversions!
    1.65 +  ByteSize(int size)                                  { _size = size; }
    1.66 +
    1.67 + public:
    1.68 +  // constructors
    1.69 +  inline friend ByteSize in_ByteSize(int size);
    1.70 +
    1.71 +  // accessors
    1.72 +  inline friend int in_bytes(ByteSize x);
    1.73 +
    1.74 +  // operators
    1.75 +  friend ByteSize operator + (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) + in_bytes(y)); }
    1.76 +  friend ByteSize operator - (ByteSize x, ByteSize y) { return ByteSize(in_bytes(x) - in_bytes(y)); }
    1.77 +  friend ByteSize operator * (ByteSize x, int      y) { return ByteSize(in_bytes(x) * y          ); }
    1.78 +
    1.79 +  // comparison
    1.80 +  friend bool operator == (ByteSize x, ByteSize y)    { return in_bytes(x) == in_bytes(y); }
    1.81 +  friend bool operator != (ByteSize x, ByteSize y)    { return in_bytes(x) != in_bytes(y); }
    1.82 +  friend bool operator <  (ByteSize x, ByteSize y)    { return in_bytes(x) <  in_bytes(y); }
    1.83 +  friend bool operator <= (ByteSize x, ByteSize y)    { return in_bytes(x) <= in_bytes(y); }
    1.84 +  friend bool operator >  (ByteSize x, ByteSize y)    { return in_bytes(x) >  in_bytes(y); }
    1.85 +  friend bool operator >= (ByteSize x, ByteSize y)    { return in_bytes(x) >= in_bytes(y); }
    1.86 +};
    1.87 +
    1.88 +inline ByteSize in_ByteSize(int size) { return ByteSize(size); }
    1.89 +inline int      in_bytes(ByteSize x)  { return x._size; }
    1.90 +
    1.91 +
    1.92 +class WordSize VALUE_OBJ_CLASS_SPEC {
    1.93 + private:
    1.94 +  int _size;
    1.95 +
    1.96 +  // Note: This constructor must be private to avoid implicit conversions!
    1.97 +  WordSize(int size)                                  { _size = size; }
    1.98 +
    1.99 + public:
   1.100 +  // constructors
   1.101 +  inline friend WordSize in_WordSize(int size);
   1.102 +
   1.103 +  // accessors
   1.104 +  inline friend int in_words(WordSize x);
   1.105 +
   1.106 +  // operators
   1.107 +  friend WordSize operator + (WordSize x, WordSize y) { return WordSize(in_words(x) + in_words(y)); }
   1.108 +  friend WordSize operator - (WordSize x, WordSize y) { return WordSize(in_words(x) - in_words(y)); }
   1.109 +  friend WordSize operator * (WordSize x, int      y) { return WordSize(in_words(x) * y          ); }
   1.110 +
   1.111 +  // comparison
   1.112 +  friend bool operator == (WordSize x, WordSize y)    { return in_words(x) == in_words(y); }
   1.113 +  friend bool operator != (WordSize x, WordSize y)    { return in_words(x) != in_words(y); }
   1.114 +  friend bool operator <  (WordSize x, WordSize y)    { return in_words(x) <  in_words(y); }
   1.115 +  friend bool operator <= (WordSize x, WordSize y)    { return in_words(x) <= in_words(y); }
   1.116 +  friend bool operator >  (WordSize x, WordSize y)    { return in_words(x) >  in_words(y); }
   1.117 +  friend bool operator >= (WordSize x, WordSize y)    { return in_words(x) >= in_words(y); }
   1.118 +};
   1.119 +
   1.120 +inline WordSize in_WordSize(int size) { return WordSize(size); }
   1.121 +inline int      in_words(WordSize x)  { return x._size; }
   1.122 +
   1.123 +
   1.124 +#else // ASSERT
   1.125 +
   1.126 +// The following definitions must match the corresponding friend declarations
   1.127 +// in the Byte/WordSize classes if they are typedef'ed to be int. This will
   1.128 +// be the case in optimized mode to ensure zero overhead for these types.
   1.129 +//
   1.130 +// Note: If a compiler does not inline these function calls away, one may
   1.131 +//       want to use #define's to make sure full optimization (constant
   1.132 +//       folding in particular) is possible.
   1.133 +
   1.134 +typedef int ByteSize;
   1.135 +inline ByteSize in_ByteSize(int size)                 { return size; }
   1.136 +inline int      in_bytes   (ByteSize x)               { return x; }
   1.137 +
   1.138 +typedef int WordSize;
   1.139 +inline WordSize in_WordSize(int size)                 { return size; }
   1.140 +inline int      in_words   (WordSize x)               { return x; }
   1.141 +
   1.142 +#endif // ASSERT
   1.143 +
   1.144 +
   1.145 +// Use the following #define to get C++ field member offsets
   1.146 +
   1.147 +#define byte_offset_of(klass,field)   in_ByteSize((int)offset_of(klass, field))

mercurial