Asterisk - The Open Source Telephony Project GIT-master-a63eec2
Loading...
Searching...
No Matches
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.
 

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.
 
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.
 
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 = ASTERISK_GPL_KEY , .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 287 of file res_sorcery_memory.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 287 of file res_sorcery_memory.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 287 of file res_sorcery_memory.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 267 of file res_sorcery_memory.c.

268{
271 }
272
274}
@ 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 262 of file res_sorcery_memory.c.

263{
264 ao2_ref(data, -1);
265}
#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:2381

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 249 of file res_sorcery_memory.c.

250{
251 RAII_VAR(void *, existing, ao2_find(data, ast_sorcery_object_get_id(object), OBJ_KEY | OBJ_UNLINK), ao2_cleanup);
252
253 return existing ? 0 : -1;
254}
#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:978

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.
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition extconf.c:1260
#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 256 of file res_sorcery_memory.c.

257{
260}
@ 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
233 ao2_unlock(data);
234 return -1;
235 }
236
237 if (existing) {
238 ao2_link(data, object);
239 } else {
240 /* Not found: only create if the global flag is enabled */
241 ao2_link_flags(data, object, OBJ_NOLOCK);
242 }
243
244 ao2_unlock(data);
245
246 return 0;
247}
int ast_sorcery_update_or_create_on_update_miss
Global control for optional update->create fallback in backends.
Definition sorcery.c:288

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

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 276 of file res_sorcery_memory.c.

277{
279 return 0;
280}
int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
Unregister a sorcery wizard.
Definition sorcery.c:538

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 = ASTERISK_GPL_KEY , .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 287 of file res_sorcery_memory.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 287 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.

54 {
55 .name = "memory",
56 .open = sorcery_memory_open,
57 .create = sorcery_memory_create,
58 .retrieve_id = sorcery_memory_retrieve_id,
59 .retrieve_fields = sorcery_memory_retrieve_fields,
60 .retrieve_multiple = sorcery_memory_retrieve_multiple,
61 .retrieve_regex = sorcery_memory_retrieve_regex,
62 .retrieve_prefix = sorcery_memory_retrieve_prefix,
63 .update = sorcery_memory_update,
64 .delete = sorcery_memory_delete,
65 .close = sorcery_memory_close,
66};
static void * sorcery_memory_retrieve_id(const struct ast_sorcery *sorcery, void *data, const char *type, const char *id)
static void sorcery_memory_retrieve_regex(const struct ast_sorcery *sorcery, void *data, const char *type, struct ao2_container *objects, const char *regex)
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_fields(const struct ast_sorcery *sorcery, void *data, const char *type, const struct ast_variable *fields)
static int sorcery_memory_create(const struct ast_sorcery *sorcery, void *data, void *object)
static void * sorcery_memory_open(const char *data)
static void sorcery_memory_close(void *data)
static int sorcery_memory_update(const struct ast_sorcery *sorcery, void *data, void *object)
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 int sorcery_memory_delete(const struct ast_sorcery *sorcery, void *data, void *object)

Referenced by load_module(), and unload_module().