mem-pool: Remove entries without online or offline leases
authorTobias Brunner <tobias@strongswan.org>
Wed, 11 Mar 2015 10:27:38 +0000 (11:27 +0100)
committerTobias Brunner <tobias@strongswan.org>
Thu, 19 Mar 2015 08:55:56 +0000 (09:55 +0100)
This avoids filling up the hash table with unused/old identities.

References #841.

src/libcharon/attributes/mem_pool.c

index 759d941..2796682 100644 (file)
@@ -110,6 +110,17 @@ static entry_t* entry_create(identification_t *id)
 }
 
 /**
+ * Destroy an entry
+ */
+static void entry_destroy(entry_t *this)
+{
+       this->id->destroy(this->id);
+       array_destroy(this->online);
+       array_destroy(this->offline);
+       free(this);
+}
+
+/**
  * hashtable hash function for identities
  */
 static u_int id_hash(identification_t *id)
@@ -356,8 +367,16 @@ static int get_reassigned(private_mem_pool_t *this, identification_t *id,
                if (array_remove(entry->offline, ARRAY_HEAD, &current))
                {
                        lease.offset = current;
-                       DBG1(DBG_CFG, "reassigning existing offline lease by '%Y'"
-                                " to '%Y'", entry->id, id);
+                       DBG1(DBG_CFG, "reassigning existing offline lease by '%Y' "
+                                "to '%Y'", entry->id, id);
+               }
+               if (!array_count(entry->online) && !array_count(entry->offline))
+               {
+                       this->leases->remove_at(this->leases, enumerator);
+                       entry_destroy(entry);
+               }
+               if (lease.offset)
+               {
                        break;
                }
        }
@@ -570,10 +589,7 @@ METHOD(mem_pool_t, destroy, void,
        enumerator = this->leases->create_enumerator(this->leases);
        while (enumerator->enumerate(enumerator, NULL, &entry))
        {
-               entry->id->destroy(entry->id);
-               array_destroy(entry->online);
-               array_destroy(entry->offline);
-               free(entry);
+               entry_destroy(entry);
        }
        enumerator->destroy(enumerator);