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.
19 #include <app_internal.h>
21 typedef struct _app_finalizer_s_ {
22 app_finalizer_cb callback;
24 struct _app_finalizer_s_ *next;
27 typedef app_finalizer_s *app_finalizer_h;
29 static app_finalizer_s finalizer_head = {
35 int app_finalizer_add(app_finalizer_cb callback, void *data)
37 app_finalizer_h finalizer_tail = &finalizer_head;
38 app_finalizer_h finalizer_new;
40 finalizer_new = malloc(sizeof(app_finalizer_s));
42 if (finalizer_new == NULL)
44 return app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);
47 finalizer_new->callback = callback;
48 finalizer_new->data = data;
49 finalizer_new->next = NULL;
51 while (finalizer_tail->next)
53 finalizer_tail = finalizer_tail->next;
56 finalizer_tail->next = finalizer_new;
58 return APP_ERROR_NONE;
61 int app_finalizer_remove(app_finalizer_cb callback)
63 app_finalizer_h finalizer_node = &finalizer_head;
65 while (finalizer_node->next)
67 if (finalizer_node->next->callback == callback)
69 app_finalizer_h removed_node = finalizer_node->next;
70 finalizer_node->next = removed_node->next;
72 return APP_ERROR_NONE;
75 finalizer_node = finalizer_node->next;
78 return APP_ERROR_INVALID_PARAMETER;
81 void app_finalizer_execute(void)
83 app_finalizer_h finalizer_node = &finalizer_head;
84 app_finalizer_h finalizer_executed;
85 app_finalizer_cb finalizer_cb = NULL;
88 finalizer_node = finalizer_node->next;
90 while (finalizer_node)
92 finalizer_cb = finalizer_node->callback;
94 finalizer_cb(finalizer_node->data);
96 finalizer_executed = finalizer_node;
98 finalizer_node = finalizer_node->next;
100 free(finalizer_executed);
103 finalizer_head.next = NULL;