View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util;
20  
21  import java.security.AccessController;
22  import java.security.PrivilegedAction;
23  
24  /**
25   * MemoryUtils provides an abstraction over memory properties and operations.
26   */
27  public class MemoryUtils
28  {
29      private static final int cacheLineBytes;
30      static
31      {
32          final int defaultValue = 64;
33          int value = defaultValue;
34          try
35          {
36              value = Integer.parseInt(AccessController.doPrivileged(new PrivilegedAction<String>()
37              {
38                  @Override
39                  public String run()
40                  {
41                      return System.getProperty("org.eclipse.jetty.util.cacheLineBytes", String.valueOf(defaultValue));
42                  }
43              }));
44          }
45          catch (Exception ignored)
46          {
47          }
48          cacheLineBytes = value;
49      }
50  
51      private MemoryUtils()
52      {
53      }
54  
55      public static int getCacheLineBytes()
56      {
57          return cacheLineBytes;
58      }
59  
60      public static int getIntegersPerCacheLine()
61      {
62          return getCacheLineBytes() >> 2;
63      }
64  
65      public static int getLongsPerCacheLine()
66      {
67          return getCacheLineBytes() >> 3;
68      }
69  }