@@ -55,6 +55,36 @@ mod test;
55
55
56
56
use core:: { mem, ptr} ;
57
57
58
+ mod sealed {
59
+ pub trait Sealed { }
60
+ }
61
+
62
+ /// Trait for machine word types.
63
+ ///
64
+ /// This trait is implemented by unsigned integers representing common machine
65
+ /// word sizes. It can not be implemented by the user.
66
+ ///
67
+ /// Types implementing this trait can be used by the [`init_data`] and
68
+ /// [`zero_bss`] functions. For that to be sound, all bit patterns need to be
69
+ /// valid for the type, the type must implement `Copy`, and the type must not
70
+ /// be zero-sized.
71
+ ///
72
+ /// [`init_data`]: fn.init_data.html
73
+ /// [`zero_bss`]: fn.zero_bss.html
74
+ pub unsafe trait Word : sealed:: Sealed + Copy { }
75
+
76
+ impl sealed:: Sealed for u8 { }
77
+ impl sealed:: Sealed for u16 { }
78
+ impl sealed:: Sealed for u32 { }
79
+ impl sealed:: Sealed for u64 { }
80
+ impl sealed:: Sealed for u128 { }
81
+
82
+ unsafe impl Word for u8 { }
83
+ unsafe impl Word for u16 { }
84
+ unsafe impl Word for u32 { }
85
+ unsafe impl Word for u64 { }
86
+ unsafe impl Word for u128 { }
87
+
58
88
/// Initializes the `.data` section.
59
89
///
60
90
/// # Arguments
@@ -67,18 +97,17 @@ use core::{mem, ptr};
67
97
///
68
98
/// # Safety
69
99
///
70
- /// - Must be called exactly once
71
- /// - `mem::size_of::<T>()` must be non-zero
72
- /// - `edata >= sdata`
100
+ /// - Must be called exactly once, before the application has started.
101
+ /// - `edata >= sdata`.
73
102
/// - The `sdata -> edata` region must not overlap with the `sidata -> ...`
74
- /// region
103
+ /// region.
75
104
/// - `sdata`, `edata` and `sidata` must be `T` aligned.
76
105
pub unsafe fn init_data < T > (
77
106
mut sdata : * mut T ,
78
107
edata : * mut T ,
79
108
mut sidata : * const T ,
80
109
) where
81
- T : Copy ,
110
+ T : Word ,
82
111
{
83
112
while sdata < edata {
84
113
ptr:: write ( sdata, ptr:: read ( sidata) ) ;
@@ -98,13 +127,12 @@ pub unsafe fn init_data<T>(
98
127
///
99
128
/// # Safety
100
129
///
101
- /// - Must be called exactly once
102
- /// - `mem::size_of::<T>()` must be non-zero
103
- /// - `ebss >= sbss`
130
+ /// - Must be called exactly once, before the application has started.
131
+ /// - `ebss >= sbss`.
104
132
/// - `sbss` and `ebss` must be `T` aligned.
105
133
pub unsafe fn zero_bss < T > ( mut sbss : * mut T , ebss : * mut T )
106
134
where
107
- T : Copy ,
135
+ T : Word ,
108
136
{
109
137
while sbss < ebss {
110
138
// NOTE(volatile) to prevent this from being transformed into `memclr`
0 commit comments