View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.pool.sizeof;
18  
19  import java.lang.reflect.Array;
20  import java.lang.reflect.Field;
21  import java.lang.reflect.Modifier;
22  
23  import net.sf.ehcache.pool.sizeof.filter.PassThroughFilter;
24  import net.sf.ehcache.pool.sizeof.filter.SizeOfFilter;
25  
26  import sun.misc.Unsafe;
27  
28  import static net.sf.ehcache.pool.sizeof.JvmInformation.MINIMUM_OBJECT_SIZE;
29  import static net.sf.ehcache.pool.sizeof.JvmInformation.OBJECT_ALIGNMENT;
30  
31  /***
32   * {@link sun.misc.Unsafe#theUnsafe} based sizeOf measurement
33   * All constructors will throw UnsupportedOperationException if theUnsafe isn't accessible on this platform
34   * @author Chris Dennis
35   */
36  @SuppressWarnings("restriction")
37  public class UnsafeSizeOf extends SizeOf {
38  
39      private static final Unsafe UNSAFE;
40  
41      static {
42          Unsafe unsafe;
43          try {
44              Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
45              unsafeField.setAccessible(true);
46              unsafe = (Unsafe)unsafeField.get(null);
47          } catch (Throwable t) {
48              unsafe = null;
49          }
50          UNSAFE = unsafe;
51      }
52  
53      /***
54       * Builds a new SizeOf that will not filter fields and will cache reflected fields
55       *
56       * @throws UnsupportedOperationException If Unsafe isn't accessible
57       * @see #UnsafeSizeOf(net.sf.ehcache.pool.sizeof.filter.SizeOfFilter, boolean)
58       */
59      public UnsafeSizeOf() throws UnsupportedOperationException {
60          this(new PassThroughFilter());
61      }
62  
63      /***
64       * Builds a new SizeOf that will filter fields according to the provided filter and will cache reflected fields
65       *
66       * @param filter The filter to apply
67       * @throws UnsupportedOperationException If Unsafe isn't accessible
68       * @see #UnsafeSizeOf(net.sf.ehcache.pool.sizeof.filter.SizeOfFilter, boolean)
69       * @see SizeOfFilter
70       */
71      public UnsafeSizeOf(SizeOfFilter filter) throws UnsupportedOperationException {
72          this(filter, true);
73      }
74  
75      /***
76       * Builds a new SizeOf that will filter fields according to the provided filter
77       *
78       * @param filter The filter to apply
79       * @param caching     whether to cache reflected fields
80       * @throws UnsupportedOperationException If Unsafe isn't accessible
81       * @see SizeOfFilter
82       */
83      public UnsafeSizeOf(SizeOfFilter filter, boolean caching) throws UnsupportedOperationException {
84          super(filter, caching);
85          if (UNSAFE == null) {
86              throw new UnsupportedOperationException("sun.misc.Unsafe instance not accessible");
87          }
88      }
89  
90      /***
91       * {@inheritDoc}
92       */
93      @Override
94      protected long measureSizeOf(Object obj) {
95          if (obj.getClass().isArray()) {
96              Class<?> klazz = obj.getClass();
97              int base = UNSAFE.arrayBaseOffset(klazz);
98              int scale = UNSAFE.arrayIndexScale(klazz);
99              long size = base + (scale * Array.getLength(obj));
100             if ((size % OBJECT_ALIGNMENT) != 0) {
101                 size += OBJECT_ALIGNMENT - (size % OBJECT_ALIGNMENT);
102             }
103             return Math.max(MINIMUM_OBJECT_SIZE, size);
104         } else {
105             for (Class<?> klazz = obj.getClass(); klazz != null; klazz = klazz.getSuperclass()) {
106                 long lastFieldOffset = -1;
107                 for (Field f : klazz.getDeclaredFields()) {
108                     if (!Modifier.isStatic(f.getModifiers())) {
109                         lastFieldOffset = Math.max(lastFieldOffset, UNSAFE.objectFieldOffset(f));
110                     }
111                 }
112                 if (lastFieldOffset > 0) {
113                     lastFieldOffset += 1;
114                     if ((lastFieldOffset % OBJECT_ALIGNMENT) != 0) {
115                         lastFieldOffset += OBJECT_ALIGNMENT - (lastFieldOffset % OBJECT_ALIGNMENT);
116                     }
117                     return Math.max(MINIMUM_OBJECT_SIZE, lastFieldOffset);
118                 }
119             }
120 
121             long size = PrimitiveType.CLASS.getSize();
122             if ((size % OBJECT_ALIGNMENT) != 0) {
123                 size += OBJECT_ALIGNMENT - (size % OBJECT_ALIGNMENT);
124             }
125             return Math.max(MINIMUM_OBJECT_SIZE, size);
126         }
127     }
128 
129 }