[asterisk-scf-commits] asterisk-scf/integration/pjproject.git branch "caching_pool_factory_capacity_bug" created.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Tue Aug 21 15:07:07 CDT 2012


branch "caching_pool_factory_capacity_bug" has been created
        at  f17ac182e75c396a51fe98373b492cf2353af95d (commit)

- Log -----------------------------------------------------------------
commit f17ac182e75c396a51fe98373b492cf2353af95d
Author: Brent Eagles <beagles at digium.com>
Date:   Wed Aug 15 17:56:54 2012 -0230

    Fix some overflow issues in pool management and make a bit of code
    a bit more clear as to what is going on.

diff --git a/pjlib/include/pj/pool.h b/pjlib/include/pj/pool.h
index 5738d4f..87fb89d 100644
--- a/pjlib/include/pj/pool.h
+++ b/pjlib/include/pj/pool.h
@@ -509,7 +509,7 @@ PJ_INLINE(void*) pj_pool_zalloc(pj_pool_t *pool, pj_size_t size)
  * Internal functions
  */
 PJ_IDECL(void*) pj_pool_alloc_from_block(pj_pool_block *block, pj_size_t size);
-PJ_DECL(void*) pj_pool_allocate_find(pj_pool_t *pool, unsigned size);
+PJ_DECL(void*) pj_pool_allocate_find(pj_pool_t *pool, pj_size_t size);
 
 
 	
diff --git a/pjlib/include/pj/pool_i.h b/pjlib/include/pj/pool_i.h
index ea4fa2d..b593353 100644
--- a/pjlib/include/pj/pool_i.h
+++ b/pjlib/include/pj/pool_i.h
@@ -47,7 +47,7 @@ PJ_IDEF(void*) pj_pool_alloc_from_block( pj_pool_block *block, pj_size_t size )
     if (size & (PJ_POOL_ALIGNMENT-1)) {
 	size = (size + PJ_POOL_ALIGNMENT) & ~(PJ_POOL_ALIGNMENT-1);
     }
-    if ((unsigned)(block->end - block->cur) >= size) {
+    if ((pj_size_t)(block->end - block->cur) >= size) {
 	void *ptr = block->cur;
 	block->cur += size;
 	return ptr;
diff --git a/pjlib/src/pj/pool.c b/pjlib/src/pj/pool.c
index 9992df7..b64a2a6 100644
--- a/pjlib/src/pj/pool.c
+++ b/pjlib/src/pj/pool.c
@@ -88,7 +88,7 @@ static pj_pool_block *pj_pool_create_block( pj_pool_t *pool, pj_size_t size)
  * If no space is available in all the blocks, a new block might be created
  * (depending on whether the pool is allowed to resize).
  */
-PJ_DEF(void*) pj_pool_allocate_find(pj_pool_t *pool, unsigned size)
+PJ_DEF(void*) pj_pool_allocate_find(pj_pool_t *pool, pj_size_t size)
 {
     pj_pool_block *block = pool->block_list.next;
     void *p;
@@ -121,7 +121,7 @@ PJ_DEF(void*) pj_pool_allocate_find(pj_pool_t *pool, unsigned size)
     if (pool->increment_size < 
 	    size + sizeof(pj_pool_block) + PJ_POOL_ALIGNMENT) 
     {
-        unsigned count;
+        pj_size_t count;
         count = (size + pool->increment_size + sizeof(pj_pool_block) +
                  PJ_POOL_ALIGNMENT) / 
                 pool->increment_size;
diff --git a/pjlib/src/pj/pool_caching.c b/pjlib/src/pj/pool_caching.c
index a15c3d9..c476e73 100644
--- a/pjlib/src/pj/pool_caching.c
+++ b/pjlib/src/pj/pool_caching.c
@@ -177,8 +177,18 @@ static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
 	/* Initialize the pool. */
 	pj_pool_init_int(pool, name, increment_sz, callback);
 
-	/* Update pool manager's free capacity. */
-	cp->capacity -= pj_pool_get_capacity(pool);
+	/* Update pool manager's free capacity. 
+       If our free capacity would dip below zero with this pool removed
+       then just set it to zero.
+    */
+    if (cp->capacity > pj_pool_get_capacity(pool))
+    {
+	    cp->capacity -= pj_pool_get_capacity(pool);
+    }
+    else
+    {
+        cp->capacity = 0;
+    }
 
 	PJ_LOG(6, (pool->obj_name, "pool reused, size=%u", pool->capacity));
     }
@@ -199,6 +209,9 @@ static pj_pool_t* cpool_create_pool(pj_pool_factory *pf,
 static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
 {
     pj_caching_pool *cp = (pj_caching_pool*)pf;
+    unsigned long currentMaxCapacity = cp->max_capacity;
+    unsigned long currentCapacity = cp->capacity;
+
     unsigned pool_capacity;
     unsigned i;
 
@@ -208,6 +221,7 @@ static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
 
     pj_lock_acquire(cp->lock);
 
+
 #if PJ_SAFE_POOL
     /* Make sure pool is still in our used list */
     if (pj_list_find_node(&cp->used_list, pool) != pool) {
@@ -223,13 +237,15 @@ static void cpool_release_pool( pj_pool_factory *pf, pj_pool_t *pool)
     --cp->used_count;
 
     pool_capacity = pj_pool_get_capacity(pool);
+    currentCapacity += pool_capacity;
+
 
     /* Destroy the pool if the size is greater than our size or if the total
      * capacity in our recycle list (plus the size of the pool) exceeds 
      * maximum capacity.
    . */
     if (pool_capacity > pool_sizes[PJ_CACHING_POOL_ARRAY_SIZE-1] ||
-	cp->capacity + pool_capacity > cp->max_capacity)
+        currentCapacity > currentMaxCapacity)
     {
 	pj_pool_destroy_int(pool);
 	pj_lock_release(cp->lock);

-----------------------------------------------------------------------


-- 
asterisk-scf/integration/pjproject.git



More information about the asterisk-scf-commits mailing list