View Javadoc

1   // HashCode.java, created Mon Feb  5 23:23:21 2001 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Runtime;
5   
6   import joeq.Allocator.ObjectLayout;
7   import joeq.Class.jq_Array;
8   import joeq.Class.jq_Class;
9   import joeq.Class.jq_Reference;
10  import joeq.Memory.HeapAddress;
11  import jwutil.util.Assert;
12  
13  /*
14   * @author  John Whaley <jwhaley@alum.mit.edu>
15   * @version $Id: HashCode.java 1941 2004-09-30 03:37:06Z joewhaley $
16   */
17  public abstract class HashCode {
18  
19      public static int identityHashCode(Object x) {
20          HeapAddress a = HeapAddress.addressOf(x);
21          int status = a.offset(ObjectLayout.STATUS_WORD_OFFSET).peek4();
22          if ((status & ObjectLayout.HASHED_MOVED) != 0) {
23              jq_Reference t = jq_Reference.getTypeOf(x);
24              if (t.isClassType()) {
25                  jq_Class k = (jq_Class) t;
26                  return a.offset(k.getInstanceSize() - ObjectLayout.OBJ_HEADER_SIZE).peek4();
27              }
28              Assert._assert(t.isArrayType());
29              jq_Array k = (jq_Array) t;
30              int arraylength = a.offset(ObjectLayout.ARRAY_LENGTH_OFFSET).peek4();
31              return a.offset(k.getInstanceSize(arraylength) - ObjectLayout.ARRAY_HEADER_SIZE).peek4();
32          }
33          a.offset(ObjectLayout.STATUS_WORD_OFFSET).poke4(status | ObjectLayout.HASHED);
34          return a.to32BitValue();
35      }
36      
37  }