1#![doc = include_str!("../README.md")]
2#![allow(unsafe_code)]
3
4pub mod declarative {
11 pub use crate::__in_section_parse as in_section;
12 pub use crate::__section_parse as section;
13}
14
15#[doc(hidden)]
16pub mod __support {
17 pub use crate::__add_section_link_attribute as add_section_link_attribute;
18 pub use crate::__def_section_name as def_section_name;
19 pub use crate::__get_section as get_section;
20 pub use crate::__in_section_crate as in_section_crate;
21 pub use crate::__in_section_parse as in_section_parse;
22 pub use crate::__section_parse as section_parse;
23
24 #[cfg(feature = "proc_macro")]
25 pub use link_section_proc_macro::hash;
26 #[cfg(feature = "proc_macro")]
27 pub use link_section_proc_macro::ident_concat;
28
29 #[macro_export]
31 #[doc(hidden)]
32 macro_rules! __def_section_name {
33 (
34 {$(
35 $__section:ident $__type:ident => $__prefix:tt __ $__suffix:tt;
36 )*}
37 AUXILIARY = $__aux_sep:literal;
38 MAX_LENGTH = $__max_length:literal;
39 HASH_LENGTH = $__hash_length:literal;
40 VALID_SECTION_CHARS = $__valid_section_chars:literal;
41 ) => {
42 #[macro_export]
44 #[doc(hidden)]
45 macro_rules! __section_name {
46 $(
47 (raw $__section $__type $name:ident) => {
48 concat!(concat! $__prefix, stringify!($name), concat! $__suffix);
49 };
50 (raw $__section $__type $name:ident $aux:ident) => {
51 concat!(concat! $__prefix, stringify!($name), $__aux_sep, stringify!($aux), concat! $__suffix);
52 };
53 ($pattern:tt $__section $__type $name:ident) => {
54 $crate::__support::hash!($pattern ($__prefix) $name ($__suffix) $__hash_length $__max_length $__valid_section_chars);
55 };
56 ($pattern:tt $__section $__type $name:ident $aux:ident) => {
57 $crate::__support::hash!($pattern ($__prefix) ($name $__aux_sep $aux) ($__suffix) $__hash_length $__max_length $__valid_section_chars);
58 };
59 )*
60 ($pattern:tt $unknown_section:ident $unknown_type:ident $name:ident) => {
61 const _: () = {
62 compile_error!("Unknown section type: `{}`/`{}`", stringify!($unknown_section), stringify!($unknown_type));
63 };
64 };
65 }
66 };
67 }
68
69 #[cfg(feature = "proc_macro")]
70 #[doc(hidden)]
71 #[macro_export]
72 macro_rules! __add_section_link_attribute(
73 ($section:ident $type:ident $name:ident $($aux:ident)? #[$attr:ident = __] $item:item) => {
74 $crate::__section_name!(
75 (#[$attr = __] #[allow(unsafe_code)] $item)
76 $section $type $name $($aux)?
77 );
78 }
79 );
80
81 #[cfg(not(feature = "proc_macro"))]
82 #[doc(hidden)]
83 #[macro_export]
84 macro_rules! __add_section_link_attribute(
85 ($section:ident $type:ident $name:ident #[$attr:ident = __] $item:item) => {
86 #[$attr = $crate::__section_name!(
87 raw $section $type $name
88 )] $item
89 }
90 );
91
92 #[cfg(target_vendor = "apple")]
94 def_section_name! {
95 {
96 data bare => ("__DATA,") __ ();
97 code bare => ("__TEXT,") __ ();
98 data section => ("__DATA,") __ (",regular,no_dead_strip");
99 code section => ("__TEXT,") __ (",regular,no_dead_strip");
100 data start => ("\x01section$start$__DATA$") __ ();
101 data end => ("\x01section$end$__DATA$") __ ();
102 }
103 AUXILIARY = "_";
104 MAX_LENGTH = 16;
105 HASH_LENGTH = 6;
106 VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
107 }
108
109 #[cfg(target_family = "wasm")]
110 def_section_name! {
111 {
112 data bare => (".data", ".link_section.") __ ();
113 data section => (".data", ".link_section.") __ ();
114 code bare => (".text", ".link_section.") __ ();
115 code section => (".text", ".link_section.") __ ();
116 }
117 AUXILIARY = ".";
118 MAX_LENGTH = 16;
119 HASH_LENGTH = 6;
120 VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
121 }
122
123 #[cfg(all(
124 not(target_vendor = "apple"),
125 not(target_vendor = "pc"),
126 not(target_family = "wasm")
127 ))]
128 def_section_name! {
129 {
130 data bare => ("_data", "_link_section_") __ ();
131 data section => ("_data", "_link_section_") __ ();
132 data start => ("__start_", "_data", "_link_section_") __ ();
133 data end => ("__stop_", "_data", "_link_section_") __ ();
134 code bare => ("_text", "_link_section_") __ ();
135 code section => ("_text", "_link_section_") __ ();
136 code start => ("__start_", "_text", "_link_section_") __ ();
137 code end => ("__stop_", "_text", "_link_section_") __ ();
138 }
139 AUXILIARY = "_";
140 MAX_LENGTH = 64;
141 HASH_LENGTH = 10;
142 VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
143 }
144
145 #[cfg(target_vendor = "pc")]
146 def_section_name! {
147 {
148 data bare => (".data", "$") __ ();
149 data section => (".data", "$") __ ("$b");
150 data start => (".data", "$") __ ("$a");
151 data end => (".data", "$") __ ("$c");
152 code bare => (".text", "$") __ ();
153 code section => (".text", "$") __ ("$b");
154 code start => (".text", "$") __ ("$a");
155 code end => (".text", "$") __ ("$c");
156 }
157 AUXILIARY = "$d$";
158 MAX_LENGTH = 64;
159 HASH_LENGTH = 10;
160 VALID_SECTION_CHARS = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
161 }
162
163 #[cfg(not(feature = "proc_macro"))]
164 #[doc(hidden)]
165 #[macro_export]
166 macro_rules! __declare_macro {
167 ($vis:vis $ident:ident $generic_macro:ident $args:tt) => {
168 #[doc(hidden)]
171 $vis use $crate::$generic_macro as $ident;
172 };
173 }
174
175 #[cfg(feature = "proc_macro")]
176 #[doc(hidden)]
177 #[macro_export]
178 macro_rules! __declare_macro {
179 ($vis:vis $ident:ident $generic_macro:ident $args:tt) => {
180 $crate::__support::ident_concat!(
181 (#[macro_export]
182 #[doc(hidden)]
183 macro_rules!) (__ $ident __link_section_private_macro__) ({
184 ($passthru:tt) => {
185 $crate::$generic_macro!($passthru $args);
186 };
187 })
188 );
189
190 $crate::__support::ident_concat!(
191 (#[doc(hidden)] pub use) (__ $ident __link_section_private_macro__) (as $ident;)
192 );
193 }
194 }
195
196 #[macro_export]
198 #[doc(hidden)]
199 macro_rules! __section_parse {
200 (#[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : $(:: $path_prefix:ident ::)? $($path:ident)::* < $($generic:tt)*) => {
202 $crate::__section_parse!(@parsed #[section $($args)*] $(#[$meta])* $vis static $ident: ( $(:: $path_prefix ::)? $($path)::* < $($generic)*) ( $($generic)* ) __in_section_helper_macro_generic);
203 };
204 (#[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : $(:: $path_prefix:ident ::)? $($path:ident)::* ;) => {
206 $crate::__section_parse!(@parsed #[section $($args)*] $(#[$meta])* $vis static $ident: ( $(:: $path_prefix ::)? $($path)::* ;) ( () > ; ) __in_section_helper_macro_no_generic);
207 };
208 (@parsed #[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : ($ty:ty ;) ( $generic_ty:ty > ; ) $generic_macro:ident) => {
210 $crate::__declare_macro!($vis $ident $generic_macro ($($args)*));
211 $crate::__section_parse!(@generate #[section $($args)*] $(#[$meta])* $vis static $ident: $ty, $generic_ty, $generic_macro);
212 };
213 (@generate #[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty, $generic_ty:ty, __in_section_helper_macro_generic) => {
214 $crate::__section_parse!(@generate #[section $($args)*] $(#[$meta])* $vis static $ident: $ty, $generic_ty, __base_case__);
215
216 impl ::core::iter::IntoIterator for $ident {
217 type Item = &'static $generic_ty;
218 type IntoIter = ::core::slice::Iter<'static, $generic_ty>;
219 fn into_iter(self) -> Self::IntoIter {
220 $ident.as_slice().iter()
221 }
222 }
223 };
224 (@generate #[section $($args:tt)*] $(#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty, $generic_ty:ty, $generic_macro:ident) => {
225 $(#[$meta])*
226 #[allow(non_camel_case_types)]
227 $vis struct $ident;
228
229 impl $crate::__support::SectionItemType for $ident {
230 type Item = $generic_ty;
231 }
232
233 impl ::core::fmt::Debug for $ident {
234 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
235 ::core::ops::Deref::deref(self).fmt(f)
236 }
237 }
238
239 impl ::core::ops::Deref for $ident {
240 type Target = $ty;
241 fn deref(&self) -> &Self::Target {
242 self.const_deref()
243 }
244 }
245
246 $crate::__section_parse!(@deref #[section $($args)*] $(#[$meta])* $vis static $ident: $ty, $generic_ty, __base_case__);
247 };
248 (@deref #[section] $(#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty, $generic_ty:ty, __base_case__) => {
249 impl $ident {
250 pub const fn const_deref(&self) -> &$ty {
253 static SECTION: $ty = {
254 let section = $crate::__support::get_section!(name=$ident, type=$generic_ty, aux=);
255 let name = $crate::__section_name!(
256 raw data bare $ident
257 );
258 unsafe { <$ty>::new(name, section.0, section.1) }
259 };
260 &SECTION
261 }
262 }
263 };
264 (@deref #[section(aux=$aux:ident)] $(#[$meta:meta])* $vis:vis static $ident:ident : $ty:ty, $generic_ty:ty, __base_case__) => {
265 impl $ident {
266 pub const fn const_deref(&self) -> &$ty {
269 static SECTION: $ty = {
270 let section = $crate::__support::get_section!(name=$ident, type=$generic_ty, aux=$aux);
271 let name = $crate::__section_name!(
272 raw data bare $aux $ident );
274 unsafe { <$ty>::new(name, section.0, section.1) }
275 };
276 &SECTION
277 }
278 }
279 };
280 }
281
282 #[cfg(miri)]
283 #[doc(hidden)]
284 #[macro_export]
285 macro_rules! __get_section {
286 (name=$ident:ident, type=$generic_ty:ty, aux=$($aux:ident)?) => {
287 {
289 (std::ptr::null_mut(), std::ptr::null_mut())
290 }
291 };
292 }
293
294 #[cfg(all(not(miri), target_family = "wasm"))]
295 #[doc(hidden)]
296 #[macro_export]
297 macro_rules! __get_section {
298 (name=$ident:ident, type=$generic_ty:ty, aux=$($aux:ident)?) => {
299 {
300 static __START: ::core::sync::atomic::AtomicPtr::<::core::marker::PhantomData<$generic_ty>> = unsafe {
301 ::core::sync::atomic::AtomicPtr::<::core::marker::PhantomData<$generic_ty>>::new(::core::ptr::null_mut())
302 };
303 static __END: ::core::sync::atomic::AtomicPtr::<::core::marker::PhantomData<$generic_ty>> = unsafe {
304 ::core::sync::atomic::AtomicPtr::<::core::marker::PhantomData<$generic_ty>>::new(::core::ptr::null_mut())
305 };
306
307 $crate::__support::ident_concat!((#[no_mangle]pub extern "C" fn) (register_link_section_ $ident) ((data_ptr: *const u8, data_len: usize) {
308 unsafe {
309 __START.store(data_ptr as *mut ::core::marker::PhantomData<$generic_ty>, ::core::sync::atomic::Ordering::Relaxed);
310 __END.store(data_ptr.add(data_len) as *mut ::core::marker::PhantomData<$generic_ty>, ::core::sync::atomic::Ordering::Relaxed);
311 }
312 }));
313
314 (&__START, &__END)
315 }
316 }
317 }
318
319 #[cfg(all(not(miri), target_vendor = "pc"))]
320 #[doc(hidden)]
321 #[macro_export]
322 macro_rules! __get_section {
323 (name=$ident:ident, type=$generic_ty:ty, aux=$($aux:ident)?) => {
324 {
325 $crate::__support::add_section_link_attribute!(
326 data start $ident $($aux)?
327 #[link_section = __]
328 static __START: [$generic_ty; 0] = [];
329 );
330 $crate::__support::add_section_link_attribute!(
331 data end $ident $($aux)?
332 #[link_section = __]
333 static __END: [$generic_ty; 0] = [];
334 );
335
336 (
337 unsafe { &raw const __START as $crate::__support::SectionPtr<$generic_ty> },
338 unsafe { &raw const __END as $crate::__support::SectionPtr<$generic_ty> },
339 )
340 }
341 }
342 }
343
344 #[cfg(all(not(miri), not(target_family = "wasm"), not(target_vendor = "pc")))]
345 #[doc(hidden)]
346 #[macro_export]
347 macro_rules! __get_section {
348 (name=$ident:ident, type=$generic_ty:ty, aux=$($aux:ident)?) => {
349 {
350 extern "C" {
351 $crate::__support::add_section_link_attribute!(
352 data start $ident $($aux)?
353 #[link_name = __]
354 static __START: $crate::__support::SectionPtr<$generic_ty>;
355 );
356 }
357 extern "C" {
358 $crate::__support::add_section_link_attribute!(
359 data end $ident $($aux)?
360 #[link_name = __]
361 static __END: $crate::__support::SectionPtr<$generic_ty>;
362 );
363 }
364
365 (
366 unsafe { &raw const __START as $crate::__support::SectionPtr<$generic_ty> },
367 unsafe { &raw const __END as $crate::__support::SectionPtr<$generic_ty> },
368 )
369 }
370 }
371 }
372
373 #[macro_export]
375 #[doc(hidden)]
376 macro_rules! __in_section_parse {
377 (#[in_section(unsafe, type = $stored_ty:ty, name = $ident:ident $( , aux = $aux:ident)?)] $($item:tt)*) => {
384 $crate::__support::in_section_crate!((type = $stored_ty), $ident, $($aux)?, $ident, ($($item)*));
385 };
386 (#[in_section(unsafe, name = $ident:ident $( , aux = $aux:ident)?)] $($item:tt)*) => {
387 $crate::__support::in_section_crate!(data, $ident, $($aux)?, $ident, ($($item)*));
388 };
389 (#[in_section( $($path:tt)* )] $($item:tt)*) => {
390 $crate::__support::in_section_parse!(path=[$($path)*] #[in_section($($path)*)] $($item)*);
391 };
392 (path=[$orig_path:path] #[in_section($name:ident)] $($item:tt)*) => {
393 $orig_path ! (
394 (v=0 (name=$name (path=[$orig_path] (item=($($item)*) ()))))
395 );
396 };
397 (path=[$orig_path:path] #[in_section(:: $($path:ident)::*)] $($item:tt)*) => {
398 $crate::__support::in_section_parse!(path=[$orig_path] #[in_section($($path)::*)] $($item)*);
399 };
400 (path=[$orig_path:path] #[in_section($prefix:ident :: $($path:ident)::*)] $($item:tt)*) => {
401 $crate::__support::in_section_parse!(path=[$orig_path] #[in_section($($path)::*)] $($item)*);
402 };
403 }
404
405 #[macro_export]
406 #[doc(hidden)]
407 macro_rules! __in_section_helper_macro_generic {
408 ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt))))) => {
409 $crate::__support::in_section_crate!(section, $ident,, $path, $item);
410 };
411 ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt)))) ((aux=$aux:ident)) )=> {
412 $crate::__support::in_section_crate!(section, $ident, $aux, $path, $item);
413 };
414 ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt)))) () )=> {
415 $crate::__support::in_section_crate!(section, $ident,, $path, $item);
416 };
417 (v=$v:literal $rest:tt) => {
418 const _: () = {
419 compile_error!(concat!(
420 "link-section: Unsupported version: `",
421 stringify!($v),
422 "`: ",
423 stringify!($rest)
424 ));
425 };
426 };
427 }
428
429 #[macro_export]
430 #[doc(hidden)]
431 macro_rules! __in_section_helper_macro_no_generic {
432 ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt))))) => {
433 $crate::__support::in_section_crate!(data, $ident,, $path, $item);
434 };
435 ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt)))) ((aux=$aux:ident)) )=> {
436 $crate::__support::in_section_crate!(data, $ident, $aux, $path, $item);
437 };
438 ((v=0 (name=$ident:ident (path=[$path:path] (item=$item:tt $rest:tt)))) () )=> {
439 $crate::__support::in_section_crate!(data, $ident,, $path, $item);
440 };
441 (v=$v:literal $rest:tt) => {
442 const _: () = {
443 compile_error!(concat!(
444 "link-section: Unsupported version: `",
445 stringify!($v),
446 "`: ",
447 stringify!($rest)
448 ));
449 };
450 };
451 }
452
453 #[macro_export]
454 #[doc(hidden)]
455 #[allow(unknown_lints, edition_2024_expr_fragment_specifier)]
456 macro_rules! __in_section_crate {
457 (@type_select section $path:path, $item_ty:ty) => {
458 <$path as $crate::__support::SectionItemType>::Item
459 };
460 (@type_select data $path:path, $item_ty:ty) => {
461 $item_ty
462 };
463 (@type_select (type = $stored_ty:ty) $path:path, $item_ty:ty) => {
464 $stored_ty
465 };
466 ($type_source:tt, $ident:ident, $($aux:ident)?, $path:path, ($(#[$meta:meta])* $vis:vis fn $ident_fn:ident($($args:tt)*) $(-> $ret:ty)? $body:block)) => {
467 $crate::__add_section_link_attribute!(
468 data section $ident $($aux)?
469 #[link_section = __]
470 $(#[$meta])*
471 #[used]
472 #[allow(non_upper_case_globals)]
473 $vis static $ident_fn: $crate::__in_section_crate!(@type_select $type_source $path, fn($($args)*) $(-> $ret)?) =
474 {
475 $crate::__add_section_link_attribute!(
476 code section $ident $($aux)?
477 #[link_section = __]
478 fn $ident_fn($($args)*) $(-> $ret)? $body
479 );
480 $ident_fn
481 };
482 );
483 };
484 ($type_source:tt, $ident:ident, $($aux:ident)?, $path:path, ($(#[$meta:meta])* $vis:vis static _ : $ty:ty = $value:expr;)) => {
485 const _: () = {
486 $crate::__add_section_link_attribute!(
487 data section $ident $($aux)?
488 #[link_section = __]
489 $(#[$meta])* #[used] $vis static ANONYMOUS: $crate::__in_section_crate!(@type_select $type_source $path, $ty) = $value;
490 );
491 };
492 };
493 ($type_source:tt, $ident:ident, $($aux:ident)?, $path:path, ($(#[$meta:meta])* $vis:vis static $ident_static:ident : $ty:ty = $value:expr;)) => {
494 $crate::__add_section_link_attribute!(
495 data section $ident $($aux)?
496 #[link_section = __]
497 $(#[$meta])* #[used] $vis static $ident_static: $crate::__in_section_crate!(@type_select $type_source $path, $ty) = $value;
498 );
499 };
500 (data, $ident:ident, $($aux:ident)?, $path:path, ($(#[$meta:meta])* $item:item)) => {
501 $crate::__add_section_link_attribute!(
502 data section $ident $($aux)?
503 #[link_section = __]
504 $(#[$meta])* #[used] $item
505 );
506 };
507 }
508
509 pub trait SectionItemType {
510 type Item;
511 }
512
513 impl<T> SectionItemType for super::TypedSection<T> {
514 type Item = T;
515 }
516
517 #[cfg(all(not(target_vendor = "pc"), not(target_family = "wasm")))]
520 pub type SectionPtr<T> = *const ::core::marker::PhantomData<T>;
521 #[cfg(target_vendor = "pc")]
525 pub type SectionPtr<T> = *const [T; 0];
526 #[cfg(target_family = "wasm")]
528 pub type SectionPtr<T> =
529 &'static ::core::sync::atomic::AtomicPtr<::core::marker::PhantomData<T>>;
530}
531
532#[cfg(feature = "proc_macro")]
547pub use ::link_section_proc_macro::section;
548
549#[cfg(feature = "proc_macro")]
556pub use ::link_section_proc_macro::in_section;
557
558#[repr(C)]
561pub struct Section {
562 name: &'static str,
563 start: __support::SectionPtr<()>,
564 end: __support::SectionPtr<()>,
565}
566
567impl Section {
568 #[doc(hidden)]
569 pub const unsafe fn new(
570 name: &'static str,
571 start: __support::SectionPtr<()>,
572 end: __support::SectionPtr<()>,
573 ) -> Self {
574 Self { name, start, end }
575 }
576}
577
578#[cfg(target_family = "wasm")]
579impl Section {
580 pub fn start_ptr(&self) -> *const () {
582 let ptr = self.start.load(::core::sync::atomic::Ordering::Relaxed) as *const ();
583 if ptr.is_null() {
584 panic!(
585 "Section {} was not initialized by the host environment",
586 self.name
587 );
588 }
589 ptr
590 }
591 pub fn end_ptr(&self) -> *const () {
593 let ptr = self.end.load(::core::sync::atomic::Ordering::Relaxed) as *const ();
594 if ptr.is_null() {
595 panic!(
596 "Section {} was not initialized by the host environment",
597 self.name
598 );
599 }
600 ptr
601 }
602 pub fn byte_len(&self) -> usize {
604 unsafe { (self.end_ptr() as *const u8).offset_from(self.start_ptr() as *const u8) as usize }
605 }
606}
607
608#[cfg(not(target_family = "wasm"))]
609impl Section {
610 pub const fn start_ptr(&self) -> *const () {
612 self.start as *const ()
613 }
614 pub const fn end_ptr(&self) -> *const () {
616 self.end as *const ()
617 }
618 pub const fn byte_len(&self) -> usize {
620 unsafe { (self.end_ptr() as *const u8).offset_from(self.start_ptr() as *const u8) as usize }
621 }
622}
623
624impl ::core::fmt::Debug for Section {
625 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
626 f.debug_struct("Section")
627 .field("name", &self.name)
628 .field("start", &self.start_ptr())
629 .field("end", &self.end_ptr())
630 .field("byte_len", &self.byte_len())
631 .finish()
632 }
633}
634
635unsafe impl Sync for Section {}
636unsafe impl Send for Section {}
637
638#[repr(C)]
641pub struct TypedSection<T: 'static> {
642 name: &'static str,
643 start: __support::SectionPtr<T>,
644 end: __support::SectionPtr<T>,
645 _phantom: ::core::marker::PhantomData<T>,
646}
647
648#[cfg(target_family = "wasm")]
649impl<T: 'static> TypedSection<T> {
650 pub fn start_ptr(&self) -> *const T {
652 let ptr = self.start.load(::core::sync::atomic::Ordering::Relaxed) as *const T;
653 if ptr.is_null() {
654 panic!(
655 "TypedSection {} was not initialized by the host environment",
656 self.name
657 );
658 }
659 ptr
660 }
661
662 pub fn end_ptr(&self) -> *const T {
664 let ptr = self.end.load(::core::sync::atomic::Ordering::Relaxed) as *const T;
665 if ptr.is_null() {
666 panic!(
667 "TypedSection {} was not initialized by the host environment",
668 self.name
669 );
670 }
671 ptr
672 }
673
674 pub fn byte_len(&self) -> usize {
676 unsafe { (self.end_ptr() as *const u8).offset_from(self.start_ptr() as *const u8) as usize }
677 }
678
679 pub fn len(&self) -> usize {
681 self.byte_len() / self.stride()
682 }
683
684 pub fn is_empty(&self) -> bool {
686 self.len() == 0
687 }
688
689 pub fn as_slice(&self) -> &[T] {
691 if self.is_empty() {
692 &[]
693 } else {
694 unsafe { ::core::slice::from_raw_parts(self.start_ptr(), self.len()) }
695 }
696 }
697}
698
699#[cfg(not(target_family = "wasm"))]
700impl<T: 'static> TypedSection<T> {
701 pub const fn start_ptr(&self) -> *const T {
703 self.start as *const T
704 }
705
706 pub const fn end_ptr(&self) -> *const T {
708 self.end as *const T
709 }
710
711 pub const fn byte_len(&self) -> usize {
713 unsafe { (self.end_ptr() as *const u8).offset_from(self.start_ptr() as *const u8) as usize }
714 }
715
716 pub const fn len(&self) -> usize {
718 self.byte_len() / self.stride()
719 }
720
721 pub const fn is_empty(&self) -> bool {
723 self.len() == 0
724 }
725
726 pub const fn as_slice(&self) -> &[T] {
728 if self.is_empty() {
729 &[]
730 } else {
731 unsafe { ::core::slice::from_raw_parts(self.start_ptr(), self.len()) }
732 }
733 }
734}
735
736impl<T: 'static> TypedSection<T> {
738 #[doc(hidden)]
739 pub const unsafe fn new(
740 name: &'static str,
741 start: __support::SectionPtr<T>,
742 end: __support::SectionPtr<T>,
743 ) -> Self {
744 Self {
745 name,
746 start,
747 end,
748 _phantom: ::core::marker::PhantomData,
749 }
750 }
751
752 pub const fn stride(&self) -> usize {
754 assert!(
755 ::core::mem::size_of::<T>() > 0
756 && ::core::mem::size_of::<T>() * 2 == ::core::mem::size_of::<[T; 2]>()
757 );
758 ::core::mem::size_of::<T>()
759 }
760
761 pub fn offset_of(&self, item: &T) -> Option<usize> {
763 let ptr = item as *const T;
764 if ptr < self.start_ptr() || ptr >= self.end_ptr() {
765 None
766 } else {
767 Some(unsafe { ptr.offset_from(self.start_ptr()) as usize })
768 }
769 }
770
771 #[allow(clippy::mut_from_ref)]
778 pub unsafe fn as_mut_slice(&self) -> &mut [T] {
779 if self.is_empty() {
780 &mut []
781 } else {
782 unsafe { ::core::slice::from_raw_parts_mut(self.start_ptr() as *mut T, self.len()) }
783 }
784 }
785}
786
787impl<'a, T> ::core::iter::IntoIterator for &'a TypedSection<T> {
788 type Item = &'a T;
789 type IntoIter = ::core::slice::Iter<'a, T>;
790 fn into_iter(self) -> Self::IntoIter {
791 self.as_slice().iter()
792 }
793}
794
795impl<T> ::core::ops::Deref for TypedSection<T> {
796 type Target = [T];
797 fn deref(&self) -> &Self::Target {
798 self.as_slice()
799 }
800}
801
802impl<T> ::core::fmt::Debug for TypedSection<T> {
803 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
804 f.debug_struct("TypedSection")
805 .field("name", &self.name)
806 .field("start", &self.start_ptr())
807 .field("end", &self.end_ptr())
808 .field("len", &self.len())
809 .field("stride", &self.stride())
810 .finish()
811 }
812}
813
814unsafe impl<T> Sync for TypedSection<T> where T: Sync {}
815unsafe impl<T> Send for TypedSection<T> where T: Send {}