tizen 2.3 release
[framework/api/application.git] / app_common / app_finalizer.c
1 /*
2  * Copyright (c) 2011 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 #include <stdlib.h>
18
19 #include <app_internal.h>
20
21 typedef struct _app_finalizer_s_ {
22         app_finalizer_cb callback;
23         void *data;
24         struct _app_finalizer_s_ *next;
25 } app_finalizer_s;
26
27 typedef app_finalizer_s *app_finalizer_h;
28
29 static app_finalizer_s finalizer_head = {
30         .callback = NULL,
31         .data = NULL,
32         .next = NULL
33 };
34
35 int app_finalizer_add(app_finalizer_cb callback, void *data)
36 {
37         app_finalizer_h finalizer_tail = &finalizer_head;
38         app_finalizer_h finalizer_new;
39
40         finalizer_new = malloc(sizeof(app_finalizer_s));
41
42         if (finalizer_new == NULL)
43         {
44                 return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
45         }
46
47         finalizer_new->callback = callback;
48         finalizer_new->data = data;
49         finalizer_new->next = NULL;
50
51         while (finalizer_tail->next)
52         {
53                 finalizer_tail = finalizer_tail->next;
54         }
55
56         finalizer_tail->next = finalizer_new;
57
58         return APP_ERROR_NONE;
59 }
60
61 int app_finalizer_remove(app_finalizer_cb callback)
62 {
63         app_finalizer_h finalizer_node = &finalizer_head;
64
65         while (finalizer_node->next)
66         {
67                 if (finalizer_node->next->callback == callback)
68                 {
69                         app_finalizer_h removed_node = finalizer_node->next;
70                         finalizer_node->next = removed_node->next;
71                         free(removed_node);
72                         return APP_ERROR_NONE;
73                 }
74
75                 finalizer_node = finalizer_node->next;
76         }
77
78         return APP_ERROR_INVALID_PARAMETER;
79 }
80
81 void app_finalizer_execute(void)
82 {
83         app_finalizer_h finalizer_node = &finalizer_head;
84         app_finalizer_h finalizer_executed;
85         app_finalizer_cb finalizer_cb = NULL;
86
87         if(finalizer_node)
88                 finalizer_node = finalizer_node->next;
89
90         while (finalizer_node)
91         {
92                 finalizer_cb = finalizer_node->callback;
93
94                 finalizer_cb(finalizer_node->data);
95
96                 finalizer_executed = finalizer_node;
97
98                 finalizer_node = finalizer_node->next;
99
100                 free(finalizer_executed);
101         }
102
103         finalizer_head.next = NULL;
104 }
105