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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use aligned_buffer::{
	alloc::{AllocError, Allocator, BufferAllocator, Global, RawBuffer},
	SharedAlignedBuffer, UniqueAlignedBuffer, DEFAULT_BUFFER_ALIGNMENT,
};
use crossbeam_queue::ArrayQueue;
use std::{
	alloc::Layout,
	mem::ManuallyDrop,
	ptr::NonNull,
	sync::{Arc, Weak},
};

pub type UniquePooledAlignedBuffer<
	P,
	const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
	A = Global,
> = UniqueAlignedBuffer<
	ALIGNMENT,
	BufferPoolAllocator<P, ALIGNMENT, WeakAlignedBufferPool<P, ALIGNMENT, A>, A>,
>;

pub type SharedPooledAlignedBuffer<
	P,
	const ALIGNMENT: usize = DEFAULT_BUFFER_ALIGNMENT,
	A = Global,
> = SharedAlignedBuffer<
	ALIGNMENT,
	BufferPoolAllocator<P, ALIGNMENT, WeakAlignedBufferPool<P, ALIGNMENT, A>, A>,
>;

/// A policy for retaining buffers in the pool.
pub trait BufferRetentionPolicy: Clone {
	fn should_retain(&self, capaicty: usize) -> bool;
}

/// A policy that retains all buffers.
#[derive(Default, Clone, Copy)]
pub struct RetainAllRetentionPolicy;

impl BufferRetentionPolicy for RetainAllRetentionPolicy {
	#[inline(always)]
	fn should_retain(&self, _: usize) -> bool {
		true
	}
}

/// A policy that retains buffers up to a maximum size.
#[derive(Default, Clone, Copy)]
pub struct ConstMaxSizeRetentionPolicy<const SIZE: usize>;

impl<const SIZE: usize> BufferRetentionPolicy for ConstMaxSizeRetentionPolicy<SIZE> {
	#[inline(always)]
	fn should_retain(&self, capacity: usize) -> bool {
		capacity <= SIZE
	}
}

pub(crate) trait WeakAlignedBufferPoolRef<
	P: BufferRetentionPolicy,
	const ALIGNMENT: usize,
	A: Allocator + Clone,
>: Clone
{
	fn with<F>(&self, f: F)
	where
		F: FnOnce(&AlignedBufferPoolInner<P, ALIGNMENT, Self, A>);
}

pub struct BufferPoolAllocator<
	P: BufferRetentionPolicy,
	const ALIGNMENT: usize,
	R,
	A: Allocator + Clone,
> {
	policy: P,
	alloc: A,
	pool_ref: R,
}

unsafe impl<
		P: BufferRetentionPolicy,
		const ALIGNMENT: usize,
		R: WeakAlignedBufferPoolRef<P, ALIGNMENT, A>,
		A: Allocator + Clone,
	> Allocator for BufferPoolAllocator<P, ALIGNMENT, R, A>
{
	#[inline(always)]
	fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
		self.alloc.allocate(layout)
	}

	#[inline(always)]
	fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
		self.alloc.allocate_zeroed(layout)
	}

	#[inline(always)]
	unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
		self.alloc.deallocate(ptr, layout)
	}

	#[inline(always)]
	unsafe fn grow(
		&self,
		ptr: NonNull<u8>,
		old_layout: Layout,
		new_layout: Layout,
	) -> Result<NonNull<[u8]>, AllocError> {
		self.alloc.grow(ptr, old_layout, new_layout)
	}

	#[inline(always)]
	unsafe fn grow_zeroed(
		&self,
		ptr: NonNull<u8>,
		old_layout: Layout,
		new_layout: Layout,
	) -> Result<NonNull<[u8]>, AllocError> {
		self.alloc.grow_zeroed(ptr, old_layout, new_layout)
	}

	#[inline(always)]
	unsafe fn shrink(
		&self,
		ptr: NonNull<u8>,
		old_layout: Layout,
		new_layout: Layout,
	) -> Result<NonNull<[u8]>, AllocError> {
		self.alloc.shrink(ptr, old_layout, new_layout)
	}
}

impl<
		P: BufferRetentionPolicy,
		const ALIGNMENT: usize,
		R: WeakAlignedBufferPoolRef<P, ALIGNMENT, A>,
		A: Allocator + Clone,
	> BufferAllocator<ALIGNMENT> for BufferPoolAllocator<P, ALIGNMENT, R, A>
{
	unsafe fn deallocate_buffer(&self, raw: RawBuffer<ALIGNMENT>) {
		struct DeallocOnDrop<'a, const ALIGNMENT: usize, A: Allocator> {
			allocator: &'a A,
			raw: RawBuffer<ALIGNMENT>,
		}

		impl<'a, const ALIGNMENT: usize, A: Allocator> Drop for DeallocOnDrop<'a, ALIGNMENT, A> {
			fn drop(&mut self) {
				let (ptr, layout) = self.raw.alloc_info();
				unsafe { self.allocator.deallocate(ptr, layout) }
			}
		}

		let guard = DeallocOnDrop {
			allocator: &self.alloc,
			raw,
		};

		if self.policy.should_retain(raw.capacity().size()) {
			let alloc = self.clone();
			self.pool_ref.with(move |pool| {
				let unguard = ManuallyDrop::new(guard);
				let buf = UniqueAlignedBuffer::from_raw_parts_in(raw.buf_ptr(), 0, raw.capacity(), alloc);
				if let Err(pool) = pool.pool.push(buf) {
					// forget the pool so it doesn't get dropped
					std::mem::forget(pool);

					// then dealloc it using the guard
					drop(ManuallyDrop::into_inner(unguard));
				}
			});
		}
	}
}

