1 /* 2 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.graalvm.word; 26 27 /** 28 * Utility methods on Pointers. 29 */ 30 public final class PointerUtils { 31 32 private PointerUtils() { 33 // This is a class of static methods, so no need for any instances. 34 } 35 36 /** 37 * The value of a null Pointer. 38 * 39 * @return A null Pointer value. 40 */ 41 public static <T extends PointerBase> T nullPointer() { 42 /* This method will be deleted soon. */ 43 return WordFactory.nullPointer(); 44 } 45 46 /** 47 * Predicate to check for the null Pointer value. 48 * 49 * @return Whether that Pointer is the null Pointer. 50 */ 51 public static boolean isNull(ComparableWord that) { 52 /* This method will be deleted soon. */ 53 return ((PointerBase) that).isNull(); 54 } 55 56 /** 57 * Predicate to check for a non-null Pointer value. 58 * 59 * @return Whether that Pointer is not the null Pointer. 60 */ 61 public static boolean isNonNull(ComparableWord that) { 62 /* This method will be deleted soon. */ 63 return ((PointerBase) that).isNonNull(); 64 } 65 66 /** 67 * Round a Pointer down to the nearest smaller multiple. 68 * 69 * @param that The Pointer to be rounded up. 70 * @param multiple The multiple to which that Pointer should be decreased. 71 * @return That Pointer, but rounded down. 72 */ 73 public static Pointer roundDown(PointerBase that, Unsigned multiple) { 74 return (Pointer) UnsignedUtils.roundDown((Unsigned) that, multiple); 75 } 76 77 /** 78 * Round a Pointer up to the nearest larger multiple. 79 * 80 * @param that The Pointer to be rounded up. 81 * @param multiple The multiple to which that Pointer should be increased. 82 * @return That Pointer, but rounded up. 83 */ 84 public static Pointer roundUp(PointerBase that, Unsigned multiple) { 85 return (Pointer) UnsignedUtils.roundUp((Unsigned) that, multiple); 86 } 87 88 /** 89 * Check that a Pointer is an even multiple. 90 * 91 * @param that The Pointer to be verified as a multiple. 92 * @param multiple The multiple against which the Pointer should be verified. 93 * @return true if that Pointer is a multiple, false otherwise. 94 */ 95 public static boolean isAMultiple(PointerBase that, Unsigned multiple) { 96 return that.equal(PointerUtils.roundDown(that, multiple)); 97 } 98 99 /** 100 * Return the distance between two Pointers. 101 * 102 * @param pointer1 A first Pointer. 103 * @param pointer2 A second Pointer. 104 * @return The distance in bytes between the two Pointers. 105 */ 106 public static Unsigned absoluteDifference(PointerBase pointer1, PointerBase pointer2) { 107 Pointer p1 = (Pointer) pointer1; 108 Pointer p2 = (Pointer) pointer2; 109 final Unsigned result; 110 if (p1.aboveOrEqual(p2)) { 111 result = p1.subtract(p2); 112 } else { 113 result = p2.subtract(p1); 114 } 115 return result; 116 } 117 118 /** 119 * The minimum of two Pointers. 120 * 121 * @param x A Pointer. 122 * @param y Another Pointer. 123 * @return The whichever Pointer is smaller. 124 */ 125 public static <T extends PointerBase> T min(T x, T y) { 126 return (((Pointer) x).belowOrEqual((Pointer) y)) ? x : y; 127 } 128 129 /** 130 * The maximum of two Pointers. 131 * 132 * @param x A Pointer. 133 * @param y Another Pointer. 134 * @return The whichever Pointer is larger. 135 */ 136 public static <T extends PointerBase> T max(T x, T y) { 137 return (((Pointer) x).aboveOrEqual((Pointer) y)) ? x : y; 138 } 139 }