Skip to content

Commit 1f4b572

Browse files
pchickeybavshin-f5
authored andcommitted
refactor(core): ngx::core::Pool's methods take &self
The interior *mut ngx_pool_t provides the mutability we need here - there is no requirement for the use of the ngx_pool_t to be unique, so restricting the methods to &mut self just adds additional difficulty without any safety benefit.
1 parent cbf33dd commit 1f4b572

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

examples/httporigdst.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct NgxHttpOrigDstCtx {
1919
}
2020

2121
impl NgxHttpOrigDstCtx {
22-
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &mut core::Pool) -> core::Status {
22+
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &core::Pool) -> core::Status {
2323
let addr_data = pool.alloc_unaligned(addr.len());
2424
if addr_data.is_null() {
2525
return core::Status::NGX_ERROR;
@@ -226,7 +226,7 @@ http_variable_get!(
226226
ip,
227227
port,
228228
);
229-
(*new_ctx).save(&ip, port, &mut request.pool());
229+
(*new_ctx).save(&ip, port, &request.pool());
230230
(*new_ctx).bind_addr(v);
231231
request
232232
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));
@@ -273,7 +273,7 @@ http_variable_get!(
273273
ip,
274274
port,
275275
);
276-
(*new_ctx).save(&ip, port, &mut request.pool());
276+
(*new_ctx).save(&ip, port, &request.pool());
277277
(*new_ctx).bind_port(v);
278278
request
279279
.set_module_ctx(new_ctx as *mut c_void, &*addr_of!(ngx_http_orig_dst_module));

