@@ -4,7 +4,7 @@ use core::mem;
44use core:: ptr:: { self , NonNull } ;
55
66use 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
0 commit comments