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