3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
25 #endif /* __cplusplus */
29 * @brief Provides conversion service from regular object to referenced object
32 /** @addtogroup utility
37 * Function which is called when need to alloc some object
39 * @return object to alloc on success, NULL on error
47 typedef void *(*sync_agent_alloc_cb) (void);
50 * Function which is called when need to free some object
52 * @param[in] void* object to free
57 * @see sync_agent_create_referenced_object(void *, sync_agent_free_cb)
60 typedef void (*sync_agent_free_cb) (void *);
63 * Structure of referenced object
65 typedef struct utility_ref_object {
66 int refcount; /**< Reference count */
67 void *data; /**< Void pointer for original regular object */
68 sync_agent_free_cb free_func;
70 } sync_agent_util_ref_object_s;
73 * API to create referenced object using given data with void pointer and function pointer for free logic
77 void data_free_cb(void *data)
83 sync_agent_util_ref_object_s *ref_obj = sync_agent_create_referenced_object(data, data_free_cb);
84 if (ref_obj == NULL) {
91 * @param[in] data data to maintain reference information for
92 * @param[in] data_free_func function pointer for free logic
94 * @return referenced object in sync_agent_util_ref_object_s type of structure on success, NULL on error
99 * @see sync_agent_create_referenced_object(void *, sync_agent_free_cb)
102 sync_agent_util_ref_object_s *sync_agent_create_referenced_object(void *data, sync_agent_free_cb data_free_func);
105 * API to return referenced object using already referenced object and just increase reference count
109 sync_agent_util_ref_object_s *object = NULL;
111 sync_agent_util_ref_object_s *ref_obj = sync_agent_get_referenced_object(object);
112 if (ref_obj == NULL) {
119 * @param[in] object referenced object to increase reference count by 1
121 * @return referenced object in sync_agent_util_ref_object_s type of structure on success, NULL on error
126 * @see sync_agent_get_referenced_object(sync_agent_util_ref_object_s *)
129 sync_agent_util_ref_object_s *sync_agent_get_referenced_object(sync_agent_util_ref_object_s * object);
132 * API to return original object taking from referenced object
136 sync_agent_util_ref_object_s *ref_obj = NULL;
138 void *origin_obj = sync_agent_get_original_object(ref_obj);
142 * @param[in] object referenced object
144 * @return original object
149 * @see sync_agent_get_original_object(sync_agent_util_ref_object_s *)
152 void *sync_agent_get_original_object(sync_agent_util_ref_object_s * object);
155 * API to compares if two referenced object has same original object
159 sync_agent_util_ref_object_s *obj1 = NULL;
160 sync_agent_util_ref_object_s *obj2 = NULL;
162 bool is_same = sync_agent_compare_object(obj1, obj2);
173 * @param[in] object1 referenced object to compare with
174 * @param[in] object2 referenced object to compare with
176 * @return 1 when two are same, otherwise 0
181 * @see sync_agent_compare_object(sync_agent_util_ref_object_s *, sync_agent_util_ref_object_s *)
184 bool sync_agent_compare_object(sync_agent_util_ref_object_s * object1, sync_agent_util_ref_object_s * object2);
187 * API to decrease reference count of given referenced object
188 * The object will be freed in case reference count becomes 0
192 sync_agent_util_ref_object_s *ref_obj = NULL;
194 if (ref_obj != NULL) {
195 sync_agent_unref_referenced_object(ref_obj);
200 * @param[in] ref referenced object to decrease ref count by 1
205 * @see sync_agent_unref_referenced_object(sync_agent_util_ref_object_s *)
208 void sync_agent_unref_referenced_object(sync_agent_util_ref_object_s * ref);
216 #endif /* __cplusplus */
217 #endif /* FW_REF_H_ */