Asterisk - The Open Source Telephony Project GIT-master-f36a736
Data Structures | Macros | Functions | Variables
res_sorcery_memory.c File Reference

Sorcery In-Memory Object Wizard. More...

#include "asterisk.h"
#include <regex.h>
#include "asterisk/module.h"
#include "asterisk/sorcery.h"
#include "asterisk/astobj2.h"
Include dependency graph for res_sorcery_memory.c:

Go to the source code of this file.

Data Structures

struct  sorcery_memory_fields_cmp_params
 Structure used for fields comparison. More...
 

Macros

#define OBJECT_BUCKETS   53
 Number of buckets for sorcery objects. More...
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static void sorcery_memory_close (void *data)
 
static int sorcery_memory_cmp (void *obj, void *arg, int flags)
 Comparator function for sorcery objects. More...
 
static int sorcery_memory_create (const struct ast_sorcery *sorcery, void *data, void *object)
 
static int sorcery_memory_delete (const struct ast_sorcery *sorcery, void *data, void *object)
 
static int sorcery_memory_fields_cmp (void *obj, void *arg, int flags)
 
static int sorcery_memory_hash (const void *obj, const int flags)
 Hashing function for sorcery objects. More...
 
static void * sorcery_memory_open (const char *data)
 
static void * sorcery_memory_retrieve_fields (const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
 
static void * sorcery_memory_retrieve_id (const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
 
static void sorcery_memory_retrieve_multiple (const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const struct ast_variable *fields)
 
static void sorcery_memory_retrieve_prefix (const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *prefix, const size_t prefix_len)
 
static void sorcery_memory_retrieve_regex (const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
 
static int sorcery_memory_update (const struct ast_sorcery *sorcery, void *data, void *object)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "Sorcery In-Memory Object Wizard" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct ast_sorcery_wizard memory_object_wizard
 

Detailed Description

Sorcery In-Memory Object Wizard.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file res_sorcery_memory.c.

Macro Definition Documentation

◆ OBJECT_BUCKETS

#define OBJECT_BUCKETS   53

Number of buckets for sorcery objects.

Definition at line 40 of file res_sorcery_memory.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 282 of file res_sorcery_memory.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 282 of file res_sorcery_memory.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 282 of file res_sorcery_memory.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 262 of file res_sorcery_memory.c.

263{
266 }
267
269}
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static struct ast_sorcery_wizard memory_object_wizard
#define ast_sorcery_wizard_register(interface)
See __ast_sorcery_wizard_register()
Definition: sorcery.h:383

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_sorcery_wizard_register, and memory_object_wizard.

◆ sorcery_memory_close()

static void sorcery_memory_close ( void *  data)
static

Definition at line 257 of file res_sorcery_memory.c.

258{
259 ao2_ref(data, -1);
260}
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459

References ao2_ref.

◆ sorcery_memory_cmp()

static int sorcery_memory_cmp ( void *  obj,
void *  arg,
int  flags 
)
static

Comparator function for sorcery objects.

Definition at line 98 of file res_sorcery_memory.c.

99{
100 const char *id = arg;
101
102 return !strcmp(ast_sorcery_object_get_id(obj), flags & OBJ_KEY ? id : ast_sorcery_object_get_id(arg)) ? CMP_MATCH | CMP_STOP : 0;
103}
@ CMP_MATCH
Definition: astobj2.h:1027
@ CMP_STOP
Definition: astobj2.h:1028
#define OBJ_KEY
Definition: astobj2.h:1151
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2317

References ast_sorcery_object_get_id(), CMP_MATCH, CMP_STOP, and OBJ_KEY.

Referenced by sorcery_memory_open().

◆ sorcery_memory_create()

static int sorcery_memory_create ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Definition at line 105 of file res_sorcery_memory.c.

106{
107 void *existing;
108
109 ao2_lock(data);
110
111 existing = ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_NOLOCK);
112 if (existing) {
113 ao2_ref(existing, -1);
114 ao2_unlock(data);
115 return -1;
116 }
117
118 ao2_link_flags(data, object, OBJ_NOLOCK);
119
120 ao2_unlock(data);
121
122 return 0;
123}
#define ao2_link_flags(container, obj, flags)
Add an object to a container.
Definition: astobj2.h:1554
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
#define ao2_unlock(a)
Definition: astobj2.h:729
#define ao2_lock(a)
Definition: astobj2.h:717
@ OBJ_NOLOCK
Assume that the ao2_container is already locked.
Definition: astobj2.h:1063

References ao2_find, ao2_link_flags, ao2_lock, ao2_ref, ao2_unlock, ast_sorcery_object_get_id(), OBJ_KEY, and OBJ_NOLOCK.

◆ sorcery_memory_delete()

static int sorcery_memory_delete ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Definition at line 244 of file res_sorcery_memory.c.

245{
246 RAII_VAR(void *, existing, ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
247
248 return existing ? 0 : -1;
249}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
@ OBJ_UNLINK
Definition: astobj2.h:1039
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

References ao2_cleanup, ao2_find, ast_sorcery_object_get_id(), OBJ_KEY, OBJ_UNLINK, and RAII_VAR.

◆ sorcery_memory_fields_cmp()

static int sorcery_memory_fields_cmp ( void *  obj,
void *  arg,
int  flags 
)
static

Definition at line 125 of file res_sorcery_memory.c.

126{
127 const struct sorcery_memory_fields_cmp_params *params = arg;
129
130 if (params->regex) {
131 /* If a regular expression has been provided see if it matches, otherwise move on */
132 if (!regexec(params->regex, ast_sorcery_object_get_id(obj), 0, NULL, 0)) {
133 ao2_link(params->container, obj);
134 }
135 return 0;
136 } else if (params->prefix) {
137 if (!strncmp(params->prefix, ast_sorcery_object_get_id(obj), params->prefix_len)) {
138 ao2_link(params->container, obj);
139 }
140 return 0;
141 } else if (params->fields &&
142 (!(objset = ast_sorcery_objectset_create(params->sorcery, obj)) ||
143 (!ast_variable_lists_match(objset, params->fields, 0)))) {
144 /* If we can't turn the object into an object set OR if differences exist between the fields
145 * passed in and what are present on the object they are not a match.
146 */
147 return 0;
148 }
149
150 if (params->container) {
151 ao2_link(params->container, obj);
152
153 /* As multiple objects are being returned keep going */
154 return 0;
155 } else {
156 /* Immediately stop and return, we only want a single object */
157 return CMP_MATCH | CMP_STOP;
158 }
159}
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
int ast_variable_lists_match(const struct ast_variable *left, const struct ast_variable *right, int exact_match)
Tests 2 variable lists to see if they match.
Definition: main/config.c:870
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1262
#define NULL
Definition: resample.c:96
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition: sorcery.h:1137
Structure for variables, used for configurations and for channel variables.
Structure used for fields comparison.
const char * prefix
Prefix for matching object id.
struct ao2_container * container
Optional container to put object into.
const size_t prefix_len
Prefix length in bytes for matching object id.
const struct ast_sorcery * sorcery
Pointer to the sorcery structure.
const struct ast_variable * fields
Pointer to the fields to check.
regex_t * regex
Regular expression for checking object id.

References ao2_link, ast_sorcery_object_get_id(), ast_sorcery_objectset_create, ast_variable_lists_match(), ast_variables_destroy(), CMP_MATCH, CMP_STOP, sorcery_memory_fields_cmp_params::container, sorcery_memory_fields_cmp_params::fields, NULL, sorcery_memory_fields_cmp_params::prefix, sorcery_memory_fields_cmp_params::prefix_len, RAII_VAR, sorcery_memory_fields_cmp_params::regex, and sorcery_memory_fields_cmp_params::sorcery.

Referenced by sorcery_memory_retrieve_fields(), sorcery_memory_retrieve_multiple(), sorcery_memory_retrieve_prefix(), and sorcery_memory_retrieve_regex().

◆ sorcery_memory_hash()

static int sorcery_memory_hash ( const void *  obj,
const int  flags 
)
static

Hashing function for sorcery objects.

Definition at line 90 of file res_sorcery_memory.c.

91{
92 const char *id = obj;
93
94 return ast_str_hash(flags & OBJ_KEY ? id : ast_sorcery_object_get_id(obj));
95}
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1259

References ast_sorcery_object_get_id(), ast_str_hash(), and OBJ_KEY.

Referenced by sorcery_memory_open().

◆ sorcery_memory_open()

static void * sorcery_memory_open ( const char *  data)
static

Definition at line 251 of file res_sorcery_memory.c.

252{
255}
@ AO2_ALLOC_OPT_LOCK_MUTEX
Definition: astobj2.h:363
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
#define OBJECT_BUCKETS
Number of buckets for sorcery objects.
static int sorcery_memory_hash(const void *obj, const int flags)
Hashing function for sorcery objects.
static int sorcery_memory_cmp(void *obj, void *arg, int flags)
Comparator function for sorcery objects.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, NULL, OBJECT_BUCKETS, sorcery_memory_cmp(), and sorcery_memory_hash().

◆ sorcery_memory_retrieve_fields()

static void * sorcery_memory_retrieve_fields ( const struct ast_sorcery sorcery,
void *  data,
const char *  type,
const struct ast_variable fields 
)
static

Definition at line 161 of file res_sorcery_memory.c.

162{
163 struct sorcery_memory_fields_cmp_params params = {
164 .sorcery = sorcery,
165 .fields = fields,
166 .container = NULL,
167 };
168
169 /* If no fields are present return nothing, we require *something* */
170 if (!fields) {
171 return NULL;
172 }
173
174 return ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
175}
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
static struct ast_sorcery * sorcery
static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)

References ao2_callback, sorcery_memory_fields_cmp_params::fields, NULL, sorcery, sorcery_memory_fields_cmp_params::sorcery, and sorcery_memory_fields_cmp().

◆ sorcery_memory_retrieve_id()

static void * sorcery_memory_retrieve_id ( const struct ast_sorcery sorcery,
void *  data,
const char *  type,
const char *  id 
)
static

Definition at line 177 of file res_sorcery_memory.c.

178{
179 return ao2_find(data, id, OBJ_KEY);
180}

References ao2_find, and OBJ_KEY.

◆ sorcery_memory_retrieve_multiple()

static void sorcery_memory_retrieve_multiple ( const struct ast_sorcery sorcery,
void *  data,
const char *  type,
struct ao2_container objects,
const struct ast_variable fields 
)
static

Definition at line 182 of file res_sorcery_memory.c.

183{
184 struct sorcery_memory_fields_cmp_params params = {
185 .sorcery = sorcery,
186 .fields = fields,
187 .container = objects,
188 };
189
190 ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
191}

References ao2_callback, sorcery_memory_fields_cmp_params::fields, sorcery, sorcery_memory_fields_cmp_params::sorcery, and sorcery_memory_fields_cmp().

◆ sorcery_memory_retrieve_prefix()

static void sorcery_memory_retrieve_prefix ( const struct ast_sorcery sorcery,
void *  data,
const char *  type,
struct ao2_container objects,
const char *  prefix,
const size_t  prefix_len 
)
static

Definition at line 214 of file res_sorcery_memory.c.

215{
216 struct sorcery_memory_fields_cmp_params params = {
217 .sorcery = sorcery,
218 .container = objects,
219 .prefix = prefix,
220 .prefix_len = prefix_len,
221 };
222
223 ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
224}
static char prefix[MAX_PREFIX]
Definition: http.c:144

References ao2_callback, prefix, sorcery_memory_fields_cmp_params::prefix_len, sorcery, sorcery_memory_fields_cmp_params::sorcery, and sorcery_memory_fields_cmp().

◆ sorcery_memory_retrieve_regex()

static void sorcery_memory_retrieve_regex ( const struct ast_sorcery sorcery,
void *  data,
const char *  type,
struct ao2_container objects,
const char *  regex 
)
static

Definition at line 193 of file res_sorcery_memory.c.

194{
195 regex_t expression;
196 struct sorcery_memory_fields_cmp_params params = {
197 .sorcery = sorcery,
198 .container = objects,
199 .regex = &expression,
200 };
201
202 if (ast_strlen_zero(regex)) {
203 regex = ".";
204 }
205
206 if (regcomp(&expression, regex, REG_EXTENDED | REG_NOSUB)) {
207 return;
208 }
209
210 ao2_callback(data, 0, sorcery_memory_fields_cmp, &params);
211 regfree(&expression);
212}
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65

References ao2_callback, ast_strlen_zero(), regex(), sorcery, sorcery_memory_fields_cmp_params::sorcery, and sorcery_memory_fields_cmp().

◆ sorcery_memory_update()

static int sorcery_memory_update ( const struct ast_sorcery sorcery,
void *  data,
void *  object 
)
static

Definition at line 226 of file res_sorcery_memory.c.

227{
228 RAII_VAR(void *, existing, NULL, ao2_cleanup);
229
230 ao2_lock(data);
231
232 if (!(existing = ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK))) {
233 ao2_unlock(data);
234 return -1;
235 }
236
237 ao2_link(data, object);
238
239 ao2_unlock(data);
240
241 return 0;
242}

References ao2_cleanup, ao2_find, ao2_link, ao2_lock, ao2_unlock, ast_sorcery_object_get_id(), NULL, OBJ_KEY, OBJ_UNLINK, and RAII_VAR.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 271 of file res_sorcery_memory.c.

272{
274 return 0;
275}
int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
Unregister a sorcery wizard.
Definition: sorcery.c:474

References ast_sorcery_wizard_unregister(), and memory_object_wizard.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "Sorcery In-Memory Object Wizard" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_REALTIME_DRIVER, }
static

Definition at line 282 of file res_sorcery_memory.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 282 of file res_sorcery_memory.c.

◆ memory_object_wizard

struct ast_sorcery_wizard memory_object_wizard
static

Definition at line 54 of file res_sorcery_memory.c.

Referenced by load_module(), and unload_module().