charon-tkm: Return current refcount when releasing ID
authorAdrian-Ken Rueegsegger <ken@codelabs.ch>
Fri, 1 Sep 2017 15:21:05 +0000 (17:21 +0200)
committerTobias Brunner <tobias@strongswan.org>
Fri, 15 Sep 2017 10:16:57 +0000 (12:16 +0200)
src/charon-tkm/src/tkm/tkm_id_manager.c
src/charon-tkm/src/tkm/tkm_id_manager.h
src/charon-tkm/tests/id_manager_tests.c

index 2d28c3c..9a2ede0 100644 (file)
@@ -130,27 +130,28 @@ METHOD(tkm_id_manager_t, acquire_ref, bool,
        return TRUE;
 }
 
-METHOD(tkm_id_manager_t, release_id, bool,
+METHOD(tkm_id_manager_t, release_id, int,
        private_tkm_id_manager_t * const this, const tkm_context_kind_t kind,
        const int id)
 {
        const int idx = id - 1;
+       int refcount = 0;
 
        if (!is_valid_kind(kind))
        {
                DBG1(DBG_LIB, "tried to release id %d for invalid context kind '%d'",
                         id, kind);
-               return FALSE;
+               return -1;
        }
 
        this->locks[kind]->write_lock(this->locks[kind]);
        if (this->ctxids[kind][idx] > 0)
        {
-               this->ctxids[kind][idx]--;
+               refcount = --this->ctxids[kind][idx];
        }
        this->locks[kind]->unlock(this->locks[kind]);
 
-       return TRUE;
+       return refcount;
 }
 
 
index ec7b07f..1c48b57 100644 (file)
@@ -90,11 +90,11 @@ struct tkm_id_manager_t {
         *
         * @param kind                  kind of context id to release
         * @param id                    id to release
-        * @return                              TRUE if id was released, FALSE otherwise
+        * @return                              current refcount if id was released, -1 otherwise
         */
-       bool (*release_id)(tkm_id_manager_t * const this,
-                                          const tkm_context_kind_t kind,
-                                          const int id);
+       int (*release_id)(tkm_id_manager_t * const this,
+                                         const tkm_context_kind_t kind,
+                                         const int id);
 
        /**
         * Destroy a tkm_id_manager instance.
index 392379a..fb5e56a 100644 (file)
@@ -151,19 +151,18 @@ END_TEST
 
 START_TEST(test_release_id)
 {
-       int i, id = 0;
-       bool released = false;
+       int i, count, id = 0;
        tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
 
        for (i = 0; i < TKM_CTX_MAX; i++)
        {
                id = idmgr->acquire_id(idmgr, i);
-               released = idmgr->release_id(idmgr, i, id);
+               count = idmgr->release_id(idmgr, i, id);
 
-               fail_unless(released, "Error releasing id of context kind %d", i);
+               fail_unless(count == 0, "Error releasing id of context kind %d", i);
 
-               /* Reset released variable */
-               released = FALSE;
+               /* Reset count variable */
+               count = 0;
        }
 
        idmgr->destroy(idmgr);
@@ -172,17 +171,17 @@ END_TEST
 
 START_TEST(test_release_id_invalid_kind)
 {
-       bool released = TRUE;
+       int count = 0;
        tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
 
-       released = idmgr->release_id(idmgr, TKM_CTX_MAX, 1);
-       fail_if(released, "Released id for invalid context kind %d", TKM_CTX_MAX);
+       count = idmgr->release_id(idmgr, TKM_CTX_MAX, 1);
+       fail_if(count >= 0, "Released id for invalid context kind %d", TKM_CTX_MAX);
 
        /* Reset test variable */
-       released = TRUE;
+       count = 0;
 
-       released = idmgr->release_id(idmgr, -1, 1);
-       fail_if(released, "Released id for invalid context kind %d", -1);
+       count = idmgr->release_id(idmgr, -1, 1);
+       fail_if(count >= 0, "Released id for invalid context kind %d", -1);
 
        idmgr->destroy(idmgr);
 }
@@ -190,11 +189,11 @@ END_TEST
 
 START_TEST(test_release_id_nonexistent)
 {
-       bool released = FALSE;
+       int count = 0;
        tkm_id_manager_t *idmgr = tkm_id_manager_create(limits);
 
-       released = idmgr->release_id(idmgr, TKM_CTX_NONCE, 1);
-       fail_unless(released, "Release of nonexistent id failed");
+       count = idmgr->release_id(idmgr, TKM_CTX_NONCE, 1);
+       fail_unless(count == 0, "Release of nonexistent id failed");
 
        idmgr->destroy(idmgr);
 }