Fix compatibility for x64 arch.
[platform/core/system/sync-agent.git] / src / framework / engine-controller / queuing_rule_spec_pool.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 <stdlib.h>
19 #include <assert.h>
20 #include "utility/sync_util.h"
21 #include "engine-controller/queuing_rule_spec_pool.h"
22 #include <stdint.h>
23 #ifndef SYNC_AGENT_LOG
24 #undef LOG_TAG
25 #define LOG_TAG "AF_EC"
26 #endif
27
28 ec_queuing_rule_spec_pool_t *ec_queuing_rule_spec_pool_alloc(sync_agent_ec_uint queuing_rule_max_cnt, ec_task_info_pool_t * task_info_pool)
29 {
30         _EXTERN_FUNC_ENTER;
31
32         retvm_if(task_info_pool == NULL, NULL, "ec_task_info_pool_t is NULL !!");
33
34         ec_queuing_rule_spec_pool_t *pool = NULL;
35         if (queuing_rule_max_cnt == 0) {
36                 goto error_part;
37         }
38
39         pool = (ec_queuing_rule_spec_pool_t *) calloc(1, sizeof(ec_queuing_rule_spec_pool_t));
40         if (pool == NULL) {
41                 goto return_part;
42         }
43
44         pool->id_provider = util_create_id_provider(queuing_rule_max_cnt - 1, 12, false);
45         if (pool->id_provider == NULL) {
46                 goto error_part;
47         }
48
49         pool->queuing_rule_spec_hash = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) sync_agent_unref_queuing_rule_spec);
50         if (pool->queuing_rule_spec_hash == NULL) {
51                 goto error_part;
52         }
53
54         pool->task_info_pool = task_info_pool;
55
56  return_part:
57         _EXTERN_FUNC_EXIT;
58         return pool;
59
60  error_part:
61         ec_queuing_rule_spec_pool_free(pool);
62         return NULL;
63 }
64
65 void ec_queuing_rule_spec_pool_free(ec_queuing_rule_spec_pool_t * pool)
66 {
67         _EXTERN_FUNC_ENTER;
68
69         if (pool != NULL) {
70                 if (pool->id_provider != NULL) {
71                         util_destroy_id_provider(pool->id_provider);
72                         pool->id_provider = NULL;
73                 }
74                 if (pool->queuing_rule_spec_hash != NULL) {
75                         g_hash_table_destroy(pool->queuing_rule_spec_hash);
76                         pool->queuing_rule_spec_hash = NULL;
77                 }
78                 if (pool->task_info_pool != NULL) {
79                         ec_task_info_pool_free(pool->task_info_pool);
80                         pool->task_info_pool = NULL;
81                 }
82                 free(pool);
83         }
84
85         _EXTERN_FUNC_EXIT;
86 }
87
88 sync_agent_ec_error_e ec_queuing_rule_spec_pool_add_queuing_rule_spec(ec_queuing_rule_spec_pool_t * pool, sync_agent_ec_queuing_rule_spec_s * spec, sync_agent_ec_uint * queuing_rule_id)
89 {
90         _EXTERN_FUNC_ENTER;
91
92         retvm_if(pool == NULL, SYNC_AGENT_EC_UNKNOWN_ERROR, "ec_queuing_rule_spec_pool_t is NULL !!");
93         retvm_if(spec == NULL, SYNC_AGENT_EC_UNKNOWN_ERROR, "sync_agent_ec_queuing_rule_spec_s is NULL !!");
94
95         sync_agent_ec_error_e ec_error = SYNC_AGENT_EC_OK;
96
97         ec_task_info_pool_t *task_info_pool = pool->task_info_pool;
98
99         /* generate id */
100         util_id_provider_error_e id_error = UTIL_ID_PROVIDER_OK;
101         sync_agent_ec_uint new_id = 0;
102
103         id_error = util_provide_id(pool->id_provider, &new_id);
104         if (id_error != UTIL_ID_PROVIDER_OK) {
105                 if (id_error == UTIL_ID_PROVIDER_NOT_ENOUGH_ID) {
106                         ec_error = SYNC_AGENT_EC_OUT_OF_FREE_ID;
107                 } else {
108                         ec_error = SYNC_AGENT_EC_UNKNOWN_ERROR;
109                 }
110                 goto error_part;
111         }
112
113         /* TODO : error handling */
114
115         /* add queuing_rule_spec info to each task_specs */
116         ec_progress_blocking_entity_t *entity = NULL;
117         ec_task_info_t *task_info = NULL;
118         GSList *iter = NULL;
119         for (iter = spec->progress_blocking_entity_list; iter != NULL; iter = g_slist_next(iter)) {
120                 entity = (ec_progress_blocking_entity_t *) (iter->data);
121
122                 task_info = ec_task_info_pool_search_task_info(task_info_pool, entity->root_task_spec_id);
123                 ec_error = ec_task_info_add_progress_blocking_entity(task_info, entity);
124                 if (ec_error != SYNC_AGENT_EC_OK) {
125                         /* undo */
126                         GSList *iter2 = NULL;
127                         ec_progress_blocking_entity_t *pEntity2 = NULL;
128                         for (iter2 = spec->progress_blocking_entity_list; iter2 != iter; iter2 = g_slist_next(iter2)) {
129                                 pEntity2 = (ec_progress_blocking_entity_t *) (iter2->data);
130                                 ec_task_info_remove_blocking_entity(task_info, pEntity2);
131                         }
132
133                         assert(UTIL_ID_PROVIDER_OK == util_delete_id(pool->id_provider, new_id));
134                         goto error_part;
135                 }
136         }
137
138         g_hash_table_insert(pool->queuing_rule_spec_hash, (gpointer) ((intptr_t)new_id), spec);
139
140         *queuing_rule_id = new_id;
141
142  error_part:
143         _EXTERN_FUNC_EXIT;
144         return ec_error;
145 }
146
147 sync_agent_ec_queuing_rule_spec_s *ec_queueing_rule_spec_pool_search_queuing_rule_spec(ec_queuing_rule_spec_pool_t * pool, sync_agent_ec_uint queuing_rule_id)
148 {
149         _EXTERN_FUNC_ENTER;
150
151         retvm_if(pool == NULL, NULL, "ec_queuing_rule_spec_pool_t is NULL !!");
152
153         sync_agent_ec_queuing_rule_spec_s *queuing_rule_spec = g_hash_table_lookup(pool->queuing_rule_spec_hash, (gconstpointer) ((intptr_t)queuing_rule_id));
154
155         _EXTERN_FUNC_EXIT;
156
157         return queuing_rule_spec;
158 }