Skip to content

Commit f3e0972

Browse files
authored
Merge pull request #5 from taiki-e/repr-transparent
Add #[repr(C, align(N))] to atomic types
2 parents 2c54a1c + 06ef7dc commit f3e0972

File tree

1 file changed

+30
-7
lines changed

1 file changed

+30
-7
lines changed

src/lib.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use core::fmt;
7878
/// A boolean type which can be safely shared between threads.
7979
///
8080
/// This type has the same in-memory representation as a `bool`.
81+
#[repr(C, align(1))]
8182
pub struct AtomicBool {
8283
v: UnsafeCell<u8>,
8384
}
@@ -95,6 +96,9 @@ unsafe impl Sync for AtomicBool {}
9596
/// A raw pointer type which can be safely shared between threads.
9697
///
9798
/// This type has the same in-memory representation as a `*mut T`.
99+
#[cfg_attr(target_pointer_width = "16", repr(C, align(2)))]
100+
#[cfg_attr(target_pointer_width = "32", repr(C, align(4)))]
101+
#[cfg_attr(target_pointer_width = "64", repr(C, align(8)))]
98102
pub struct AtomicPtr<T> {
99103
p: UnsafeCell<*mut T>,
100104
}
@@ -422,10 +426,11 @@ impl<T> AtomicPtr<T> {
422426
}
423427

424428
macro_rules! atomic_int {
425-
($int_type:ident $atomic_type:ident $atomic_init:ident $asm_suffix:expr) => {
429+
($int_type:ident $atomic_type:ident $atomic_init:ident $asm_suffix:literal $align:literal) => {
426430
/// An integer type which can be safely shared between threads.
427431
///
428432
/// This type has the same in-memory representation as the underlying integer type.
433+
#[repr(C, align($align))]
429434
pub struct $atomic_type {
430435
v: UnsafeCell<$int_type>,
431436
}
@@ -718,27 +723,45 @@ macro_rules! atomic_int {
718723
}
719724

720725
atomic_int! {
721-
i8 AtomicI8 ATOMIC_I8_INIT ".b"
726+
i8 AtomicI8 ATOMIC_I8_INIT ".b" 1
722727
}
723728

724729
atomic_int! {
725-
u8 AtomicU8 ATOMIC_U8_INIT ".b"
730+
u8 AtomicU8 ATOMIC_U8_INIT ".b" 1
726731
}
727732

728733
atomic_int! {
729-
i16 AtomicI16 ATOMIC_I16_INIT ".w"
734+
i16 AtomicI16 ATOMIC_I16_INIT ".w" 2
730735
}
731736

732737
atomic_int! {
733-
u16 AtomicU16 ATOMIC_U16_INIT ".w"
738+
u16 AtomicU16 ATOMIC_U16_INIT ".w" 2
734739
}
735740

741+
#[cfg(target_pointer_width = "16")]
736742
atomic_int! {
737-
isize AtomicIsize ATOMIC_ISIZE_INIT ".w"
743+
isize AtomicIsize ATOMIC_ISIZE_INIT ".w" 2
744+
}
745+
#[cfg(target_pointer_width = "32")]
746+
atomic_int! {
747+
isize AtomicIsize ATOMIC_ISIZE_INIT ".w" 4
748+
}
749+
#[cfg(target_pointer_width = "64")]
750+
atomic_int! {
751+
isize AtomicIsize ATOMIC_ISIZE_INIT ".w" 8
738752
}
739753

754+
#[cfg(target_pointer_width = "16")]
755+
atomic_int! {
756+
usize AtomicUsize ATOMIC_USIZE_INIT ".w" 2
757+
}
758+
#[cfg(target_pointer_width = "32")]
759+
atomic_int! {
760+
usize AtomicUsize ATOMIC_USIZE_INIT ".w" 4
761+
}
762+
#[cfg(target_pointer_width = "64")]
740763
atomic_int! {
741-
usize AtomicUsize ATOMIC_USIZE_INIT ".w"
764+
usize AtomicUsize ATOMIC_USIZE_INIT ".w" 8
742765
}
743766

744767
/// Atomic arithmetic and bitwise operations implemented for numerical types. Each operation is

0 commit comments

Comments
 (0)