GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
SizeThunk.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005 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.cgram.types.TypeComparator.SemanticEqualityOp;
45
46/** Provides a level of indirection between the definition of a type's
47 size and the absolute value of this size. Necessary when
48 generating glue code for two different CPU architectures (e.g.,
49 32-bit and 64-bit) from the same internal representation of the
50 various types involved. */
51public abstract class SizeThunk implements Cloneable, SemanticEqualityOp {
52 /* pp */ static boolean relaxedEqSem = false;
53 private final boolean fixedNativeSize;
54
55 public static void setRelaxedEqualSemanticsTest(final boolean v) {
56 relaxedEqSem = v;
57 }
58
59 // Private constructor because there are only a few of these
60 private SizeThunk(final boolean fixedNativeSize) {
61 this.fixedNativeSize = fixedNativeSize;
62 }
63
64 @Override
65 public Object clone() {
66 try {
67 return super.clone();
68 } catch (final CloneNotSupportedException ex) {
69 throw new InternalError();
70 }
71 }
72
73 public final boolean hasFixedNativeSize() { return fixedNativeSize; }
74
75 public abstract long computeSize(MachineDataInfo machDesc);
76 public abstract long computeAlignment(MachineDataInfo machDesc);
77
78 @Override
79 public final int hashCode() {
80 final int hash = 0x02DEAD6F; // magic hash start
81 return ((hash << 5) - hash) + hashCodeImpl();
82 }
83 /* pp */ abstract int hashCodeImpl();
84
85 @Override
86 public final boolean equals(final Object arg) {
87 if (arg == this) {
88 return true;
89 } else if ( !(arg instanceof SizeThunk) ) {
90 return false;
91 } else {
92 final SizeThunk t = (SizeThunk) arg;
93 return hashCodeImpl() == t.hashCodeImpl();
94 }
95 }
96
97 @Override
98 public final int hashCodeSemantics() {
99 final int hash = 0x01DEAD5F; // magic hash start
100 return ((hash << 5) - hash) + hashCodeSemanticsImpl();
101 }
102 /* pp */ abstract int hashCodeSemanticsImpl();
103
104 @Override
105 public final boolean equalSemantics(final SemanticEqualityOp arg) {
106 if (arg == this) {
107 return true;
108 } else if ( !(arg instanceof SizeThunk) ) {
109 return false;
110 } else {
111 final SizeThunk t = (SizeThunk) arg;
112 return hashCodeSemanticsImpl() == t.hashCodeSemanticsImpl();
113 }
114 }
115
116 static final int magic_int08 = 0x00000010;
117 static final int magic_int16 = 0x00000012;
118 static final int magic_int32 = 0x00000014;
119 static final int magic_intxx = 0x00000016;
120 static final int magic_long64 = 0x00000020;
121 static final int magic_longxx = 0x00000022;
122 static final int magic_float32 = 0x00000030;
123 static final int magic_float64 = 0x00000032;
124 static final int magic_aptr64 = 0x00000040;
125 static final int magic_ops = 0x00010000;
126
127 public static final SizeThunk INT8 = new SizeThunk(true) {
128 @Override
129 public long computeSize(final MachineDataInfo machDesc) {
130 return machDesc.int8SizeInBytes();
131 }
132 @Override
133 public long computeAlignment(final MachineDataInfo machDesc) {
134 return machDesc.int8AlignmentInBytes();
135 }
136 @Override
137 protected int hashCodeImpl() { return 1; }
138 @Override
139 protected int hashCodeSemanticsImpl() { return relaxedEqSem ? magic_int32 : magic_int08; }
140 };
141
142 public static final SizeThunk INT16 = new SizeThunk(true) {
143 @Override
144 public long computeSize(final MachineDataInfo machDesc) {
145 return machDesc.int16SizeInBytes();
146 }
147 @Override
148 public long computeAlignment(final MachineDataInfo machDesc) {
149 return machDesc.int16AlignmentInBytes();
150 }
151 @Override
152 protected int hashCodeImpl() { return 2; }
153 @Override
154 protected int hashCodeSemanticsImpl() { return relaxedEqSem ? magic_int32 : magic_int16; }
155 };
156
157 public static final SizeThunk INT32 = new SizeThunk(true) {
158 @Override
159 public long computeSize(final MachineDataInfo machDesc) {
160 return machDesc.int32SizeInBytes();
161 }
162 @Override
163 public long computeAlignment(final MachineDataInfo machDesc) {
164 return machDesc.int32AlignmentInBytes();
165 }
166 @Override
167 protected int hashCodeImpl() { return 3; }
168 @Override
169 protected int hashCodeSemanticsImpl() { return magic_int32; }
170 };
171
172 public static final SizeThunk INTxx = new SizeThunk(false) {
173 @Override
174 public long computeSize(final MachineDataInfo machDesc) {
175 return machDesc.intSizeInBytes();
176 }
177 @Override
178 public long computeAlignment(final MachineDataInfo machDesc) {
179 return machDesc.intAlignmentInBytes();
180 }
181 @Override
182 protected int hashCodeImpl() { return 4; }
183 @Override
184 protected int hashCodeSemanticsImpl() { return relaxedEqSem ? magic_int32 : magic_intxx; }
185 };
186
187 public static final SizeThunk LONG = new SizeThunk(false) {
188 @Override
189 public long computeSize(final MachineDataInfo machDesc) {
190 return machDesc.longSizeInBytes();
191 }
192 @Override
193 public long computeAlignment(final MachineDataInfo machDesc) {
194 return machDesc.longAlignmentInBytes();
195 }
196 @Override
197 protected int hashCodeImpl() { return 5; }
198 @Override
199 protected int hashCodeSemanticsImpl() { return relaxedEqSem ? magic_long64 : magic_longxx; }
200 };
201
202 public static final SizeThunk INT64 = new SizeThunk(true) {
203 @Override
204 public long computeSize(final MachineDataInfo machDesc) {
205 return machDesc.int64SizeInBytes();
206 }
207 @Override
208 public long computeAlignment(final MachineDataInfo machDesc) {
209 return machDesc.int64AlignmentInBytes();
210 }
211 @Override
212 protected int hashCodeImpl() { return 6; }
213 @Override
214 protected int hashCodeSemanticsImpl() { return magic_long64; }
215 };
216
217 public static final SizeThunk FLOAT = new SizeThunk(true) {
218 @Override
219 public long computeSize(final MachineDataInfo machDesc) {
220 return machDesc.floatSizeInBytes();
221 }
222 @Override
223 public long computeAlignment(final MachineDataInfo machDesc) {
224 return machDesc.floatAlignmentInBytes();
225 }
226 @Override
227 protected int hashCodeImpl() { return 7; }
228 @Override
229 protected int hashCodeSemanticsImpl() { return magic_float32; }
230 };
231
232 public static final SizeThunk DOUBLE = new SizeThunk(true) {
233 @Override
234 public long computeSize(final MachineDataInfo machDesc) {
235 return machDesc.doubleSizeInBytes();
236 }
237 @Override
238 public long computeAlignment(final MachineDataInfo machDesc) {
239 return machDesc.doubleAlignmentInBytes();
240 }
241 @Override
242 protected int hashCodeImpl() { return 8; }
243 @Override
244 protected int hashCodeSemanticsImpl() { return magic_float64; }
245 };
246
247 public static final SizeThunk POINTER = new SizeThunk(false) {
248 @Override
249 public long computeSize(final MachineDataInfo machDesc) {
250 return machDesc.pointerSizeInBytes();
251 }
252 @Override
253 public long computeAlignment(final MachineDataInfo machDesc) {
254 return machDesc.pointerAlignmentInBytes();
255 }
256 @Override
257 protected int hashCodeImpl() { return 9; }
258 @Override
259 protected int hashCodeSemanticsImpl() { return magic_aptr64; }
260 };
261
262 // Factory methods for performing certain limited kinds of
263 // arithmetic on these values
264 public static SizeThunk add(final SizeThunk thunk1,
265 final SizeThunk thunk2) {
266 return new SizeThunk(false) {
267 @Override
268 public long computeSize(final MachineDataInfo machDesc) {
269 return thunk1.computeSize(machDesc) + thunk2.computeSize(machDesc);
270 }
271 @Override
272 public long computeAlignment(final MachineDataInfo machDesc) {
273 final long thunk1A = thunk1.computeAlignment(machDesc);
274 final long thunk2A = thunk2.computeAlignment(machDesc);
275 return ( thunk1A > thunk2A ) ? thunk1A : thunk2A ;
276 }
277 @Override
278 protected int hashCodeImpl() {
279 // 31 * x == (x << 5) - x
280 int hash = 31 + 10;
281 hash = ((hash << 5) - hash) + ( null != thunk1 ? thunk1.hashCode() : 0 );
282 return ((hash << 5) - hash) + ( null != thunk2 ? thunk2.hashCode() : 0 );
283 }
284 @Override
285 protected int hashCodeSemanticsImpl() { return magic_ops + 1; }
286 };
287 }
288
289 public static SizeThunk mul(final SizeThunk thunk1,
290 final SizeThunk thunk2) {
291 return new SizeThunk(false) {
292 @Override
293 public long computeSize(final MachineDataInfo machDesc) {
294 return thunk1.computeSize(machDesc) * thunk2.computeSize(machDesc);
295 }
296 @Override
297 public long computeAlignment(final MachineDataInfo machDesc) {
298 final long thunk1A = thunk1.computeAlignment(machDesc);
299 final long thunk2A = thunk2.computeAlignment(machDesc);
300 return ( thunk1A > thunk2A ) ? thunk1A : thunk2A ;
301 }
302 @Override
303 protected int hashCodeImpl() {
304 // 31 * x == (x << 5) - x
305 int hash = 31 + 11;
306 hash = ((hash << 5) - hash) + ( null != thunk1 ? thunk1.hashCode() : 0 );
307 return ((hash << 5) - hash) + ( null != thunk2 ? thunk2.hashCode() : 0 );
308 }
309 @Override
310 protected int hashCodeSemanticsImpl() { return magic_ops + 2; }
311 };
312 }
313
314 public static SizeThunk align(final SizeThunk offsetThunk,
315 final SizeThunk alignmentThunk) {
316 return new SizeThunk(false) {
317 @Override
318 public long computeSize(final MachineDataInfo machDesc) {
319 /**
320 * padding = ( alignment - ( net_size % alignment ) ) % alignment ;
321 * aligned_size = net_size + padding ;
322 *
323 * With x % 2n == x & (2n - 1)
324 *
325 * Either:
326 * remainder = net_size & ( alignment - 1 )
327 * padding = ( remainder > 0 ) ? alignment - remainder ;
328 * aligned_size = net_size + padding ;
329 *
330 * Or:
331 * padding = ( alignment - ( net_size & ( alignment - 1 ) ) ) & ( alignment - 1 );
332 * aligned_size = net_size + padding ;
333 *
334 */
335
336 final long net_size = offsetThunk.computeSize(machDesc);
337 final long alignment = alignmentThunk.computeAlignment(machDesc);
338
339 /**
340 final long remainder = net_size & ( alignment - 1 ) ;
341 final long padding = (remainder > 0) ? alignment - remainder : 0;
342 */
343 final long padding = ( alignment - ( net_size & ( alignment - 1 ) ) ) & ( alignment - 1 );
344 return net_size + padding;
345 }
346
347 @Override
348 public long computeAlignment(final MachineDataInfo machDesc) {
349 final long thunk1A = offsetThunk.computeAlignment(machDesc);
350 final long thunk2A = alignmentThunk.computeAlignment(machDesc);
351 return ( thunk1A > thunk2A ) ? thunk1A : thunk2A ;
352 }
353 @Override
354 protected int hashCodeImpl() {
355 // 31 * x == (x << 5) - x
356 int hash = 31 + 12;
357 hash = ((hash << 5) - hash) + ( null != offsetThunk ? offsetThunk.hashCode() : 0 );
358 return ((hash << 5) - hash) + ( null != alignmentThunk ? alignmentThunk.hashCode() : 0 );
359 }
360 @Override
361 protected int hashCodeSemanticsImpl() { return magic_ops + 3; }
362 };
363 }
364
365 public static SizeThunk max(final SizeThunk thunk1,
366 final SizeThunk thunk2) {
367 return new SizeThunk(false) {
368 @Override
369 public long computeSize(final MachineDataInfo machDesc) {
370 return Math.max(thunk1.computeSize(machDesc), thunk2.computeSize(machDesc));
371 }
372 @Override
373 public long computeAlignment(final MachineDataInfo machDesc) {
374 final long thunk1A = thunk1.computeAlignment(machDesc);
375 final long thunk2A = thunk2.computeAlignment(machDesc);
376 return ( thunk1A > thunk2A ) ? thunk1A : thunk2A ;
377 }
378 @Override
379 protected int hashCodeImpl() {
380 // 31 * x == (x << 5) - x
381 int hash = 31 + 13;
382 hash = ((hash << 5) - hash) + ( null != thunk1 ? thunk1.hashCode() : 0 );
383 return ((hash << 5) - hash) + ( null != thunk2 ? thunk2.hashCode() : 0 );
384 }
385 @Override
386 protected int hashCodeSemanticsImpl() { return magic_ops + 4; }
387 };
388 }
389
390 public static SizeThunk constant(final int constant) {
391 return new SizeThunk(false) {
392 @Override
393 public long computeSize(final MachineDataInfo machDesc) {
394 return constant;
395 }
396 @Override
397 public long computeAlignment(final MachineDataInfo machDesc) {
398 return 1; // no alignment for constants
399 }
400 @Override
401 protected int hashCodeImpl() {
402 // 31 * x == (x << 5) - x
403 final int hash = 31 + 14;
404 return ((hash << 5) - hash) + constant;
405 }
406 @Override
407 protected int hashCodeSemanticsImpl() { return magic_ops + 5; }
408 };
409 }
410}
Machine data description for alignment and size onle, see com.jogamp.gluegen.
Provides a level of indirection between the definition of a type's size and the absolute value of thi...
Definition: SizeThunk.java:51
final boolean equals(final Object arg)
Definition: SizeThunk.java:86
abstract long computeAlignment(MachineDataInfo machDesc)
static SizeThunk max(final SizeThunk thunk1, final SizeThunk thunk2)
Definition: SizeThunk.java:365
final boolean equalSemantics(final SemanticEqualityOp arg)
Semantic equality test for Types exclusive its given name.
Definition: SizeThunk.java:105
abstract long computeSize(MachineDataInfo machDesc)
static SizeThunk add(final SizeThunk thunk1, final SizeThunk thunk2)
Definition: SizeThunk.java:264
static void setRelaxedEqualSemanticsTest(final boolean v)
Definition: SizeThunk.java:55
static SizeThunk constant(final int constant)
Definition: SizeThunk.java:390
static SizeThunk mul(final SizeThunk thunk1, final SizeThunk thunk2)
Definition: SizeThunk.java:289
static SizeThunk align(final SizeThunk offsetThunk, final SizeThunk alignmentThunk)
Definition: SizeThunk.java:314
final int hashCodeSemantics()
Semantic hashcode for Types exclusive its given name.
Definition: SizeThunk.java:98
Supports semantic equality and hash functions for types.