datastore.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. }
  69. /* DO NOT PUT ADDITIONAL FUNCTIONS BELOW THIS BOUNDARY
  70. *
  71. * ONLY FUNCTIONS FOR PROVIDING BACKWARDS ABI COMPATIBILITY BELONG HERE
  72. *
  73. */
  74. /* Provide binary compatibility for modules that call ast_datastore_alloc() directly;
  75. * newly compiled modules will call __ast_datastore_alloc() via the macros in datastore.h
  76. */
  77. #undef ast_datastore_alloc
  78. struct ast_datastore *ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid);
  79. struct ast_datastore *ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
  80. {
  81. return __ast_datastore_alloc(info, uid, __FILE__, __LINE__, __FUNCTION__);
  82. }