Author: mturk
Date: Mon Jun 22 07:09:33 2009
New Revision: 787155

URL: http://svn.apache.org/viewvc?rev=787155&view=rev
Log:
Use provided native structure alignment

Modified:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java
    
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java?rev=787155&r1=787154&r2=787155&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/AbstractStructure.java
 Mon Jun 22 07:09:33 2009
@@ -44,7 +44,7 @@
     {
         sizeof = 0;
         count  = 0;
-        align  = 4;
+        align  = Platform.STRUCT_ALIGN;
     }
 
     private void init()
@@ -84,7 +84,6 @@
                                         " has invalid class signature");
                             }
                             asizes[i] = getArrayElementLength(f, this);
-                            align     = Math.max(align, esizes[i]);
                         }
                     }
                 }
@@ -100,19 +99,17 @@
                 // where align is primitive element size
                 //
                 // sizeof += (sizeof + esizes[i]) % esizes[i];
-                sizeof = esizes[i] + ((sizeof - 1) & ~(esizes[i] - 1));
+                if (esizes[i] > align) {
+                    sizeof = align + ((sizeof - 1) & ~(align - 1));
+                }
+                else
+                    sizeof = esizes[i] + ((sizeof - 1) & ~(esizes[i] - 1));
                 offset[i] = sizeof;
                 if (asizes[i] > 0)
                     sizeof += (esizes[i] * asizes[i]);
                 else
                     sizeof += esizes[i];
             }
-            if (align > Pointer.SIZEOF) {
-                // Align to the pointer size except
-                // for the structures which elements are not
-                // larger then a size of a pointer.
-                align = Pointer.SIZEOF;
-            }
             sizeof = align(sizeof, align);
         }
         inited = true;

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java?rev=787155&r1=787154&r2=787155&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure32.java
 Mon Jun 22 07:09:33 2009
@@ -26,6 +26,7 @@
 class Structure32 extends Structure {
 
     private Pointer32 POINTER;
+    private static final int amask = Platform.STRUCT_ALIGN - 1;
 
     protected Structure32(Pointer32 ptr)
     {
@@ -134,7 +135,7 @@
             throw new NullPointerException();
         else if (index < 0 || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         return peek3(POINTER.POINTER + index);
     }
@@ -160,7 +161,7 @@
             throw new NullPointerException();
         else if (index < 0 || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         return peek5(POINTER.POINTER + index);
     }
@@ -222,7 +223,7 @@
             throw new NullPointerException();
         else if (index < 0 || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         poke3(POINTER.POINTER + index, value);
     }
@@ -235,7 +236,7 @@
             throw new NullPointerException();
         else if (index < 0 || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         poke5(POINTER.POINTER + index, value);
     }

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java?rev=787155&r1=787154&r2=787155&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Structure64.java
 Mon Jun 22 07:09:33 2009
@@ -26,6 +26,7 @@
 class Structure64 extends Structure {
 
     private Pointer64 POINTER;
+    private static final int amask = Platform.STRUCT_ALIGN - 1;
 
     protected Structure64(Pointer64 ptr)
     {
@@ -72,7 +73,7 @@
             throw new NullPointerException();
         else if (index < 0L || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         return get0(POINTER.POINTER + index);
     }
@@ -85,7 +86,7 @@
             throw new NullPointerException();
         else if (index < 0L || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         poke3(POINTER.POINTER + index, ((Pointer64)v).POINTER);
     }
@@ -134,7 +135,7 @@
             throw new NullPointerException();
         else if (index < 0L || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         return peek3(POINTER.POINTER + index);
     }
@@ -160,7 +161,7 @@
             throw new NullPointerException();
         else if (index < 0L || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         return peek5(POINTER.POINTER + index);
     }
@@ -222,7 +223,7 @@
             throw new NullPointerException();
         else if (index < 0L || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         poke3(POINTER.POINTER + index, value);
     }
@@ -235,7 +236,7 @@
             throw new NullPointerException();
         else if (index < 0L || (index + 8) > POINTER.PLENGTH)
             throw new IndexOutOfBoundsException();
-        else if ((index & 0x07) != 0)
+        else if ((index & amask) != 0)
             throw new UnalignedMemoryException();
         poke5(POINTER.POINTER + index, value);
     }

Modified: 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java?rev=787155&r1=787154&r2=787155&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestStructure.java
 Mon Jun 22 07:09:33 2009
@@ -73,11 +73,11 @@
 
         @OffsetOf(0)
         public int   I1FIELD;
-        @OffsetOf(4)  // 8
+        @OffsetOf(4)  // 4 or 8 depending on alignment
         public long  L1FIELD;
-        @OffsetOf(8)  // 16
+        @OffsetOf(12) // 12 or 16
         public long  L2FIELD;
-        @OffsetOf(12) // 20
+        @OffsetOf(20) // 20 or 24
         public char  C1FIELD;
     }
 
@@ -147,7 +147,7 @@
     {
         MyUStructure s = new MyUStructure();
         assertEquals("Fields",  4, s.count());
-        assertEquals("Sizeof", 28, s.sizeof());
+        assertEquals("Sizeof", 20 + Platform.STRUCT_ALIGN, s.sizeof());
     }
 
     public void testOffset()
@@ -155,7 +155,7 @@
     {
         MyUStructure s = new MyUStructure();
         assertEquals("Fields",  4, s.count());
-        assertEquals("OffsetOf", 24, s.offsetof("C1FIELD"));
+        assertEquals("OffsetOf", 16 + Platform.STRUCT_ALIGN, 
s.offsetof("C1FIELD"));
         assertEquals("OffsetOf", -1, s.offsetof("Unknown"));
     }
 


Reply via email to