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 sun.misc;
26
27 import java.util.Random;
28
29 /**
30 * Hashing utilities.
31 *
32 * Little endian implementations of Murmur3 hashing.
33 */
34 public class Hashing {
35
36 /**
37 * Static utility methods only.
38 */
39 private Hashing() {
40 throw new Error("No instances");
41 }
42
43 public static int murmur3_32(byte[] data) {
44 return murmur3_32(0, data, 0, data.length);
45 }
46
47 public static int murmur3_32(int seed, byte[] data) {
190 h1 = h1 * 5 + 0xe6546b64;
191 }
192
193 // tail (always empty, as body is always 32-bit chunks)
194
195 // finalization
196
197 h1 ^= len * (Integer.SIZE / Byte.SIZE);
198
199 // finalization mix force all bits of a hash block to avalanche
200 h1 ^= h1 >>> 16;
201 h1 *= 0x85ebca6b;
202 h1 ^= h1 >>> 13;
203 h1 *= 0xc2b2ae35;
204 h1 ^= h1 >>> 16;
205
206 return h1;
207 }
208
209 /**
210 * Holds references to things that can't be initialized until after VM
211 * is fully booted.
212 */
213 private static class Holder {
214
215 /**
216 * Used for generating per-instance hash seeds.
217 *
218 * We try to improve upon the default seeding.
219 */
220 static final Random SEED_MAKER = new Random(
221 Double.doubleToRawLongBits(Math.random())
222 ^ System.identityHashCode(Hashing.class)
223 ^ System.currentTimeMillis()
224 ^ System.nanoTime()
225 ^ Runtime.getRuntime().freeMemory());
226 }
227
228 public static int randomHashSeed(Object instance) {
229 int seed;
230 if (sun.misc.VM.isBooted()) {
231 seed = Holder.SEED_MAKER.nextInt();
232 } else {
233 // lower quality "random" seed value--still better than zero and not
234 // not practically reversible.
235 int hashing_seed[] = {
236 System.identityHashCode(Hashing.class),
237 System.identityHashCode(instance),
238 System.identityHashCode(Thread.currentThread()),
239 (int) Thread.currentThread().getId(),
240 (int) (System.currentTimeMillis() >>> 2), // resolution is poor
241 (int) (System.nanoTime() >>> 5), // resolution is poor
242 (int) (Runtime.getRuntime().freeMemory() >>> 4) // alloc min
243 };
244
245 seed = murmur3_32(hashing_seed);
246 }
247
248 // force to non-zero.
249 return (0 != seed) ? seed : 1;
250 }
251 }
|
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 sun.misc;
26
27 import java.util.concurrent.ThreadLocalRandom;
28
29 /**
30 * Hashing utilities.
31 *
32 * Little endian implementations of Murmur3 hashing.
33 */
34 public class Hashing {
35
36 /**
37 * Static utility methods only.
38 */
39 private Hashing() {
40 throw new Error("No instances");
41 }
42
43 public static int murmur3_32(byte[] data) {
44 return murmur3_32(0, data, 0, data.length);
45 }
46
47 public static int murmur3_32(int seed, byte[] data) {
190 h1 = h1 * 5 + 0xe6546b64;
191 }
192
193 // tail (always empty, as body is always 32-bit chunks)
194
195 // finalization
196
197 h1 ^= len * (Integer.SIZE / Byte.SIZE);
198
199 // finalization mix force all bits of a hash block to avalanche
200 h1 ^= h1 >>> 16;
201 h1 *= 0x85ebca6b;
202 h1 ^= h1 >>> 13;
203 h1 *= 0xc2b2ae35;
204 h1 ^= h1 >>> 16;
205
206 return h1;
207 }
208
209 /**
210 * Return a non-zero 32-bit pseudo random value. The {@code instance} object
211 * may be used as part of the value.
212 *
213 * @param instance an object to use if desired in choosing value.
214 * @return a non-zero 32-bit pseudo random value.
215 */
216 public static int randomHashSeed(Object instance) {
217 int seed;
218 if (sun.misc.VM.isBooted()) {
219 seed = ThreadLocalRandom.current().nextInt();
220 } else {
221 // lower quality "random" seed value--still better than zero and not
222 // not practically reversible.
223 int hashing_seed[] = {
224 System.identityHashCode(Hashing.class),
225 System.identityHashCode(instance),
226 System.identityHashCode(Thread.currentThread()),
227 (int) Thread.currentThread().getId(),
228 (int) (System.currentTimeMillis() >>> 2), // resolution is poor
229 (int) (System.nanoTime() >>> 5), // resolution is poor
230 (int) (Runtime.getRuntime().freeMemory() >>> 4) // alloc min
231 };
232
233 seed = murmur3_32(hashing_seed);
234 }
235
236 // force to non-zero.
237 return (0 != seed) ? seed : 1;
238 }
239 }
|