Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
UnsafeUtil.java
Go to the documentation of this file.
1 //Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.util;
16 
17 import java.lang.reflect.Field;
18 import java.security.AccessController;
19 import java.security.PrivilegedAction;
20 
21 import sun.misc.Unsafe;
22 
23 @SuppressWarnings("restriction")
27 public class UnsafeUtil {
28  // object to allow us to use unsafe APIs. This lets us read native memory without
29  // copies and not have to switch back and forth between little endian and big endian.
30  public static final Unsafe UNSAFE;
31 
32  // This is the offset to the start of the array data. (There's some bytes
33  // before the array data like the size and other java stuff).
34  private static final int BYTE_ARRAY_DATA_OFFSET;
35 
36  static {
37  UNSAFE = (Unsafe) AccessController.doPrivileged(
38  new PrivilegedAction<Object>() {
39  @Override
40  public Object run() {
41  try {
42  Field f = Unsafe.class.getDeclaredField("theUnsafe");
43  f.setAccessible(true);
44  return f.get(null);
45  } catch (NoSuchFieldException e) {
46  throw new Error();
47  } catch (IllegalAccessException e) {
48  throw new Error();
49  }
50  }
51  });
52 
53  BYTE_ARRAY_DATA_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
54  }
55 
56  // Copies src[srcOffset, srcOffset + len) into dst.
57  public static void Copy(long dst, byte[] src, int srcOffset, int len) {
58  UNSAFE.copyMemory(src, BYTE_ARRAY_DATA_OFFSET + srcOffset, null, dst, len);
59  }
60 
61  // Copies src[0, len) into dst[dstOffset, dstOffset + len).
62  public static void Copy(byte[] dst, int dstOffset, long src, int len) {
63  UNSAFE.copyMemory(null, src, dst, dstOffset + BYTE_ARRAY_DATA_OFFSET, len);
64  }
65 }
static void Copy(byte[] dst, int dstOffset, long src, int len)
Definition: UnsafeUtil.java:62
static final int BYTE_ARRAY_DATA_OFFSET
Definition: UnsafeUtil.java:34
static void Copy(long dst, byte[] src, int srcOffset, int len)
Definition: UnsafeUtil.java:57