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 @SuppressWarnings("unchecked") 42 public static <T extends PointerBase> T nullPointer() { 43 return (T) WordFactory.zero(); 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 return that.equal(nullPointer()); 53 } 54 55 /** 56 * Predicate to check for a non-null Pointer value. 57 * 58 * @return Whether that Pointer is not the null Pointer. 59 */ 60 public static boolean isNonNull(ComparableWord that) { 61 return that.notEqual(nullPointer()); 62 } 63 64 /** 65 * Round a Pointer down to the nearest smaller multiple. 66 * 67 * @param that The Pointer to be rounded up. 68 * @param multiple The multiple to which that Pointer should be decreased. 69 * @return That Pointer, but rounded down. 70 */ 71 public static Pointer roundDown(PointerBase that, Unsigned multiple) { 72 return (Pointer) UnsignedUtils.roundDown((Unsigned) that, multiple); 73 } 74 75 /** 76 * Round a Pointer up to the nearest larger multiple. 77 * 78 * @param that The Pointer to be rounded up. 79 * @param multiple The multiple to which that Pointer should be increased. 80 * @return That Pointer, but rounded up. 81 */ 82 public static Pointer roundUp(PointerBase that, Unsigned multiple) { 83 return (Pointer) UnsignedUtils.roundUp((Unsigned) that, multiple); 84 } 85 86 /** 87 * Check that a Pointer is an even multiple. 88 * 89 * @param that The Pointer to be verified as a multiple. 90 * @param multiple The multiple against which the Pointer should be verified. 91 * @return true if that Pointer is a multiple, false otherwise. 92 */ 93 public static boolean isAMultiple(PointerBase that, Unsigned multiple) { 94 return that.equal(PointerUtils.roundDown(that, multiple)); 95 } 96 97 /** 98 * Return the distance between two Pointers. 99 * 100 * @param pointer1 A first Pointer. 101 * @param pointer2 A second Pointer. 102 * @return The distance in bytes between the two Pointers. 103 */ 104 public static Unsigned absoluteDifference(PointerBase pointer1, PointerBase pointer2) { 105 Pointer p1 = (Pointer) pointer1; 106 Pointer p2 = (Pointer) pointer2; 107 final Unsigned result; 108 if (p1.aboveOrEqual(p2)) { 109 result = p1.subtract(p2); 110 } else { 111 result = p2.subtract(p1); 112 } 113 return result; 114 } 115 116 /** 117 * The minimum of two Pointers. 118 * 119 * @param x A Pointer. 120 * @param y Another Pointer. 121 * @return The whichever Pointer is smaller. 122 */ 123 public static <T extends PointerBase> T min(T x, T y) { 124 return (((Pointer) x).belowOrEqual((Pointer) y)) ? x : y; 125 } 126 127 /** 128 * The maximum of two Pointers. 129 * 130 * @param x A Pointer. 131 * @param y Another Pointer. 132 * @return The whichever Pointer is larger. 133 */ 134 public static <T extends PointerBase> T max(T x, T y) { 135 return (((Pointer) x).aboveOrEqual((Pointer) y)) ? x : y; 136 } 137 }