Add ref count for db init/deinit 86/109986/1
authorWonnam Jang <wn.jang@samsung.com>
Wed, 28 Dec 2016 12:09:18 +0000 (21:09 +0900)
committerWonnam Jang <wn.jang@samsung.com>
Thu, 12 Jan 2017 10:46:33 +0000 (02:46 -0800)
Change-Id: I4234b263278ef94db192d1142b8e12a9451b9416
Signed-off-by: Wonnam Jang <wn.jang@samsung.com>
(cherry picked from commit 45e9a90b27cbb347f2839bd645513687520186c3)

common/vc_cmd_db.c

index 16ca81d..3a040a6 100644 (file)
@@ -59,6 +59,7 @@ const char* vc_db_tag()
 static sqlite3* db_handle = NULL;
 char* path = NULL;
 int g_fpid = -1;
+int g_ref_cnt = 0;
 
 static int __vc_db_transaction(const char* transaction)
 {
@@ -1063,6 +1064,11 @@ int vc_db_initialize(void)
 {
        SLOG(LOG_INFO, vc_db_tag(), "DB initialization");
 
+       if (0 < g_ref_cnt) {
+               g_ref_cnt++;
+               return VC_DB_ERROR_NONE;
+       }
+
        path = (char*)calloc(256, sizeof(char));
        if (NULL == path) {
                SLOG(LOG_ERROR, vc_db_tag(), "[ERROR] Fail to allocate memory");
@@ -1112,12 +1118,16 @@ int vc_db_initialize(void)
                        SLOG(LOG_ERROR, vc_db_tag(), "sqlite3_exec returned %d: %s", ret, err_msg);
                }
        }
-
+       g_ref_cnt++;
        return VC_DB_ERROR_NONE;
 }
 
 int vc_db_finalize(void)
 {
+       if (0 >= g_ref_cnt) return VC_DB_ERROR_INVALID_STATE;
+       if (0 != --g_ref_cnt)
+               return VC_DB_ERROR_NONE;
+
        if (NULL != path) {
                free(path);
                path = NULL;