GlueGen: src/java/com/jogamp/gluegen/cgram/types/StructLayout.java Source File

GlueGen v2.6.0-rc-20250822 GlueGen, Native Binding Generator for Java™ (public API).

StructLayout.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (c) 2010 JogAmp Community. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * - Redistribution of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * - Redistribution in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of Sun Microsystems, Inc. or the names of
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * This software is provided "AS IS," without a warranty of any kind. ALL
21 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
22 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
23 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN
24 * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR
25 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
26 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR
27 * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR
28 * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
29 * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
30 * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF
31 * SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 *
33 * You acknowledge that this software is not designed or intended for use
34 * in the design, construction, operation or maintenance of any nuclear
35 * facility.
36 *
37 * Sun gratefully acknowledges that this software was originally authored
38 * and developed by Kenneth Bradley Russell and Christopher John Kline.
39 */
40
41package com.jogamp.gluegen.cgram.types;
42
43import com.jogamp.common.os.MachineDataInfo;
44import com.jogamp.gluegen.GlueGen;
45
46/** Encapsulates algorithm for laying out data structures. Note that
47 this ends up embedding code in various places via SizeThunks. If
48 the 32-bit and 64-bit ports on a given platform differ
49 fundamentally in their handling of struct layout then this code
50 will need to be updated and, most likely, two versions of the
51 SizeThunks maintained in various places. */
52
53 public class StructLayout {
54 private final int baseOffset;
55
56 protected StructLayout(final int baseOffset) {
57 this.baseOffset = baseOffset;
58 }
59
60 public void layout(final CompoundType t) {
61 /**
62 * - 1) align offset for the new data type,
63 * - 2) add the aligned size of the new data type
64 * - 3) add trailing padding (largest element size)
65 */
66 final int n = t.getNumFields();
67 SizeThunk curOffset = SizeThunk.constant(baseOffset);
68 SizeThunk maxSize = SizeThunk.constant(0);
69
70 final MachineDataInfo dbgMD;
71 if( GlueGen.debug() ) {
72 dbgMD = MachineDataInfo.StaticConfig.LP64_UNIX.md;
73 System.err.printf("SL.__: o %03d, s %03d, t %s{%d}%n", curOffset.computeSize(dbgMD), 0, t, t.getNumFields());
74 } else {
75 dbgMD = null;
76 }
77
78 for (int i = 0; i < n; i++) {
79 final Field f = t.getField(i);
80 final Type ft = f.getType();
81 if (ft.isInt() || ft.isFloat() || ft.isDouble() || ft.isPointer()) {
82 final SizeThunk sz = ft.getSize();
83 curOffset = SizeThunk.align(curOffset, sz);
84 f.setOffset(curOffset);
85 if (t.isUnion()) {
86 maxSize = SizeThunk.max(maxSize, sz);
87 } else {
88 curOffset = SizeThunk.add(curOffset, sz);
89 }
90 } else if (ft.isCompound()) {
91 final CompoundType ct = ft.asCompound();
92 if(!ct.isLayouted()) {
93 StructLayout.layout(0, ct);
94 }
95 final SizeThunk sz = ct.getSize();
96 curOffset = SizeThunk.align(curOffset, sz);
97 f.setOffset(curOffset);
98 if (t.isUnion()) {
99 maxSize = SizeThunk.max(maxSize, sz);
100 } else {
101 curOffset = SizeThunk.add(curOffset, sz);
102 }
103 } else if (ft.isArray()) {
104 final ArrayType arrayType = ft.asArray();
105 if(!arrayType.isLayouted()) {
106 final CompoundType compoundElementType = arrayType.getBaseType().asCompound();
107 if (compoundElementType != null) {
108 if(!compoundElementType.isLayouted()) {
109 StructLayout.layout(0, compoundElementType);
110 }
111 arrayType.recomputeSize();
112 }
113 arrayType.setLayouted();
114 }
115 final SizeThunk sz = ft.getSize();
116 curOffset = SizeThunk.align(curOffset, sz);
117 f.setOffset(curOffset);
118 curOffset = SizeThunk.add(curOffset, sz);
119 } else {
120 // FIXME
121 String name = t.getName();
122 if (name == null) {
123 name = t.toString();
124 }
125 throw new RuntimeException("Complicated field types (" + ft +
126 " " + f.getName() +
127 " in type " + name +
128 ") not implemented yet");
129 }
130 if( GlueGen.debug() ) {
131 System.err.printf("SL.%02d: o %03d, s %03d: %s, %s%n", (i+1), f.getOffset(dbgMD), ft.getSize(dbgMD), f, ft.getDebugString());
132 }
133 }
134 if (t.isUnion()) {
135 t.setSize(maxSize);
136 } else {
137 // trailing struct padding ..
138 curOffset = SizeThunk.align(curOffset, curOffset);
139 t.setSize(curOffset);
140 }
141 if( GlueGen.debug() ) {
142 System.err.printf("SL.XX: o %03d, s %03d, t %s{%d}%n%n", curOffset.computeSize(dbgMD), t.getSize(dbgMD), t, t.getNumFields());
143 }
144 t.setLayouted();
145 }
146
147 public static StructLayout create(final int baseOffset) {
148 return new StructLayout(baseOffset);
149 }
150
151 public static void layout(final int baseOffset, final CompoundType t) {
152 create(baseOffset).layout(t);
153 }
154}
com.jogamp.common.os.MachineDataInfo
Machine data description for alignment and size onle, see com.jogamp.gluegen.
Definition: MachineDataInfo.java:57
com.jogamp.gluegen.GlueGen
Glue code generator for C functions and data structures.
Definition: GlueGen.java:59
com.jogamp.gluegen.cgram.types.ArrayType
Represents an array type.
Definition: ArrayType.java:50
com.jogamp.gluegen.cgram.types.ArrayType.getBaseType
final Type getBaseType()
Helper method to returns the bottom-most element type of this type, i.e.
Definition: ArrayType.java:120
com.jogamp.gluegen.cgram.types.CompoundType
Models all compound types, i.e., those containing fields: structs and unions.
Definition: CompoundType.java:51
com.jogamp.gluegen.cgram.types.CompoundType.getField
Field getField(final int i)
Returns the ith field of this type.
Definition: CompoundType.java:178
com.jogamp.gluegen.cgram.types.CompoundType.getNumFields
int getNumFields()
Returns the number of fields in this type.
Definition: CompoundType.java:173
com.jogamp.gluegen.cgram.types.CompoundType.toString
String toString()
Returns a string representation of this type.
Definition: CompoundType.java:226
com.jogamp.gluegen.cgram.types.CompoundType.isUnion
abstract boolean isUnion()
Indicates whether this type was declared as a union.
com.jogamp.gluegen.cgram.types.Field
Represents a field in a struct or union.
Definition: Field.java:47
com.jogamp.gluegen.cgram.types.Field.getOffset
SizeThunk getOffset()
SizeThunk computing offset, in bytes, of this field in the containing data structure.
Definition: Field.java:104
com.jogamp.gluegen.cgram.types.Field.getName
String getName()
Name of this field in the containing data structure.
Definition: Field.java:98
com.jogamp.gluegen.cgram.types.Field.getType
Type getType()
Type of this field.
Definition: Field.java:101
com.jogamp.gluegen.cgram.types.Field.setOffset
void setOffset(final SizeThunk offset)
Sets the offset of this field in the containing data structure.
Definition: Field.java:111
com.jogamp.gluegen.cgram.types.MemoryLayoutType.isLayouted
boolean isLayouted()
Definition: MemoryLayoutType.java:44
com.jogamp.gluegen.cgram.types.MemoryLayoutType.setLayouted
void setLayouted()
Definition: MemoryLayoutType.java:45
com.jogamp.gluegen.cgram.types.SizeThunk
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
com.jogamp.gluegen.cgram.types.SizeThunk.max
static SizeThunk max(final SizeThunk thunk1, final SizeThunk thunk2)
Definition: SizeThunk.java:365
com.jogamp.gluegen.cgram.types.SizeThunk.computeSize
abstract long computeSize(MachineDataInfo machDesc)
com.jogamp.gluegen.cgram.types.SizeThunk.add
static SizeThunk add(final SizeThunk thunk1, final SizeThunk thunk2)
Definition: SizeThunk.java:264
com.jogamp.gluegen.cgram.types.SizeThunk.constant
static SizeThunk constant(final int constant)
Definition: SizeThunk.java:390
com.jogamp.gluegen.cgram.types.SizeThunk.align
static SizeThunk align(final SizeThunk offsetThunk, final SizeThunk alignmentThunk)
Definition: SizeThunk.java:314
com.jogamp.gluegen.cgram.types.StructLayout
Encapsulates algorithm for laying out data structures.
Definition: StructLayout.java:53
com.jogamp.gluegen.cgram.types.StructLayout.layout
void layout(final CompoundType t)
Definition: StructLayout.java:60
com.jogamp.gluegen.cgram.types.StructLayout.create
static StructLayout create(final int baseOffset)
Definition: StructLayout.java:147
com.jogamp.gluegen.cgram.types.StructLayout.layout
static void layout(final int baseOffset, final CompoundType t)
Definition: StructLayout.java:151
com.jogamp.gluegen.cgram.types.StructLayout.StructLayout
StructLayout(final int baseOffset)
Definition: StructLayout.java:56
com.jogamp.gluegen.cgram.types.Type
Models a C type.
Definition: Type.java:54
com.jogamp.gluegen.cgram.types.Type.isArray
final boolean isArray()
Indicates whether this is an ArrayType.
Definition: Type.java:409
com.jogamp.gluegen.cgram.types.Type.isInt
final boolean isInt()
Indicates whether this is an IntType.
Definition: Type.java:399
com.jogamp.gluegen.cgram.types.Type.isFloat
final boolean isFloat()
Indicates whether this is a FloatType.
Definition: Type.java:403
com.jogamp.gluegen.cgram.types.Type.getSize
final SizeThunk getSize()
SizeThunk which computes size of this type in bytes.
Definition: Type.java:360
com.jogamp.gluegen.cgram.types.Type.isDouble
final boolean isDouble()
Indicates whether this is a DoubleType.
Definition: Type.java:405
com.jogamp.gluegen.cgram.types.Type.getDebugString
final String getDebugString()
Definition: Type.java:285
com.jogamp.gluegen.cgram.types.Type.asArray
ArrayType asArray()
Casts this to an ArrayType or returns null if not an ArrayType.
Definition: Type.java:388
com.jogamp.gluegen.cgram.types.Type.isPointer
final boolean isPointer()
Indicates whether this is a PointerType.
Definition: Type.java:407
com.jogamp.gluegen.cgram.types.Type.asCompound
CompoundType asCompound()
Casts this to a CompoundType or returns null if not a CompoundType.
Definition: Type.java:390
com.jogamp.gluegen.cgram.types.Type.isCompound
final boolean isCompound()
Indicates whether this is a CompoundType.
Definition: Type.java:411
com.jogamp.common.os.MachineDataInfo.StaticConfig
Static enumeration of MachineDataInfo instances used for high performance data size and alignment loo...
Definition: MachineDataInfo.java:99
com.jogamp.common.os.MachineDataInfo.StaticConfig.LP64_UNIX
LP64_UNIX
LP64 Unix, e.g.
Definition: MachineDataInfo.java:115
com.jogamp.gluegen.cgram.types.AliasedSymbol.getName
String getName()
Return the current-name, which is the last renamed-name if issued, or the original-name.