Last active
June 24, 2025 17:42
-
-
Save kassane/69a6f777bf27d662fc7556f6a866892f to your computer and use it in GitHub Desktop.
Packed struct (C, C++, D, Rust, Zig)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stddef.h> | |
#include <stdio.h> | |
#include <stdalign.h> | |
#pragma pack(push, 4) | |
struct vm_region_submap_short_info_64 { | |
int protection; | |
int max_protection; | |
unsigned int inheritance; | |
unsigned long long offset; | |
unsigned int user_tag; | |
unsigned int ref_count; | |
unsigned short shadow_depth; | |
unsigned char external_pager; | |
}; | |
#pragma pack(pop) | |
#define X(a) printf("%d: %zu/%zu\n", (int)offsetof(struct vm_region_submap_short_info_64, a), sizeof(((struct vm_region_submap_short_info_64){0}).a), alignof(((struct vm_region_submap_short_info_64){0}).a)) | |
void a(void) { | |
X(protection); | |
X(max_protection); | |
X(inheritance); | |
X(offset); | |
X(user_tag); | |
X(ref_count); | |
X(shadow_depth); | |
X(external_pager); | |
} | |
int main(void) { | |
a(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
#include <cstddef> | |
#include <cstdio> | |
#pragma pack(push, 4) | |
struct vm_region_submap_short_info_64 { | |
int protection; | |
int max_protection; | |
unsigned int inheritance; | |
unsigned long long offset; | |
unsigned int user_tag; | |
unsigned int ref_count; | |
unsigned short shadow_depth; | |
unsigned char external_pager; | |
}; | |
#pragma pack(pop) | |
#define X(a) printf("%d: %zd/%zd\n", (int)offsetof(vm_region_submap_short_info_64, a), sizeof(vm_region_submap_short_info_64::a), alignof(vm_region_submap_short_info_64::a)) | |
void a() | |
{ | |
X(protection); | |
X(max_protection); | |
X(inheritance); | |
X(offset); | |
X(user_tag); | |
X(ref_count); | |
X(shadow_depth); | |
X(external_pager); | |
} | |
int main() { | |
a(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module vm_region_info; | |
import core.stdc.stdio : printf; | |
struct vm_region_submap_short_info_64 { | |
align(1): | |
int protection; | |
int max_protection; | |
uint inheritance; | |
ulong offset; | |
uint user_tag; | |
uint ref_count; | |
ushort shadow_depth; | |
ubyte external_pager; | |
} | |
template FieldType(string field) { | |
static if (field == "protection" || field == "max_protection" || field == "inheritance" || | |
field == "user_tag" || field == "ref_count") | |
alias FieldType = int; | |
else static if (field == "offset") | |
alias FieldType = ulong; | |
else static if (field == "shadow_depth") | |
alias FieldType = ushort; | |
else static if (field == "external_pager") | |
alias FieldType = ubyte; | |
else | |
static assert(0, "Unknown field: " ~ field); | |
} | |
void printFieldInfo(string field)() @nogc { | |
alias M = __traits(getMember, vm_region_submap_short_info_64, field); | |
printf("%zu: %zu/%zu\n", M.offsetof, M.sizeof, FieldType!field.alignof); | |
} | |
void a() @nogc { | |
static foreach (field; __traits(allMembers, vm_region_submap_short_info_64)) { | |
printFieldInfo!field(); | |
} | |
} | |
void main() @nogc { | |
a(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::mem::{size_of, align_of, offset_of}; | |
#[repr(packed(4))] | |
#[derive(Default)] | |
struct VmRegionSubmapShortInfo64 { | |
protection: i32, | |
max_protection: i32, | |
inheritance: u32, | |
offset: u64, | |
user_tag: u32, | |
ref_count: u32, | |
shadow_depth: u16, | |
external_pager: u8, | |
} | |
macro_rules! print_field_info { | |
($struct:ty, protection) => { | |
println!( | |
"{}: {}/{}", | |
offset_of!($struct, protection), | |
size_of::<<$struct as FieldType>::Protection>(), | |
align_of::<<$struct as FieldType>::Protection>() | |
); | |
}; | |
($struct:ty, max_protection) => { | |
println!( | |
"{}: {}/{}", | |
offset_of!($struct, max_protection), | |
size_of::<<$struct as FieldType>::MaxProtection>(), | |
align_of::<<$struct as FieldType>::MaxProtection>() | |
); | |
}; | |
($struct:ty, inheritance) => { | |
println!( | |
"{}: {}/{}", | |
offset_of!($struct, inheritance), | |
size_of::<<$struct as FieldType>::Inheritance>(), | |
align_of::<<$struct as FieldType>::Inheritance>() | |
); | |
}; | |
($struct:ty, offset) => { | |
println!( | |
"{}: {}/{}", | |
offset_of!($struct, offset), | |
size_of::<<$struct as FieldType>::Offset>(), | |
align_of::<<$struct as FieldType>::Offset>() | |
); | |
}; | |
($struct:ty, user_tag) => { | |
println!( | |
"{}: {}/{}", | |
offset_of!($struct, user_tag), | |
size_of::<<$struct as FieldType>::UserTag>(), | |
align_of::<<$struct as FieldType>::UserTag>() | |
); | |
}; | |
($struct:ty, ref_count) => { | |
println!( | |
"{}: {}/{}", | |
offset_of!($struct, ref_count), | |
size_of::<<$struct as FieldType>::RefCount>(), | |
align_of::<<$struct as FieldType>::RefCount>() | |
); | |
}; | |
($struct:ty, shadow_depth) => { | |
println!( | |
"{}: {}/{}", | |
offset_of!($struct, shadow_depth), | |
size_of::<<$struct as FieldType>::ShadowDepth>(), | |
align_of::<<$struct as FieldType>::ShadowDepth>() | |
); | |
}; | |
($struct:ty, external_pager) => { | |
println!( | |
"{}: {}/{}", | |
offset_of!($struct, external_pager), | |
size_of::<<$struct as FieldType>::ExternalPager>(), | |
align_of::<<$struct as FieldType>::ExternalPager>() | |
); | |
}; | |
} | |
trait FieldType { | |
type Protection; | |
type MaxProtection; | |
type Inheritance; | |
type Offset; | |
type UserTag; | |
type RefCount; | |
type ShadowDepth; | |
type ExternalPager; | |
} | |
impl FieldType for VmRegionSubmapShortInfo64 { | |
type Protection = i32; | |
type MaxProtection = i32; | |
type Inheritance = u32; | |
type Offset = u64; | |
type UserTag = u32; | |
type RefCount = u32; | |
type ShadowDepth = u16; | |
type ExternalPager = u8; | |
} | |
fn a() { | |
print_field_info!(VmRegionSubmapShortInfo64, protection); | |
print_field_info!(VmRegionSubmapShortInfo64, max_protection); | |
print_field_info!(VmRegionSubmapShortInfo64, inheritance); | |
print_field_info!(VmRegionSubmapShortInfo64, offset); | |
print_field_info!(VmRegionSubmapShortInfo64, user_tag); | |
print_field_info!(VmRegionSubmapShortInfo64, ref_count); | |
print_field_info!(VmRegionSubmapShortInfo64, shadow_depth); | |
print_field_info!(VmRegionSubmapShortInfo64, external_pager); | |
} | |
fn main() { | |
a(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const VmRegionSubmapShortInfo64 = packed struct { | |
protection: i32, | |
max_protection: i32, | |
inheritance: u32, | |
offset: u64, | |
user_tag: u32, | |
ref_count: u32, | |
shadow_depth: u16, | |
external_pager: u8, | |
}; | |
fn printFieldInfo(comptime T: type, comptime field: []const u8) void { | |
std.debug.print("{}: {}/{}\n", .{ | |
@offsetOf(T, field), | |
@sizeOf(@TypeOf(@field(std.mem.zeroes(T), field))), | |
@alignOf(@TypeOf(@field(std.mem.zeroes(T), field))), | |
}); | |
} | |
fn a() void { | |
printFieldInfo(VmRegionSubmapShortInfo64, "protection"); | |
printFieldInfo(VmRegionSubmapShortInfo64, "max_protection"); | |
printFieldInfo(VmRegionSubmapShortInfo64, "inheritance"); | |
printFieldInfo(VmRegionSubmapShortInfo64, "offset"); | |
printFieldInfo(VmRegionSubmapShortInfo64, "user_tag"); | |
printFieldInfo(VmRegionSubmapShortInfo64, "ref_count"); | |
printFieldInfo(VmRegionSubmapShortInfo64, "shadow_depth"); | |
printFieldInfo(VmRegionSubmapShortInfo64, "external_pager"); | |
} | |
pub fn main() void { | |
a(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output test: https://godbolt.org/z/3EoK3741v
reference: dlang/dmd#21468 (comment)