diff --git a/core/Cargo.toml b/core/Cargo.toml index bdbc2e60e21cb675ba912a2c56e1b7a760375df9..b0752da3be9afc37a3696ab12eb4b5122c71388d 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -5,11 +5,10 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default=["alloc"] -alloc=["stdlib/alloc"] -std=["stdlib/std"] +default = ["alloc"] +alloc = ["stdlib/alloc"] +std = ["stdlib/std"] [dependencies] -types={path="../types"} -stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false,branch="develop"} - - +types = { path = "../types" } +tools = { path = "../tools" } +stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false, branch = "master" } diff --git a/core/src/lib.rs b/core/src/lib.rs index f949ac26d89f0fffec0e6b46c1658d84090f7889..021123c3a1db1097d7a14e8a19b8857284670ea1 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -1,4 +1,8 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 #![cfg_attr(feature = "alloc", no_std)] extern crate alloc; -pub mod traits; +mod serialization; +mod types; + diff --git a/core/src/serialization/encoding.rs b/core/src/serialization/encoding.rs new file mode 100644 index 0000000000000000000000000000000000000000..6ba5e5ce5d7ca2fe6216f4d636fcbcb154c29677 --- /dev/null +++ b/core/src/serialization/encoding.rs @@ -0,0 +1,159 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +mod test; +use tools::ChunkBuf; +pub trait BinaryEncoder { + /// 返回结构体所占字节长度 + fn byte_len(&self) -> usize; + /// 将 实例 写入 stream + fn encode(&self, stream: &mut ChunkBuf) -> stdlib::io::Result; + /// 将实例读出一个实例 + fn decode(stream: &mut ChunkBuf) -> stdlib::io::Result; +} + +pub fn write_u8(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let buf: [u8; 1] = [value.into()]; + stream.write(&buf) +} + +pub fn write_i16(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let value: i16 = value.into(); + let buf: [u8; 2] = value.to_le_bytes(); + stream.write(&buf) +} + +pub fn write_u16(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let value: u16 = value.into(); + let buf: [u8; 2] = value.to_le_bytes(); + stream.write(&buf) +} + +pub fn write_i32(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let value: i32 = value.into(); + let buf: [u8; 4] = value.to_le_bytes(); + stream.write(&buf) +} + +pub fn write_u32(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let value: u32 = value.into(); + let buf: [u8; 4] = value.to_le_bytes(); + stream.write(&buf) +} + +pub fn write_i64(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let value: i64 = value.into(); + let buf: [u8; 8] = value.to_le_bytes(); + stream.write(&buf) +} + +pub fn write_u64(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let value: u64 = value.into(); + let buf: [u8; 8] = value.to_le_bytes(); + stream.write(&buf) +} + +pub fn write_f32(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let value: f32 = value.into(); + let buf: [u8; 4] = value.to_le_bytes(); + stream.write(&buf) +} + +pub fn write_f64(stream: &mut ChunkBuf, value: T) -> stdlib::io::Result +where + T: Into, +{ + let value: f64 = value.into(); + let buf: [u8; 8] = value.to_le_bytes(); + stream.write(&buf) +} + +pub fn read_bytes(stream: &mut ChunkBuf, buf: &mut [u8]) -> stdlib::io::Result { + stream.read_exact(buf) +} + +pub fn read_u8(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8]; + let _ = stream.read_exact(&mut buf)?; + Ok(buf[0]) +} + +pub fn read_i16(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8; 2]; + let _ = stream.read_exact(&mut buf)?; + let value = i16::from_le_bytes(buf); + Ok(value) +} + +pub fn read_u16(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8; 2]; + let _ = stream.read_exact(&mut buf)?; + let value = u16::from_le_bytes(buf); + Ok(value) +} + +pub fn read_i32(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8; 4]; + let _ = stream.read_exact(&mut buf)?; + let value = i32::from_le_bytes(buf); + Ok(value) +} + +pub fn read_u32(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8; 4]; + let _ = stream.read_exact(&mut buf)?; + let value = u32::from_le_bytes(buf); + Ok(value) +} + +pub fn read_i64(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8; 8]; + let _ = stream.read_exact(&mut buf)?; + let value = i64::from_le_bytes(buf); + Ok(value) +} + +pub fn read_u64(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8; 8]; + let _ = stream.read_exact(&mut buf)?; + let value = u64::from_le_bytes(buf); + Ok(value) +} + +pub fn read_f32(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8; 4]; + let _ = stream.read_exact(&mut buf)?; + let value = f32::from_le_bytes(buf); + Ok(value) +} + +pub fn read_f64(stream: &mut ChunkBuf) -> stdlib::io::Result { + let mut buf = [0u8; 8]; + let _ = stream.read_exact(&mut buf)?; + let value = f64::from_le_bytes(buf); + Ok(value) +} + diff --git a/core/src/serialization/encoding/test.rs b/core/src/serialization/encoding/test.rs new file mode 100644 index 0000000000000000000000000000000000000000..f744f4eb7f719a33c718a44d095e35edccfdc9c8 --- /dev/null +++ b/core/src/serialization/encoding/test.rs @@ -0,0 +1,61 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +#[test] +fn test_read_write_basic_type() { + use super::*; + use tools::ChunkBuf; + struct A { + a: u8, + b: i16, + c: u16, + d: i32, + e: u32, + f: i64, + g: u64, + h: f32, + i: f64, + } + let a_struct = A { + a: 1, + b: -2, + c: 3, + d: -4, + e: 5, + f: -6, + g: 7, + h: 8.8, + i: 9.9, + }; + let mut chunk_buf = ChunkBuf::new(); + + let _ = write_u8(&mut chunk_buf, a_struct.a); + let _ = write_i16(&mut chunk_buf, a_struct.b); + let _ = write_u16(&mut chunk_buf, a_struct.c); + let _ = write_i32(&mut chunk_buf, a_struct.d); + let _ = write_u32(&mut chunk_buf, a_struct.e); + let _ = write_i64(&mut chunk_buf, a_struct.f); + let _ = write_u64(&mut chunk_buf, a_struct.g); + let _ = write_f32(&mut chunk_buf, a_struct.h); + let _ = write_f64(&mut chunk_buf, a_struct.i); + + let i = read_f64(&mut chunk_buf).unwrap(); + let h = read_f32(&mut chunk_buf).unwrap(); + let g = read_u64(&mut chunk_buf).unwrap(); + let f = read_i64(&mut chunk_buf).unwrap(); + let e = read_u32(&mut chunk_buf).unwrap(); + let d = read_i32(&mut chunk_buf).unwrap(); + let c = read_u16(&mut chunk_buf).unwrap(); + let b = read_i16(&mut chunk_buf).unwrap(); + let a = read_u8(&mut chunk_buf).unwrap(); + + assert_eq!(a, a_struct.a); + assert_eq!(b, a_struct.b); + assert_eq!(c, a_struct.c); + assert_eq!(d, a_struct.d); + assert_eq!(e, a_struct.e); + assert_eq!(f, a_struct.f); + assert_eq!(g, a_struct.g); + assert_eq!(h, a_struct.h); + assert_eq!(i, a_struct.i); +} + diff --git a/core/src/serialization/mod.rs b/core/src/serialization/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..8d34c2b4fc5d509c0046e281ef702bb2a2eb18a5 --- /dev/null +++ b/core/src/serialization/mod.rs @@ -0,0 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +mod encoding; +pub use encoding::BinaryEncoder; + diff --git a/core/src/traits/block_process_trait.rs b/core/src/traits/block_process_trait.rs deleted file mode 100644 index bd2c573af9d3e2a4c3857f42475a0028e5aba5ca..0000000000000000000000000000000000000000 --- a/core/src/traits/block_process_trait.rs +++ /dev/null @@ -1,16 +0,0 @@ -use types::build_in::Result; -pub trait BolckProcessTrait { - fn check_blocks() -> Result; - - fn encode() -> Result; - - fn decode() -> Result; -} - -pub trait BlockInfoTrait { - fn new() -> Result; - - fn write_ack(&mut self) -> Result; - - fn write(&mut self) -> Result; -} diff --git a/core/src/traits/mod.rs b/core/src/traits/mod.rs deleted file mode 100644 index fb6efcfda13fa0c571d178bfd1ac3f6b35640068..0000000000000000000000000000000000000000 --- a/core/src/traits/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod block_process_trait; diff --git a/core/src/types/mod.rs b/core/src/types/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..9941d6aedf0af1a1e996f418b91fc35dcd2fbffa --- /dev/null +++ b/core/src/types/mod.rs @@ -0,0 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +mod tcp_types; + diff --git a/core/src/types/tcp_types.rs b/core/src/types/tcp_types.rs new file mode 100644 index 0000000000000000000000000000000000000000..290b56efb2078de5b9eaa0d0435fa15792e303ed --- /dev/null +++ b/core/src/types/tcp_types.rs @@ -0,0 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + + diff --git a/cypto/Cargo.toml b/cypto/Cargo.toml index 06add120d0e7726aa0abeb891d9d7a33ba752550..05af5b5ff079337cf4ec0554c016d6ac0aa1c6ed 100644 --- a/cypto/Cargo.toml +++ b/cypto/Cargo.toml @@ -6,10 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default=["alloc"] -alloc=["stdlib/alloc"] -std=["stdlib/std"] +default = ["alloc"] +alloc = ["stdlib/alloc"] +std = ["stdlib/std"] [dependencies] -stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false,branch="develop"} -types={path="../types"} - +stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false, branch = "master" } +types = { path = "../types" } diff --git a/cypto/src/lib.rs b/cypto/src/lib.rs index 809d0675dcdba2bcdceceecf45c704f5c1484859..896c38e94f590acb2bf021378fd1417286753534 100644 --- a/cypto/src/lib.rs +++ b/cypto/src/lib.rs @@ -1,2 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 #![cfg_attr(feature = "alloc", no_std)] extern crate alloc; + diff --git a/server/Cargo.toml b/server/Cargo.toml index dda00aff75e34894c518e347f49ad81b43310207..8ce02326a9326091a82882c2e8ac97291981df44 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -6,9 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default=["alloc"] -alloc=["stdlib/alloc"] -std=["stdlib/std"] +default = ["alloc"] +alloc = ["stdlib/alloc"] +std = ["stdlib/std"] [dependencies] -stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false,branch="develop"} -types={path="../types"} +stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false, branch = "master" } +types = { path = "../types" } diff --git a/server/src/address_space/mod.rs b/server/src/address_space/mod.rs index f6ac8fc7b614f061d618379b93867305252f71b8..e1131db039106197a4158f0e91552ba72ab6b8d5 100644 --- a/server/src/address_space/mod.rs +++ b/server/src/address_space/mod.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod traits; + diff --git a/server/src/address_space/traits/address_space_trait.rs b/server/src/address_space/traits/address_space_trait.rs index 1947b0a85574a3efb35fc1921894947466ceef01..8245dd8ec56d7aaefd95d1a701bfd51855e4b7f8 100644 --- a/server/src/address_space/traits/address_space_trait.rs +++ b/server/src/address_space/traits/address_space_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait AddressSpaceTrait { /// Constructs a default address space @@ -46,3 +48,4 @@ pub trait AddressSpaceTrait { /// Returns the references fn references(&self) -> Result; } + diff --git a/server/src/address_space/traits/mod.rs b/server/src/address_space/traits/mod.rs index 5bed418039a446674ab326ba41c9b25c5da707a2..bb0b16c3babdd62460f2ba09b4ade8bf488051a3 100644 --- a/server/src/address_space/traits/mod.rs +++ b/server/src/address_space/traits/mod.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod address_space_trait; + diff --git a/server/src/communication_services/mod.rs b/server/src/communication_services/mod.rs index f6ac8fc7b614f061d618379b93867305252f71b8..e1131db039106197a4158f0e91552ba72ab6b8d5 100644 --- a/server/src/communication_services/mod.rs +++ b/server/src/communication_services/mod.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod traits; + diff --git a/server/src/communication_services/traits/mod.rs b/server/src/communication_services/traits/mod.rs index af4705a81461606c5f3bed2809efa16c1959a097..a0d62f2020ca22032ac5c2985b58e1b469663cc9 100644 --- a/server/src/communication_services/traits/mod.rs +++ b/server/src/communication_services/traits/mod.rs @@ -1,2 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod secure_channel_service_trait; pub mod transport_tarit; + diff --git a/server/src/communication_services/traits/secure_channel_service_trait.rs b/server/src/communication_services/traits/secure_channel_service_trait.rs index e82b58e97b9d259f55448033a1e05ea8830236d1..181cb18ad07c00fe151bf944ef68c13edfa52cf2 100644 --- a/server/src/communication_services/traits/secure_channel_service_trait.rs +++ b/server/src/communication_services/traits/secure_channel_service_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait SecureChannelServiceTrait { fn new() -> Result; @@ -6,3 +8,4 @@ pub trait SecureChannelServiceTrait { fn close_secure_channel() -> Result; } + diff --git a/server/src/communication_services/traits/transport_tarit.rs b/server/src/communication_services/traits/transport_tarit.rs index 1ddab0ee8d0452f8ec3b7ace3567a4bae8486d78..33bc626255e00a854f0c0c85edb0d1ffa05e558a 100644 --- a/server/src/communication_services/traits/transport_tarit.rs +++ b/server/src/communication_services/traits/transport_tarit.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use alloc::string::String; use types::build_in::Result; pub trait Transport { @@ -19,3 +21,4 @@ pub trait Transport { /// Test if the session is terminated fn is_session_terminated(&self) -> bool; } + diff --git a/server/src/discovery/mod.rs b/server/src/discovery/mod.rs index f6ac8fc7b614f061d618379b93867305252f71b8..e1131db039106197a4158f0e91552ba72ab6b8d5 100644 --- a/server/src/discovery/mod.rs +++ b/server/src/discovery/mod.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod traits; + diff --git a/server/src/discovery/traits/discovery_trait.rs b/server/src/discovery/traits/discovery_trait.rs index d446b5aee82728f8bc1b3931ed85365f6c0d14f4..d74e4a66edd5d881980204bb8a6c38a20054fbe8 100644 --- a/server/src/discovery/traits/discovery_trait.rs +++ b/server/src/discovery/traits/discovery_trait.rs @@ -1,4 +1,7 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub trait DiscoveryTrait { /// Registers the specified endpoints with the specified discovery server fn register_with_discovery_server(discovery_server_url: &str); } + diff --git a/server/src/discovery/traits/mod.rs b/server/src/discovery/traits/mod.rs index 24c8a1c503dd5d6dd0469666f464495fe42970ec..03041bc1142ee95fb5ec2818f39b562742c8be46 100644 --- a/server/src/discovery/traits/mod.rs +++ b/server/src/discovery/traits/mod.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod discovery_trait; + diff --git a/server/src/history/mod.rs b/server/src/history/mod.rs index f6ac8fc7b614f061d618379b93867305252f71b8..e1131db039106197a4158f0e91552ba72ab6b8d5 100644 --- a/server/src/history/mod.rs +++ b/server/src/history/mod.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod traits; + diff --git a/server/src/history/traits/mod.rs b/server/src/history/traits/mod.rs index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8b2c719dcba44b120d20cda29a6204eeed5a2510 100644 --- a/server/src/history/traits/mod.rs +++ b/server/src/history/traits/mod.rs @@ -0,0 +1,3 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + diff --git a/server/src/lib.rs b/server/src/lib.rs index 746c75c0cbcb1a0c12b099b804ecc1e226bdc52a..0982cdb8de6ae75f142a59a1f216d015d4577d5b 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 #![cfg_attr(feature = "alloc", no_std)] extern crate alloc; @@ -7,3 +9,4 @@ pub mod discovery; pub mod history; pub mod server_security; pub mod services; + diff --git a/server/src/server_security/mod.rs b/server/src/server_security/mod.rs index f6ac8fc7b614f061d618379b93867305252f71b8..e1131db039106197a4158f0e91552ba72ab6b8d5 100644 --- a/server/src/server_security/mod.rs +++ b/server/src/server_security/mod.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod traits; + diff --git a/server/src/server_security/traits/mod.rs b/server/src/server_security/traits/mod.rs index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..8b2c719dcba44b120d20cda29a6204eeed5a2510 100644 --- a/server/src/server_security/traits/mod.rs +++ b/server/src/server_security/traits/mod.rs @@ -0,0 +1,3 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 + diff --git a/server/src/services/mod.rs b/server/src/services/mod.rs index f6ac8fc7b614f061d618379b93867305252f71b8..e1131db039106197a4158f0e91552ba72ab6b8d5 100644 --- a/server/src/services/mod.rs +++ b/server/src/services/mod.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod traits; + diff --git a/server/src/services/traits/attribute_trait.rs b/server/src/services/traits/attribute_trait.rs index 01ac98e575623570675025c2ff513249ea5b9c6f..b761deb6d725afc2a477162db1aa64f4d6667458 100644 --- a/server/src/services/traits/attribute_trait.rs +++ b/server/src/services/traits/attribute_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait AttributeTrait { fn new() -> Result; @@ -14,3 +16,4 @@ pub trait AttributeTrait { /// Used to update or update historical values fn history_update(&self /* TODO */) -> Result; } + diff --git a/server/src/services/traits/discovery_trait.rs b/server/src/services/traits/discovery_trait.rs index 7aceb077b8af224ac9ea1532128d3f5286de08df..2c98051955a80534068215919382f8db20da6a9a 100644 --- a/server/src/services/traits/discovery_trait.rs +++ b/server/src/services/traits/discovery_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait DiscoverTrait { fn new() -> Result; @@ -8,3 +10,4 @@ pub trait DiscoverTrait { fn find_servers(&self /* TODO */) -> Result; } + diff --git a/server/src/services/traits/method_trait.rs b/server/src/services/traits/method_trait.rs index 310d07f360bf820f47eb5f198c0e5191aff3d9ba..9d1eb36b0bc9e2aeb17b54cad924f8b2267eab0c 100644 --- a/server/src/services/traits/method_trait.rs +++ b/server/src/services/traits/method_trait.rs @@ -1,6 +1,9 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait MethodTrait { fn new() -> Result; fn call(&self /* TODO */) -> Result; } + diff --git a/server/src/services/traits/mod.rs b/server/src/services/traits/mod.rs index e23ab1e3a683f6c007bc66b1b5cb93ab2aad9688..51f8f034d2b62cc75b4230db75887515b0056fb4 100644 --- a/server/src/services/traits/mod.rs +++ b/server/src/services/traits/mod.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub mod attribute_trait; pub mod discovery_trait; pub mod method_trait; @@ -7,3 +9,4 @@ pub mod query_trait; pub mod session_trait; pub mod subscription_trait; pub mod view_trait; + diff --git a/server/src/services/traits/monitored_item_trait.rs b/server/src/services/traits/monitored_item_trait.rs index 9a5236045ce3f7b0b53752a5607a3ca0a9768b94..cade736b371284b4f567127374367e692a6141ee 100644 --- a/server/src/services/traits/monitored_item_trait.rs +++ b/server/src/services/traits/monitored_item_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait MonitoredItemTrait { fn new() -> Result; @@ -12,3 +14,4 @@ pub trait MonitoredItemTrait { fn delete_monitored_items(&self /* TODO */) -> Result; } + diff --git a/server/src/services/traits/node_management_trait.rs b/server/src/services/traits/node_management_trait.rs index 6a3ce54bbcb7bd2b3b07079cf83676fd171be633..58bad6dec5c32f367546913865c5ec5acc712dea 100644 --- a/server/src/services/traits/node_management_trait.rs +++ b/server/src/services/traits/node_management_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait NodeManagementTrait { fn new() -> Result; @@ -10,3 +12,4 @@ pub trait NodeManagementTrait { fn delete_references(&self /* TODO */) -> Result; } + diff --git a/server/src/services/traits/query_trait.rs b/server/src/services/traits/query_trait.rs index 781a90ecd9f651ffd7a7eadb126c415d41cdb888..bf4bcc13ca8f0e377182d9e1ea1029e7992d2c65 100644 --- a/server/src/services/traits/query_trait.rs +++ b/server/src/services/traits/query_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait QuertTrait { fn new() -> Result; @@ -6,3 +8,4 @@ pub trait QuertTrait { fn query_next(&self /* TODO */) -> Result; } + diff --git a/server/src/services/traits/session_trait.rs b/server/src/services/traits/session_trait.rs index 181a089ada4be1e96960c83f4eacc196c05a0116..79858328670818e3134b6f627d1eda7211683229 100644 --- a/server/src/services/traits/session_trait.rs +++ b/server/src/services/traits/session_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait SessionTrait { fn new() -> Result; @@ -10,3 +12,4 @@ pub trait SessionTrait { fn cancel(&self /* TODO */) -> Result; } + diff --git a/server/src/services/traits/subscription_trait.rs b/server/src/services/traits/subscription_trait.rs index 6c91a5c341b6a3b2285005114311586488cabb22..e3f760d181f108ff4b6ee4bb907b88f0be234428 100644 --- a/server/src/services/traits/subscription_trait.rs +++ b/server/src/services/traits/subscription_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait SubscriptionTrait { fn new() -> Result; @@ -16,3 +18,4 @@ pub trait SubscriptionTrait { fn republish(&self /* TODO */) -> Result; } + diff --git a/server/src/services/traits/view_trait.rs b/server/src/services/traits/view_trait.rs index 520bfda385261813a5ef71413fc1d6585f320511..40d870fb83e1a3270120697559fd6671a9081be8 100644 --- a/server/src/services/traits/view_trait.rs +++ b/server/src/services/traits/view_trait.rs @@ -1,3 +1,5 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 use types::build_in::Result; pub trait ViewTrait { fn new() -> Result; @@ -10,3 +12,4 @@ pub trait ViewTrait { fn unregister_nodes(&self /* TODO */) -> Result; } + diff --git a/tools/Cargo.toml b/tools/Cargo.toml index 51ba7e8f5954cfee0c75b2af8b107e6dbc263ad1..8cb985572d4d24a6d545bb030f36d2acb3bf873e 100644 --- a/tools/Cargo.toml +++ b/tools/Cargo.toml @@ -6,9 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default=["alloc"] -alloc=["stdlib/alloc"] -std=["stdlib/std"] +default = ["alloc"] +alloc = ["stdlib/alloc"] +std = ["stdlib/std"] [dependencies] -stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false,branch="develop"} -types={path="../types"} +stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false, branch = "master" } +types = { path = "../types" } diff --git a/tools/src/io.rs b/tools/src/io.rs new file mode 100644 index 0000000000000000000000000000000000000000..1369473897a29257a166203393820dc434ad6ac4 --- /dev/null +++ b/tools/src/io.rs @@ -0,0 +1,240 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +mod test; +use core::sync::atomic::{AtomicU64, Ordering}; + +use alloc::vec::Vec; + +use crate::memory_poll::{MemoryPoll, CHUNKE_SIZE}; +#[derive(Debug)] +pub struct ChunkBuf { + inner: Vec, + pos: AtomicU64, +} + +unsafe impl Send for ChunkBuf {} +unsafe impl Sync for ChunkBuf {} + +impl ChunkBuf { + pub fn new() -> ChunkBuf { + let mut inner = Vec::::with_capacity(10); + inner.push(Self::alloc_memory_chunk()); + ChunkBuf { + inner, + pos: AtomicU64::new(0), + } + } + + fn alloc_memory_chunk() -> usize { + MemoryPoll::memroy_allocat().unwrap() as usize + } + + /// 返回光标当前的位置 + pub fn position(&self) -> u64 { + self.pos.load(Ordering::Relaxed) + } + + /// 返回剩余容量 + pub fn get_margin(&self) -> u64 { + let total_capacity = self.inner.len() * CHUNKE_SIZE; + total_capacity as u64 - self.position() + } + + // /// 返回剩余的片 + // pub fn remaining_slice(&self) -> &[u8] { + // let len = self.position().min(CHUNKE_SIZE as u64); + // unsafe { &(*(self.inner as *const [u8; CHUNKE_SIZE]))[(len as usize)..] } + // } + + /// slice 为空返回 true + pub fn is_empty(&self) -> bool { + self.position() >= self.get_margin() + } + + pub fn write(&mut self, buf: &[u8]) -> stdlib::io::Result { + let pos = self.pos.load(Ordering::Relaxed); + loop { + // 剩余容量 + let margin = self.get_margin(); + if pos == u64::MAX { + continue; + } + match self + .pos + .compare_exchange_weak(pos, u64::MAX, Ordering::SeqCst, Ordering::Relaxed) + { + Ok(_) => { + // 开始写入的索引位置 + let start_index = pos; + // 结束写入的索引位置 + let end_index = pos + buf.len() as u64; + // 当前应该写入的块索引 + let mut current_vec_index = pos / CHUNKE_SIZE as u64; + // pos 指向 chunk 中真实的索引 + let mut chunk_index = pos % CHUNKE_SIZE as u64; + // 若剩余容量与开始索引的和小于结束索引,则先分配内存再进行写入 + if margin + start_index < end_index { + // 内存不够,继续分配内存 + self.inner.push(Self::alloc_memory_chunk()); + // 将数据分段写入chunk + if chunk_index == 0 && margin == 0 { + // 满足上述条件则说明刚好写满一块内存,数据没有跨段存储 + current_vec_index -= 1; + chunk_index = CHUNKE_SIZE as u64; + } + // 首段数据 + let first_memeory_chunk_slice = unsafe { + &mut (*(self.inner[current_vec_index as usize] + as *mut [u8; CHUNKE_SIZE]))[(chunk_index as usize)..] + }; + // 尾段数据 + let last_memeory_chunk_slice = unsafe { + &mut (*(self.inner[(current_vec_index + 1) as usize] + as *mut [u8; CHUNKE_SIZE])) + [0..(buf.len() - margin as usize)] + }; + // 将 buf 按照 margin 进行拆分 + let (left, right) = unsafe { buf.split_at_unchecked(margin as usize) }; + // 首段写入left + first_memeory_chunk_slice.copy_from_slice(left); + // 尾段写入right + last_memeory_chunk_slice.copy_from_slice(right); + // 修改原子变量 pos + self.pos.store(pos + (buf.len()) as u64, Ordering::SeqCst); + return Ok(buf.len()); + } else { + let memeory_chunk_slice = unsafe { + &mut (*(self.inner[current_vec_index as usize] + as *mut [u8; CHUNKE_SIZE]))[(chunk_index + as usize) + ..(chunk_index as usize + buf.len() as usize)] + }; + memeory_chunk_slice.copy_from_slice(buf); + self.pos.store(pos + (buf.len()) as u64, Ordering::SeqCst); + return Ok(buf.len()); + } + } + Err(_) => { + continue; + } + } + } + } + + /// 从 point 处读取指定长度字节 + pub fn read(&mut self, buf: &mut [u8]) -> stdlib::io::Result { + // 准备读取字节数 + let len = buf.len() as u64; + // 当前光标位置 + let mut pos = self.position(); + // 实际读取长度 + let mut read_len; + loop { + if len > pos { + buf.get_mut(0..(pos as usize)) + .unwrap() + .copy_from_slice(self.read_all()); + read_len = pos; + } else { + // chunk 块内真实数据指针位 + let chunk_index = pos % CHUNKE_SIZE as u64; + // 当前pos指向的chunk块在vec中的索引位置 + let current_vec_index = pos / CHUNKE_SIZE as u64; + // 若len大于chunk_index则说明,buf被分段存储 + if len > chunk_index { + // 尾段长度 + let last_len = chunk_index; + // 首段长度 + let first_len = len - last_len; + // 尾段数据 + let last_chunk_res = unsafe { + &(*(self.inner[current_vec_index as usize] as *const [u8; CHUNKE_SIZE])) + [0..(chunk_index as usize)] + }; + // 首段数据 + let first_chunk_res = unsafe { + &(*(self.inner[(current_vec_index - 1) as usize] + as *const [u8; CHUNKE_SIZE])) + [(CHUNKE_SIZE - first_len as usize)..] + }; + // 首段写入buf + let _ = + &(*buf)[..(len - chunk_index) as usize].copy_from_slice(first_chunk_res); + // 尾段写入buf + let _ = &(*buf)[(len - chunk_index) as usize..].copy_from_slice(last_chunk_res); + } else { + let res = unsafe { + &(*(self.inner[current_vec_index as usize] as *const [u8; CHUNKE_SIZE])) + [((chunk_index - len) as usize)..(chunk_index as usize)] + }; + buf.copy_from_slice(res); + } + read_len = len; + } + match self.pos.compare_exchange_weak( + pos, + pos - read_len, + Ordering::SeqCst, + Ordering::Relaxed, + ) { + Ok(_) => return Ok(read_len as usize), + Err(v) => { + pos = v; + continue; + } + } + } + } + + pub fn read_exact(&mut self, mut buf: &mut [u8]) -> stdlib::io::Result { + while !buf.is_empty() { + match self.read(buf) { + Ok(0) => break, + Ok(n) => { + let tmp = buf; + buf = &mut tmp[n..]; + } + Err(e) => return Err(e), + } + } + if !buf.is_empty() { + Err(stdlib::io::Error::new( + "there is no more memory to allocate", + )) + } else { + Ok(buf.len()) + } + } + + /// 从 point 出读取全部字节 + fn read_all(&mut self) -> &[u8] { + let mut pos = self.position(); + + let mut res; + if pos == 0 { + return &[]; + } + loop { + res = unsafe { &(*(self.inner[0] as *const [u8; CHUNKE_SIZE]))[..(pos as usize)] }; + match self + .pos + .compare_exchange_weak(pos, 0, Ordering::SeqCst, Ordering::Relaxed) + { + Ok(_) => return res, + Err(v) => { + pos = v; + continue; + } + } + } + } +} + +impl Drop for ChunkBuf { + fn drop(&mut self) { + for item in &self.inner { + MemoryPoll::memory_free(*(item) as *const u8) + } + } +} + diff --git a/tools/src/io/test.rs b/tools/src/io/test.rs new file mode 100644 index 0000000000000000000000000000000000000000..628464c60ebcaf68ceef904c67035f391ee8e6a3 --- /dev/null +++ b/tools/src/io/test.rs @@ -0,0 +1,141 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +#[test] +fn test_write_read() { + use super::ChunkBuf; + use std::println; + extern crate std; + let mut chunk_buf = ChunkBuf::new(); + // 剩余容量 + println!("剩余容量 len:{}", chunk_buf.get_margin()); + // 0xFFu8 1 + let _ = chunk_buf.write(&0xFFu8.to_le_bytes()[..]); + // 0xFEu8 2 + let _ = chunk_buf.write(&0xFEu8.to_le_bytes()[..]); + // 0xFDu8 3 + let _ = chunk_buf.write(&0xFDu8.to_le_bytes()[..]); + // 0xFCu8 4 + let _ = chunk_buf.write(&0xFCu8.to_le_bytes()[..]); + // 0xFBu8 5 + let _ = chunk_buf.write(&0xFBu8.to_le_bytes()[..]); + // 0xFAu8 6 + let _ = chunk_buf.write(&0xFAu8.to_le_bytes()[..]); + // 0xF9u8 7 + let _ = chunk_buf.write(&0xF9u8.to_le_bytes()[..]); + // 0xF8u8 8 + let _ = chunk_buf.write(&0xF8u8.to_le_bytes()[..]); + // 0xF7u8 9 + let _ = chunk_buf.write(&0xF7u8.to_le_bytes()[..]); + // 0xFFF6u16 10,11 + let _ = chunk_buf.write(&0xFFF6u16.to_le_bytes()[..]); + // 0xF5u8 12 + let _ = chunk_buf.write(&0xF5u8.to_le_bytes()[..]); + // 0xF4u8 13 + let _ = chunk_buf.write(&0xF4u8.to_le_bytes()[..]); + // 0xF3u8 14 + let _ = chunk_buf.write(&0xF3u8.to_le_bytes()[..]); + // 0xF2u8 15 + let _ = chunk_buf.write(&0xF2u8.to_le_bytes()[..]); + // 0xF1u8 16 + let _ = chunk_buf.write(&0xF1u8.to_le_bytes()[..]); + // 0xF0u8 17 + let _ = chunk_buf.write(&0xF0u8.to_le_bytes()[..]); + // 0xEFu8 18 + let _ = chunk_buf.write(&0xEFu8.to_le_bytes()[..]); + // 0xEEu8 19 + let _ = chunk_buf.write(&0xEEu8.to_le_bytes()[..]); + // 0xEDu8 20 + let _ = chunk_buf.write(&0xEDu8.to_le_bytes()[..]); + // 0xECu8 21 + let _ = chunk_buf.write(&0xECu8.to_le_bytes()[..]); + // 剩余容量 + println!(" 剩余容量 len:{}", chunk_buf.get_margin()); + + let mut read_buf = [0u8; 1]; + // 0xECu8 21 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xECu8); + // 0xEDu8 20 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xEDu8); + // 0xEEu8 19 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xEEu8); + // 0xEFu8 18 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xEFu8); + // 0xF0u8 17 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF0u8); + // 0xF1u8 16 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF1u8); + // 0xF2u8 15 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF2u8); + // 0xF3u8 14 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF3u8); + // 0xF4u8 13 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF4u8); + // 0xF5u8 12 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF5u8); + // 0xFFF6u16 10,11 + let mut read_buf2 = [0u8; 2]; + let _ = chunk_buf.read(&mut read_buf2); + assert_eq!(u16::from_le_bytes(read_buf2), 0xFFF6u16); + // 0xF7u8 9 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF7u8); + // 0xF8u8 8 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF8u8); + // 0xF9u8 7 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xF9u8); + // 0xFAu8 6 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xFAu8); + // 0xFBu8 5 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xFBu8); + // 0xFCu8 4 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xFCu8); + // 0xFDu8 3 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xFDu8); + // 0xFEu8 2 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xFEu8); + // 0xFFu8 1 + let _ = chunk_buf.read(&mut read_buf); + assert_eq!(read_buf[0], 0xFFu8); +} + +#[test] +fn test_thread() { + use super::ChunkBuf; + use std::println; + extern crate std; + let mut chunk_buf = ChunkBuf::new(); + for i in 0..10 { + let chunk_buf_ref = &chunk_buf; + let mut_ref = unsafe { &mut *(chunk_buf_ref as *const _ as *mut ChunkBuf) }; + stdlib::thread::spawn(move || { + let _ = mut_ref.write(&(0xFFu8 - i).to_le_bytes()[..]); + println!("len:{}", mut_ref.get_margin()); + }) + .join(); + } + println!("len:{}", chunk_buf.get_margin()); + let mut buf = [0u8; 10]; + let _ = chunk_buf.read(&mut buf); + for item in buf.iter() { + println!("{}", item) + } + assert_eq!(chunk_buf.get_margin(), 10240); +} + diff --git a/tools/src/lib.rs b/tools/src/lib.rs index 809d0675dcdba2bcdceceecf45c704f5c1484859..fa0442813e29cca608bd4f8b98aa594b160def0c 100644 --- a/tools/src/lib.rs +++ b/tools/src/lib.rs @@ -1,2 +1,11 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 #![cfg_attr(feature = "alloc", no_std)] +#![feature(const_maybe_uninit_zeroed)] +#![feature(slice_split_at_unchecked)] extern crate alloc; +mod io; +mod memory_poll; +pub use io::*; +pub use memory_poll::*; + diff --git a/tools/src/memory_poll.rs b/tools/src/memory_poll.rs new file mode 100644 index 0000000000000000000000000000000000000000..c9bea47f7deb5d98281f7cf183079ed786bc0de6 --- /dev/null +++ b/tools/src/memory_poll.rs @@ -0,0 +1,116 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +mod test; +use core::mem::size_of; +use core::mem::MaybeUninit; +use core::sync::atomic::{AtomicUsize, Ordering}; +use stdlib::io::Result; +// pub const CHUNKE_SIZE: usize = 1024 * 10; +pub const CHUNKE_SIZE: usize = 10; +const CHUNKE_NUMBER: usize = 8; +const LAST_CHUNK: usize = CHUNKE_NUMBER - 1; +static mut POOL: [Chunk; CHUNKE_NUMBER] = unsafe { MaybeUninit::zeroed().assume_init() }; +static _MEMORY_HEADER: AtomicUsize = AtomicUsize::new(usize::max_value()); +static _HOLD_HEADER: AtomicUsize = AtomicUsize::new(0); +#[repr(C, align(8))] +struct Chunk { + next: *mut Chunk, + body: [u8; CHUNKE_SIZE - size_of::<*mut Chunk>()], +} + +pub struct MemoryPoll; + +impl MemoryPoll { + unsafe fn init() -> usize { + for i in &mut POOL[..CHUNKE_NUMBER - 1] { + i.next = (i as *mut Chunk).offset(1); + } + POOL[LAST_CHUNK].next = core::ptr::null_mut(); + &POOL[0] as *const _ as usize + } + + // 分配内存 + pub fn memroy_allocat() -> Result<*const u8> { + // 当前可使用的内存块指针 + let mut current = _MEMORY_HEADER.load(Ordering::SeqCst); + let mut spin = 0; + + loop { + match current { + 1 => { + current = _MEMORY_HEADER.load(Ordering::Acquire); + spin = spin + 1; + if spin < 100 { + core::hint::spin_loop(); + continue; + } + continue; + } + 0 => { + // 已经没有内存块可用 + return Err(stdlib::io::Error::new( + "there is no more memory to allocate", + )); + } + usize::MAX => { + match _MEMORY_HEADER.compare_exchange_weak( + current, + 1, + Ordering::AcqRel, + Ordering::Acquire, + ) { + Ok(_) => { + let rtn = unsafe { Self::init() }; + let current = unsafe { (*(rtn as *const Chunk)).next } as usize; + _MEMORY_HEADER.store(current, Ordering::Release); + return Ok(rtn as *const u8); + } + Err(s) => current = s, + } + } + _ => { + // 将 current 赋值给 _HOLD_HEADER + _HOLD_HEADER.store(current, Ordering::SeqCst); + let next = unsafe { (*(current as *const Chunk)).next } as usize; + match _MEMORY_HEADER.compare_exchange_weak( + current, + next, + Ordering::AcqRel, + Ordering::Acquire, + ) { + Ok(_) => { + return Ok(current as *const u8); + } + Err(s) => { + current = s; + continue; + } + } + } + } + } + } + + // 回收内存 + pub fn memory_free(memory_chunk: *const u8) { + loop { + let now_header = _MEMORY_HEADER.load(Ordering::SeqCst); + let return_header = unsafe { *(memory_chunk as *mut usize) }; + if return_header == _HOLD_HEADER.load(Ordering::SeqCst) { + continue; + } + unsafe { *(memory_chunk as *mut usize) = now_header }; + let result = _MEMORY_HEADER.compare_exchange( + now_header, + memory_chunk as usize, + Ordering::SeqCst, + Ordering::SeqCst, + ); + match result { + Ok(_) => break, + Err(_) => continue, + } + } + } +} + diff --git a/tools/src/memory_poll/test.rs b/tools/src/memory_poll/test.rs new file mode 100644 index 0000000000000000000000000000000000000000..c92bed7c3ee8e9ddc24b0893c47faee3e7bda724 --- /dev/null +++ b/tools/src/memory_poll/test.rs @@ -0,0 +1,31 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 +#[test] +fn memory_poll_test() { + use crate::memory_poll::MemoryPoll; + use alloc::string::ToString; + + extern crate std; + for _ in 0..4 { + stdlib::thread::spawn(|| { + for _ in 0..10000000 { + let memory_chunk = MemoryPoll::memroy_allocat(); + match memory_chunk { + Ok(pointer) => { + unsafe { + *(pointer as *mut [usize; 1280]) = + [usize::max_value(); (10 * 1024) / core::mem::size_of::()] + }; + MemoryPoll::memory_free(pointer); + // std::println!("{}", "分配成功,写入成功,内存回收成功"); + } + Err(e) => { + std::println!("{}", e.to_string()); + } + } + } + }) + .join(); + } +} + diff --git a/types/Cargo.toml b/types/Cargo.toml index 29b349a8050dc17cd1f0051a676f6d5d4deab22d..68c2d2a7af8a532e6eebdcf778dbdacddbb52b84 100644 --- a/types/Cargo.toml +++ b/types/Cargo.toml @@ -6,8 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default=["alloc"] -alloc=["stdlib/alloc"] -std=["stdlib/std"] +default = ["alloc"] +alloc = ["stdlib/alloc"] +std = ["stdlib/std"] [dependencies] -stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false,branch="develop"} +stdlib = { git = "https://gitee.com/iot-ua/stdlib.git", default-features = false, branch = "master" } diff --git a/types/src/build_in.rs b/types/src/build_in.rs index 9e6060faee6e350ecc9f10229f8e8f804acec092..53113fe62c059e3a1a88405a65f9635bb837775f 100644 --- a/types/src/build_in.rs +++ b/types/src/build_in.rs @@ -1 +1,4 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 pub struct Result; + diff --git a/types/src/lib.rs b/types/src/lib.rs index 2e62dbd4bfcfd6373000bfd15c4a518e17b24201..674169919292a4108b6059d6d81e5334a4838121 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -1,4 +1,7 @@ +// Copyright (C) 2022-2022 Agilor Co.,Ltd. All rights reserved. +// SPDX-License-Identifier: Apache-2.0 #![cfg_attr(feature = "alloc", no_std)] extern crate alloc; pub mod build_in; +