srdb1/db_res.c

Go to the documentation of this file.
00001 /* 
00002  * $Id$ 
00003  *
00004  * Copyright (C) 2001-2003 FhG Fokus
00005  * Copyright (C) 2007-2008 1&1 Internet AG
00006  *
00007  * This file is part of Kamailio, a free SIP server.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License 
00020  * along with this program; if not, write to the Free Software 
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00033 #include "db_res.h"
00034 
00035 #include "db_row.h"
00036 #include "../../dprint.h"
00037 #include "../../mem/mem.h"
00038 
00039 #include <string.h>
00040 
00041 /*
00042  * Release memory used by rows
00043  */
00044 inline int db_free_rows(db1_res_t* _r)
00045 {
00046         int i;
00047 
00048         if (!_r) {
00049                 LM_ERR("invalid parameter value\n");
00050                 return -1;
00051         }
00052 
00053         if(RES_ROWS(_r)){
00054                 LM_DBG("freeing %d rows\n", RES_ROW_N(_r));
00055                 for(i = 0; i < RES_ROW_N(_r); i++) {
00056                         db_free_row(&(RES_ROWS(_r)[i]));
00057                 }
00058         }
00059         RES_ROW_N(_r) = 0;
00060 
00061         if (RES_ROWS(_r)) {
00062                 LM_DBG("freeing rows at %p\n", RES_ROWS(_r));
00063                 pkg_free(RES_ROWS(_r));
00064                 RES_ROWS(_r) = NULL;
00065         }
00066         return 0;
00067 }
00068 
00069 
00070 /*
00071  * Release memory used by columns
00072  */
00073 inline int db_free_columns(db1_res_t* _r)
00074 {
00075         int col;
00076 
00077         if (!_r) {
00078                 LM_ERR("invalid parameter value\n");
00079                 return -1;
00080         }
00081         LM_DBG("freeing %d columns\n", RES_COL_N(_r));
00082         /* free memory previously allocated to save column names */
00083         for(col = 0; col < RES_COL_N(_r); col++) {
00084                 if (RES_NAMES(_r)[col]!=NULL) {
00085                         LM_DBG("freeing RES_NAMES[%d] at %p\n", col, RES_NAMES(_r)[col]);
00086                         pkg_free((str *)RES_NAMES(_r)[col]);
00087                         RES_NAMES(_r)[col] = NULL;
00088                 }
00089         }
00090         RES_COL_N(_r) = 0;
00091 
00092         /* free names and types */
00093         if (RES_NAMES(_r)) {
00094                 LM_DBG("freeing result names at %p\n", RES_NAMES(_r));
00095                 pkg_free(RES_NAMES(_r));
00096                 RES_NAMES(_r) = NULL;
00097         }
00098         if (RES_TYPES(_r)) {
00099                 LM_DBG("freeing result types at %p\n", RES_TYPES(_r));
00100                 pkg_free(RES_TYPES(_r));
00101                 RES_TYPES(_r) = NULL;
00102         }
00103         return 0;
00104 }
00105 
00106 /*
00107  * Create a new result structure and initialize it
00108  */
00109 inline db1_res_t* db_new_result(void)
00110 {
00111         db1_res_t* r = NULL;
00112         r = (db1_res_t*)pkg_malloc(sizeof(db1_res_t));
00113         if (!r) {
00114                 LM_ERR("no private memory left\n");
00115                 return 0;
00116         }
00117         LM_DBG("allocate %d bytes for result set at %p\n",
00118                 (int)sizeof(db1_res_t), r);
00119         memset(r, 0, sizeof(db1_res_t));
00120         return r;
00121 }
00122 
00123 /*
00124  * Release memory used by a result structure
00125  */
00126 inline int db_free_result(db1_res_t* _r)
00127 {
00128         if (!_r)
00129         {
00130                 LM_ERR("invalid parameter\n");
00131                 return -1;
00132         }
00133 
00134         db_free_columns(_r);
00135         db_free_rows(_r);
00136         LM_DBG("freeing result set at %p\n", _r);
00137         pkg_free(_r);
00138         _r = NULL;
00139         return 0;
00140 }
00141 
00142 /*
00143  * Allocate storage for column names and type in existing
00144  * result structure.
00145  */
00146 inline int db_allocate_columns(db1_res_t* _r, const unsigned int cols)
00147 {
00148         RES_NAMES(_r) = (db_key_t*)pkg_malloc(sizeof(db_key_t) * cols);
00149         if (!RES_NAMES(_r)) {
00150                 LM_ERR("no private memory left\n");
00151                 return -1;
00152         }
00153         LM_DBG("allocate %d bytes for result names at %p\n",
00154                 (int)(sizeof(db_key_t) * cols),
00155                 RES_NAMES(_r));
00156 
00157         RES_TYPES(_r) = (db_type_t*)pkg_malloc(sizeof(db_type_t) * cols);
00158         if (!RES_TYPES(_r)) {
00159                 LM_ERR("no private memory left\n");
00160                 pkg_free(RES_NAMES(_r));
00161                 return -1;
00162         }
00163         LM_DBG("allocate %d bytes for result types at %p\n",
00164                 (int)(sizeof(db_type_t) * cols),
00165                 RES_TYPES(_r));
00166 
00167         return 0;
00168 }
00169 
00170 
00176 inline int db_allocate_rows(db1_res_t* _res)
00177 {
00178         int len = sizeof(db_row_t) * RES_ROW_N(_res);
00179         RES_ROWS(_res) = (struct db_row*)pkg_malloc(len);
00180         if (!RES_ROWS(_res)) {
00181                 LM_ERR("no private memory left\n");
00182                 return -1;
00183         }
00184         LM_DBG("allocate %d bytes for rows at %p\n", len, RES_ROWS(_res));
00185         memset(RES_ROWS(_res), 0, len);
00186         
00187         return 0;
00188 }