ca04a0fe93c24b5ab2834a9fa9baa4b85baff23b
[asterisk/asterisk.git] / main / global_datastores.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2007, Digium, Inc.
5  *
6  * Mark Michelson <mmichelson@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief globally-accessible datastore information and callbacks
22  *
23  * \author Mark Michelson <mmichelson@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/global_datastores.h"
31 #include "asterisk/linkedlists.h"
32
33 static void dialed_interface_destroy(void *data)
34 {
35         struct ast_dialed_interface *di = NULL;
36         AST_LIST_HEAD(, ast_dialed_interface) *dialed_interface_list = data;
37         
38         if (!dialed_interface_list) {
39                 return;
40         }
41
42         AST_LIST_LOCK(dialed_interface_list);
43         while ((di = AST_LIST_REMOVE_HEAD(dialed_interface_list, list)))
44                 ast_free(di);
45         AST_LIST_UNLOCK(dialed_interface_list);
46
47         AST_LIST_HEAD_DESTROY(dialed_interface_list);
48         ast_free(dialed_interface_list);
49 }
50
51 static void *dialed_interface_duplicate(void *data)
52 {
53         struct ast_dialed_interface *di = NULL;
54         AST_LIST_HEAD(, ast_dialed_interface) *old_list;
55         AST_LIST_HEAD(, ast_dialed_interface) *new_list = NULL;
56
57         if(!(old_list = data)) {
58                 return NULL;
59         }
60
61         if(!(new_list = ast_calloc(1, sizeof(*new_list)))) {
62                 return NULL;
63         }
64
65         AST_LIST_HEAD_INIT(new_list);
66         AST_LIST_LOCK(old_list);
67         AST_LIST_TRAVERSE(old_list, di, list) {
68                 struct ast_dialed_interface *di2 = ast_calloc(1, sizeof(*di2) + strlen(di->interface));
69                 if(!di2) {
70                         AST_LIST_UNLOCK(old_list);
71                         dialed_interface_destroy(new_list);
72                         return NULL;
73                 }
74                 strcpy(di2->interface, di->interface);
75                 AST_LIST_INSERT_TAIL(new_list, di2, list);
76         }
77         AST_LIST_UNLOCK(old_list);
78
79         return new_list;
80 }
81
82
83 static void *dial_features_duplicate(void *data)
84 {
85         struct ast_dial_features *df = data, *df_copy;
86
87         if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
88                 return NULL;
89         }
90
91         memcpy(df_copy, df, sizeof(*df));
92
93         return df_copy;
94 }
95
96 static void dial_features_destroy(void *data) {
97         struct ast_dial_features *df = data;
98         if (df) {
99                 ast_free(df);
100         }
101 }
102
103 const struct ast_datastore_info dialed_interface_info = {
104         .type = "dialed-interface",
105         .destroy = dialed_interface_destroy,
106         .duplicate = dialed_interface_duplicate,
107 };
108
109 const struct ast_datastore_info dial_features_info = {
110         .type = "dial-features",
111         .destroy = dial_features_destroy,
112         .duplicate = dial_features_duplicate,
113 };