datastore.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007 - 2008, Digium, Inc.
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*! \file
00018  *
00019  * \brief Asterisk datastore objects
00020  */
00021 
00022 /*** MODULEINFO
00023    <support_level>core</support_level>
00024  ***/
00025 
00026 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 393239 $")
00029 
00030 #include "asterisk/_private.h"
00031 
00032 #include "asterisk/datastore.h"
00033 #include "asterisk/utils.h"
00034 
00035 struct ast_datastore *__ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid,
00036                    const char *file, int line, const char *function)
00037 {
00038    struct ast_datastore *datastore = NULL;
00039 
00040    /* Make sure we at least have type so we can identify this */
00041    if (!info) {
00042       return NULL;
00043    }
00044 
00045 #if defined(__AST_DEBUG_MALLOC)
00046    if (!(datastore = __ast_calloc(1, sizeof(*datastore), file, line, function))) {
00047       return NULL;
00048    }
00049 #else
00050    if (!(datastore = ast_calloc(1, sizeof(*datastore)))) {
00051       return NULL;
00052    }
00053 #endif
00054 
00055    datastore->info = info;
00056 
00057    if (!ast_strlen_zero(uid) && !(datastore->uid = ast_strdup(uid))) {
00058       ast_free(datastore);
00059       datastore = NULL;
00060    }
00061 
00062    return datastore;
00063 }
00064 
00065 int ast_datastore_free(struct ast_datastore *datastore)
00066 {
00067    int res = 0;
00068 
00069    /* Using the destroy function (if present) destroy the data */
00070    if (datastore->info->destroy != NULL && datastore->data != NULL) {
00071       datastore->info->destroy(datastore->data);
00072       datastore->data = NULL;
00073    }
00074 
00075    /* Free allocated UID memory */
00076    if (datastore->uid != NULL) {
00077       ast_free((void *) datastore->uid);
00078       datastore->uid = NULL;
00079    }
00080 
00081    /* Finally free memory used by ourselves */
00082    ast_free(datastore);
00083 
00084    return res;
00085 }

Generated on Thu Apr 16 06:27:32 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6