Debian Codegraph @

main/datastore.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2007 - 2008, Digium, Inc.
5 *
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
11 *
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
15 */
16
17 /*! \file
18 *
19 * \brief Asterisk datastore objects
20 */
21
22 /*** MODULEINFO
23 <support_level>core</support_level>
24 ***/
25
26 #include "asterisk.h"
27
28 #include "asterisk/_private.h"
29
30 #include "asterisk/datastore.h"
31 #include "asterisk/utils.h"
32 #include "asterisk/astobj2.h"
33 #include "asterisk/uuid.h"
34 #include "asterisk/module.h"
35
36 /*! \brief Number of buckets for datastore container */
37 #define DATASTORE_BUCKETS 53
38
39 struct ast_datastore *__ast_datastore_alloc(
40 const struct ast_datastore_info *info, const char *uid, struct ast_module *mod,
41 const char *file, int line, const char *function)
42 {
43 struct ast_datastore *datastore = NULL;
44
45 /* Make sure we at least have type so we can identify this */
46 if (!info) {
47 return NULL;
48 }
49
50 datastore = __ast_calloc(1, sizeof(*datastore), file, line, function);
51 if (!datastore) {
52 return NULL;
53 }
54
55 datastore->info = info;
56 datastore->mod = mod;
57
58 if (!ast_strlen_zero(uid) && !(datastore->uid = ast_strdup(uid))) {
59 ast_free(datastore);
60 datastore = NULL;
61 }
62
63 ast_module_ref(mod);
64
65 return datastore;
66 }
67
68 int ast_datastore_free(struct ast_datastore *datastore)
69 {
70 int res = 0;
71
72 if (!datastore) {
73 return 0;
74 }
75
76 /* Using the destroy function (if present) destroy the data */
77 if (datastore->info->destroy != NULL && datastore->data != NULL) {
78 datastore->info->destroy(datastore->data);
79 datastore->data = NULL;
80 }
81
82 /* Free allocated UID memory */
83 if (datastore->uid != NULL) {
84 ast_free((void *) datastore->uid);
85 datastore->uid = NULL;
86 }
87
88 ast_module_unref(datastore->mod);
89
90 /* Finally free memory used by ourselves */
91 ast_free(datastore);
92
93 return res;
94 }
95
96 AO2_STRING_FIELD_HASH_FN(ast_datastore, uid);
97 AO2_STRING_FIELD_CMP_FN(ast_datastore, uid);
98
99 struct ao2_container *ast_datastores_alloc(void)
100 {
101 return ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0,
102 DATASTORE_BUCKETS, ast_datastore_hash_fn, NULL, ast_datastore_cmp_fn);
103 }
104
105 int ast_datastores_add(struct ao2_container *datastores, struct ast_datastore *datastore)
106 {
107 ast_assert(datastore != NULL);
108 ast_assert(datastore->info != NULL);
109 ast_assert(!ast_strlen_zero(datastore->uid));
110
111 if (!ao2_link(datastores, datastore)) {
112 return -1;
113 }
114
115 return 0;
116 }
117
118 void ast_datastores_remove(struct ao2_container *datastores, const char *name)
119 {
120 ao2_find(datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
121 }
122
123 struct ast_datastore *ast_datastores_find(struct ao2_container *datastores, const char *name)
124 {
125 return ao2_find(datastores, name, OBJ_SEARCH_KEY);
126 }
127
128 static void datastore_destroy(void *obj)
129 {
130 struct ast_datastore *datastore = obj;
131
132 /* Using the destroy function (if present) destroy the data */
133 if (datastore->info->destroy != NULL && datastore->data != NULL) {
134 datastore->info->destroy(datastore->data);
135 datastore->data = NULL;
136 }
137
138 ast_free((void *) datastore->uid);
139 datastore->uid = NULL;
140 }
141
142 struct ast_datastore *ast_datastores_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
143 {
144 struct ast_datastore *datastore;
145 char uuid_buf[AST_UUID_STR_LEN];
146 const char *uid_ptr = uid;
147
148 if (!info) {
149 return NULL;
150 }
151
152 datastore = ao2_alloc(sizeof(*datastore), datastore_destroy);
153 if (!datastore) {
154 return NULL;
155 }
156
157 datastore->info = info;
158 if (ast_strlen_zero(uid)) {
159 /* They didn't provide an ID so we'll provide one ourself */
160 uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
161 }
162
163 datastore->uid = ast_strdup(uid_ptr);
164 if (!datastore->uid) {
165 ao2_ref(datastore, -1);
166 return NULL;
167 }
168
169 return datastore;
170 }