examples/shared_dict.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ extern "C" fn ngx_http_shared_dict_add_variable(
198198
) -> *mut c_char {
199199
// SAFETY: configuration handlers always receive a valid `cf` pointer.
200200
let cf = unsafe { cf.as_mut().unwrap() };
201-
let mut pool = unsafe { Pool::from_ngx_pool(cf.pool) };
201+
let pool = unsafe { Pool::from_ngx_pool(cf.pool) };
202202

203203
let key = pool.calloc_type::<ngx_http_complex_value_t>();
204204
if key.is_null() {

examples/upstream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ impl HttpModule for Module {
311311
}
312312

313313
unsafe extern "C" fn create_srv_conf(cf: *mut ngx_conf_t) -> *mut c_void {
314-
let mut pool = Pool::from_ngx_pool((*cf).pool);
314+
let pool = Pool::from_ngx_pool((*cf).pool);
315315
let conf = pool.alloc_type::<SrvConfig>();
316316
if conf.is_null() {
317317
ngx_conf_log_error!(

src/core/pool.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ impl Pool {
101101
///
102102
/// Returns `Some(TemporaryBuffer)` if the buffer is successfully created, or `None` if
103103
/// allocation fails.
104-
pub fn create_buffer(&mut self, size: usize) -> Option<TemporaryBuffer> {
105-
let buf = unsafe { ngx_create_temp_buf(self.as_mut(), size) };
104+
pub fn create_buffer(&self, size: usize) -> Option<TemporaryBuffer> {
105+
let buf = unsafe { ngx_create_temp_buf(self.0.as_ptr(), size) };
106106
if buf.is_null() {
107107
return None;
108108
}
@@ -114,7 +114,7 @@ impl Pool {
114114
///
115115
/// Returns `Some(TemporaryBuffer)` if the buffer is successfully created, or `None` if
116116
/// allocation fails.
117-
pub fn create_buffer_from_str(&mut self, str: &str) -> Option<TemporaryBuffer> {
117+
pub fn create_buffer_from_str(&self, str: &str) -> Option<TemporaryBuffer> {
118118
let mut buffer = self.create_buffer(str.len())?;
119119
unsafe {
120120
let buf = buffer.as_ngx_buf_mut();
@@ -128,7 +128,7 @@ impl Pool {
128128
///
129129
/// Returns `Some(MemoryBuffer)` if the buffer is successfully created, or `None` if allocation
130130
/// fails.
131-
pub fn create_buffer_from_static_str(&mut self, str: &'static str) -> Option<MemoryBuffer> {
131+
pub fn create_buffer_from_static_str(&self, str: &'static str) -> Option<MemoryBuffer> {
132132
let buf = self.calloc_type::<ngx_buf_t>();
133133
if buf.is_null() {
134134
return None;
@@ -156,7 +156,7 @@ impl Pool {
156156
///
157157
/// # Safety
158158
/// This function is marked as unsafe because it involves raw pointer manipulation.
159-
unsafe fn add_cleanup_for_value<T>(&mut self, value: *mut T) -> Result<(), ()> {
159+
unsafe fn add_cleanup_for_value<T>(&self, value: *mut T) -> Result<(), ()> {
160160
let cln = ngx_pool_cleanup_add(self.0.as_ptr(), 0);
161161
if cln.is_null() {
162162
return Err(());
@@ -171,45 +171,45 @@ impl Pool {
171171
/// The resulting pointer is aligned to a platform word size.
172172
///
173173
/// Returns a raw pointer to the allocated memory.
174-
pub fn alloc(&mut self, size: usize) -> *mut c_void {
174+
pub fn alloc(&self, size: usize) -> *mut c_void {
175175
unsafe { ngx_palloc(self.0.as_ptr(), size) }
176176
}
177177

178178
/// Allocates memory for a type from the pool.
179179
/// The resulting pointer is aligned to a platform word size.
180180
///
181181
/// Returns a typed pointer to the allocated memory.
182-
pub fn alloc_type<T: Copy>(&mut self) -> *mut T {
182+
pub fn alloc_type<T: Copy>(&self) -> *mut T {
183183
self.alloc(mem::size_of::<T>()) as *mut T
184184
}
185185

186186
/// Allocates zeroed memory from the pool of the specified size.
187187
/// The resulting pointer is aligned to a platform word size.
188188
///
189189
/// Returns a raw pointer to the allocated memory.
190-
pub fn calloc(&mut self, size: usize) -> *mut c_void {
190+
pub fn calloc(&self, size: usize) -> *mut c_void {
191191
unsafe { ngx_pcalloc(self.0.as_ptr(), size) }
192192
}
193193

194194
/// Allocates zeroed memory for a type from the pool.
195195
/// The resulting pointer is aligned to a platform word size.
196196
///
197197
/// Returns a typed pointer to the allocated memory.
198-
pub fn calloc_type<T: Copy>(&mut self) -> *mut T {
198+
pub fn calloc_type<T: Copy>(&self) -> *mut T {
199199
self.calloc(mem::size_of::<T>()) as *mut T
200200
}
201201

202202
/// Allocates unaligned memory from the pool of the specified size.
203203
///
204204
/// Returns a raw pointer to the allocated memory.
205-
pub fn alloc_unaligned(&mut self, size: usize) -> *mut c_void {
205+
pub fn alloc_unaligned(&self, size: usize) -> *mut c_void {
206206
unsafe { ngx_pnalloc(self.0.as_ptr(), size) }
207207
}
208208

209209
/// Allocates unaligned memory for a type from the pool.
210210
///
211211
/// Returns a typed pointer to the allocated memory.
212-
pub fn alloc_type_unaligned<T: Copy>(&mut self) -> *mut T {
212+
pub fn alloc_type_unaligned<T: Copy>(&self) -> *mut T {
213213
self.alloc_unaligned(mem::size_of::<T>()) as *mut T
214214
}
215215

@@ -218,7 +218,7 @@ impl Pool {
218218
///
219219
/// Returns a typed pointer to the allocated memory if successful, or a null pointer if
220220
/// allocation or cleanup handler addition fails.
221-
pub fn allocate<T>(&mut self, value: T) -> *mut T {
221+
pub fn allocate<T>(&self, value: T) -> *mut T {
222222
unsafe {
223223
let p = self.alloc(mem::size_of::<T>()) as *mut T;
224224
ptr::write(p, value);

src/http/module.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub trait HttpModule {
7777
Self: super::HttpModuleMainConf,
7878
Self::MainConf: Default,
7979
{
80-
let mut pool = Pool::from_ngx_pool((*cf).pool);
80+
let pool = Pool::from_ngx_pool((*cf).pool);
8181
pool.allocate::<Self::MainConf>(Default::default()) as *mut c_void
8282
}
8383

@@ -102,7 +102,7 @@ pub trait HttpModule {
102102
Self: super::HttpModuleServerConf,
103103
Self::ServerConf: Default,
104104
{
105-
let mut pool = Pool::from_ngx_pool((*cf).pool);
105+
let pool = Pool::from_ngx_pool((*cf).pool);
106106
pool.allocate::<Self::ServerConf>(Default::default()) as *mut c_void
107107
}
108108

@@ -136,7 +136,7 @@ pub trait HttpModule {
136136
Self: super::HttpModuleLocationConf,
137137
Self::LocationConf: Default,
138138
{
139-
let mut pool = Pool::from_ngx_pool((*cf).pool);
139+
let pool = Pool::from_ngx_pool((*cf).pool);
140140
pool.allocate::<Self::LocationConf>(Default::default()) as *mut c_void
141141
}
142142

0 commit comments

Comments
 (0)