Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / utility / fw_ref.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #include "utility/sync_util.h"
19 #include "utility/fw_ref.h"
20 #include <stdlib.h>
21 #include <glib.h>
22
23 #ifndef EXPORT_API
24 #define EXPORT_API __attribute__ ((visibility("default")))
25 #endif
26
27 #ifndef SYNC_AGENT_LOG
28 #undef LOG_TAG
29 #define LOG_TAG "AF_REF"
30 #endif
31
32 EXPORT_API sync_agent_util_ref_object_s *sync_agent_create_referenced_object(void *data, sync_agent_free_cb data_free_func)
33 {
34         _EXTERN_FUNC_ENTER;
35
36         retvm_if(data == NULL, NULL, "data parameter is NULL !!!");
37 //      retvm_if(data_free_func == NULL, NULL, "data_free_func parameter is 0 !!!");
38
39         sync_agent_util_ref_object_s *ref = (sync_agent_util_ref_object_s *) malloc(sizeof(sync_agent_util_ref_object_s));
40         if (ref == NULL) {
41                 goto error_part;
42         }
43
44         ref->refcount = 1;
45         ref->data = data;
46         ref->free_func = data_free_func;
47
48         _EXTERN_FUNC_EXIT;
49
50         return ref;
51
52  error_part:
53         return NULL;
54 }
55
56 EXPORT_API sync_agent_util_ref_object_s *sync_agent_get_referenced_object(sync_agent_util_ref_object_s * object)
57 {
58         _EXTERN_FUNC_ENTER;
59
60         retvm_if(object == NULL, NULL, "object parameter is NULL !!!");
61
62         g_atomic_int_inc(&(object->refcount));
63
64         _EXTERN_FUNC_EXIT;
65
66         return object;
67 }
68
69 EXPORT_API void *sync_agent_get_original_object(sync_agent_util_ref_object_s * object)
70 {
71         _EXTERN_FUNC_ENTER;
72
73         retvm_if(object == NULL, NULL, "object parameter is NULL !!!");
74
75         _EXTERN_FUNC_EXIT;
76
77         return object->data;
78 }
79
80 EXPORT_API bool sync_agent_compare_object(sync_agent_util_ref_object_s * object1, sync_agent_util_ref_object_s * object2)
81 {
82         _EXTERN_FUNC_ENTER;
83
84         retvm_if(object1 == NULL, false, "object1 parameter is NULL !!!");
85         retvm_if(object2 == NULL, false, "object2 parameter is NULL !!!");
86
87         if ((object1->data) == (object2->data)) {
88                 _EXTERN_FUNC_EXIT;
89                 return true;
90         } else {
91                 _EXTERN_FUNC_EXIT;
92                 return false;
93         }
94 }
95
96 EXPORT_API void sync_agent_unref_referenced_object(sync_agent_util_ref_object_s * ref)
97 {
98         _EXTERN_FUNC_ENTER;
99
100         retm_if(ref == NULL, "ref parameter is NULL !!!");
101
102         if (g_atomic_int_dec_and_test(&(ref->refcount))) {
103                 if (ref->free_func != NULL)
104                         ref->free_func(ref->data);
105
106                 free(ref);
107         }
108
109         _EXTERN_FUNC_EXIT;
110 }