Skip to content

Conversation

@patstew
Copy link
Contributor

@patstew patstew commented Nov 16, 2016

Previously all types are marked unaligned in buffer format strings, now we test for alignment before adding the '=' marker.

It's not a problem to mark all types as unaligned per-se, it's equivalent because of the explicit padding, but this goes partway to allowing comparison to format strings from other sources, e.g. numpy. It's much easier to test for alignment here rather than parsing the string and trying to work it out. In combination with my other PR #488 , it will be possible to construct a vector<struct> from a structured array without casting in some cases (namely where the member names are identical and there's no multibyte padding).

@wjakob
Copy link
Member

wjakob commented Nov 16, 2016

cc @aldanor

for (auto& field : ordered_fields) {
if (field.offset > offset)
oss << (field.offset - offset) << 'x';
// note that '=' is required to cover the case of unaligned fields
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but maybe this comment line should be updated to reflect what's happening below

@aldanor
Copy link
Member

aldanor commented Nov 21, 2016

This looks ok, I guess, and doesn't seem to break anything (I don't see much value since comparing format strings directly is a bad idea, but it doesn't harm to have aligned fields mark as aligned).

#define PYBIND11_FIELD_DESCRIPTOR_EX(T, Field, Name) \
::pybind11::detail::field_descriptor { \
Name, offsetof(T, Field), sizeof(decltype(std::declval<T>().Field)), \
std::alignment_of<decltype(std::declval<T>().Field)>::value, \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to use the alignof keyword? (AFAIK the template version only exists for historical reasons.)

Minor formatting note: see the position of the continuation \ in comparison to the lines above and below.

Previously all types are marked unaligned in buffer format strings,
now we test for alignment before adding the '=' marker.
@wjakob wjakob merged commit 47681c1 into pybind:master Nov 22, 2016
@patstew patstew deleted the buffer_alignment branch November 22, 2016 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants