Add new api of stream and instance for multi-instance
[platform/core/multimedia/mm-resource-manager.git] / src / common / mm_resource_manager_utils.h
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
17 #ifndef __MM_RESOURCE_MANAGER_UTILS__
18 #define __MM_RESOURCE_MANAGER_UTILS__
19
20 #include <dlog.h>
21 #include <glib.h>
22 #include <stdint.h>
23
24 #include "lib/mm_resource_manager.h"
25
26
27
28 #ifdef LOG_TAG
29 #undef LOG_TAG
30 #endif
31
32 #define LOG_TAG "MM_RESOURCE_MANAGER"
33
34 #define FONT_COLOR_RESET    "\033[0m"
35 #define FONT_COLOR_RED      "\033[31m"
36 #define FONT_COLOR_GREEN    "\033[32m"
37
38 #define MM_RM_DEBUG(fmt, arg...)                                    \
39         do {                                                            \
40                 LOGD(FONT_COLOR_RESET""fmt""FONT_COLOR_RESET, ##arg);       \
41         } while (0)
42
43 #define MM_RM_INFO(fmt, arg...)                                     \
44         do {                                                            \
45                 LOGI(FONT_COLOR_GREEN""fmt""FONT_COLOR_RESET, ##arg);       \
46         } while (0)
47
48 #define MM_RM_ERROR(fmt, arg...)                                    \
49         do {                                                            \
50                 LOGE(FONT_COLOR_RED""fmt""FONT_COLOR_RESET, ##arg);         \
51         } while (0)
52
53 #define MM_RM_RETM_IF(expr, fmt, arg...)                            \
54         do {                                                            \
55                 if (expr) {                                                 \
56                         LOGE(FONT_COLOR_RED""fmt""FONT_COLOR_RESET, ##arg);     \
57                         return;                                                 \
58                 }                                                           \
59         } while (0)
60
61 #define MM_RM_RETVM_IF(expr, val, fmt, arg...)                      \
62         do {                                                            \
63                 if (expr) {                                                 \
64                         LOGE(FONT_COLOR_RED""fmt""FONT_COLOR_RESET, ##arg);     \
65                         return(val);                                            \
66                 }                                                           \
67         } while (0)
68
69 #define MM_RM_RET_IF_GERR(gerr, msg)                                \
70         do {                                                            \
71                 if (gerr != NULL) {                                         \
72                         LOGE(FONT_COLOR_RED""msg""" '%s'"""FONT_COLOR_RESET,    \
73                                 gerr->message);                                     \
74                         g_error_free(error);                                    \
75                         return(MM_RESOURCE_MANAGER_ERROR_INVALID_OPERATION);    \
76                 }                                                           \
77         } while (0)
78
79 #define MM_RM_UNLOCK_RETVM_IF(expr, mtx, val, fmt, arg...)          \
80         do {                                                            \
81                 if (expr) {                                                 \
82                         LOGE(FONT_COLOR_RED""fmt""FONT_COLOR_RESET, ##arg);     \
83                         g_mutex_unlock(&mtx);                                   \
84                         return(val);                                            \
85                 }                                                           \
86         } while (0)
87
88 #define MM_RM_RETM_IF_E(expr, fmt, arg...)                          \
89         do {                                                            \
90                 int err = expr;                                             \
91                 if (err != MM_RESOURCE_MANAGER_ERROR_NONE) {                \
92                         LOGE(FONT_COLOR_RED""fmt""FONT_COLOR_RESET, ##arg);     \
93                         return err;                                             \
94                 }                                                           \
95         } while (0)
96
97
98 #define MM_RM_G_RAND_NEW g_rand_new()
99 #define MM_RM_RAND64(var)                      \
100         do {                                       \
101                 GRand *rnd = MM_RM_G_RAND_NEW;         \
102                 union {                                \
103                         mm_resource_manager_id i;          \
104                         gdouble d;                         \
105                 } i_d_rnd;                             \
106                 i_d_rnd.d = g_rand_double(rnd);        \
107                 g_rand_free(rnd);                      \
108                 var = i_d_rnd.i;                       \
109         } while (0)
110
111 #define MM_RM_HASH64(var)                                           \
112         do {                                                            \
113                 var = (var ^ (var >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);   \
114                 var = (var ^ (var >> 27)) * UINT64_C(0x94d049bb133111eb);   \
115                 var = var ^ (var >> 31);                                    \
116         } while (0)
117
118 #define MM_RESOURCE_MANAGER_NO_RES                      (-2)
119 #define MM_RESOURCE_MANAGER_NO_APP_CLASS        (-1)
120 #define RELEASE_CB_SYNC_PATH                            "/tmp/.mm-res-mgr.fifo"
121
122 typedef uint64_t mm_resource_manager_id;
123
124 const char* _mm_resource_manager_get_res_str(mm_resource_manager_res_type_e type);
125 const char* _mm_resource_manager_get_app_class_str(mm_resource_manager_app_class_e cls);
126 const char* _mm_resource_manager_get_condition_str(mm_resource_manager_res_type_cond_e cond);
127 uint64_t _mm_rm_hash64(uint64_t id);
128
129 #endif /* __MM_RESOURCE_MANAGER_UTILS__ */