db_res.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_res.c
  25. * \brief Functions to manage result structures
  26. * \ingroup db1
  27. *
  28. * Provides some convenience macros and some memory management
  29. * functions for result structures.
  30. */
  31. #include "db_res.h"
  32. #include "db_row.h"
  33. #include "../../dprint.h"
  34. #include "../../mem/mem.h"
  35. #include <string.h>
  36. /*
  37. * Release memory used by rows
  38. */
  39. int db_free_rows(db1_res_t* _r)
  40. {
  41. int i;
  42. if (!_r) {
  43. LM_ERR("invalid parameter value\n");
  44. return -1;
  45. }
  46. if(RES_ROWS(_r)){
  47. LM_DBG("freeing %d rows\n", RES_ROW_N(_r));
  48. for(i = 0; i < RES_ROW_N(_r); i++) {
  49. db_free_row(&(RES_ROWS(_r)[i]));
  50. }
  51. }
  52. RES_ROW_N(_r) = 0;
  53. if (RES_ROWS(_r)) {
  54. LM_DBG("freeing rows at %p\n", RES_ROWS(_r));
  55. pkg_free(RES_ROWS(_r));
  56. RES_ROWS(_r) = NULL;
  57. }
  58. return 0;
  59. }
  60. /*
  61. * Release memory used by columns
  62. */
  63. int db_free_columns(db1_res_t* _r)
  64. {
  65. int col;
  66. if (!_r) {
  67. LM_ERR("invalid parameter value\n");
  68. return -1;
  69. }
  70. LM_DBG("freeing %d columns\n", RES_COL_N(_r));
  71. /* free memory previously allocated to save column names */
  72. for(col = 0; col < RES_COL_N(_r); col++) {
  73. if (RES_NAMES(_r)[col]!=NULL) {
  74. LM_DBG("freeing RES_NAMES[%d] at %p\n", col, RES_NAMES(_r)[col]);
  75. pkg_free((str *)RES_NAMES(_r)[col]);
  76. RES_NAMES(_r)[col] = NULL;
  77. }
  78. }
  79. RES_COL_N(_r) = 0;
  80. /* free names and types */
  81. if (RES_NAMES(_r)) {
  82. LM_DBG("freeing result names at %p\n", RES_NAMES(_r));
  83. pkg_free(RES_NAMES(_r));
  84. RES_NAMES(_r) = NULL;
  85. }
  86. if (RES_TYPES(_r)) {
  87. LM_DBG("freeing result types at %p\n", RES_TYPES(_r));
  88. pkg_free(RES_TYPES(_r));
  89. RES_TYPES(_r) = NULL;
  90. }
  91. return 0;
  92. }
  93. /*
  94. * Create a new result structure and initialize it
  95. */
  96. db1_res_t* db_new_result(void)
  97. {
  98. db1_res_t* r = NULL;
  99. r = (db1_res_t*)pkg_malloc(sizeof(db1_res_t));
  100. if (!r) {
  101. LM_ERR("no private memory left\n");
  102. return 0;
  103. }
  104. LM_DBG("allocate %d bytes for result set at %p\n",
  105. (int)sizeof(db1_res_t), r);
  106. memset(r, 0, sizeof(db1_res_t));
  107. return r;
  108. }
  109. /*
  110. * Release memory used by a result structure
  111. */
  112. int db_free_result(db1_res_t* _r)
  113. {
  114. if (!_r)
  115. {
  116. LM_ERR("invalid parameter\n");
  117. return -1;
  118. }
  119. db_free_columns(_r);
  120. db_free_rows(_r);
  121. LM_DBG("freeing result set at %p\n", _r);
  122. pkg_free(_r);
  123. _r = NULL;
  124. return 0;
  125. }
  126. /*
  127. * Allocate storage for column names and type in existing
  128. * result structure.
  129. */
  130. int db_allocate_columns(db1_res_t* _r, const unsigned int cols)
  131. {
  132. RES_NAMES(_r) = (db_key_t*)pkg_malloc(sizeof(db_key_t) * cols);
  133. if (!RES_NAMES(_r)) {
  134. LM_ERR("no private memory left\n");
  135. return -1;
  136. }
  137. LM_DBG("allocate %d bytes for result names at %p\n",
  138. (int)(sizeof(db_key_t) * cols),
  139. RES_NAMES(_r));
  140. RES_TYPES(_r) = (db_type_t*)pkg_malloc(sizeof(db_type_t) * cols);
  141. if (!RES_TYPES(_r)) {
  142. LM_ERR("no private memory left\n");
  143. pkg_free(RES_NAMES(_r));
  144. return -1;
  145. }
  146. LM_DBG("allocate %d bytes for result types at %p\n",
  147. (int)(sizeof(db_type_t) * cols),
  148. RES_TYPES(_r));
  149. return 0;
  150. }
  151. /**
  152. * Allocate memory for rows.
  153. * \param _res result set
  154. * \return zero on success, negative on errors
  155. */
  156. int db_allocate_rows(db1_res_t* _res)
  157. {
  158. int len = sizeof(db_row_t) * RES_ROW_N(_res);
  159. RES_ROWS(_res) = (struct db_row*)pkg_malloc(len);
  160. if (!RES_ROWS(_res)) {
  161. LM_ERR("no private memory left\n");
  162. return -1;
  163. }
  164. LM_DBG("allocate %d bytes for rows at %p\n", len, RES_ROWS(_res));
  165. memset(RES_ROWS(_res), 0, len);
  166. return 0;
  167. }
  168. /**
  169. * Reallocate memory for rows.
  170. * \param _res result set
  171. * \param _nsize new number of rows in result set
  172. * \return zero on success, negative on errors
  173. */
  174. int db_reallocate_rows(db1_res_t* _res, int _nsize)
  175. {
  176. int len;
  177. int osize;
  178. db_row_t *orows;
  179. orows = RES_ROWS(_res);
  180. osize = RES_ROW_N(_res);
  181. RES_ROW_N(_res) = _nsize;
  182. len = sizeof(db_row_t) * RES_ROW_N(_res);
  183. RES_ROWS(_res) = (struct db_row*)pkg_malloc(len);
  184. if (!RES_ROWS(_res)) {
  185. LM_ERR("no private memory left\n");
  186. return -1;
  187. }
  188. LM_DBG("allocate %d bytes for rows at %p\n", len, RES_ROWS(_res));
  189. memset(RES_ROWS(_res), 0, len);
  190. if(orows==NULL)
  191. return 0;
  192. memcpy(RES_ROWS(_res), orows,
  193. ((osize<_nsize)?osize:_nsize)*sizeof(db_row_t));
  194. pkg_free(orows);
  195. return 0;
  196. }