id_manager: Use calloc instead of malloc
authorReto Buerki <reet@codelabs.ch>
Tue, 21 Aug 2012 13:29:30 +0000 (15:29 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 19 Mar 2013 14:23:46 +0000 (15:23 +0100)
This way we don't need to manually initialize the slot status; free
slots are now indicated by 0 though.

src/charon-tkm/src/tkm/tkm_id_manager.c

index f069e0e..8cafa41 100644 (file)
@@ -84,9 +84,9 @@ METHOD(tkm_id_manager_t, acquire_id, int,
        this->locks[kind]->write_lock(this->locks[kind]);
        for (j = 0; j < this->limits[kind]; j++)
        {
-               if (this->ctxids[kind][j])
+               if (!this->ctxids[kind][j])
                {
-                       this->ctxids[kind][j] = false;
+                       this->ctxids[kind][j] = true;
                        id = j + 1;
                        break;
                }
@@ -116,7 +116,7 @@ METHOD(tkm_id_manager_t, release_id, bool,
        }
 
        this->locks[kind]->write_lock(this->locks[kind]);
-       this->ctxids[kind][idx] = true;
+       this->ctxids[kind][idx] = false;
        this->locks[kind]->unlock(this->locks[kind]);
 
        return TRUE;
@@ -142,7 +142,6 @@ tkm_id_manager_t *tkm_id_manager_create(const tkm_limits_t limits)
 {
        private_tkm_id_manager_t *this;
        int i;
-       uint64_t j;
 
        INIT(this,
                .public = {
@@ -155,13 +154,8 @@ tkm_id_manager_t *tkm_id_manager_create(const tkm_limits_t limits)
        for (i = 0; i < TKM_CTX_MAX; i++)
        {
                this->limits[i] = limits[i];
-               this->ctxids[i] = malloc(limits[i] * sizeof(bool));
+               this->ctxids[i] = calloc(limits[i], sizeof(bool));
                this->locks[i] = rwlock_create(RWLOCK_TYPE_DEFAULT);
-               for (j = 0; j < limits[i]; j++)
-               {
-                       /* available id slots are in true state (is_available) */
-                       this->ctxids[i][j] = true;
-               }
                DBG2(DBG_LIB, "%N initialized, %llu slot(s)",
                                tkm_context_kind_names, i, limits[i]);
        }