2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <native_service/ns_logger_if.h>
19 #include <native_service/ns_resource_controler.h>
25 struct frameworkunifiedResource {
27 long resource; // NOLINT (readability/nolint)
30 typedef std::map<std::string, frameworkunifiedResource> frameworkunifiedResourceTable_t;
31 typedef std::map<long, std::string> frameworkunifiedResourceTableR_t; // NOLINT (readability/nolint)
33 struct frameworkunifiedResourceTable {
34 frameworkunifiedResourceTable_t resTable;
35 frameworkunifiedResourceTableR_t resTableR;
38 typedef std::map<std::string, frameworkunifiedResourceTable> frameworkunifiedResourceModuleTable_t;
40 static __thread frameworkunifiedResourceModuleTable_t *frameworkunifiedResourceModuleTable;
42 static frameworkunifiedResource *frameworkunifiedSearchResourse(const char *mod, const char *key) {
43 if (frameworkunifiedResourceModuleTable == NULL) {
44 frameworkunifiedResourceModuleTable = new frameworkunifiedResourceModuleTable_t;
47 * There is no release processing for "new frameworkunifiedResourceModuleTable_t" created under McOpenMonitor() (Valgrind pointed).
50 frameworkunifiedResourceModuleTable_t::iterator mod_it = frameworkunifiedResourceModuleTable->find(mod);
51 if (mod_it == frameworkunifiedResourceModuleTable->end()) {
55 frameworkunifiedResourceTable_t::iterator res_it = mod_it->second.resTable.find(key);
56 if (res_it == mod_it->second.resTable.end()) {
60 return &res_it->second;
63 int frameworkunifiedGetResource(const char *mod, const char *key, long *resource) { // NOLINT (readability/nolint)
64 frameworkunifiedResource *r = frameworkunifiedSearchResourse(mod, key);
68 if (resource == NULL) {
69 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __func__, "resource is NULL");
72 *resource = r->resource;
76 int frameworkunifiedAcquireResouce(const char *mod, const char *key, long *resource) { // NOLINT (readability/nolint)
77 frameworkunifiedResource *r = frameworkunifiedSearchResourse(mod, key);
81 *resource = r->resource;
82 return ++(r->ref_counter);
85 int frameworkunifiedReleaseResouce(const char *mod, const char *key) {
86 frameworkunifiedResource *r = frameworkunifiedSearchResourse(mod, key);
90 return --(r->ref_counter);
93 int frameworkunifiedSearchResourseKey(const char *mod, long resource, const char **key) { // NOLINT (readability/nolint)
94 if (frameworkunifiedResourceModuleTable == NULL) {
95 frameworkunifiedResourceModuleTable = new frameworkunifiedResourceModuleTable_t;
97 frameworkunifiedResourceModuleTable_t::iterator mod_it = frameworkunifiedResourceModuleTable->find(mod);
98 if (mod_it == frameworkunifiedResourceModuleTable->end()) {
102 frameworkunifiedResourceTableR_t::iterator resR_it = mod_it->second.resTableR.find(resource);
103 if (resR_it == mod_it->second.resTableR.end()) {
107 *key = (resR_it->second).c_str();
111 int frameworkunifiedRegistResouce(const char *mod, const char *key, long resource, // NOLINT (readability/nolint)
115 if (frameworkunifiedResourceModuleTable == NULL) {
116 frameworkunifiedResourceModuleTable = new frameworkunifiedResourceModuleTable_t;
118 frameworkunifiedResourceModuleTable_t::iterator mod_it = frameworkunifiedResourceModuleTable->find(mod);
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
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));
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);
142 int frameworkunifiedUnregistResouce(const char *mod, const char *key) {
143 if (frameworkunifiedResourceModuleTable == NULL) {
144 frameworkunifiedResourceModuleTable = new frameworkunifiedResourceModuleTable_t;
146 frameworkunifiedResourceModuleTable_t::iterator mod_it = frameworkunifiedResourceModuleTable->find(mod);
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);
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);
158 if (mod_it->second.resTableR.find(resource) != mod_it->second.resTableR.end()) {
159 mod_it->second.resTableR.erase(resource);
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);