MCPcopy Create free account
hub / github.com/java-native-access/jna / getNativeAlignment

Method getNativeAlignment

src/com/sun/jna/Structure.java:1554–1609  ·  view source on GitHub ↗
(Class<?> type, Object value, boolean isFirstElement)

Source from the content-addressed store, hash-verified

1552 // TODO: move this into a native call which detects default alignment
1553 // automatically
1554 protected int getNativeAlignment(Class<?> type, Object value, boolean isFirstElement) {
1555 int alignment = 1;
1556 if (NativeMapped.class.isAssignableFrom(type)) {
1557 NativeMappedConverter tc = NativeMappedConverter.getInstance(type);
1558 type = tc.nativeType();
1559 value = tc.toNative(value, new ToNativeContext());
1560 }
1561 int size = Native.getNativeSize(type, value);
1562 if (type.isPrimitive() || Long.class == type || Integer.class == type
1563 || Short.class == type || Character.class == type
1564 || Byte.class == type || Boolean.class == type
1565 || Float.class == type || Double.class == type) {
1566 alignment = size;
1567 }
1568 else if ((Pointer.class.isAssignableFrom(type) && !Function.class.isAssignableFrom(type))
1569 || (Platform.HAS_BUFFERS && Buffer.class.isAssignableFrom(type))
1570 || Callback.class.isAssignableFrom(type)
1571 || WString.class == type
1572 || String.class == type) {
1573 alignment = Native.POINTER_SIZE;
1574 }
1575 else if (Structure.class.isAssignableFrom(type)) {
1576 if (ByReference.class.isAssignableFrom(type)) {
1577 alignment = Native.POINTER_SIZE;
1578 }
1579 else {
1580 if (value == null)
1581 value = newInstance((Class<? extends Structure>) type, PLACEHOLDER_MEMORY);
1582 alignment = ((Structure)value).getStructAlignment();
1583 }
1584 }
1585 else if (type.isArray()) {
1586 alignment = getNativeAlignment(type.getComponentType(), null, isFirstElement);
1587 }
1588 else {
1589 throw new IllegalArgumentException("Type " + type + " has unknown "
1590 + "native alignment");
1591 }
1592 if (actualAlignType == ALIGN_NONE) {
1593 alignment = 1;
1594 }
1595 else if (actualAlignType == ALIGN_MSVC) {
1596 alignment = Math.min(8, alignment);
1597 }
1598 else if (actualAlignType == ALIGN_GNUC) {
1599 // NOTE this is published ABI for 32-bit gcc/linux/x86, osx/x86,
1600 // and osx/ppc. osx/ppc special-cases the first element
1601 if (!isFirstElement || !(Platform.isMac() && Platform.isPPC())) {
1602 alignment = Math.min(Native.MAX_ALIGNMENT, alignment);
1603 }
1604 if (!isFirstElement && Platform.isAIX() && (type == double.class || type == Double.class)) {
1605 alignment = 4;
1606 }
1607 }
1608 return alignment;
1609 }
1610
1611 /**

Callers 1

deriveLayoutMethod · 0.95

Calls 10

getInstanceMethod · 0.95
nativeTypeMethod · 0.95
toNativeMethod · 0.95
getNativeSizeMethod · 0.95
newInstanceMethod · 0.95
isMacMethod · 0.95
isPPCMethod · 0.95
isAIXMethod · 0.95
getStructAlignmentMethod · 0.80
isArrayMethod · 0.80

Tested by

no test coverage detected