datastore.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #include "asterisk.h"
  21. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  22. #include "asterisk/_private.h"
  23. #include "asterisk/datastore.h"
  24. #include "asterisk/utils.h"
  25. struct ast_datastore *__ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid,
  26. const char *file, int line, const char *function)
  27. {
  28. struct ast_datastore *datastore = NULL;
  29. /* Make sure we at least have type so we can identify this */
  30. if (!info) {
  31. return NULL;
  32. }
  33. #if defined(__AST_DEBUG_MALLOC)
  34. if (!(datastore = __ast_calloc(1, sizeof(*datastore), file, line, function))) {
  35. return NULL;
  36. }
  37. #else
  38. if (!(datastore = ast_calloc(1, sizeof(*datastore)))) {
  39. return NULL;
  40. }
  41. #endif
  42. datastore->info = info;
  43. datastore->uid = ast_strdup(uid);
  44. return datastore;
  45. }
  46. int ast_datastore_free(struct ast_datastore *datastore)
  47. {
  48. int res = 0;
  49. /* Using the destroy function (if present) destroy the data */
  50. if (datastore->info->destroy != NULL && datastore->data != NULL) {
  51. datastore->info->destroy(datastore->data);
  52. datastore->data = NULL;
  53. }
  54. /* Free allocated UID memory */
  55. if (datastore->uid != NULL) {
  56. ast_free((void *) datastore->uid);
  57. datastore->uid = NULL;
  58. }
  59. /* Finally free memory used by ourselves */
  60. ast_free(datastore);
  61. return res;
  62. }
  63. /* DO NOT PUT ADDITIONAL FUNCTIONS BELOW THIS BOUNDARY
  64. *
  65. * ONLY FUNCTIONS FOR PROVIDING BACKWARDS ABI COMPATIBILITY BELONG HERE
  66. *
  67. */
  68. /* Provide binary compatibility for modules that call ast_datastore_alloc() directly;
  69. * newly compiled modules will call __ast_datastore_alloc() via the macros in datastore.h
  70. */
  71. #undef ast_datastore_alloc
  72. struct ast_datastore *ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid);
  73. struct ast_datastore *ast_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
  74. {
  75. return __ast_datastore_alloc(info, uid, __FILE__, __LINE__, __FUNCTION__);
  76. }