PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/engine_java/000_Engine_Core/lib/JavaOpenAL/joal-1.1.2-src/gluegen/src/java/com/sun/gluegen/StructLayout.java

http://cellengine.googlecode.com/
Java | 153 lines | 89 code | 9 blank | 55 comment | 40 complexity | d00837a1885f4fff8a10c0147c4570b7 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause, AGPL-3.0, LGPL-2.1, Apache-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0
  1. /*
  2. * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * - Redistribution of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistribution in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * Neither the name of Sun Microsystems, Inc. or the names of
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * This software is provided "AS IS," without a warranty of any kind. ALL
  20. * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
  21. * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
  22. * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
  23. * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
  24. * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  25. * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
  26. * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
  27. * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
  28. * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
  29. * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
  30. * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  31. *
  32. * You acknowledge that this software is not designed or intended for use
  33. * in the design, construction, operation or maintenance of any nuclear
  34. * facility.
  35. *
  36. * Sun gratefully acknowledges that this software was originally authored
  37. * and developed by Kenneth Bradley Russell and Christopher John Kline.
  38. */
  39. package com.sun.gluegen;
  40. import com.sun.gluegen.cgram.types.*;
  41. /** Encapsulates algorithm for laying out data structures. Note that
  42. this ends up embedding code in various places via SizeThunks. If
  43. the 32-bit and 64-bit ports on a given platform differ
  44. fundamentally in their handling of struct layout then this code
  45. will need to be updated and, most likely, two versions of the
  46. SizeThunks maintained in various places. */
  47. public class StructLayout {
  48. private int baseOffset;
  49. private int structAlignment;
  50. protected StructLayout(int baseOffset,
  51. int structAlignment) {
  52. this.baseOffset = baseOffset;
  53. this.structAlignment = structAlignment;
  54. }
  55. public void layout(CompoundType t) {
  56. int n = t.getNumFields();
  57. SizeThunk curOffset = SizeThunk.constant(baseOffset);
  58. SizeThunk maxSize = SizeThunk.constant(0);
  59. for (int i = 0; i < n; i++) {
  60. Field f = t.getField(i);
  61. Type ft = f.getType();
  62. if (ft.isInt() || ft.isFloat() || ft.isDouble() || ft.isPointer()) {
  63. SizeThunk sz = ft.getSize();
  64. curOffset = SizeThunk.roundUp(curOffset, sz);
  65. f.setOffset(curOffset);
  66. if (t.isUnion()) {
  67. maxSize = SizeThunk.max(maxSize, sz);
  68. } else {
  69. curOffset = SizeThunk.add(curOffset, sz);
  70. }
  71. } else if (ft.isCompound()) {
  72. new StructLayout(0, structAlignment).layout(ft.asCompound());
  73. curOffset = SizeThunk.roundUp(curOffset, SizeThunk.constant(structAlignment));
  74. f.setOffset(curOffset);
  75. if (t.isUnion()) {
  76. maxSize = SizeThunk.max(maxSize, ft.getSize());
  77. } else {
  78. curOffset = SizeThunk.add(curOffset, ft.getSize());
  79. }
  80. } else if (ft.isArray()) {
  81. ArrayType arrayType = ft.asArray();
  82. CompoundType compoundElementType = arrayType.getBaseElementType().asCompound();
  83. if (compoundElementType != null) {
  84. new StructLayout(0, structAlignment).layout(compoundElementType);
  85. arrayType.recomputeSize();
  86. }
  87. // Note: not sure how this rounding is done
  88. curOffset = SizeThunk.roundUp(curOffset, SizeThunk.constant(structAlignment));
  89. f.setOffset(curOffset);
  90. curOffset = SizeThunk.add(curOffset, ft.getSize());
  91. } else {
  92. // FIXME
  93. String name = t.getName();
  94. if (name == null) {
  95. name = t.toString();
  96. }
  97. throw new RuntimeException("Complicated field types (" + ft +
  98. " " + f.getName() +
  99. " in type " + name +
  100. ") not implemented yet");
  101. }
  102. }
  103. // FIXME: I think the below is wrong; better check with some examples
  104. // if ((curOffset % structAlignment) != 0) {
  105. // curOffset += structAlignment - (curOffset % structAlignment);
  106. // }
  107. if (t.isUnion()) {
  108. t.setSize(maxSize);
  109. } else {
  110. t.setSize(curOffset);
  111. }
  112. }
  113. public static StructLayout createForCurrentPlatform() {
  114. // Note: this code is replicated in CPU.java
  115. String os = System.getProperty("os.name").toLowerCase();
  116. String cpu = System.getProperty("os.arch").toLowerCase();
  117. if ((os.startsWith("windows") && cpu.equals("x86"))) {
  118. // It appears that Windows uses a packing alignment of 4 bytes in 32-bit mode
  119. return new StructLayout(0, 4);
  120. } else if ((os.startsWith("windows") && cpu.equals("amd64")) ||
  121. (os.startsWith("linux") && cpu.equals("i386")) ||
  122. (os.startsWith("linux") && cpu.equals("x86")) ||
  123. (os.startsWith("linux") && cpu.equals("amd64")) ||
  124. (os.startsWith("linux") && cpu.equals("x86_64")) ||
  125. (os.startsWith("linux") && cpu.equals("ia64")) ||
  126. (os.startsWith("sunos") && cpu.equals("sparc")) ||
  127. (os.startsWith("sunos") && cpu.equals("sparcv9")) ||
  128. (os.startsWith("sunos") && cpu.equals("x86")) ||
  129. (os.startsWith("sunos") && cpu.equals("amd64")) ||
  130. (os.startsWith("mac os") && cpu.equals("ppc")) ||
  131. (os.startsWith("mac os") && cpu.equals("i386")) ||
  132. (os.startsWith("mac os") && cpu.equals("x86_64")) ||
  133. (os.startsWith("freebsd") && cpu.equals("i386")) ||
  134. (os.startsWith("hp-ux") && cpu.equals("pa_risc2.0"))
  135. ) {
  136. // FIXME: make struct alignment configurable? May need to change
  137. // packing rules on a per-type basis?
  138. return new StructLayout(0, 8);
  139. } else {
  140. // FIXME: add more ports
  141. throw new RuntimeException("Please port StructLayout to your OS (" + os + ") and CPU (" + cpu + ")");
  142. }
  143. }
  144. }