@@ -4,7 +4,7 @@ use core::mem;
4
4
use core:: ptr:: { self , NonNull } ;
5
5
6
6
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,
8
8
ngx_pool_cleanup_add, ngx_pool_t, NGX_ALIGNMENT ,
9
9
} ;
10
10
@@ -183,10 +183,7 @@ impl Pool {
183
183
/// Returns `Some(MemoryBuffer)` if the buffer is successfully created, or `None` if allocation
184
184
/// fails.
185
185
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 ;
190
187
191
188
// We cast away const, but buffers with the memory flag are read-only
192
189
let start = str. as_ptr ( ) as * mut u8 ;
@@ -229,44 +226,6 @@ impl Pool {
229
226
unsafe { ngx_palloc ( self . 0 . as_ptr ( ) , size) }
230
227
}
231
228
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
-
270
229
/// Allocates memory for a value of a specified type and adds a cleanup handler to the memory
271
230
/// pool.
272
231
///
@@ -284,6 +243,33 @@ impl Pool {
284
243
}
285
244
}
286
245
246
+ /// Allocates unaligned memory from the pool of the specified size.
247
+ ///
248
+ /// Returns Result::Ok with a typed pointer to the allocated memory if successful,
249
+ /// or Result::Err(AllocError) if allocation or cleanup handler addition fails.
250
+ pub fn allocate_unaligned ( & self , size : usize ) -> Result < NonNull < [ u8 ] > , AllocError > {
251
+ self . allocate ( unsafe { Layout :: from_size_align_unchecked ( size, 1 ) } )
252
+ }
253
+
254
+ /// Allocates memory for a type from the pool.
255
+ /// The resulting pointer is aligned to a platform word size.
256
+ ///
257
+ /// Returns Result::Ok with a typed pointer to the allocated memory if successful,
258
+ /// or Result::Err(AllocError) if allocation fails.
259
+ pub fn allocate_type < T > ( & self ) -> Result < NonNull < T > , AllocError > {
260
+ self . allocate ( Layout :: new :: < T > ( ) ) . map ( |ptr| ptr. cast ( ) )
261
+ }
262
+
263
+ /// Allocates zeroed memory for a type from the pool.
264
+ /// The resulting pointer is aligned to a platform word size.
265
+ ///
266
+ /// Returns Result::Ok with a typed pointer to the allocated memory if successful,
267
+ /// or Result::Err(AllocError) if allocation fails.
268
+ pub fn allocate_type_zeroed < T > ( & self ) -> Result < NonNull < T > , AllocError > {
269
+ self . allocate_zeroed ( Layout :: new :: < T > ( ) )
270
+ . map ( |ptr| ptr. cast ( ) )
271
+ }
272
+
287
273
/// Resizes a memory allocation in place if possible.
288
274
///
289
275
/// If resizing is requested for the last allocation in the pool, it may be
0 commit comments