Remove unused directories and files in video_in_hal
[staging/basesystem.git] / service / native / framework_unified / client / NS_ResourceControler / src / ns_resource_controler.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <pthread.h>
17
18 #include <native_service/ns_logger_if.h>
19 #include <native_service/ns_resource_controler.h>
20
21 #include <map>
22 #include <string>
23 #include <utility>
24
25 struct frameworkunifiedResource {
26   int ref_counter;
27   long resource;  // NOLINT  (readability/nolint)
28 };
29
30 typedef std::map<std::string, frameworkunifiedResource> frameworkunifiedResourceTable_t;
31 typedef std::map<long, std::string> frameworkunifiedResourceTableR_t;  // NOLINT  (readability/nolint)
32
33 struct frameworkunifiedResourceTable {
34   frameworkunifiedResourceTable_t resTable;
35   frameworkunifiedResourceTableR_t resTableR;
36 };
37
38 typedef std::map<std::string, frameworkunifiedResourceTable> frameworkunifiedResourceModuleTable_t;
39
40 static __thread frameworkunifiedResourceModuleTable_t *frameworkunifiedResourceModuleTable;
41
42 static frameworkunifiedResource *frameworkunifiedSearchResourse(const char *mod, const char *key) {
43   if (frameworkunifiedResourceModuleTable == NULL) {
44     frameworkunifiedResourceModuleTable = new frameworkunifiedResourceModuleTable_t;
45     /*
46      * @todo
47      * There is no release processing for "new frameworkunifiedResourceModuleTable_t" created under McOpenMonitor() (Valgrind pointed).
48      */
49   }
50   frameworkunifiedResourceModuleTable_t::iterator mod_it = frameworkunifiedResourceModuleTable->find(mod);
51   if (mod_it == frameworkunifiedResourceModuleTable->end()) {
52     return NULL;
53   }
54
55   frameworkunifiedResourceTable_t::iterator res_it = mod_it->second.resTable.find(key);
56   if (res_it == mod_it->second.resTable.end()) {
57     return NULL;
58   }
59
60   return &res_it->second;
61 }
62
63 int frameworkunifiedGetResource(const char *mod, const char *key, long *resource) {  // NOLINT  (readability/nolint)
64   frameworkunifiedResource *r = frameworkunifiedSearchResourse(mod, key);
65   if (r == NULL) {
66     return -1;
67   }
68   if (resource == NULL) {
69     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "resource is NULL");
70     return -1;
71   }
72   *resource = r->resource;
73   return 0;
74 }
75
76 int frameworkunifiedAcquireResouce(const char *mod, const char *key, long *resource) {  // NOLINT  (readability/nolint)
77   frameworkunifiedResource *r = frameworkunifiedSearchResourse(mod, key);
78   if (r == NULL) {
79     return -1;
80   }
81   *resource = r->resource;
82   return ++(r->ref_counter);
83 }
84
85 int frameworkunifiedReleaseResouce(const char *mod, const char *key) {
86   frameworkunifiedResource *r = frameworkunifiedSearchResourse(mod, key);
87   if (r == NULL) {
88     return -1;
89   }
90   return --(r->ref_counter);
91 }
92
93 int frameworkunifiedSearchResourseKey(const char *mod, long resource, const char **key) {  // NOLINT  (readability/nolint)
94   if (frameworkunifiedResourceModuleTable == NULL) {
95     frameworkunifiedResourceModuleTable = new frameworkunifiedResourceModuleTable_t;
96   }
97   frameworkunifiedResourceModuleTable_t::iterator mod_it = frameworkunifiedResourceModuleTable->find(mod);
98   if (mod_it == frameworkunifiedResourceModuleTable->end()) {
99     return -1;
100   }
101
102   frameworkunifiedResourceTableR_t::iterator resR_it = mod_it->second.resTableR.find(resource);
103   if (resR_it == mod_it->second.resTableR.end()) {
104     return -1;
105   }
106
107   *key = (resR_it->second).c_str();
108   return 0;
109 }
110
111 int frameworkunifiedRegistResouce(const char *mod, const char *key, long resource,  // NOLINT  (readability/nolint)
112                     int init_counter) {
113   int ret = 0;
114
115   if (frameworkunifiedResourceModuleTable == NULL) {
116     frameworkunifiedResourceModuleTable = new frameworkunifiedResourceModuleTable_t;
117   }
118   frameworkunifiedResourceModuleTable_t::iterator mod_it = frameworkunifiedResourceModuleTable->find(mod);
119
120   if (mod_it == frameworkunifiedResourceModuleTable->end()) {
121     frameworkunifiedResourceTable mod_res;
122     frameworkunifiedResourceModuleTable->insert(std::make_pair(mod, mod_res));  // LCOV_EXCL_BR_LINE 11:unexpected branch
123     mod_it = frameworkunifiedResourceModuleTable->find(mod);  // LCOV_EXCL_BR_LINE 11:unexpected branch
124   }
125
126   if (mod_it->second.resTable.find(key) == mod_it->second.resTable.end()) {
127     frameworkunifiedResource regist_res;
128     regist_res.ref_counter = init_counter;
129     regist_res.resource = resource;
130     mod_it->second.resTable.insert(std::make_pair(key, regist_res));
131     mod_it->second.resTableR.insert(std::make_pair(resource, key));
132   } else {
133     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
134     FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __func__, "Key(%s) already regist", key != 0 ? key : NULL);
135     // LCOV_EXCL_BR_STOP
136     ret = -1;
137   }
138
139   return ret;
140 }
141
142 int frameworkunifiedUnregistResouce(const char *mod, const char *key) {
143   if (frameworkunifiedResourceModuleTable == NULL) {
144     frameworkunifiedResourceModuleTable = new frameworkunifiedResourceModuleTable_t;
145   }
146   frameworkunifiedResourceModuleTable_t::iterator mod_it = frameworkunifiedResourceModuleTable->find(mod);
147
148   if (mod_it == frameworkunifiedResourceModuleTable->end()) {
149     // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
150     FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __func__, "Mod(%s) undefined", mod != 0 ? mod : NULL);
151     // LCOV_EXCL_BR_STOP
152   } else {
153     frameworkunifiedResourceTable_t::iterator res_it = mod_it->second.resTable.find(key);
154     if (res_it != mod_it->second.resTable.end()) {
155       long resource = res_it->second.resource;  // NOLINT  (readability/nolint)
156       mod_it->second.resTable.erase(key);
157
158       if (mod_it->second.resTableR.find(resource) != mod_it->second.resTableR.end()) {
159         mod_it->second.resTableR.erase(resource);
160       }
161     } else {
162       // LCOV_EXCL_BR_START 15:marco defined in "native_service/ns_logger_if.h"
163       FRAMEWORKUNIFIEDLOG(ZONE_NS_WAR, __func__, "Key(%s) no regist", key != 0 ? key : NULL);
164       // LCOV_EXCL_BR_STOP
165     }
166   }
167   return 0;
168 }