2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <app_private.h>
24 typedef struct _app_finalizer_s_ {
25 app_finalizer_cb callback;
27 struct _app_finalizer_s_ *next;
30 typedef app_finalizer_s *app_finalizer_h;
32 static app_finalizer_s finalizer_head = {
38 int app_finalizer_add(app_finalizer_cb callback, void *data)
40 app_finalizer_h finalizer_tail = &finalizer_head;
41 app_finalizer_h finalizer_new;
43 finalizer_new = malloc(sizeof(app_finalizer_s));
45 if (finalizer_new == NULL)
47 return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
50 finalizer_new->callback = callback;
51 finalizer_new->data = data;
52 finalizer_new->next = NULL;
54 while (finalizer_tail->next)
56 finalizer_tail = finalizer_tail->next;
59 finalizer_tail->next = finalizer_new;
61 return APP_ERROR_NONE;
64 int app_finalizer_remove(app_finalizer_cb callback)
66 app_finalizer_h finalizer_node = &finalizer_head;
68 while (finalizer_node->next)
70 if (finalizer_node->next->callback == callback)
72 app_finalizer_h removed_node = finalizer_node->next;
73 finalizer_node->next = removed_node->next;
75 return APP_ERROR_NONE;
78 finalizer_node = finalizer_node->next;
81 return APP_ERROR_INVALID_PARAMETER;
84 void app_finalizer_execute(void)
86 app_finalizer_h finalizer_node = &finalizer_head;
87 app_finalizer_h finalizer_executed;
88 app_finalizer_cb finalizer_cb = NULL;
90 while (finalizer_node->next)
92 finalizer_cb = finalizer_node->next->callback;
94 finalizer_cb(finalizer_node->next->data);
96 finalizer_executed = finalizer_node->next;
98 finalizer_node = finalizer_node->next;
100 free(finalizer_executed);
103 finalizer_head.next = NULL;