Skip to content

Commit 2ad203c

Browse files
committed
refactor: overlook pool methods
1 parent 0ba4fa7 commit 2ad203c

File tree

5 files changed

+63
-91
lines changed

5 files changed

+63
-91
lines changed

examples/httporigdst.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,14 @@ struct NgxHttpOrigDstCtx {
1919
}
2020

2121
impl NgxHttpOrigDstCtx {
22-
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &core::Pool) -> core::Status {
23-
let addr_data = pool.alloc_unaligned(addr.len());
24-
if addr_data.is_null() {
25-
return core::Status::NGX_ERROR;
26-
}
22+
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &core::Pool) -> core::NgxResult {
23+
let addr_data = pool.allocate_unaligned(addr.len())?;
2724
unsafe { libc::memcpy(addr_data, addr.as_ptr() as *const c_void, addr.len()) };
2825
self.orig_dst_addr.len = addr.len();
2926
self.orig_dst_addr.data = addr_data as *mut u8;
3027

3128
let port_str = port.to_string();
32-
let port_data = pool.alloc_unaligned(port_str.len());
33-
if port_data.is_null() {
34-
return core::Status::NGX_ERROR;
35-
}
29+
let port_data = pool.allocate_unaligned(port_str.len())?;
3630
unsafe {
3731
libc::memcpy(
3832
port_data,
@@ -43,7 +37,7 @@ impl NgxHttpOrigDstCtx {
4337
self.orig_dst_port.len = port_str.len();
4438
self.orig_dst_port.data = port_data as *mut u8;
4539

46-
core::Status::NGX_OK
40+
Ok(core::Status::NGX_OK.into())
4741
}
4842

4943
pub unsafe fn bind_addr(&self, v: *mut ngx_variable_value_t) {
@@ -222,7 +216,7 @@ http_variable_get!(
222216
ip,
223217
port,
224218
);
225-
(*new_ctx).save(&ip, port, &request.pool());
219+
(*new_ctx).save(&ip, port, &request.pool())?;
226220
(*new_ctx).bind_addr(v);
227221
request
228222
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
@@ -265,7 +259,7 @@ http_variable_get!(
265259
ip,
266260
port,
267261
);
268-
(*new_ctx).save(&ip, port, &request.pool());
262+
(*new_ctx).save(&ip, port, &request.pool())?;
269263
(*new_ctx).bind_port(v);
270264
request
271265
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));

examples/shared_dict.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,10 @@ extern "C" fn ngx_http_shared_dict_add_variable(
200200
let cf = unsafe { cf.as_mut().unwrap() };
201201
let pool = unsafe { Pool::from_ngx_pool(cf.pool) };
202202

203-
let key = pool.calloc_type::<ngx_http_complex_value_t>();
204-
if key.is_null() {
205-
return NGX_CONF_ERROR;
206-
}
203+
let key = match pool.allocate_type_zeroed::<ngx_http_complex_value_t>() {
204+
Ok(p) => p,
205+
Err(_) => return NGX_CONF_ERROR,
206+
};
207207

208208
// SAFETY:
209209
// - `cf.args` is guaranteed to be a pointer to an array with 3 elements (NGX_CONF_TAKE2).

examples/upstream.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -120,44 +120,34 @@ http_upstream_init_peer_pt!(
120120
|request: &mut Request, us: *mut ngx_http_upstream_srv_conf_t| {
121121
ngx_log_debug_http!(request, "CUSTOM UPSTREAM request peer init");
122122

123-
let hcpd = request.pool().alloc_type::<UpstreamPeerData>();
124-
if hcpd.is_null() {
125-
return Status::NGX_ERROR;
126-
}
123+
let hcpd = request.pool().allocate_type::<UpstreamPeerData>()?;
127124

128125
// SAFETY: this function is called with non-NULL uf always
129126
let us = unsafe { &mut *us };
130-
let hccf = match Module::server_conf(us) {
131-
Some(x) => x,
132-
None => return Status::NGX_ERROR,
133-
};
127+
let hccf = Module::server_conf(us).ok_or(ngx::core::NgxError {})?;
134128

135129
let original_init_peer = hccf.original_init_peer.unwrap();
136130
if unsafe { original_init_peer(request.into(), us) != Status::NGX_OK.into() } {
137-
return Status::NGX_ERROR;
131+
return Status::NGX_ERROR.into();
138132
}
139133

140-
let maybe_upstream = request.upstream();
141-
if maybe_upstream.is_none() {
142-
return Status::NGX_ERROR;
143-
}
144-
let upstream_ptr = maybe_upstream.unwrap();
134+
let upstream_ptr = request.upstream().ok_or(ngx::core::NgxError {})?;
145135

146136
unsafe {
147137
(*hcpd).conf = Some(hccf);
148-
(*hcpd).upstream = maybe_upstream;
138+
(*hcpd).upstream = Some(upstream_ptr);
149139
(*hcpd).data = (*upstream_ptr).peer.data;
150140
(*hcpd).client_connection = Some(request.connection());
151141
(*hcpd).original_get_peer = (*upstream_ptr).peer.get;
152142
(*hcpd).original_free_peer = (*upstream_ptr).peer.free;
153143

154-
(*upstream_ptr).peer.data = hcpd as *mut c_void;
144+
(*upstream_ptr).peer.data = hcpd as _;
155145
(*upstream_ptr).peer.get = Some(ngx_http_upstream_get_custom_peer);
156146
(*upstream_ptr).peer.free = Some(ngx_http_upstream_free_custom_peer);
157147
}
158148

159149
ngx_log_debug_http!(request, "CUSTOM UPSTREAM end request peer init");
160-
Status::NGX_OK
150+
Status::NGX_OK.into()
161151
}
162152
);
163153

@@ -312,24 +302,25 @@ impl HttpModule for Module {
312302

313303
unsafe extern "C" fn create_srv_conf(cf: *mut ngx_conf_t) -> *mut c_void {
314304
let pool = Pool::from_ngx_pool((*cf).pool);
315-
let conf = pool.alloc_type::<SrvConfig>();
316-
if conf.is_null() {
305+
306+
if let Ok(conf) = pool.allocate_type::<SrvConfig>() {
307+
unsafe {
308+
(*conf).max = NGX_CONF_UNSET as u32;
309+
}
310+
ngx_log_debug_mask!(
311+
DebugMask::Http,
312+
(*cf).log,
313+
"CUSTOM UPSTREAM end create_srv_conf"
314+
);
315+
conf as *mut c_void
316+
} else {
317317
ngx_conf_log_error!(
318318
NGX_LOG_EMERG,
319319
cf,
320320
"CUSTOM UPSTREAM could not allocate memory for config"
321321
);
322-
return std::ptr::null_mut();
322+
std::ptr::null_mut()
323323
}
324-
325-
(*conf).max = NGX_CONF_UNSET as u32;
326-
327-
ngx_log_debug_mask!(
328-
DebugMask::Http,
329-
(*cf).log,
330-
"CUSTOM UPSTREAM end create_srv_conf"
331-
);
332-
conf as *mut c_void
333324
}
334325
}
335326

src/core/pool.rs

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use core::mem;
44
use core::ptr::{self, NonNull};
55

66
use nginx_sys::{
7-
ngx_buf_t, ngx_create_temp_buf, ngx_palloc, ngx_pcalloc, ngx_pfree, ngx_pmemalign, ngx_pnalloc,
7+
ngx_buf_t, ngx_create_temp_buf, ngx_palloc, ngx_pfree, ngx_pmemalign, ngx_pnalloc,
88
ngx_pool_cleanup_add, ngx_pool_t, NGX_ALIGNMENT,
99
};
1010

@@ -183,10 +183,7 @@ impl Pool {
183183
/// Returns `Some(MemoryBuffer)` if the buffer is successfully created, or `None` if allocation
184184
/// fails.
185185
pub fn create_buffer_from_static_str(&self, str: &'static str) -> Option<MemoryBuffer> {
186-
let buf = self.calloc_type::<ngx_buf_t>();
187-
if buf.is_null() {
188-
return None;
189-
}
186+
let buf = self.allocate(Layout::new::<ngx_buf_t>()).ok()?.as_ptr() as *mut ngx_buf_t;
190187

191188
// We cast away const, but buffers with the memory flag are read-only
192189
let start = str.as_ptr() as *mut u8;
@@ -229,44 +226,6 @@ impl Pool {
229226
unsafe { ngx_palloc(self.0.as_ptr(), size) }
230227
}
231228

232-
/// Allocates memory for a type from the pool.
233-
/// The resulting pointer is aligned to a platform word size.
234-
///
235-
/// Returns a typed pointer to the allocated memory.
236-
pub fn alloc_type<T: Copy>(&self) -> *mut T {
237-
self.alloc(mem::size_of::<T>()) as *mut T
238-
}
239-
240-
/// Allocates zeroed memory from the pool of the specified size.
241-
/// The resulting pointer is aligned to a platform word size.
242-
///
243-
/// Returns a raw pointer to the allocated memory.
244-
pub fn calloc(&self, size: usize) -> *mut c_void {
245-
unsafe { ngx_pcalloc(self.0.as_ptr(), size) }
246-
}
247-
248-
/// Allocates zeroed memory for a type from the pool.
249-
/// The resulting pointer is aligned to a platform word size.
250-
///
251-
/// Returns a typed pointer to the allocated memory.
252-
pub fn calloc_type<T: Copy>(&self) -> *mut T {
253-
self.calloc(mem::size_of::<T>()) as *mut T
254-
}
255-
256-
/// Allocates unaligned memory from the pool of the specified size.
257-
///
258-
/// Returns a raw pointer to the allocated memory.
259-
pub fn alloc_unaligned(&self, size: usize) -> *mut c_void {
260-
unsafe { ngx_pnalloc(self.0.as_ptr(), size) }
261-
}
262-
263-
/// Allocates unaligned memory for a type from the pool.
264-
///
265-
/// Returns a typed pointer to the allocated memory.
266-
pub fn alloc_type_unaligned<T: Copy>(&self) -> *mut T {
267-
self.alloc_unaligned(mem::size_of::<T>()) as *mut T
268-
}
269-
270229
/// Allocates memory for a value of a specified type and adds a cleanup handler to the memory
271230
/// pool.
272231
///
@@ -288,7 +247,7 @@ impl Pool {
288247
/// pool.
289248
///
290249
/// Returns Result::Ok with a typed pointer to the allocated memory if successful,
291-
/// or Result::Err(NgxError) if allocation or cleanup handler addition fails.
250+
/// or Result::Err(AllocError) if allocation or cleanup handler addition fails.
292251
pub fn allocate_with_cleanup<T>(&self, value: T) -> Result<*mut T, AllocError> {
293252
unsafe {
294253
let p = self.allocate(Layout::new::<T>())?.as_ptr() as *mut T;
@@ -301,6 +260,34 @@ impl Pool {
301260
}
302261
}
303262

263+
/// Allocates unaligned memory from the pool of the specified size.
264+
///
265+
/// Returns Result::Ok with a typed pointer to the allocated memory if successful,
266+
/// or Result::Err(AllocError) if allocation or cleanup handler addition fails.
267+
pub fn allocate_unaligned(&self, size: usize) -> Result<*mut c_void, AllocError> {
268+
Ok(self
269+
.allocate(unsafe { Layout::from_size_align_unchecked(size, 1) })?
270+
.as_ptr() as _)
271+
}
272+
273+
/// Allocates memory for a type from the pool.
274+
/// The resulting pointer is aligned to a platform word size.
275+
///
276+
/// Returns Result::Ok with a typed pointer to the allocated memory if successful,
277+
/// or Result::Err(AllocError) if allocation fails.
278+
pub fn allocate_type<T>(&self) -> Result<*mut T, AllocError> {
279+
Ok(self.allocate(Layout::new::<T>())?.as_ptr() as *mut T)
280+
}
281+
282+
/// Allocates zeroed memory for a type from the pool.
283+
/// The resulting pointer is aligned to a platform word size.
284+
///
285+
/// Returns Result::Ok with a typed pointer to the allocated memory if successful,
286+
/// or Result::Err(AllocError) if allocation fails.
287+
pub fn allocate_type_zeroed<T>(&self) -> Result<*mut T, AllocError> {
288+
Ok(self.allocate_zeroed(Layout::new::<T>())?.as_ptr() as *mut T)
289+
}
290+
304291
/// Resizes a memory allocation in place if possible.
305292
///
306293
/// If resizing is requested for the last allocation in the pool, it may be

src/http/upstream.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ macro_rules! http_upstream_init_peer_pt {
1616
r: *mut $crate::ffi::ngx_http_request_t,
1717
us: *mut $crate::ffi::ngx_http_upstream_srv_conf_t,
1818
) -> $crate::ffi::ngx_int_t {
19-
let status: $crate::core::Status = $handler(
19+
let res: $crate::core::NgxResult = $handler(
2020
unsafe { &mut $crate::http::Request::from_ngx_http_request(r) },
2121
us,
2222
);
23-
status.0
23+
res.map_or_else(|_| $crate::core::Status::NGX_ERROR.into(), |code| code)
2424
}
2525
};
2626
}

0 commit comments

Comments
 (0)