db_row.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2001-2003 FhG Fokus
  3. * Copyright (C) 2007-2008 1&1 Internet AG
  4. *
  5. * This file is part of Kamailio, a free SIP server.
  6. *
  7. * Kamailio is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version
  11. *
  12. * Kamailio is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * \file lib/srdb1/db_row.c
  23. * \brief Type that represents a row in a database.
  24. *
  25. * This file holds a type that represents a row in a database, some convenience
  26. * macros and a function for memory managements.
  27. * \ingroup db1
  28. */
  29. #include "db_row.h"
  30. #include <string.h>
  31. #include "../../core/dprint.h"
  32. #include "../../core/mem/mem.h"
  33. /*
  34. * Release memory used by row
  35. */
  36. int db_free_row(db_row_t* _r)
  37. {
  38. int col;
  39. db_val_t* _val;
  40. if (!_r) {
  41. LM_ERR("invalid parameter value\n");
  42. return -1;
  43. }
  44. /*
  45. * Loop thru each column, then check to determine if the storage pointed to
  46. * by db_val_t structure must be freed. This is required for all data types
  47. * which use a pointer to a buffer like DB1_STRING, DB1_STR and DB1_BLOB and
  48. * the database module copied them during the assignment.
  49. * If this is not done, a memory leak will happen.
  50. * Don't try to free the static dummy string (as indicated from the NULL value),
  51. * as this is not valid.
  52. */
  53. for (col = 0; col < ROW_N(_r); col++) {
  54. _val = &(ROW_VALUES(_r)[col]);
  55. switch (VAL_TYPE(_val)) {
  56. case DB1_STRING:
  57. if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
  58. LM_DBG("free VAL_STRING[%d] '%s' at %p\n", col,
  59. (char *)VAL_STRING(_val),
  60. (char *)VAL_STRING(_val));
  61. pkg_free((char *)VAL_STRING(_val));
  62. VAL_STRING(_val) = NULL;
  63. }
  64. break;
  65. case DB1_STR:
  66. if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
  67. LM_DBG("free VAL_STR[%d] '%.*s' at %p\n", col,
  68. VAL_STR(_val).len,
  69. VAL_STR(_val).s, VAL_STR(_val).s);
  70. pkg_free(VAL_STR(_val).s);
  71. VAL_STR(_val).s = NULL;
  72. }
  73. break;
  74. case DB1_BLOB:
  75. if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
  76. LM_DBG("free VAL_BLOB[%d] at %p\n", col, VAL_BLOB(_val).s);
  77. pkg_free(VAL_BLOB(_val).s);
  78. VAL_BLOB(_val).s = NULL;
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. }
  85. /* now as we freed all, set number of columns to zero again */
  86. ROW_N(_r) = 0;
  87. if (ROW_VALUES(_r)) {
  88. LM_DBG("freeing row values at %p\n", ROW_VALUES(_r));
  89. pkg_free(ROW_VALUES(_r));
  90. ROW_VALUES(_r) = NULL;
  91. }
  92. return 0;
  93. }
  94. /**
  95. * Allocate memory for row value.
  96. * \param _res result set
  97. * \param _row filled row
  98. * \return zero on success, negative on errors
  99. */
  100. int db_allocate_row(const db1_res_t* _res, db_row_t* _row)
  101. {
  102. int len = sizeof(db_val_t) * RES_COL_N(_res);
  103. ROW_VALUES(_row) = (db_val_t*)pkg_malloc(len);
  104. if (!ROW_VALUES(_row)) {
  105. PKG_MEM_ERROR;
  106. return -1;
  107. }
  108. LM_DBG("allocate %d bytes for row values at %p\n", len, ROW_VALUES(_row));
  109. memset(ROW_VALUES(_row), 0, len);
  110. /* Save the number of columns in the ROW structure */
  111. ROW_N(_row) = RES_COL_N(_res);
  112. return 0;
  113. }