8000 Use _Alignof to query alignments in the struct module · Issue #132868 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Use _Alignof to query alignments in the struct module #132868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skirpichev opened this issue Apr 24, 2025 · 2 comments
Closed

Use _Alignof to query alignments in the struct module #132868

skirpichev opened this issue Apr 24, 2025 · 2 comments
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement

Comments

@skirpichev
Copy link
Contributor
skirpichev commented Apr 24, 2025

Feature or enhancement

Proposal:

The struct module uses pre-C11 hacks to determine alignment for types:

cpython/Modules/_struct.c

Lines 79 to 106 in 5808889

/* Define various structs to figure out the alignments of types */
typedef struct { char c; short x; } st_short;
typedef struct { char c; int x; } st_int;
typedef struct { char c; long x; } st_long;
typedef struct { char c; float x; } st_float;
typedef struct { char c; double x; } st_double;
#ifdef Py_HAVE_C_COMPLEX
typedef struct { char c; float complex x; } st_float_complex;
typedef struct { char c; double complex x; } st_double_complex;
#endif
typedef struct { char c; void *x; } st_void_p;
typedef struct { char c; size_t x; } st_size_t;
typedef struct { char c; _Bool x; } st_bool;
#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
#define INT_ALIGN (sizeof(st_int) - sizeof(int))
#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
#ifdef Py_HAVE_C_COMPLEX
# define FLOAT_COMPLEX_ALIGN (sizeof(st_float_complex) - sizeof(float complex))
# define DOUBLE_COMPLEX_ALIGN (sizeof(st_double_complex) - sizeof(double complex))
#endif
#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
#define SIZE_T_ALIGN (sizeof(st_size_t) - sizeof(size_t))
#define BOOL_ALIGN (sizeof(st_bool) - sizeof(_Bool))

Now we can use _Alignof operator, as C11-compatible compiler is a build requireent.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@skirpichev skirpichev added the type-feature A feature request or enhancement label Apr 24, 2025
@skirpichev skirpichev self-assigned this Apr 24, 2025
@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) extension-modules C modules in the Modules dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Apr 24, 2025
@picnixz
Copy link
Member
picnixz commented Apr 24, 2025

Considering I did use _Alignof in my recent PR (#132428), I support this proposal.

@skirpichev
Copy link
Contributor Author

PR is ready: #132872

@picnixz picnixz closed this as completed Apr 25, 2025
@skirpichev skirpichev removed their assignment Apr 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants
0