-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_initialization.rs
More file actions
125 lines (103 loc) · 3.46 KB
/
custom_initialization.rs
File metadata and controls
125 lines (103 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Custom initialization and configuration example.
use fastalloc::{GrowingPool, GrowthStrategy, PoolConfig};
fn main() {
println!("=== Custom Initialization Example ===\n");
// Example 1: Custom initializer
println!("1. Pool with Custom Initializer:");
let config = PoolConfig::builder()
.capacity(10)
.max_capacity(Some(50))
.growth_strategy(GrowthStrategy::Exponential { factor: 2.0 })
.alignment(64) // Cache-line aligned
.initializer(|| {
println!(" [Init] Creating new object");
String::from("initialized")
})
.build()
.expect("Invalid configuration");
let pool = GrowingPool::with_config(config).expect("Failed to create pool");
println!("Pool created with custom initializer");
println!(
"Capacity: {}, Available: {}\n",
pool.capacity(),
pool.available()
);
// Example 2: Custom reset function
println!("2. Pool with Reset Function:");
#[derive(Debug, Clone)]
struct Buffer {
data: Vec<u8>,
position: usize,
}
impl fastalloc::Poolable for Buffer {}
let config = PoolConfig::builder()
.capacity(5)
.reset_fn(
|| Buffer {
data: Vec::with_capacity(1024),
position: 0,
},
|buffer| {
// Reset buffer when returned to pool
buffer.data.clear();
buffer.position = 0;
println!(" [Reset] Buffer cleaned");
},
)
.build()
.expect("Invalid configuration");
let buffer_pool = GrowingPool::with_config(config).expect("Failed to create pool");
{
let mut buf = buffer_pool
.allocate(Buffer {
data: vec![1, 2, 3, 4, 5],
position: 5,
})
.unwrap();
println!("Buffer allocated: {:?}", *buf);
buf.data.extend_from_slice(&[6, 7, 8]);
println!("Buffer modified: {:?}", *buf);
} // Buffer returned to pool and reset function is called
println!();
// Example 3: Different growth strategies
println!("3. Growth Strategies:");
// Linear growth
let linear_config = PoolConfig::builder()
.capacity(10)
.growth_strategy(GrowthStrategy::Linear { amount: 5 })
.build()
.unwrap();
let linear_pool = GrowingPool::<i32>::with_config(linear_config).unwrap();
println!(
"Linear growth pool: initial capacity = {}",
linear_pool.capacity()
);
// Exponential growth
let exp_config = PoolConfig::builder()
.capacity(10)
.growth_strategy(GrowthStrategy::Exponential { factor: 2.0 })
.build()
.unwrap();
let exp_pool = GrowingPool::<i32>::with_config(exp_config).unwrap();
println!(
"Exponential growth pool: initial capacity = {}",
exp_pool.capacity()
);
// Custom growth
let custom_config = PoolConfig::builder()
.capacity(10)
.growth_strategy(GrowthStrategy::Custom {
compute: Box::new(|current| {
// Grow by 50% rounded up
(current as f32 * 1.5).ceil() as usize
}),
})
.build()
.unwrap();
let custom_pool = GrowingPool::<i32>::with_config(custom_config).unwrap();
println!(
"Custom growth pool: initial capacity = {}",
custom_pool.capacity()
);
println!("\n=== Example Complete ===");
}