jaulib v1.3.0
Jau Support Library (C++, Java, ..)
MachineDataInfo.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Author: Kenneth Bradley Russell
4 * Copyright (c) 2021 Gothel Software e.K.
5 * Copyright (c) 2010 Gothel Software e.K.
6 * Copyright (c) 2010 JogAmp Community.
7 * Copyright (c) 2003 Sun Microsystems
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the
11 * "Software"), to deal in the Software without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 */
28
29package org.jau.sys;
30
31/**
32 * Machine data description for alignment and size onle, see {@link com.jogamp.gluegen}.
33 * <p>
34 * {@code little-endian} / {@code big/endian} description is left,
35 * allowing re-using instances in {@link MachineDataInfo.StaticConfig StaticConfig}.
36 * Use {@link {@link RuntimeProps#LITTLE_ENDIAN}.
37 * </p>
38 * <p>
39 * Further more, the value {@ MachineDataInfo#pageSizeInBytes} shall be ignored
40 * in {@link MachineDataInfo.StaticConfig StaticConfig}, see {@link MachineDataInfo#compatible(MachineDataInfo)}.
41 * </p>
42 */
43public class MachineDataInfo {
44 /* arch os int, long, float, doubl, ldoubl, ptr, page */
45 private final static int[] size_arm_mips_32 = { 4, 4, 4, 8, 8, 4, 4096 };
46 private final static int[] size_x86_32_unix = { 4, 4, 4, 8, 12, 4, 4096 };
47 private final static int[] size_x86_32_android = { 4, 4, 4, 8, 8, 4, 4096 };
48 private final static int[] size_x86_32_macos = { 4, 4, 4, 8, 16, 4, 4096 };
49 private final static int[] size_ppc_32_unix = { 4, 4, 4, 8, 16, 4, 4096 };
50 private final static int[] size_sparc_32_sunos = { 4, 4, 4, 8, 16, 4, 8192 };
51 private final static int[] size_x86_32_windows = { 4, 4, 4, 8, 12, 4, 4096 };
52 private final static int[] size_lp64_unix = { 4, 8, 4, 8, 16, 8, 4096 };
53 private final static int[] size_x86_64_windows = { 4, 4, 4, 8, 16, 8, 4096 };
54 private final static int[] size_arm64_ios = { 4, 8, 4, 8, 8, 8, 8192 };
55
56 /* arch os i8, i16, i32, i64, int, long, float, doubl, ldoubl, ptr */
57 private final static int[] align_arm_mips_32 = { 1, 2, 4, 8, 4, 4, 4, 8, 8, 4 };
58 private final static int[] align_x86_32_unix = { 1, 2, 4, 4, 4, 4, 4, 4, 4, 4 };
59 private final static int[] align_x86_32_macos = { 1, 2, 4, 4, 4, 4, 4, 4, 16, 4 };
60 private final static int[] align_ppc_32_unix = { 1, 2, 4, 8, 4, 4, 4, 8, 16, 4 };
61 private final static int[] align_sparc_32_sunos = { 1, 2, 4, 8, 4, 4, 4, 8, 8, 4 };
62 private final static int[] align_x86_32_windows = { 1, 2, 4, 8, 4, 4, 4, 8, 4, 4 };
63 private final static int[] align_lp64_unix = { 1, 2, 4, 8, 4, 8, 4, 8, 16, 8 };
64 private final static int[] align_x86_64_windows = { 1, 2, 4, 8, 4, 4, 4, 8, 16, 8 };
65 private final static int[] align_arm64_ios = { 1, 2, 4, 8, 4, 8, 4, 8, 8, 8 };
66
67 /**
68 * Static enumeration of {@link MachineDataInfo} instances
69 * used for high performance data size and alignment lookups,
70 * e.g. for generated structures using the {@link MachineDataInfo.StaticConfig} index.
71 * <p>
72 * The value {@link MachineDataInfo#pageSizeInBytes} shall be ignored
73 * for static instances!
74 * </p>
75 * <p>
76 * If changing this table, you need to:
77 * <ul>
78 * <li>Rebuild GlueGen.</li>
79 * <li>Run ant {@code build.xml} target {@code generate.os.sources}.</li>
80 * <li>Rebuild everything.</li>
81 * </ul>
82 * .. b/c the generated code for glued structures must reflect this change!
83 * </p>
84 */
85 public enum StaticConfig {
86 /** {@link Platform.CPUType#ARM} or {@link Platform.CPUType#MIPS_32} */
87 ARM_MIPS_32( size_arm_mips_32, align_arm_mips_32),
88 /** {@link Platform.CPUType#X86_32} Unix */
89 X86_32_UNIX( size_x86_32_unix, align_x86_32_unix),
90 /** {@link Platform.CPUType#X86_32} Android/Bionic */
91 X86_32_ANDROID( size_x86_32_android, align_x86_32_unix),
92 /** {@link Platform.CPUType#X86_32} MacOS (Special case gcc4/OSX) */
93 X86_32_MACOS( size_x86_32_macos, align_x86_32_macos),
94 /** {@link Platform.CPUType#PPC} Unix */
95 PPC_32_UNIX( size_ppc_32_unix, align_ppc_32_unix),
96 /** {@link Platform.CPUType#SPARC_32} Solaris */
97 SPARC_32_SUNOS( size_sparc_32_sunos, align_sparc_32_sunos),
98 /** {@link Platform.CPUType#X86_32} Windows */
99 X86_32_WINDOWS( size_x86_32_windows, align_x86_32_windows),
100 /** LP64 Unix, e.g.: {@link Platform.CPUType#X86_64} Unix, {@link Platform.CPUType#ARM64} EABI, {@link Platform.CPUType#PPC64} Unix, .. */
101 LP64_UNIX( size_lp64_unix, align_lp64_unix),
102 /** {@link Platform.CPUType#X86_64} Windows */
103 X86_64_WINDOWS( size_x86_64_windows, align_x86_64_windows),
104 /** {@link Platform.CPUType#ARM64 } iOS */
105 ARM64_IOS( size_arm64_ios, align_arm64_ios);
106 // 9
107
108 public final MachineDataInfo md;
109
110 StaticConfig(final int[] sizes, final int[] alignments) {
111 int i=0, j=0;
112 this.md = new MachineDataInfo(false,
113 sizes[i++],
114 sizes[i++],
115 sizes[i++],
116 sizes[i++],
117 sizes[i++],
118 sizes[i++],
119 sizes[i++],
120 alignments[j++],
121 alignments[j++],
122 alignments[j++],
123 alignments[j++],
124 alignments[j++],
125 alignments[j++],
126 alignments[j++],
127 alignments[j++],
128 alignments[j++],
129 alignments[j++]);
130 }
131
132 public final StringBuilder toString(StringBuilder sb) {
133 if(null==sb) {
134 sb = new StringBuilder();
135 }
136 sb.append("MachineDataInfoStatic: ").append(this.name()).append("(").append(this.ordinal()).append("): ");
137 md.toString(sb);
138 return sb;
139 }
140 public final String toShortString() {
141 return this.name()+"("+this.ordinal()+")";
142 }
143 @Override
144 public String toString() {
145 return toString(null).toString();
146 }
147
148 /**
149 * Static's {@link MachineDataInfo} shall be unique by the
150 * {@link MachineDataInfo#compatible(MachineDataInfo) compatible} criteria.
151 */
152 public static final void validateUniqueMachineDataInfo() {
153 final StaticConfig[] scs = StaticConfig.values();
154 for(int i=scs.length-1; i>=0; i--) {
155 final StaticConfig a = scs[i];
156 for(int j=scs.length-1; j>=0; j--) {
157 if( i != j ) {
158 final StaticConfig b = scs[j];
159 if( a.md.compatible(b.md) ) {
160 // oops
161 final String msg = "Duplicate/Compatible MachineDataInfo in StaticConfigs: Elements ["+i+": "+a.toShortString()+"] and ["+j+": "+b.toShortString()+"]";
162 System.err.println(msg);
163 System.err.println(a);
164 System.err.println(b);
165 throw new InternalError(msg);
166 }
167 }
168 }
169 }
170 }
171 public static final StaticConfig findCompatible(final MachineDataInfo md) {
172 final StaticConfig[] scs = StaticConfig.values();
173 for(int i=scs.length-1; i>=0; i--) {
174 final StaticConfig a = scs[i];
175 if( a.md.compatible(md) ) {
176 return a;
177 }
178 }
179 return null;
180 }
181 }
182
183 final private boolean runtimeValidated;
184
185 final private int int8SizeInBytes = 1;
186 final private int int16SizeInBytes = 2;
187 final private int int32SizeInBytes = 4;
188 final private int int64SizeInBytes = 8;
189
190 final private int intSizeInBytes;
191 final private int longSizeInBytes;
192 final private int floatSizeInBytes;
193 final private int doubleSizeInBytes;
194 final private int ldoubleSizeInBytes;
195 final private int pointerSizeInBytes;
196 final private int pageSizeInBytes;
197
198 final private int int8AlignmentInBytes;
199 final private int int16AlignmentInBytes;
200 final private int int32AlignmentInBytes;
201 final private int int64AlignmentInBytes;
202 final private int intAlignmentInBytes;
203 final private int longAlignmentInBytes;
204 final private int floatAlignmentInBytes;
205 final private int doubleAlignmentInBytes;
206 final private int ldoubleAlignmentInBytes;
207 final private int pointerAlignmentInBytes;
208
209 public MachineDataInfo(final boolean runtimeValidated,
210
211 final int intSizeInBytes,
212 final int longSizeInBytes,
213 final int floatSizeInBytes,
214 final int doubleSizeInBytes,
215 final int ldoubleSizeInBytes,
216 final int pointerSizeInBytes,
217 final int pageSizeInBytes,
218
219 final int int8AlignmentInBytes,
220 final int int16AlignmentInBytes,
221 final int int32AlignmentInBytes,
222 final int int64AlignmentInBytes,
223 final int intAlignmentInBytes,
224 final int longAlignmentInBytes,
225 final int floatAlignmentInBytes,
226 final int doubleAlignmentInBytes,
227 final int ldoubleAlignmentInBytes,
228 final int pointerAlignmentInBytes) {
229 this.runtimeValidated = runtimeValidated;
230
231 this.intSizeInBytes = intSizeInBytes;
232 this.longSizeInBytes = longSizeInBytes;
233 this.floatSizeInBytes = floatSizeInBytes;
234 this.doubleSizeInBytes = doubleSizeInBytes;
235 this.ldoubleSizeInBytes = ldoubleSizeInBytes;
236 this.pointerSizeInBytes = pointerSizeInBytes;
237 this.pageSizeInBytes = pageSizeInBytes;
238
239 this.int8AlignmentInBytes = int8AlignmentInBytes;
240 this.int16AlignmentInBytes = int16AlignmentInBytes;
241 this.int32AlignmentInBytes = int32AlignmentInBytes;
242 this.int64AlignmentInBytes = int64AlignmentInBytes;
243 this.intAlignmentInBytes = intAlignmentInBytes;
244 this.longAlignmentInBytes = longAlignmentInBytes;
245 this.floatAlignmentInBytes = floatAlignmentInBytes;
246 this.doubleAlignmentInBytes = doubleAlignmentInBytes;
247 this.ldoubleAlignmentInBytes = ldoubleAlignmentInBytes;
248 this.pointerAlignmentInBytes = pointerAlignmentInBytes;
249 }
250
251 /**
252 * @return true if all values are validated at runtime, otherwise false (i.e. for static compilation w/ preset values)
253 */
254 public final boolean isRuntimeValidated() {
255 return runtimeValidated;
256 }
257
258 public final int intSizeInBytes() { return intSizeInBytes; }
259 public final int longSizeInBytes() { return longSizeInBytes; }
260 public final int int8SizeInBytes() { return int8SizeInBytes; }
261 public final int int16SizeInBytes() { return int16SizeInBytes; }
262 public final int int32SizeInBytes() { return int32SizeInBytes; }
263 public final int int64SizeInBytes() { return int64SizeInBytes; }
264 public final int floatSizeInBytes() { return floatSizeInBytes; }
265 public final int doubleSizeInBytes() { return doubleSizeInBytes; }
266 public final int ldoubleSizeInBytes() { return ldoubleSizeInBytes; }
267 public final int pointerSizeInBytes() { return pointerSizeInBytes; }
268 public final int pageSizeInBytes() { return pageSizeInBytes; }
269
270 public final int intAlignmentInBytes() { return intAlignmentInBytes; }
271 public final int longAlignmentInBytes() { return longAlignmentInBytes; }
272 public final int int8AlignmentInBytes() { return int8AlignmentInBytes; }
273 public final int int16AlignmentInBytes() { return int16AlignmentInBytes; }
274 public final int int32AlignmentInBytes() { return int32AlignmentInBytes; }
275 public final int int64AlignmentInBytes() { return int64AlignmentInBytes; }
276 public final int floatAlignmentInBytes() { return floatAlignmentInBytes; }
277 public final int doubleAlignmentInBytes() { return doubleAlignmentInBytes; }
280
281 /**
282 * @return number of pages required for size in bytes
283 */
284 public int pageCount(final int size) {
285 return ( size + ( pageSizeInBytes - 1) ) / pageSizeInBytes ; // integer arithmetic
286 }
287
288 /**
289 * @return page aligned size in bytes
290 */
291 public int pageAlignedSize(final int size) {
292 return pageCount(size) * pageSizeInBytes;
293 }
294
295 /**
296 * Checks whether two size objects are equal. Two instances
297 * of <code>MachineDataInfo</code> are considered equal if all components
298 * match but {@link #runtimeValidated}, {@link #isRuntimeValidated()}.
299 * @return <code>true</code> if the two MachineDataInfo are equal;
300 * otherwise <code>false</code>.
301 */
302 @Override
303 public final boolean equals(final Object obj) {
304 if (this == obj) { return true; }
305 if ( !(obj instanceof MachineDataInfo) ) { return false; }
306 final MachineDataInfo md = (MachineDataInfo) obj;
307
308 return pageSizeInBytes == md.pageSizeInBytes &&
309 compatible(md);
310 }
311
312 /**
313 * Checks whether two {@link MachineDataInfo} objects are equal.
314 * <p>
315 * Two {@link MachineDataInfo} instances are considered equal if all components
316 * match but {@link #isRuntimeValidated()} and {@link #pageSizeInBytes()}.
317 * </p>
318 * @return <code>true</code> if the two {@link MachineDataInfo} are equal;
319 * otherwise <code>false</code>.
320 */
321 public final boolean compatible(final MachineDataInfo md) {
322 return intSizeInBytes == md.intSizeInBytes &&
323 longSizeInBytes == md.longSizeInBytes &&
324 floatSizeInBytes == md.floatSizeInBytes &&
325 doubleSizeInBytes == md.doubleSizeInBytes &&
326 ldoubleSizeInBytes == md.ldoubleSizeInBytes &&
327 pointerSizeInBytes == md.pointerSizeInBytes &&
328
329 int8AlignmentInBytes == md.int8AlignmentInBytes &&
330 int16AlignmentInBytes == md.int16AlignmentInBytes &&
331 int32AlignmentInBytes == md.int32AlignmentInBytes &&
332 int64AlignmentInBytes == md.int64AlignmentInBytes &&
333 intAlignmentInBytes == md.intAlignmentInBytes &&
334 longAlignmentInBytes == md.longAlignmentInBytes &&
335 floatAlignmentInBytes == md.floatAlignmentInBytes &&
336 doubleAlignmentInBytes == md.doubleAlignmentInBytes &&
337 ldoubleAlignmentInBytes == md.ldoubleAlignmentInBytes &&
338 pointerAlignmentInBytes == md.pointerAlignmentInBytes ;
339 }
340
341 public StringBuilder toString(StringBuilder sb) {
342 if(null==sb) {
343 sb = new StringBuilder();
344 }
345 sb.append("MachineDataInfo: runtimeValidated ").append(isRuntimeValidated()).append(", 32Bit ").append(4 == pointerAlignmentInBytes).append(", primitive size / alignment:").append(PlatformProps.NEWLINE);
346 sb.append(" int8 ").append(int8SizeInBytes) .append(" / ").append(int8AlignmentInBytes);
347 sb.append(", int16 ").append(int16SizeInBytes) .append(" / ").append(int16AlignmentInBytes).append(PlatformProps.NEWLINE);
348 sb.append(" int ").append(intSizeInBytes) .append(" / ").append(intAlignmentInBytes);
349 sb.append(", long ").append(longSizeInBytes) .append(" / ").append(longAlignmentInBytes).append(PlatformProps.NEWLINE);
350 sb.append(" int32 ").append(int32SizeInBytes) .append(" / ").append(int32AlignmentInBytes);
351 sb.append(", int64 ").append(int64SizeInBytes) .append(" / ").append(int64AlignmentInBytes).append(PlatformProps.NEWLINE);
352 sb.append(" float ").append(floatSizeInBytes) .append(" / ").append(floatAlignmentInBytes);
353 sb.append(", double ").append(doubleSizeInBytes) .append(" / ").append(doubleAlignmentInBytes);
354 sb.append(", ldouble ").append(ldoubleSizeInBytes).append(" / ").append(ldoubleAlignmentInBytes).append(PlatformProps.NEWLINE);
355 sb.append(" pointer ").append(pointerSizeInBytes).append(" / ").append(pointerAlignmentInBytes);
356 sb.append(", page ").append(pageSizeInBytes);
357 return sb;
358 }
359
360 @Override
361 public String toString() {
362 return toString(null).toString();
363 }
364
366 if( cpuType.is32Bit ) {
367 if( PlatformTypes.CPUFamily.ARM32 == cpuType.family ||
368 PlatformTypes.CPUType.MIPS_32 == cpuType ) {
370 } else if( PlatformTypes.OSType.WINDOWS == osType ) {
372 } else if( PlatformTypes.OSType.MACOS == osType ) {
374 } else if ( PlatformTypes.OSType.SUNOS == osType &&
375 PlatformTypes.CPUType.SPARC_32 == cpuType ) {
377 } else if ( PlatformTypes.CPUType.PPC == cpuType ) {
379 } else {
381 }
382 } else {
383 if( PlatformTypes.OSType.WINDOWS == osType ) {
385 } else if( PlatformTypes.OSType.IOS == osType && PlatformTypes.CPUType.ARM64 == cpuType ) {
386 return StaticConfig.ARM64_IOS;
387 } else {
388 // for all 64bit unix types (x86_64, aarch64, sparcv9, ..)
389 return StaticConfig.LP64_UNIX;
390 }
391 }
392 }
393}
Machine data description for alignment and size onle, see com.jogamp.gluegen.
static StaticConfig guessStaticMachineDataInfo(final PlatformTypes.OSType osType, final PlatformTypes.CPUType cpuType)
int pageCount(final int size)
final boolean equals(final Object obj)
Checks whether two size objects are equal.
final boolean compatible(final MachineDataInfo md)
Checks whether two MachineDataInfo objects are equal.
MachineDataInfo(final boolean runtimeValidated, final int intSizeInBytes, final int longSizeInBytes, final int floatSizeInBytes, final int doubleSizeInBytes, final int ldoubleSizeInBytes, final int pointerSizeInBytes, final int pageSizeInBytes, final int int8AlignmentInBytes, final int int16AlignmentInBytes, final int int32AlignmentInBytes, final int int64AlignmentInBytes, final int intAlignmentInBytes, final int longAlignmentInBytes, final int floatAlignmentInBytes, final int doubleAlignmentInBytes, final int ldoubleAlignmentInBytes, final int pointerAlignmentInBytes)
StringBuilder toString(StringBuilder sb)
int pageAlignedSize(final int size)
Platform Properties derived from Java properties.
static final String NEWLINE
Exposing types describing the underlying platform.
Static enumeration of MachineDataInfo instances used for high performance data size and alignment loo...
static final StaticConfig findCompatible(final MachineDataInfo md)
final StringBuilder toString(StringBuilder sb)
X86_32_ANDROID
Platform.CPUType#X86_32 Android/Bionic
ARM_MIPS_32
Platform.CPUType#ARM or Platform.CPUType#MIPS_32
X86_32_MACOS
Platform.CPUType#X86_32 MacOS (Special case gcc4/OSX)
StaticConfig(final int[] sizes, final int[] alignments)
X86_64_WINDOWS
Platform.CPUType#X86_64 Windows
SPARC_32_SUNOS
Platform.CPUType#SPARC_32 Solaris
static final void validateUniqueMachineDataInfo()
Static's MachineDataInfo shall be unique by the compatible criteria.
X86_32_WINDOWS
Platform.CPUType#X86_32 Windows
X86_32_UNIX
Platform.CPUType#X86_32 Unix
SPARC_32
SPARC 32bit, big endian.
ARM64
ARM64 default (64bit), usually little endian.
MIPS_32
MIPS 32bit, big endian (mips) or little endian (mipsel)
PPC
PPC 32bit default, usually big endian.