use crate::error::CRDTResult;
use crate::memory::MemoryConfig;
use crate::traits::CRDT;
pub trait PlatformCRDT<C: MemoryConfig>: CRDT<C> {
type Platform: Platform;
fn target_platform() -> Self::Platform;
fn platform_init(&mut self) -> CRDTResult<()>;
fn platform_cleanup(&mut self) -> CRDTResult<()>;
fn platform_capabilities(&self) -> PlatformCapabilities;
fn is_platform_supported() -> bool;
fn platform_alignment() -> usize {
Self::Platform::memory_alignment()
}
fn platform_cache_line_size() -> usize {
Self::Platform::cache_line_size()
}
fn platform_merge(&mut self, other: &Self) -> CRDTResult<()>;
}
pub trait Platform {
const NAME: &'static str;
const ARCHITECTURE: Architecture;
fn memory_alignment() -> usize;
fn cache_line_size() -> usize;
fn max_interrupt_latency() -> u32;
fn supports_atomics() -> bool;
fn supports_fpu() -> bool;
fn supports_simd() -> bool;
fn features() -> PlatformFeatures;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Architecture {
CortexM,
CortexR,
TriCore,
RiscV,
X86,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PlatformCapabilities {
pub hardware_acceleration: bool,
pub dma_support: bool,
pub memory_protection: bool,
pub realtime_support: bool,
pub multicore_support: bool,
pub max_cores: u8,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PlatformFeatures {
pub atomics: bool,
pub fpu: bool,
pub simd: bool,
pub hardware_crc: bool,
pub hardware_crypto: bool,
pub mmu: bool,
pub cache_coherent: bool,
}
pub struct AurixPlatform;
impl Platform for AurixPlatform {
const NAME: &'static str = "AURIX";
const ARCHITECTURE: Architecture = Architecture::TriCore;
fn memory_alignment() -> usize {
4
}
fn cache_line_size() -> usize {
32
}
fn max_interrupt_latency() -> u32 {
100
}
fn supports_atomics() -> bool {
true
}
fn supports_fpu() -> bool {
true
}
fn supports_simd() -> bool {
false
}
fn features() -> PlatformFeatures {
PlatformFeatures {
atomics: true,
fpu: true,
simd: false,
hardware_crc: true,
hardware_crypto: true,
mmu: true,
cache_coherent: true,
}
}
}
pub struct STM32Platform;
impl Platform for STM32Platform {
const NAME: &'static str = "STM32";
const ARCHITECTURE: Architecture = Architecture::CortexM;
fn memory_alignment() -> usize {
4
}
fn cache_line_size() -> usize {
32
}
fn max_interrupt_latency() -> u32 {
50
}
fn supports_atomics() -> bool {
true
}
fn supports_fpu() -> bool {
true
}
fn supports_simd() -> bool {
false
}
fn features() -> PlatformFeatures {
PlatformFeatures {
atomics: true,
fpu: true,
simd: false,
hardware_crc: true,
hardware_crypto: false,
mmu: false,
cache_coherent: false,
}
}
}
pub struct CortexMPlatform;
impl Platform for CortexMPlatform {
const NAME: &'static str = "Cortex-M";
const ARCHITECTURE: Architecture = Architecture::CortexM;
fn memory_alignment() -> usize {
4
}
fn cache_line_size() -> usize {
32
}
fn max_interrupt_latency() -> u32 {
25
}
fn supports_atomics() -> bool {
true
}
fn supports_fpu() -> bool {
false
}
fn supports_simd() -> bool {
false
}
fn features() -> PlatformFeatures {
PlatformFeatures {
atomics: true,
fpu: false,
simd: false,
hardware_crc: false,
hardware_crypto: false,
mmu: false,
cache_coherent: false,
}
}
}
pub struct RiscVPlatform;
impl Platform for RiscVPlatform {
const NAME: &'static str = "RISC-V";
const ARCHITECTURE: Architecture = Architecture::RiscV;
fn memory_alignment() -> usize {
4
}
fn cache_line_size() -> usize {
64
}
fn max_interrupt_latency() -> u32 {
30
}
fn supports_atomics() -> bool {
true
}
fn supports_fpu() -> bool {
true
}
fn supports_simd() -> bool {
true
}
fn features() -> PlatformFeatures {
PlatformFeatures {
atomics: true,
fpu: true,
simd: true,
hardware_crc: false,
hardware_crypto: false,
mmu: true,
cache_coherent: true,
}
}
}
pub trait PlatformOptimized<C: MemoryConfig, P: Platform>: PlatformCRDT<C> {
fn optimized_serialize(&self, buffer: &mut [u8]) -> CRDTResult<usize>;
fn optimized_deserialize(buffer: &[u8]) -> CRDTResult<Self>
where
Self: Sized;
fn platform_hash(&self) -> u32;
fn platform_memcpy(&mut self, src: &Self) -> CRDTResult<()>;
fn performance_metrics(&self) -> PerformanceMetrics;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PerformanceMetrics {
pub merge_cycles: u32,
pub serialize_cycles: u32,
pub hash_cycles: u32,
pub memcpy_cycles: u32,
pub cache_misses: u32,
}
pub trait MultiCorePlatform<C: MemoryConfig>: PlatformCRDT<C> {
fn core_count() -> u8;
fn current_core_id() -> u8;
fn sync_cores(&mut self) -> CRDTResult<()>;
fn distribute_work(&mut self, core_mask: u8) -> CRDTResult<()>;
fn collect_results(&mut self) -> CRDTResult<()>;
fn supports_core_local_ops() -> bool;
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error::CRDTError;
use crate::memory::DefaultConfig;
struct MockPlatformCRDT {
value: u32,
initialized: bool,
}
impl MockPlatformCRDT {
fn new() -> Self {
Self {
value: 0,
initialized: false,
}
}
}
impl CRDT<DefaultConfig> for MockPlatformCRDT {
type Error = CRDTError;
fn merge(&mut self, other: &Self) -> CRDTResult<()> {
self.value = self.value.max(other.value);
Ok(())
}
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
fn size_bytes(&self) -> usize {
core::mem::size_of::<Self>()
}
fn validate(&self) -> CRDTResult<()> {
Ok(())
}
fn state_hash(&self) -> u32 {
self.value
}
fn can_merge(&self, _other: &Self) -> bool {
true
}
}
impl PlatformCRDT<DefaultConfig> for MockPlatformCRDT {
type Platform = STM32Platform;
fn target_platform() -> Self::Platform {
STM32Platform
}
fn platform_init(&mut self) -> CRDTResult<()> {
self.initialized = true;
Ok(())
}
fn platform_cleanup(&mut self) -> CRDTResult<()> {
self.initialized = false;
Ok(())
}
fn platform_capabilities(&self) -> PlatformCapabilities {
PlatformCapabilities {
hardware_acceleration: false,
dma_support: true,
memory_protection: false,
realtime_support: true,
multicore_support: false,
max_cores: 1,
}
}
fn is_platform_supported() -> bool {
true
}
fn platform_merge(&mut self, other: &Self) -> CRDTResult<()> {
self.merge(other)
}
}
#[test]
fn test_platform_features() {
assert_eq!(STM32Platform::NAME, "STM32");
assert_eq!(STM32Platform::ARCHITECTURE, Architecture::CortexM);
assert_eq!(STM32Platform::memory_alignment(), 4);
assert_eq!(STM32Platform::cache_line_size(), 32);
assert!(STM32Platform::supports_atomics());
assert!(STM32Platform::supports_fpu());
assert!(!STM32Platform::supports_simd());
let features = STM32Platform::features();
assert!(features.atomics);
assert!(features.fpu);
assert!(!features.simd);
assert!(features.hardware_crc);
assert!(!features.hardware_crypto);
}
#[test]
fn test_platform_crdt() {
let mut crdt = MockPlatformCRDT::new();
assert!(!crdt.initialized);
assert!(MockPlatformCRDT::is_platform_supported());
assert!(crdt.platform_init().is_ok());
assert!(crdt.initialized);
let capabilities = crdt.platform_capabilities();
assert!(capabilities.dma_support);
assert!(capabilities.realtime_support);
assert!(!capabilities.multicore_support);
assert_eq!(capabilities.max_cores, 1);
assert!(crdt.platform_cleanup().is_ok());
assert!(!crdt.initialized);
}
#[test]
fn test_platform_alignment() {
assert_eq!(MockPlatformCRDT::platform_alignment(), 4);
assert_eq!(MockPlatformCRDT::platform_cache_line_size(), 32);
}
#[test]
fn test_architecture_types() {
assert_eq!(AurixPlatform::ARCHITECTURE, Architecture::TriCore);
assert_eq!(STM32Platform::ARCHITECTURE, Architecture::CortexM);
assert_eq!(CortexMPlatform::ARCHITECTURE, Architecture::CortexM);
assert_eq!(RiscVPlatform::ARCHITECTURE, Architecture::RiscV);
}
}