[nginx] Core: added ngx_slab_calloc() and ngx_slab_calloc_locked().

Ruslan Ermilov ru at nginx.com
Wed Jun 18 09:40:02 UTC 2014


details:   http://hg.nginx.org/nginx/rev/25ade23cf281
branches:  
changeset: 5726:25ade23cf281
user:      Ruslan Ermilov <ru at nginx.com>
date:      Wed Jun 04 15:09:19 2014 +0400
description:
Core: added ngx_slab_calloc() and ngx_slab_calloc_locked().

These functions return zeroed memory, analogous to ngx_pcalloc().

diffstat:

 src/core/ngx_slab.c            |  29 +++++++++++++++++++++++++++++
 src/core/ngx_slab.h            |   2 ++
 src/http/ngx_http_file_cache.c |  22 ++++++----------------
 3 files changed, 37 insertions(+), 16 deletions(-)

diffs (112 lines):

diff -r eadf46f888e9 -r 25ade23cf281 src/core/ngx_slab.c
--- a/src/core/ngx_slab.c	Wed Jun 18 13:39:20 2014 +0400
+++ b/src/core/ngx_slab.c	Wed Jun 04 15:09:19 2014 +0400
@@ -398,6 +398,35 @@ done:
 }
 
 
+void *
+ngx_slab_calloc(ngx_slab_pool_t *pool, size_t size)
+{
+    void  *p;
+
+    ngx_shmtx_lock(&pool->mutex);
+
+    p = ngx_slab_calloc_locked(pool, size);
+
+    ngx_shmtx_unlock(&pool->mutex);
+
+    return p;
+}
+
+
+void *
+ngx_slab_calloc_locked(ngx_slab_pool_t *pool, size_t size)
+{
+    void  *p;
+
+    p = ngx_slab_alloc_locked(pool, size);
+    if (p) {
+        ngx_memzero(p, size);
+    }
+
+    return p;
+}
+
+
 void
 ngx_slab_free(ngx_slab_pool_t *pool, void *p)
 {
diff -r eadf46f888e9 -r 25ade23cf281 src/core/ngx_slab.h
--- a/src/core/ngx_slab.h	Wed Jun 18 13:39:20 2014 +0400
+++ b/src/core/ngx_slab.h	Wed Jun 04 15:09:19 2014 +0400
@@ -50,6 +50,8 @@ typedef struct {
 void ngx_slab_init(ngx_slab_pool_t *pool);
 void *ngx_slab_alloc(ngx_slab_pool_t *pool, size_t size);
 void *ngx_slab_alloc_locked(ngx_slab_pool_t *pool, size_t size);
+void *ngx_slab_calloc(ngx_slab_pool_t *pool, size_t size);
+void *ngx_slab_calloc_locked(ngx_slab_pool_t *pool, size_t size);
 void ngx_slab_free(ngx_slab_pool_t *pool, void *p);
 void ngx_slab_free_locked(ngx_slab_pool_t *pool, void *p);
 
diff -r eadf46f888e9 -r 25ade23cf281 src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Wed Jun 18 13:39:20 2014 +0400
+++ b/src/http/ngx_http_file_cache.c	Wed Jun 04 15:09:19 2014 +0400
@@ -678,8 +678,8 @@ ngx_http_file_cache_exists(ngx_http_file
         goto done;
     }
 
-    fcn = ngx_slab_alloc_locked(cache->shpool,
-                                sizeof(ngx_http_file_cache_node_t));
+    fcn = ngx_slab_calloc_locked(cache->shpool,
+                                 sizeof(ngx_http_file_cache_node_t));
     if (fcn == NULL) {
         ngx_shmtx_unlock(&cache->shpool->mutex);
 
@@ -687,8 +687,8 @@ ngx_http_file_cache_exists(ngx_http_file
 
         ngx_shmtx_lock(&cache->shpool->mutex);
 
-        fcn = ngx_slab_alloc_locked(cache->shpool,
-                                    sizeof(ngx_http_file_cache_node_t));
+        fcn = ngx_slab_calloc_locked(cache->shpool,
+                                     sizeof(ngx_http_file_cache_node_t));
         if (fcn == NULL) {
             rc = NGX_ERROR;
             goto failed;
@@ -704,8 +704,6 @@ ngx_http_file_cache_exists(ngx_http_file
 
     fcn->uses = 1;
     fcn->count = 1;
-    fcn->updating = 0;
-    fcn->deleting = 0;
 
 renew:
 
@@ -1618,8 +1616,8 @@ ngx_http_file_cache_add(ngx_http_file_ca
 
     if (fcn == NULL) {
 
-        fcn = ngx_slab_alloc_locked(cache->shpool,
-                                    sizeof(ngx_http_file_cache_node_t));
+        fcn = ngx_slab_calloc_locked(cache->shpool,
+                                     sizeof(ngx_http_file_cache_node_t));
         if (fcn == NULL) {
             ngx_shmtx_unlock(&cache->shpool->mutex);
             return NGX_ERROR;
@@ -1633,15 +1631,7 @@ ngx_http_file_cache_add(ngx_http_file_ca
         ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
 
         fcn->uses = 1;
-        fcn->count = 0;
-        fcn->valid_msec = 0;
-        fcn->error = 0;
         fcn->exists = 1;
-        fcn->updating = 0;
-        fcn->deleting = 0;
-        fcn->uniq = 0;
-        fcn->valid_sec = 0;
-        fcn->body_start = 0;
         fcn->fs_size = c->fs_size;
 
         cache->sh->size += c->fs_size;



More information about the nginx-devel mailing list