Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.unsafe.array;
0019 
0020 import org.apache.spark.unsafe.Platform;
0021 import org.apache.spark.unsafe.memory.MemoryBlock;
0022 
0023 /**
0024  * An array of long values. Compared with native JVM arrays, this:
0025  * <ul>
0026  *   <li>supports using both on-heap and off-heap memory</li>
0027  *   <li>has no bound checking, and thus can crash the JVM process when assert is turned off</li>
0028  * </ul>
0029  */
0030 public final class LongArray {
0031 
0032   // This is a long so that we perform long multiplications when computing offsets.
0033   private static final long WIDTH = 8;
0034 
0035   private final MemoryBlock memory;
0036   private final Object baseObj;
0037   private final long baseOffset;
0038 
0039   private final long length;
0040 
0041   public LongArray(MemoryBlock memory) {
0042     assert memory.size() < (long) Integer.MAX_VALUE * 8: "Array size >= Integer.MAX_VALUE elements";
0043     this.memory = memory;
0044     this.baseObj = memory.getBaseObject();
0045     this.baseOffset = memory.getBaseOffset();
0046     this.length = memory.size() / WIDTH;
0047   }
0048 
0049   public MemoryBlock memoryBlock() {
0050     return memory;
0051   }
0052 
0053   public Object getBaseObject() {
0054     return baseObj;
0055   }
0056 
0057   public long getBaseOffset() {
0058     return baseOffset;
0059   }
0060 
0061   /**
0062    * Returns the number of elements this array can hold.
0063    */
0064   public long size() {
0065     return length;
0066   }
0067 
0068   /**
0069    * Fill this all with 0L.
0070    */
0071   public void zeroOut() {
0072     for (long off = baseOffset; off < baseOffset + length * WIDTH; off += WIDTH) {
0073       Platform.putLong(baseObj, off, 0);
0074     }
0075   }
0076 
0077   /**
0078    * Sets the value at position {@code index}.
0079    */
0080   public void set(int index, long value) {
0081     assert index >= 0 : "index (" + index + ") should >= 0";
0082     assert index < length : "index (" + index + ") should < length (" + length + ")";
0083     Platform.putLong(baseObj, baseOffset + index * WIDTH, value);
0084   }
0085 
0086   /**
0087    * Returns the value at position {@code index}.
0088    */
0089   public long get(int index) {
0090     assert index >= 0 : "index (" + index + ") should >= 0";
0091     assert index < length : "index (" + index + ") should < length (" + length + ")";
0092     return Platform.getLong(baseObj, baseOffset + index * WIDTH);
0093   }
0094 }