2 * Copyright (c) 2011 - 2016 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_internal.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));
44 if (finalizer_new == NULL)
45 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)
52 finalizer_tail = finalizer_tail->next;
54 finalizer_tail->next = finalizer_new;
56 return APP_ERROR_NONE;
59 int app_finalizer_remove(app_finalizer_cb callback)
61 app_finalizer_h finalizer_node = &finalizer_head;
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;
68 return APP_ERROR_NONE;
71 finalizer_node = finalizer_node->next;
74 return APP_ERROR_INVALID_PARAMETER;
77 void app_finalizer_execute(void)
79 app_finalizer_h finalizer_node = &finalizer_head;
80 app_finalizer_h finalizer_executed;
81 app_finalizer_cb finalizer_cb = NULL;
84 finalizer_node = finalizer_node->next;
86 while (finalizer_node) {
87 finalizer_cb = finalizer_node->callback;
89 finalizer_cb(finalizer_node->data);
91 finalizer_executed = finalizer_node;
93 finalizer_node = finalizer_node->next;
95 free(finalizer_executed);
98 finalizer_head.next = NULL;