Skip to content

Commit

Permalink
[chore] Rename the crate as ctor_base
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure-stars committed Nov 20, 2024
1 parent 624a9d8 commit 1efd2ee
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 170 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
/.vscode
.DS_Store
Cargo.lock
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
resolver = "2"

members = [
"constructor_array",
"constructor_array_macros",
"ctor_bare",
"ctor_bare_macros",
]

[workspace.package]
version = "0.1.0"
authors = ["Youjie Zheng <[email protected]>"]
license = "GPL-3.0-or-later OR Apache-2.0 OR MulanPSL-2.0"
homepage = "https://github.com/arceos-org/arceos"
documentation = "https://arceos-org.github.io/constructor_array"
repository = "https://github.com/arceos-org/constructor_array"
documentation = "https://arceos-org.github.io/ctor_bare"
repository = "https://github.com/arceos-org/ctor_bare"
keywords = ["arceos", "constructor"]
categories = ["development-tools::procedural-macro-helpers", "no-std"]

[packages]
constructor_array = { path = "constructor_array" }
constructor_array_macros = { path = "constructor_array_macros" }
ctor_bare = { path = "ctor_bare" }
ctor_bare_macros = { path = "ctor_bare_macros" }
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# constructor_array
# ctor_bare

[![Crates.io](https://img.shields.io/crates/v/constructor_array)](https://crates.io/crates/constructor_array)
[![Docs.rs](https://docs.rs/constructor_array/badge.svg)](https://docs.rs/constructor_array)
[![CI](https://github.com/arceos-org/constructor_array/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/arceos-org/constructor_array/actions/workflows/ci.yml)
[![Crates.io](https://img.shields.io/crates/v/ctor_bare)](https://crates.io/crates/ctor_bare)
[![Docs.rs](https://docs.rs/ctor_bare/badge.svg)](https://docs.rs/ctor_bare)
[![CI](https://github.com/arceos-org/ctor_bare/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/arceos-org/ctor_bare/actions/workflows/ci.yml)


Module initialization functions for Rust (like __attribute__((constructor)) in C/C++) under no_std.
Expand All @@ -17,12 +17,12 @@ It can support Linux, Windows, MacOS and other systems, and can be also used in
In Linux, Windows, MacOS and other systems, the `.init_array` section is a default section to store initialization functions. When the program starts, the system will call all functions in the `.init_array` section in order.


When you are running your own operating system, you can call `constructor_array::invoke_ctors` to invoke all registered constructor functions.
When you are running your own operating system, you can call `ctor_bare::call_ctors` to invoke all registered constructor functions.

## Usage

```rust
use constructor_array::register_ctor;
use ctor_bare::register_ctor;
#[register_ctor]
fn hello_world() {
println!("Hello, world!");
Expand All @@ -49,10 +49,10 @@ Because the `.init_array` section is a default section to store initialization f
.text : ALIGN(4K) {
# other sections in the `.text` section
_init_array_start = .;
_init_array_end = _init_array_start + SIZEOF(.init_array);
__init_array_start = .;
__init_array_end = __init_array_start + SIZEOF(.init_array);
*(.init_array .init_array.*)
. = _init_array_end;
. = __init_array_end;
# other sections in the `.text` section
}
Expand Down
70 changes: 0 additions & 70 deletions constructor_array/README.md

This file was deleted.

70 changes: 0 additions & 70 deletions constructor_array_macros/README.md

This file was deleted.

6 changes: 3 additions & 3 deletions constructor_array/Cargo.toml → ctor_bare/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "constructor_array"
name = "ctor_bare"
edition = "2021"
description = "Register constructor functions for Rust at complie time under no_std."
documentation = "https://docs.rs/constructor_array"
documentation = "https://docs.rs/ctor_bare"
version.workspace = true
authors.workspace = true
license.workspace = true
Expand All @@ -13,4 +13,4 @@ categories.workspace = true
readme = "README.md"

[dependencies]
constructor_array_macros = { path = "../constructor_array_macros" }
ctor_bare_macros = { path = "../ctor_bare_macros" }
1 change: 1 addition & 0 deletions ctor_bare/README.md
4 changes: 2 additions & 2 deletions constructor_array/src/lib.rs → ctor_bare/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_std]
#![doc = include_str!("../README.md")]

pub use constructor_array_macros::register_ctor;
pub use ctor_bare_macros::register_ctor;

/// Placeholder for the `.init_array` section, so that
/// the `__init_array_start` and `__init_array_end` symbols can be generated.
Expand All @@ -18,7 +18,7 @@ extern "C" {
///
/// # Notes
/// Caller should ensure that the `.init_array` section will not be disturbed by other sections.
pub fn invoke_ctors() {
pub fn call_ctors() {
for ctor_ptr in (__init_array_start as usize..__init_array_end as usize)
.step_by(core::mem::size_of::<*const core::ffi::c_void>())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::sync::{atomic::AtomicUsize, Mutex};

use constructor_array::*;
use ctor_bare::*;

static INIT_NUM: AtomicUsize = AtomicUsize::new(0);

Expand All @@ -20,7 +20,7 @@ fn init_vector() {
}

#[test]
fn test_constructor_array() {
fn test_ctor_bare() {
// The constructor functions will be called before the main function.
assert!(INIT_NUM.load(std::sync::atomic::Ordering::Relaxed) == 20);
let vec = INIT_VEC.lock().unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[test]
fn test_empty() {
// Sometimes under certain conditions, we may not have any constructor functions.
// But the `invoke_ctors` function should still work, and the `__init_array_start` and
// But the `call_ctors` function should still work, and the `__init_array_start` and
// `__init_array_end` symbols should be valid.
constructor_array::invoke_ctors();
ctor_bare::call_ctors();
println!("It should exit successfully when we don't specify any constructor functions.");
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "constructor_array_macros"
name = "ctor_bare_macros"
edition = "2021"
description = "Macros for registering constructor functions for Rust under no_std."
documentation = "https://docs.rs/constructor_array_macros"
documentation = "https://docs.rs/ctor_bare_macros"
version.workspace = true
authors.workspace = true
license.workspace = true
Expand Down
1 change: 1 addition & 0 deletions ctor_bare_macros/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//!Macros for registering constructor functions for Rust under no_std, which is like __attribute__((constructor)) in C/C++.
//!
//! **DO NOT** use this crate directly. Use the [constructor_array](https://docs.rs/constructor_array) crate instead.
//! **DO NOT** use this crate directly. Use the [ctor_bare](https://docs.rs/ctor_bare) crate instead.
//!
//! After attching the `register_ctor` macro to the given function, a pointer pointing to it will be stored in the `.init_array` section.
//! When the program is loaded, this section will be linked into the binary. The `invoke_ctors` function in the `constructor_array`
//! When the program is loaded, this section will be linked into the binary. The `call_ctors` function in the `ctor_bare`
//! crate will call all the constructor functions in the `.init_array` section.
//!
//! See the documentation of the [constructor_array](https://docs.rs/constructor_array) crate for more details.
//! See the documentation of the [ctor_bare](https://docs.rs/ctor_bare) crate for more details.
use proc_macro::TokenStream;
use proc_macro2::Span;
Expand All @@ -17,7 +17,7 @@ use syn::{parse_macro_input, Error, Item};
///
/// The function should have no input arguments and return nothing.
///
/// See the documentation of the [constructor_array](https://docs.rs/constructor_array) crate for more details.
/// See the documentation of the [ctor_bare](https://docs.rs/ctor_bare) crate for more details.
#[proc_macro_attribute]
pub fn register_ctor(attr: TokenStream, function: TokenStream) -> TokenStream {
if !attr.is_empty() {
Expand Down

0 comments on commit 1efd2ee

Please sign in to comment.