datastore.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /*! \file
  17. *
  18. * \brief Asterisk datastore objects
  19. */
  20. /*** MODULEINFO
  21. <support_level>core</support_level>
  22. ***/
  23. #include "asterisk.h"
  24. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  25. #include "asterisk/_private.h"
  26. #include "asterisk/datastore.h"
  27. #include "asterisk/utils.h"
  28. struct ast_datastore *__ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid,
  29. const char *file, int line, const char *function)
  30. {
  31. struct ast_datastore *datastore = NULL;
  32. /* Make sure we at least have type so we can identify this */
  33. if (!info) {
  34. return NULL;
  35. }
  36. #if defined(__AST_DEBUG_MALLOC)
  37. if (!(datastore = __ast_calloc(1, sizeof(*datastore), file, line, function))) {
  38. return NULL;
  39. }
  40. #else
  41. if (!(datastore = ast_calloc(1, sizeof(*datastore)))) {
  42. return NULL;
  43. }
  44. #endif
  45. datastore->info = info;
  46. if (!ast_strlen_zero(uid) && !(datastore->uid = ast_strdup(uid))) {
  47. ast_free(datastore);
  48. datastore = NULL;
  49. }
  50. return datastore;
  51. }
  52. int ast_datastore_free(struct ast_datastore *datastore)
  53. {
  54. int res = 0;
  55. /* Using the destroy function (if present) destroy the data */
  56. if (datastore->info->destroy != NULL && datastore->data != NULL) {
  57. datastore->info->destroy(datastore->data);
  58. datastore->data = NULL;
  59. }
  60. /* Free allocated UID memory */
  61. if (datastore->uid != NULL) {
  62. ast_free((void *) datastore->uid);
  63. datastore->uid = NULL;
  64. }
  65. /* Finally free memory used by ourselves */
  66. ast_free(datastore);
  67. return res;
  68. }