Skip to content

Commit e6077eb

Browse files
committed
feat: support unaligned allocations from the pool
Use unaligned allocations for strings.
1 parent 3aedd7c commit e6077eb

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

examples/httporigdst.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl Default for NgxHttpOrigDstCtx {
2828

2929
impl NgxHttpOrigDstCtx {
3030
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &mut core::Pool) -> core::Status {
31-
let addr_data = pool.alloc(addr.len());
31+
let addr_data = pool.alloc_unaligned(addr.len());
3232
if addr_data.is_null() {
3333
return core::Status::NGX_ERROR;
3434
}
@@ -37,7 +37,7 @@ impl NgxHttpOrigDstCtx {
3737
self.orig_dst_addr.data = addr_data as *mut u8;
3838

3939
let port_str = port.to_string();
40-
let port_data = pool.alloc(port_str.len());
40+
let port_data = pool.alloc_unaligned(port_str.len());
4141
if port_data.is_null() {
4242
return core::Status::NGX_ERROR;
4343
}

nginx-sys/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub use bindings::*;
5757
/// * `data` - The string slice to convert to a raw pointer.
5858
///
5959
/// # Safety
60-
/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory allocation using `ngx_palloc`.
60+
/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory allocation using `ngx_pnalloc`.
6161
///
6262
/// # Returns
6363
/// A raw pointer (`*mut u_char`) to the allocated memory containing the converted string data.
@@ -69,7 +69,7 @@ pub use bindings::*;
6969
/// let ptr = str_to_uchar(pool, data);
7070
/// ```
7171
pub unsafe fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
72-
let ptr: *mut u_char = ngx_palloc(pool, data.len() as _) as _;
72+
let ptr: *mut u_char = ngx_pnalloc(pool, data.len()) as _;
7373
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
7474
ptr
7575
}

src/core/pool.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,51 @@ impl Pool {
8585
}
8686

8787
/// Allocates memory from the pool of the specified size.
88+
/// The resulting pointer is aligned to a platform word size.
8889
///
8990
/// Returns a raw pointer to the allocated memory.
9091
pub fn alloc(&mut self, size: usize) -> *mut c_void {
9192
unsafe { ngx_palloc(self.0, size) }
9293
}
9394

9495
/// Allocates memory for a type from the pool.
96+
/// The resulting pointer is aligned to a platform word size.
9597
///
9698
/// Returns a typed pointer to the allocated memory.
9799
pub fn alloc_type<T: Copy>(&mut self) -> *mut T {
98100
self.alloc(mem::size_of::<T>()) as *mut T
99101
}
100102

101103
/// Allocates zeroed memory from the pool of the specified size.
104+
/// The resulting pointer is aligned to a platform word size.
102105
///
103106
/// Returns a raw pointer to the allocated memory.
104107
pub fn calloc(&mut self, size: usize) -> *mut c_void {
105108
unsafe { ngx_pcalloc(self.0, size) }
106109
}
107110

108111
/// Allocates zeroed memory for a type from the pool.
112+
/// The resulting pointer is aligned to a platform word size.
109113
///
110114
/// Returns a typed pointer to the allocated memory.
111115
pub fn calloc_type<T: Copy>(&mut self) -> *mut T {
112116
self.calloc(mem::size_of::<T>()) as *mut T
113117
}
114118

119+
/// Allocates unaligned memory from the pool of the specified size.
120+
///
121+
/// Returns a raw pointer to the allocated memory.
122+
pub fn alloc_unaligned(&mut self, size: usize) -> *mut c_void {
123+
unsafe { ngx_pnalloc(self.0, size) }
124+
}
125+
126+
/// Allocates unaligned memory for a type from the pool.
127+
///
128+
/// Returns a typed pointer to the allocated memory.
129+
pub fn alloc_type_unaligned<T: Copy>(&mut self) -> *mut T {
130+
self.alloc_unaligned(mem::size_of::<T>()) as *mut T
131+
}
132+
115133
/// Allocates memory for a value of a specified type and adds a cleanup handler to the memory pool.
116134
///
117135
/// Returns a typed pointer to the allocated memory if successful, or a null pointer if allocation or cleanup handler addition fails.

0 commit comments

Comments
 (0)