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;
0019 
0020 /**
0021  * Class to make changes to record length offsets uniform through out
0022  * various areas of Apache Spark core and unsafe.  The SPARC platform
0023  * requires this because using a 4 byte Int for record lengths causes
0024  * the entire record of 8 byte Items to become misaligned by 4 bytes.
0025  * Using a 8 byte long for record length keeps things 8 byte aligned.
0026  */
0027 public class UnsafeAlignedOffset {
0028 
0029   private static final int UAO_SIZE = Platform.unaligned() ? 4 : 8;
0030 
0031   private static int TEST_UAO_SIZE = 0;
0032 
0033   // used for test only
0034   public static void setUaoSize(int size) {
0035     assert size == 0 || size == 4 || size == 8;
0036     TEST_UAO_SIZE = size;
0037   }
0038 
0039   public static int getUaoSize() {
0040     return TEST_UAO_SIZE == 0 ? UAO_SIZE : TEST_UAO_SIZE;
0041   }
0042 
0043   public static int getSize(Object object, long offset) {
0044     switch (getUaoSize()) {
0045       case 4:
0046         return Platform.getInt(object, offset);
0047       case 8:
0048         return (int)Platform.getLong(object, offset);
0049       default:
0050         // checkstyle.off: RegexpSinglelineJava
0051         throw new AssertionError("Illegal UAO_SIZE");
0052         // checkstyle.on: RegexpSinglelineJava
0053     }
0054   }
0055 
0056   public static void putSize(Object object, long offset, int value) {
0057     switch (getUaoSize()) {
0058       case 4:
0059         Platform.putInt(object, offset, value);
0060         break;
0061       case 8:
0062         Platform.putLong(object, offset, value);
0063         break;
0064       default:
0065         // checkstyle.off: RegexpSinglelineJava
0066         throw new AssertionError("Illegal UAO_SIZE");
0067         // checkstyle.on: RegexpSinglelineJava
0068     }
0069   }
0070 }