Asterisk - The Open Source Telephony Project  21.4.1
Macros | Functions
datastore.c File Reference

Asterisk datastore objects. More...

#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/datastore.h"
#include "asterisk/utils.h"
#include "asterisk/astobj2.h"
#include "asterisk/uuid.h"
#include "asterisk/module.h"

Go to the source code of this file.

Macros

#define DATASTORE_BUCKETS   53
 Number of buckets for datastore container.
 

Functions

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. More...
 
 AO2_STRING_FIELD_CMP_FN (ast_datastore, uid)
 
 AO2_STRING_FIELD_HASH_FN (ast_datastore, uid)
 
int ast_datastore_free (struct ast_datastore *datastore)
 Free a data store object. More...
 
int ast_datastores_add (struct ao2_container *datastores, struct ast_datastore *datastore)
 Add a data store to a container. More...
 
struct ao2_containerast_datastores_alloc (void)
 Allocate a specialized data stores container. More...
 
struct ast_datastoreast_datastores_alloc_datastore (const struct ast_datastore_info *info, const char *uid)
 Allocate a datastore for use with the datastores container. More...
 
struct ast_datastoreast_datastores_find (struct ao2_container *datastores, const char *name)
 Find a data store in a container. More...
 
void ast_datastores_remove (struct ao2_container *datastores, const char *name)
 Remove a data store from a container. More...
 
static void datastore_destroy (void *obj)
 

Detailed Description

Asterisk datastore objects.

Definition in file datastore.c.

Function Documentation

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.

Parameters
[in]infoinformation describing the data store object
[in]uidunique identifer
[in]modThe module to hold until this datastore is freed.
file,line,function
Version
1.6.1 moved here and renamed from ast_channel_datastore_alloc

Definition at line 39 of file datastore.c.

References ast_module_ref, ast_strdup, ast_datastore::info, ast_datastore::mod, and ast_datastore::uid.

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 }
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
Structure for a data store object.
Definition: datastore.h:64
const char * uid
Definition: datastore.h:65
struct ast_module * mod
Definition: datastore.h:68
const struct ast_datastore_info * info
Definition: datastore.h:67
#define ast_module_ref(mod)
Hold a reference to the module.
Definition: module.h:457
int ast_datastore_free ( struct ast_datastore datastore)

Free a data store object.

Parameters
[in]datastoredatastore to free
Version
1.6.1 moved here and renamed from ast_channel_datastore_free

Definition at line 68 of file datastore.c.

References ast_module_unref, ast_datastore::data, ast_datastore_info::destroy, ast_datastore::info, ast_datastore::mod, and ast_datastore::uid.

Referenced by ast_bridge_discard_after_goto(), ast_bridge_setup_after_goto(), ast_cel_fabricate_channel_from_event(), ast_channel_destructor(), ast_channel_unsuppress(), ast_do_pickup(), ast_dummy_channel_destructor(), ast_iax2_new(), ast_jb_create_framehook(), ast_setup_cc_recall_datastore(), audiohook_volume_get(), authenticate_reply(), bridge_timeout(), fixup_callback(), lua_get_state(), raise_exception(), save_dialstring(), and setup_async_playback_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 }
const char * uid
Definition: datastore.h:65
struct ast_module * mod
Definition: datastore.h:68
const struct ast_datastore_info * info
Definition: datastore.h:67
void(* destroy)(void *data)
Definition: datastore.h:34
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
void * data
Definition: datastore.h:66
int ast_datastores_add ( struct ao2_container datastores,
struct ast_datastore datastore 
)

Add a data store to a container.

Parameters
[in]datastorescontainer to store datastore in
[in]datastoredatastore to add
Return values
0success
-1failure
Since
14.0.0

Definition at line 105 of file datastore.c.

References ao2_link, ast_datastore::info, and ast_datastore::uid.

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 }
const char * uid
Definition: datastore.h:65
const struct ast_datastore_info * info
Definition: datastore.h:67
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
struct ao2_container* ast_datastores_alloc ( void  )

Allocate a specialized data stores container.

Returns
a container for storing data stores
Since
14.0.0

Definition at line 99 of file datastore.c.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, and DATASTORE_BUCKETS.

100 {
102  DATASTORE_BUCKETS, ast_datastore_hash_fn, NULL, ast_datastore_cmp_fn);
103 }
#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 DATASTORE_BUCKETS
Number of buckets for datastore container.
Definition: datastore.c:37
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.

Parameters
[in]infoinformation about the datastore
[in]uidunique identifier for the datastore
Return values
non-NULLsuccess
NULLfailure
Since
14.0.0

Definition at line 142 of file datastore.c.

References ao2_ref, ast_strdup, ast_uuid_generate_str(), ast_datastore::info, and ast_datastore::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 }
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
Structure for a data store object.
Definition: datastore.h:64
const char * uid
Definition: datastore.h:65
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
const struct ast_datastore_info * info
Definition: datastore.h:67
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.
Definition: uuid.c:141
struct ast_datastore* ast_datastores_find ( struct ao2_container datastores,
const char *  name 
)

Find a data store in a container.

Parameters
[in]datastorescontainer to find datastore in
[in]namename of the data store to find
Return values
non-NULLsuccess
NULLfailure
Since
14.0.0

Definition at line 123 of file datastore.c.

References OBJ_SEARCH_KEY.

124 {
125  return ao2_find(datastores, name, OBJ_SEARCH_KEY);
126 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
void ast_datastores_remove ( struct ao2_container datastores,
const char *  name 
)

Remove a data store from a container.

Parameters
[in]datastorescontainer to remove datastore from
[in]namename of the data store to remove
Since
14.0.0

Definition at line 118 of file datastore.c.

References OBJ_NODATA, OBJ_SEARCH_KEY, and OBJ_UNLINK.

119 {
120  ao2_find(datastores, name, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
121 }
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101