Skip to content

Commit e427747

Browse files
committed
core: Split sys::align_of into min_align_of, pref_align_of
1 parent f4f909b commit e427747

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

src/libcore/sys.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
export type_desc;
44
export get_type_desc;
55
export size_of;
6-
export align_of;
6+
export min_align_of;
7+
export pref_align_of;
78
export refcount;
89
export log_str;
910

@@ -43,8 +44,21 @@ fn size_of<T>() -> uint unsafe {
4344
rusti::size_of::<T>()
4445
}
4546

46-
#[doc = "Returns the alignment of a type"]
47-
fn align_of<T>() -> uint unsafe {
47+
#[doc = "
48+
Returns the ABI-required minimum alignment of a type
49+
50+
This is the alignment used for struct fields. It may be smaller
51+
than the preferred alignment.
52+
"]
53+
fn min_align_of<T>() -> uint unsafe {
54+
// FIXME: use rusti::min_align_of after snapshot
55+
// rusti::align_of::<T>()
56+
fail "FIXME: uncomment the above line to use min_align_of";
57+
}
58+
59+
#[doc = "Returns the preferred alignment of a type"]
60+
fn pref_align_of<T>() -> uint unsafe {
61+
// FIXME: use rusti::pref_align_of after snapshot
4862
rusti::align_of::<T>()
4963
}
5064

@@ -85,24 +99,24 @@ mod tests {
8599

86100
#[test]
87101
fn align_of_basic() {
88-
assert align_of::<u8>() == 1u;
89-
assert align_of::<u16>() == 2u;
90-
assert align_of::<u32>() == 4u;
102+
assert pref_align_of::<u8>() == 1u;
103+
assert pref_align_of::<u16>() == 2u;
104+
assert pref_align_of::<u32>() == 4u;
91105
}
92106

93107
#[test]
94108
#[cfg(target_arch = "x86")]
95109
#[cfg(target_arch = "arm")]
96110
fn align_of_32() {
97-
assert align_of::<uint>() == 4u;
98-
assert align_of::<*uint>() == 4u;
111+
assert pref_align_of::<uint>() == 4u;
112+
assert pref_align_of::<*uint>() == 4u;
99113
}
100114

101115
#[test]
102116
#[cfg(target_arch = "x86_64")]
103117
fn align_of_64() {
104-
assert align_of::<uint>() == 8u;
105-
assert align_of::<*uint>() == 8u;
118+
assert pref_align_of::<uint>() == 8u;
119+
assert pref_align_of::<*uint>() == 8u;
106120
}
107121
}
108122

src/rustc/middle/trans/debuginfo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ fn create_boxed_type(cx: @crate_ctxt, outer: ty::t, _inner: ty::t,
451451
ast::ty_uint(ast::ty_u), span);
452452
let scx = create_structure(file_node, ty_to_str(cx.tcx, outer), 0);
453453
add_member(scx, "refcnt", 0, sys::size_of::<uint>() as int,
454-
sys::align_of::<uint>() as int, refcount_type.node);
454+
sys::min_align_of::<uint>() as int, refcount_type.node);
455455
add_member(scx, "boxed", 0, 8, //XXX member_size_and_align(??)
456456
8, //XXX just a guess
457457
boxed.node);
@@ -502,17 +502,17 @@ fn create_vec(cx: @crate_ctxt, vec_t: ty::t, elem_t: ty::t,
502502
let size_t_type = create_basic_type(cx, ty::mk_uint(cx.tcx),
503503
ast::ty_uint(ast::ty_u), vec_ty_span);
504504
add_member(scx, "fill", 0, sys::size_of::<libc::size_t>() as int,
505-
sys::align_of::<libc::size_t>() as int, size_t_type.node);
505+
sys::min_align_of::<libc::size_t>() as int, size_t_type.node);
506506
add_member(scx, "alloc", 0, sys::size_of::<libc::size_t>() as int,
507-
sys::align_of::<libc::size_t>() as int, size_t_type.node);
507+
sys::min_align_of::<libc::size_t>() as int, size_t_type.node);
508508
let subrange = llmdnode([lltag(SubrangeTag), lli64(0), lli64(0)]);
509509
let (arr_size, arr_align) = size_and_align_of(cx, elem_t);
510510
let data_ptr = create_composite_type(ArrayTypeTag, "", file_node.node, 0,
511511
arr_size, arr_align, 0,
512512
option::some(elem_ty_md.node),
513513
option::some([subrange]));
514514
add_member(scx, "data", 0, 0, // clang says the size should be 0
515-
sys::align_of::<u8>() as int, data_ptr);
515+
sys::min_align_of::<u8>() as int, data_ptr);
516516
let llnode = finish_structure(scx);
517517
ret @{node: llnode, data: {hash: ty::type_id(vec_t)}};
518518
}

src/test/run-pass/rec-align-32-bit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() {
2222
// Send it through the shape code
2323
let y = #fmt["%?", x];
2424

25-
#debug("align inner = %?", sys::align_of::<inner>()); // 8
25+
#debug("align inner = %?", sys::pref_align_of::<inner>()); // 8
2626
#debug("size outer = %?", sys::size_of::<outer>()); // 12
2727
#debug("y = %s", y); // (22, (0))
2828

0 commit comments

Comments
 (0)