db_row.c 3.4 KB

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