[nginx] Core: introduced the ngx_palloc_small() function.

Valentin Bartenev vbart at nginx.com
Wed Mar 23 14:45:39 UTC 2016


details:   http://hg.nginx.org/nginx/rev/12248fe20689
branches:  
changeset: 6453:12248fe20689
user:      Valentin Bartenev <vbart at nginx.com>
date:      Wed Mar 23 17:44:04 2016 +0300
description:
Core: introduced the ngx_palloc_small() function.

It deduplicates some code for allocations from memory pool.
No functional changes.

diffstat:

 src/core/ngx_palloc.c |  61 ++++++++++++++++++++++----------------------------
 1 files changed, 27 insertions(+), 34 deletions(-)

diffs (96 lines):

diff -r 6be7e59fdd2c -r 12248fe20689 src/core/ngx_palloc.c
--- a/src/core/ngx_palloc.c	Wed Mar 23 17:44:04 2016 +0300
+++ b/src/core/ngx_palloc.c	Wed Mar 23 17:44:04 2016 +0300
@@ -9,6 +9,8 @@
 #include <ngx_core.h>
 
 
+static ngx_inline void *ngx_palloc_small(ngx_pool_t *pool, size_t size,
+    ngx_uint_t align);
 static void *ngx_palloc_block(ngx_pool_t *pool, size_t size);
 static void *ngx_palloc_large(ngx_pool_t *pool, size_t size);
 
@@ -120,27 +122,8 @@ ngx_reset_pool(ngx_pool_t *pool)
 void *
 ngx_palloc(ngx_pool_t *pool, size_t size)
 {
-    u_char      *m;
-    ngx_pool_t  *p;
-
     if (size <= pool->max) {
-
-        p = pool->current;
-
-        do {
-            m = ngx_align_ptr(p->d.last, NGX_ALIGNMENT);
-
-            if ((size_t) (p->d.end - m) >= size) {
-                p->d.last = m + size;
-
-                return m;
-            }
-
-            p = p->d.next;
-
-        } while (p);
-
-        return ngx_palloc_block(pool, size);
+        return ngx_palloc_small(pool, size, 1);
     }
 
     return ngx_palloc_large(pool, size);
@@ -150,30 +133,40 @@ ngx_palloc(ngx_pool_t *pool, size_t size
 void *
 ngx_pnalloc(ngx_pool_t *pool, size_t size)
 {
+    if (size <= pool->max) {
+        return ngx_palloc_small(pool, size, 0);
+    }
+
+    return ngx_palloc_large(pool, size);
+}
+
+
+static ngx_inline void *
+ngx_palloc_small(ngx_pool_t *pool, size_t size, ngx_uint_t align)
+{
     u_char      *m;
     ngx_pool_t  *p;
 
-    if (size <= pool->max) {
+    p = pool->current;
 
-        p = pool->current;
+    do {
+        m = p->d.last;
 
-        do {
-            m = p->d.last;
+        if (align) {
+            m = ngx_align_ptr(m, NGX_ALIGNMENT);
+        }
 
-            if ((size_t) (p->d.end - m) >= size) {
-                p->d.last = m + size;
+        if ((size_t) (p->d.end - m) >= size) {
+            p->d.last = m + size;
 
-                return m;
-            }
+            return m;
+        }
 
-            p = p->d.next;
+        p = p->d.next;
 
-        } while (p);
+    } while (p);
 
-        return ngx_palloc_block(pool, size);
-    }
-
-    return ngx_palloc_large(pool, size);
+    return ngx_palloc_block(pool, size);
 }
 
 



More information about the nginx-devel mailing list