Wed Jan 8 2020 09:49:46

Asterisk developer's documentation


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 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
29 
30 #include "asterisk/_private.h"
31 
32 #include "asterisk/datastore.h"
33 #include "asterisk/utils.h"
34 
35 struct ast_datastore *__ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid,
36  const char *file, int line, const char *function)
37 {
38  struct ast_datastore *datastore = NULL;
39 
40  /* Make sure we at least have type so we can identify this */
41  if (!info) {
42  return NULL;
43  }
44 
45 #if defined(__AST_DEBUG_MALLOC)
46  if (!(datastore = __ast_calloc(1, sizeof(*datastore), file, line, function))) {
47  return NULL;
48  }
49 #else
50  if (!(datastore = ast_calloc(1, sizeof(*datastore)))) {
51  return NULL;
52  }
53 #endif
54 
55  datastore->info = info;
56 
57  if (!ast_strlen_zero(uid) && !(datastore->uid = ast_strdup(uid))) {
58  ast_free(datastore);
59  datastore = NULL;
60  }
61 
62  return datastore;
63 }
64 
65 int ast_datastore_free(struct ast_datastore *datastore)
66 {
67  int res = 0;
68 
69  /* Using the destroy function (if present) destroy the data */
70  if (datastore->info->destroy != NULL && datastore->data != NULL) {
71  datastore->info->destroy(datastore->data);
72  datastore->data = NULL;
73  }
74 
75  /* Free allocated UID memory */
76  if (datastore->uid != NULL) {
77  ast_free((void *) datastore->uid);
78  datastore->uid = NULL;
79  }
80 
81  /* Finally free memory used by ourselves */
82  ast_free(datastore);
83 
84  return res;
85 }
86 
87 /* DO NOT PUT ADDITIONAL FUNCTIONS BELOW THIS BOUNDARY
88  *
89  * ONLY FUNCTIONS FOR PROVIDING BACKWARDS ABI COMPATIBILITY BELONG HERE
90  *
91  */
92 
93 /* Provide binary compatibility for modules that call ast_datastore_alloc() directly;
94  * newly compiled modules will call __ast_datastore_alloc() via the macros in datastore.h
95  */
96 #undef ast_datastore_alloc
97 struct ast_datastore *ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid);
98 struct ast_datastore *ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
99 {
100  return __ast_datastore_alloc(info, uid, __FILE__, __LINE__, __FUNCTION__);
101 }
void(* destroy)(void *data)
Definition: datastore.h:34
Asterisk main include file. File version handling, generic pbx functions.
#define ast_strdup(a)
Definition: astmm.h:109
struct ast_datastore *attribute_malloc __ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid, const char *file, int line, const char *function)
Create a data store object.
Definition: datastore.c:35
struct ast_datastore_info * info
Definition: datastore.h:57
Structure for a data store type.
Definition: datastore.h:31
void * __ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
Structure for a data store object.
Definition: datastore.h:54
int ast_datastore_free(struct ast_datastore *datastore)
Free a data store object.
Definition: datastore.c:65
const char * uid
Definition: datastore.h:55
Utility functions.
Asterisk datastore objects.
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
struct ast_datastore * ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
Definition: datastore.c:98
#define ast_free(a)
Definition: astmm.h:97
void * data
Definition: datastore.h:56
#define ast_calloc(a, b)
Definition: astmm.h:82
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180