impl<
		P: BufferRetentionPolicy,
		const ALIGNMENT: usize,
		R: WeakAlignedBufferPoolRef<P, ALIGNMENT, A>,
		A: Allocator + Clone,
	> Clone for BufferPoolAllocator<P, ALIGNMENT, R, A>
{
	fn clone(&self) -> Self {
		Self {
			policy: self.policy.clone(),
			alloc: self.alloc.clone(),
			pool_ref: self.pool_ref.clone(),
		}
	}
}

pub(crate) struct AlignedBufferPoolInner<
	P: BufferRetentionPolicy,
	const ALIGNMENT: usize,
	R: WeakAlignedBufferPoolRef<P, ALIGNMENT, A>,
	A: Allocator + Clone,
> {
	pool: ArrayQueue<UniqueAlignedBuffer<ALIGNMENT, BufferPoolAllocator<P, ALIGNMENT, R, A>>>,
	alloc: BufferPoolAllocator<P, ALIGNMENT, R, A>,
}

impl<
		P: BufferRetentionPolicy,
		const ALIGNMENT: usize,
		R: WeakAlignedBufferPoolRef<P, ALIGNMENT, A>,
		A: Allocator + Clone,
	> AlignedBufferPoolInner<P, ALIGNMENT, R, A>
{
	pub fn new(policy: P, alloc: A, self_ref: R, capacity: usize) -> Self {
		Self {
			pool: ArrayQueue::new(capacity),
			alloc: BufferPoolAllocator {
				policy,
				alloc,
				pool_ref: self_ref.clone(),
			},
		}
	}

	/// Gets a buffer from the pool. If the pool is empty, a new buffer is
	/// allocated and returned.
	#[inline]
	pub fn get(&self) -> UniqueAlignedBuffer<ALIGNMENT, BufferPoolAllocator<P, ALIGNMENT, R, A>> {
		if let Some(buf) = self.pool.pop() {
			buf
		} else {
			let alloc = self.alloc.clone();
			UniqueAlignedBuffer::new_in(alloc.clone())
		}
	}
}

/// A pool for allocating and recycling aligned buffers.
pub struct AlignedBufferPool<
	P: BufferRetentionPolicy,
	const ALIGNMENT: usize,
	A: Allocator + Clone = Global,
> {
	inner: Arc<AlignedBufferPoolInner<P, ALIGNMENT, WeakAlignedBufferPool<P, ALIGNMENT, A>, A>>,
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> AlignedBufferPool<P, ALIGNMENT, Global> {
	pub fn new(policy: P, capacity: usize) -> Self {
		Self::new_in(policy, capacity, Global)
	}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize> AlignedBufferPool<P, ALIGNMENT, Global>
where
	P: Default,
{
	pub fn with_capacity(capacity: usize) -> Self {
		Self::with_capacity_in(capacity, Global)
	}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize, A: Allocator + Clone>
	AlignedBufferPool<P, ALIGNMENT, A>
where
	P: Default,
{
	pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
		Self::new_in(P::default(), capacity, alloc)
	}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize, A: Allocator + Clone>
	AlignedBufferPool<P, ALIGNMENT, A>
{
	pub fn new_in(policy: P, capacity: usize, alloc: A) -> Self {
		Self {
			inner: Arc::new_cyclic(|weak| {
				let weak = WeakAlignedBufferPool {
					inner: weak.clone(),
				};
				AlignedBufferPoolInner::new(policy, alloc, weak, capacity)
			}),
		}
	}

	/// Gets a buffer from the pool. If the pool is empty, a new buffer is
	/// allocated and returned.
	#[inline]
	pub fn get(&self) -> UniquePooledAlignedBuffer<P, ALIGNMENT, A> {
		self.inner.get()
	}

	pub fn weak(&self) -> WeakAlignedBufferPool<P, ALIGNMENT, A> {
		WeakAlignedBufferPool {
			inner: Arc::downgrade(&self.inner),
		}
	}
}

pub struct WeakAlignedBufferPool<
	P: BufferRetentionPolicy,
	const ALIGNMENT: usize,
	A: Allocator + Clone = Global,
> {
	inner: Weak<AlignedBufferPoolInner<P, ALIGNMENT, WeakAlignedBufferPool<P, ALIGNMENT, A>, A>>,
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize, A: Allocator + Clone> Clone
	for WeakAlignedBufferPool<P, ALIGNMENT, A>
{
	#[inline]
	fn clone(&self) -> Self {
		Self {
			inner: self.inner.clone(),
		}
	}
}

impl<P: BufferRetentionPolicy, const ALIGNMENT: usize, A: Allocator + Clone>
	WeakAlignedBufferPoolRef<P, ALIGNMENT, A> for WeakAlignedBufferPool<P, ALIGNMENT, A>
{
	fn with<F>(&self, f: F)
	where
		F: FnOnce(&AlignedBufferPoolInner<P, ALIGNMENT, Self, A>),
	{
		if let Some(inner) = self.inner.upgrade() {
			f(&inner);
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn empty_pool_reuses_buffers() {
		let pool = AlignedBufferPool::<RetainAllRetentionPolicy, 64>::with_capacity(2);
		let mut buf = pool.get();
		buf.extend([1, 2, 3]);
		drop(buf);

		let buf = pool.get();
		assert!(buf.capacity() >= 3);
	}
}