db_res.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: db_res.c 5418 2009-01-05 15:42:12Z henningw $ 
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 
00024 /**
00025  * \file db/db_res.c
00026  * \brief Functions to manage result structures
00027  * \ingroup db
00028  *
00029  * Provides some convenience macros and some memory management
00030  * functions for result structures.
00031  */
00032 
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(db_res_t* _r)
00045 {
00046    int i;
00047 
00048    if (!_r) {
00049       LM_ERR("invalid parameter value\n");
00050       return -1;
00051    }
00052    LM_DBG("freeing %d rows\n", RES_ROW_N(_r));
00053 
00054    for(i = 0; i < RES_ROW_N(_r); i++) {
00055       db_free_row(&(RES_ROWS(_r)[i]));
00056    }
00057    RES_ROW_N(_r) = 0;
00058 
00059    if (RES_ROWS(_r)) {
00060       LM_DBG("freeing rows at %p\n", RES_ROWS(_r));
00061       pkg_free(RES_ROWS(_r));
00062       RES_ROWS(_r) = NULL;
00063    }
00064    return 0;
00065 }
00066 
00067 
00068 /*
00069  * Release memory used by columns
00070  */
00071 inline int db_free_columns(db_res_t* _r)
00072 {
00073    int col;
00074 
00075    if (!_r) {
00076       LM_ERR("invalid parameter value\n");
00077       return -1;
00078    }
00079    LM_DBG("freeing %d columns\n", RES_COL_N(_r));
00080    /* free memory previously allocated to save column names */
00081    for(col = 0; col < RES_COL_N(_r); col++) {
00082       if (RES_NAMES(_r)[col]!=NULL) {
00083          LM_DBG("freeing RES_NAMES[%d] at %p\n", col, RES_NAMES(_r)[col]);
00084          pkg_free((str *)RES_NAMES(_r)[col]);
00085          RES_NAMES(_r)[col] = NULL;
00086       }
00087    }
00088    RES_COL_N(_r) = 0;
00089 
00090    /* free names and types */
00091    if (RES_NAMES(_r)) {
00092       LM_DBG("freeing result names at %p\n", RES_NAMES(_r));
00093       pkg_free(RES_NAMES(_r));
00094       RES_NAMES(_r) = NULL;
00095    }
00096    if (RES_TYPES(_r)) {
00097       LM_DBG("freeing result types at %p\n", RES_TYPES(_r));
00098       pkg_free(RES_TYPES(_r));
00099       RES_TYPES(_r) = NULL;
00100    }
00101    return 0;
00102 }
00103 
00104 /*
00105  * Create a new result structure and initialize it
00106  */
00107 inline db_res_t* db_new_result(void)
00108 {
00109    db_res_t* r = NULL;
00110    r = (db_res_t*)pkg_malloc(sizeof(db_res_t));
00111    if (!r) {
00112       LM_ERR("no private memory left\n");
00113       return 0;
00114    }
00115    LM_DBG("allocate %d bytes for result set at %p\n",
00116       (int)sizeof(db_res_t), r);
00117    memset(r, 0, sizeof(db_res_t));
00118    return r;
00119 }
00120 
00121 /*
00122  * Release memory used by a result structure
00123  */
00124 inline int db_free_result(db_res_t* _r)
00125 {
00126    if (!_r)
00127    {
00128       LM_ERR("invalid parameter\n");
00129       return -1;
00130    }
00131 
00132    db_free_columns(_r);
00133    db_free_rows(_r);
00134    LM_DBG("freeing result set at %p\n", _r);
00135    pkg_free(_r);
00136    _r = NULL;
00137    return 0;
00138 }
00139 
00140 /*
00141  * Allocate storage for column names and type in existing
00142  * result structure.
00143  */
00144 inline int db_allocate_columns(db_res_t* _r, const unsigned int cols)
00145 {
00146    RES_NAMES(_r) = (db_key_t*)pkg_malloc(sizeof(db_key_t) * cols);
00147    if (!RES_NAMES(_r)) {
00148       LM_ERR("no private memory left\n");
00149       return -1;
00150    }
00151    LM_DBG("allocate %d bytes for result names at %p\n",
00152       (int)(sizeof(db_key_t) * cols),
00153       RES_NAMES(_r));
00154 
00155    RES_TYPES(_r) = (db_type_t*)pkg_malloc(sizeof(db_type_t) * cols);
00156    if (!RES_TYPES(_r)) {
00157       LM_ERR("no private memory left\n");
00158       pkg_free(RES_NAMES(_r));
00159       return -1;
00160    }
00161    LM_DBG("allocate %d bytes for result types at %p\n",
00162       (int)(sizeof(db_type_t) * cols),
00163       RES_TYPES(_r));
00164 
00165    return 0;
00166 }
00167 
00168 
00169 /**
00170  * Allocate memory for rows.
00171  * \param _res result set
00172  * \return zero on success, negative on errors
00173  */
00174 inline int db_allocate_rows(db_res_t* _res)
00175 {
00176    int len = sizeof(db_row_t) * RES_ROW_N(_res);
00177    RES_ROWS(_res) = (struct db_row*)pkg_malloc(len);
00178    if (!RES_ROWS(_res)) {
00179       LM_ERR("no private memory left\n");
00180       return -1;
00181    }
00182    LM_DBG("allocate %d bytes for rows at %p\n", len, RES_ROWS(_res));
00183    memset(RES_ROWS(_res), 0, len);
00184    
00185    return 0;
00186 }

Generated on Tue May 22 14:00:25 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6