Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
name: CI
on: [push, pull_request]
jobs:
test:
name: Test (${{ matrix.os }}, ${{ matrix.rust }})
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.experimental }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
rust: [stable, beta]
experimental: [false]
include:
- os: ubuntu-latest
rust: nightly
experimental: true
- os: windows-latest
rust: nightly
experimental: true
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install rust (${{ matrix.rust }})
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
- name: Build
run: cargo build
- name: Test
run: cargo test --verbose

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/rust-toolchain@stable
with:
components: clippy
- name: Clippy
run: cargo clippy -- -D warnings

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt
- name: Format check
run: cargo fmt -- --check

doc:
name: Documentation
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4
- name: Install rust
uses: dtolnay/rust-toolchain@stable
- name: Build documentation
run: cargo doc --verbose --no-deps
11 changes: 0 additions & 11 deletions .travis.yml

This file was deleted.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ homepage = "https://github.com/sozu-proxy/poule"
repository = "https://github.com/sozu-proxy/poule"
readme = "README.md"
keywords = ["pool"]
edition = "2018"
edition = "2024"
rust-version = "1.85.0"
exclude = [
".gitignore",
".travis.yml",
"deploy.sh",
".github/",
"test/**/*",
]

[dependencies]
libc = "0.2"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.48", features = ["Win32_Foundation", "Win32_System_Memory"] }
windows-sys = { version = "0.61", features = ["Win32_System_Memory"] }

[[test]]
name = "test"
Expand Down
32 changes: 26 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Known clippy lints from the unsafe transmute-heavy pool internals.
// These should be addressed in a future refactor replacing transmutes with proper pointer casts.
#![allow(
clippy::len_without_is_empty,
clippy::legacy_numeric_constants,
clippy::missing_transmute_annotations,
clippy::mut_from_ref,
clippy::ptr_offset_with_cast,
clippy::transmute_ptr_to_ref,
clippy::unnecessary_cast,
clippy::useless_transmute
)]

//! # A store of pre-initialized values.
//!
//! Values can be checked out when needed, operated on, and will automatically
Expand Down Expand Up @@ -47,8 +60,8 @@
//! to wrap `Pool` in a mutex.
pub use reset::{Dirty, Reset};
use std::cell::UnsafeCell;
use std::sync::atomic::{self, AtomicUsize, Ordering};
use std::sync::Arc;
use std::sync::atomic::{self, AtomicUsize, Ordering};
use std::{mem, ops, ptr, usize};

mod mmap;
Expand Down Expand Up @@ -275,8 +288,9 @@ impl<T> PoolInner<T> {
}

fn initialize<F>(&mut self, initializer: F) -> bool
where F: Fn() -> T {

where
F: Fn() -> T,
{
if self.init < self.count {
unsafe {
ptr::write(
Expand All @@ -286,7 +300,7 @@ impl<T> PoolInner<T> {
next: self.init + 1,
extra: self.entry_size - mem::size_of::<Entry<T>>(),
},
);
);
}
self.init += 1;

Expand All @@ -311,7 +325,10 @@ impl<T> PoolInner<T> {

debug_assert!(nxt <= self.init, "invalid next index: {}", idx);

match self.next.compare_exchange(idx, nxt, Ordering::Relaxed, Ordering::Relaxed) {
match self
.next
.compare_exchange(idx, nxt, Ordering::Relaxed, Ordering::Relaxed)
{
Ok(_) => break,
Err(res) => {
// Re-acquire the memory before trying again
Expand Down Expand Up @@ -343,7 +360,10 @@ impl<T> PoolInner<T> {
// Update the entry's next pointer
entry.next = nxt;

match self.next.compare_exchange(nxt, idx, Ordering::Release, Ordering::Relaxed) {
match self
.next
.compare_exchange(nxt, idx, Ordering::Release, Ordering::Relaxed)
{
Ok(_) => break,
Err(actual) => nxt = actual,
}
Expand Down
33 changes: 8 additions & 25 deletions src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ use std::{

#[cfg(unix)]
use libc::{
mmap, mprotect, munmap, MAP_ANON, MAP_FAILED, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE,
MAP_ANON, MAP_FAILED, MAP_PRIVATE, PROT_NONE, PROT_READ, PROT_WRITE, mmap, mprotect, munmap,
};

#[cfg(windows)]
use windows_sys::Win32::System::Memory::{
VirtualAlloc, VirtualFree, VirtualProtect,
MEM_COMMIT, MEM_RELEASE, MEM_RESERVE,
PAGE_NOACCESS, PAGE_READWRITE,
MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE, VirtualAlloc, VirtualFree,
VirtualProtect,
};

/// Memory map backend for the pool
Expand All @@ -39,14 +38,8 @@ impl GrowableMemoryMap {
pub fn new(capacity: usize) -> Result<Self, &'static str> {
let capacity = page_size(capacity);

let ptr = unsafe {
VirtualAlloc(
std::ptr::null_mut(),
capacity,
MEM_RESERVE,
PAGE_NOACCESS,
)
};
let ptr =
unsafe { VirtualAlloc(std::ptr::null_mut(), capacity, MEM_RESERVE, PAGE_NOACCESS) };

if ptr.is_null() {
return Err("could not map memory");
Expand All @@ -72,22 +65,12 @@ impl GrowableMemoryMap {

unsafe {
// Commit the memory and change protection
if VirtualAlloc(
self.ptr as _,
size,
MEM_COMMIT,
PAGE_READWRITE,
).is_null() {
if VirtualAlloc(self.ptr as _, size, MEM_COMMIT, PAGE_READWRITE).is_null() {
return Err("could not commit memory");
}

let mut old_protect = 0;
if VirtualProtect(
self.ptr as _,
size,
PAGE_READWRITE,
&mut old_protect,
) == 0 {
if VirtualProtect(self.ptr as _, size, PAGE_READWRITE, &mut old_protect) == 0 {
return Err("could not change permissions on memory");
}
}
Expand Down Expand Up @@ -188,7 +171,7 @@ impl Drop for GrowableMemoryMap {
}

pub fn page_size(data_len: usize) -> usize {
let page_size = if cfg!(windows) { 0x1000 } else { 0x1000 };
let page_size = 0x1000;
let count = data_len / page_size;
let rem = data_len % page_size;

Expand Down
2 changes: 0 additions & 2 deletions test/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate poule;

use poule::{Checkout, Pool, Reset};
use std::ops::{Deref, DerefMut};

Expand Down
16 changes: 8 additions & 8 deletions test/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate poule;

use poule::{Dirty, Pool};

#[test]
Expand Down Expand Up @@ -51,20 +49,22 @@ pub fn test_depleting_pool() {
);
}

assert!(pool
.checkout(|| {
assert!(
pool.checkout(|| {
println!("initializing element B");
0
})
.is_none());
.is_none()
);
drop(vec);
println!("dropped vec");
assert!(pool
.checkout(|| {
assert!(
pool.checkout(|| {
println!("initializing element C");
0
})
.is_some());
.is_some()
);
}

#[test]
Expand Down