Asterisk - The Open Source Telephony Project  18.5.0
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.

Referenced by sorcery_memory_open().

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.

References AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, and ast_sorcery_wizard_register.

Referenced by unload_module().

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

◆ sorcery_memory_close()

static void sorcery_memory_close ( void *  data)
static

Definition at line 257 of file res_sorcery_memory.c.

References ao2_ref.

258 {
259  ao2_ref(data, -1);
260 }
#define ao2_ref(o, delta)
Definition: astobj2.h:464

◆ 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.

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

Referenced by sorcery_memory_open().

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 }
#define OBJ_KEY
Definition: astobj2.h:1155
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312

◆ 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.

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

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 OBJ_KEY
Definition: astobj2.h:1155
Assume that the ao2_container is already locked.
Definition: astobj2.h:1067
#define ao2_link_flags(container, obj, flags)
Definition: astobj2.h:1572
#define ao2_unlock(a)
Definition: astobj2.h:730
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_lock(a)
Definition: astobj2.h:718
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756

◆ 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.

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

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 OBJ_KEY
Definition: astobj2.h:1155
#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:911
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

◆ 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.

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().

126 {
127  const struct sorcery_memory_fields_cmp_params *params = arg;
128  RAII_VAR(struct ast_variable *, objset, NULL, ast_variables_destroy);
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 }
void ast_variables_destroy(struct ast_variable *var)
Free variable list.
Definition: extconf.c:1263
Structure for variables, used for configurations and for channel variables.
#define NULL
Definition: resample.c:96
struct ao2_container * container
Optional container to put object into.
#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:911
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
regex_t * regex
Regular expression for checking object id.
const size_t prefix_len
Prefix length in bytes for matching object id.
#define ast_sorcery_objectset_create(sorcery, object)
Create an object set (KVP list) for an object.
Definition: sorcery.h:1136
const char * prefix
Prefix for matching object id.
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:772
const struct ast_variable * fields
Pointer to the fields to check.
const struct ast_sorcery * sorcery
Pointer to the sorcery structure.
Structure used for fields comparison.
#define ao2_link(container, obj)
Definition: astobj2.h:1549

◆ 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.

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

Referenced by sorcery_memory_open().

91 {
92  const char *id = obj;
93 
94  return ast_str_hash(flags & OBJ_KEY ? id : ast_sorcery_object_get_id(obj));
95 }
#define OBJ_KEY
Definition: astobj2.h:1155
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
static force_inline int attribute_pure ast_str_hash(const char *str)
Compute a hash value on a string.
Definition: strings.h:1206

◆ sorcery_memory_open()

static void * sorcery_memory_open ( const char *  data)
static

Definition at line 251 of file res_sorcery_memory.c.

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

252 {
255 }
#define NULL
Definition: resample.c:96
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
#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.

◆ 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.

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

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)
Definition: astobj2.h:1716
#define NULL
Definition: resample.c:96
static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
static struct ast_sorcery * sorcery
const struct ast_variable * fields
Pointer to the fields to check.
const struct ast_sorcery * sorcery
Pointer to the sorcery structure.
Structure used for fields comparison.

◆ 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.

References ao2_find, and OBJ_KEY.

178 {
179  return ao2_find(data, id, OBJ_KEY);
180 }
#define OBJ_KEY
Definition: astobj2.h:1155
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756

◆ 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.

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

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 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
static struct ast_sorcery * sorcery
const struct ast_variable * fields
Pointer to the fields to check.
const struct ast_sorcery * sorcery
Pointer to the sorcery structure.
Structure used for fields comparison.

◆ 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.

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

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 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
const size_t prefix_len
Prefix length in bytes for matching object id.
static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
static struct ast_sorcery * sorcery
const struct ast_sorcery * sorcery
Pointer to the sorcery structure.
Structure used for fields comparison.
static char prefix[MAX_PREFIX]
Definition: http.c:141

◆ 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.

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

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 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
#define ast_strlen_zero(foo)
Definition: strings.h:52
static int regex(struct ast_channel *chan, const char *cmd, char *parse, char *buf, size_t len)
Definition: func_strings.c:948
static int sorcery_memory_fields_cmp(void *obj, void *arg, int flags)
static struct ast_sorcery * sorcery
const struct ast_sorcery * sorcery
Pointer to the sorcery structure.
Structure used for fields comparison.

◆ 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.

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

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 }
#define OBJ_KEY
Definition: astobj2.h:1155
#define ao2_unlock(a)
Definition: astobj2.h:730
#define NULL
Definition: resample.c:96
#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:911
#define ao2_lock(a)
Definition: astobj2.h:718
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2312
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ao2_link(container, obj)
Definition: astobj2.h:1549

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 271 of file res_sorcery_memory.c.

References AST_MODFLAG_GLOBAL_SYMBOLS, AST_MODFLAG_LOAD_ORDER, AST_MODPRI_REALTIME_DRIVER, AST_MODULE_INFO(), AST_MODULE_SUPPORT_CORE, ast_sorcery_wizard_unregister(), ASTERISK_GPL_KEY, and load_module().

272 {
274  return 0;
275 }
int ast_sorcery_wizard_unregister(const struct ast_sorcery_wizard *interface)
Unregister a sorcery wizard.
Definition: sorcery.c:474
static struct ast_sorcery_wizard 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.