Asterisk - The Open Source Telephony Project  GIT-master-b7027de
datastore.c
Go to the documentation of this file.
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 
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  /* Using the destroy function (if present) destroy the data */
73  if (datastore->info->destroy != NULL && datastore->data != NULL) {
74  datastore->info->destroy(datastore->data);
75  datastore->data = NULL;
76  }
77 
78  /* Free allocated UID memory */
79  if (datastore->uid != NULL) {
80  ast_free((void *) datastore->uid);
81  datastore->uid = NULL;
82  }
83 
84  ast_module_unref(datastore->mod);
85 
86  /* Finally free memory used by ourselves */
87  ast_free(datastore);
88 
89  return res;
90 }
91 
94 
96 {
98  DATASTORE_BUCKETS, ast_datastore_hash_fn, NULL, ast_datastore_cmp_fn);
99 }
100 
101 int ast_datastores_add(struct ao2_container *datastores, struct ast_datastore *datastore)
102 {
103  ast_assert(datastore != NULL);
104  ast_assert(datastore->info != NULL);
105  ast_assert(!ast_strlen_zero(datastore->uid));
106 
107  if (!ao2_link(datastores, datastore)) {
108  return -1;
109  }
110 
111  return 0;
112 }
113 
114 void ast_datastores_remove(struct ao2_container *datastores, const char *name)
115 {
116  ao2_find(datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
117 }
118 
119 struct ast_datastore *ast_datastores_find(struct ao2_container *datastores, const char *name)
120 {
121  return ao2_find(datastores, name, OBJ_SEARCH_KEY);
122 }
123 
124 static void datastore_destroy(void *obj)
125 {
126  struct ast_datastore *datastore = obj;
127 
128  /* Using the destroy function (if present) destroy the data */
129  if (datastore->info->destroy != NULL && datastore->data != NULL) {
130  datastore->info->destroy(datastore->data);
131  datastore->data = NULL;
132  }
133 
134  ast_free((void *) datastore->uid);
135  datastore->uid = NULL;
136 }
137 
139 {
140  struct ast_datastore *datastore;
141  char uuid_buf[AST_UUID_STR_LEN];
142  const char *uid_ptr = uid;
143 
144  if (!info) {
145  return NULL;
146  }
147 
148  datastore = ao2_alloc(sizeof(*datastore), datastore_destroy);
149  if (!datastore) {
150  return NULL;
151  }
152 
153  datastore->info = info;
154  if (ast_strlen_zero(uid)) {
155  /* They didn't provide an ID so we'll provide one ourself */
156  uid_ptr = ast_uuid_generate_str(uuid_buf, sizeof(uuid_buf));
157  }
158 
159  datastore->uid = ast_strdup(uid_ptr);
160  if (!datastore->uid) {
161  ao2_ref(datastore, -1);
162  return NULL;
163  }
164 
165  return datastore;
166 }
void * __ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1635
Asterisk main include file. File version handling, generic pbx functions.
int ast_datastores_add(struct ao2_container *datastores, struct ast_datastore *datastore)
Add a data store to a container.
Definition: datastore.c:101
#define AST_UUID_STR_LEN
Definition: uuid.h:27
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:68
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
struct ast_datastore * ast_datastores_find(struct ao2_container *datastores, const char *name)
Find a data store in a container.
Definition: datastore.c:119
Universally unique identifier support.
Structure for a data store type.
Definition: datastore.h:31
#define ast_assert(a)
Definition: utils.h:710
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
Structure for a data store object.
Definition: datastore.h:68
#define NULL
Definition: resample.c:96
const char * uid
Definition: datastore.h:69
Utility functions.
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
AO2_STRING_FIELD_HASH_FN(ast_datastore, uid)
Asterisk datastore objects.
struct ast_module * mod
Definition: datastore.h:72
#define ao2_ref(o, delta)
Definition: astobj2.h:464
const struct ast_datastore_info * info
Definition: datastore.h:71
void(* destroy)(void *data)
Definition: datastore.h:34
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
def info(msg)
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
#define DATASTORE_BUCKETS
Number of buckets for datastore container.
Definition: datastore.c:37
#define ast_strlen_zero(a)
Definition: muted.c:73
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.
Definition: uuid.c:143
Prototypes for public functions only of internal interest,.
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
static void datastore_destroy(void *obj)
Definition: datastore.c:124
AO2_STRING_FIELD_CMP_FN(ast_datastore, uid)
void * data
Definition: datastore.h:70
struct ao2_container * ast_datastores_alloc(void)
Allocate a specialized data stores container.
Definition: datastore.c:95
void ast_datastores_remove(struct ao2_container *datastores, const char *name)
Remove a data store from a container.
Definition: datastore.c:114
Generic container type.
struct ast_datastore * __ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid, struct ast_module *mod, const char *file, int line, const char *function)
Create a data store object.
Definition: datastore.c:39
struct ast_datastore * ast_datastores_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
Allocate a datastore for use with the datastores container.
Definition: datastore.c:138
Asterisk module definitions.
#define ast_module_ref(mod)
Hold a reference to the module.
Definition: module.h:443
#define ao2_link(container, obj)
Definition: astobj2.h:1549