LICENSE LICENSE Documentation Crates.io Version

Module initialization termination function with priorities and (mutable) statics initialization with non const functions.

Minimum rust version required: 1.49

# Functionalities

Safety

Use of the functionnalities provided by this library are inherently unsafe. During execution of a constructor, any access to variable initialized with a lower or equal priority (higher or equal priority number) will cause undefined behavior. During execution of a destructor any access to variable droped with a lower or equal priority (higher or equal priority number) will cause undefined behavior.

This is actually the reason to be of the priorities: this is the coder own responsability to ensure that no access is performed to lower or equal priorities.

```rust use static_init::dynamic;

#[dynamic] static V1: Vec = unsafe {vec![1,2,3]};

//potential undefined behavior: V1 may not have been initialized yet #[dynamic] static V2: i32 = unsafe {V1[0]};

//undefined behavior, V3 is unconditionnaly initialized before V1 #[dynamic(1000)] static V3: i32 = unsafe {V1[0]};

#[dynamic(1000)] static V4: Vec = unsafe {vec![1,2,3]};

//Good, V5 initialized after V4 #[dynamic(2000)] static V5: i32 = unsafe {V4[0]};

//Good, V6 initialized after V5 and v4 #[dynamic] static V6: i32 = unsafe {*V5+V4[1]};

# fn main(){} ```

# Comparisons against other crates

## lazystatic - lazystatic only provides const statics. - Each access to lazystatic statics costs 2ns on a x86. - lazystatic does not provide priorities. - lazy_static statics initialization is safe.

## ctor - ctor only provides const statics. - ctor does not provide priorities.

# Documentation and details

## Mac - MACH_O specification - GCC source code gcc/config/darwin.c indicates that priorities are not supported.

Initialization functions pointers are placed in section "DATA,modinitfunc" and "DATA,modtermfunc"

## ELF plateforms: - info ld - linker script: ld --verbose - ELF specification

The runtime will run fonctions pointers of section ".initarray" at startup and function pointers in ".finiarray" at program exit. The linker place in the target object file sectio .initarray all sections from the source objects whose name is of the form .initarray.NNNNN in lexicographical order then the .initarray sections of those same source objects. It does equivalently with .finiarray and .fini_array.NNNN sections.

Usage can be seen in gcc source gcc/config/pru.c

Resources of libstdc++ are initialized with priority 100 (see gcc source libstdc++-v3/c++17/defaultresource.h) The rust standard library function that capture the environment and executable arguments is executed at priority 99. Some callbacks constructors and destructors with priority 0 are registered by rust/rtlibrary. Static C++ objects are usually initialized with no priority (TBC). lib-c resources are initialized by the C-runtime before any function in the initarray (whatever the priority) are executed.

## Windows