Thu Apr 28 2011 16:56:46

Asterisk developer's documentation


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 #include "asterisk.h"
00023 
00024 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 273571 $")
00025 
00026 #include "asterisk/_private.h"
00027 
00028 #include "asterisk/datastore.h"
00029 #include "asterisk/utils.h"
00030 
00031 struct ast_datastore *__ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid,
00032                    const char *file, int line, const char *function)
00033 {
00034    struct ast_datastore *datastore = NULL;
00035 
00036    /* Make sure we at least have type so we can identify this */
00037    if (!info) {
00038       return NULL;
00039    }
00040 
00041 #if defined(__AST_DEBUG_MALLOC)
00042    if (!(datastore = __ast_calloc(1, sizeof(*datastore), file, line, function))) {
00043       return NULL;
00044    }
00045 #else
00046    if (!(datastore = ast_calloc(1, sizeof(*datastore)))) {
00047       return NULL;
00048    }
00049 #endif
00050 
00051    datastore->info = info;
00052 
00053    if (!ast_strlen_zero(uid) && !(datastore->uid = ast_strdup(uid))) {
00054       ast_free(datastore);
00055       datastore = NULL;
00056    }
00057 
00058    return datastore;
00059 }
00060 
00061 int ast_datastore_free(struct ast_datastore *datastore)
00062 {
00063    int res = 0;
00064 
00065    /* Using the destroy function (if present) destroy the data */
00066    if (datastore->info->destroy != NULL && datastore->data != NULL) {
00067       datastore->info->destroy(datastore->data);
00068       datastore->data = NULL;
00069    }
00070 
00071    /* Free allocated UID memory */
00072    if (datastore->uid != NULL) {
00073       ast_free((void *) datastore->uid);
00074       datastore->uid = NULL;
00075    }
00076 
00077    /* Finally free memory used by ourselves */
00078    ast_free(datastore);
00079 
00080    return res;
00081 }
00082 
00083 /* DO NOT PUT ADDITIONAL FUNCTIONS BELOW THIS BOUNDARY
00084  *
00085  * ONLY FUNCTIONS FOR PROVIDING BACKWARDS ABI COMPATIBILITY BELONG HERE
00086  *
00087  */
00088 
00089 /* Provide binary compatibility for modules that call ast_datastore_alloc() directly;
00090  * newly compiled modules will call __ast_datastore_alloc() via the macros in datastore.h
00091  */
00092 #undef ast_datastore_alloc
00093 struct ast_datastore *ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid);
00094 struct ast_datastore *ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
00095 {
00096    return __ast_datastore_alloc(info, uid, __FILE__, __LINE__, __FUNCTION__);
00097 }