db_row.c

Go to the documentation of this file.
00001 /* 
00002  * $Id: db_row.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_row.c
00026  * \brief Type that represents a row in a database.
00027  *
00028  * This file holds a type that represents a row in a database, some convenience
00029  * macros and a function for memory managements.
00030  * \ingroup db
00031  */
00032 
00033 #include "db_row.h"
00034 
00035 #include <string.h>
00036 #include "../dprint.h"
00037 #include "../mem/mem.h"
00038 
00039 /*
00040  * Release memory used by row
00041  */
00042 inline int db_free_row(db_row_t* _r)
00043 {
00044    int col;
00045    db_val_t* _val;
00046 
00047    if (!_r) {
00048       LM_ERR("invalid parameter value\n");
00049       return -1;
00050    }
00051 
00052    /*
00053     * Loop thru each columm, then check to determine if the storage pointed to
00054     * by db_val_t structure must be freed. This is required for all data types
00055     * which use a pointer to a buffer like DB_STRING, DB_STR and DB_BLOB and
00056     * the database module copied them during the assignment.
00057     * If this is not done, a memory leak will happen.
00058     * Don't try to free the static dummy string (as indicated from the NULL value),
00059     * as this is not valid.
00060     */
00061    for (col = 0; col < ROW_N(_r); col++) {
00062       _val = &(ROW_VALUES(_r)[col]);
00063       switch (VAL_TYPE(_val)) {
00064          case DB_STRING:
00065             if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
00066                LM_DBG("free VAL_STRING[%d] '%s' at %p\n", col,
00067                      (char *)VAL_STRING(_val),
00068                      (char *)VAL_STRING(_val));
00069                pkg_free((char *)VAL_STRING(_val));
00070                VAL_STRING(_val) = NULL;
00071             }
00072             break;
00073          case DB_STR:
00074             if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
00075                LM_DBG("free VAL_STR[%d] '%.*s' at %p\n", col,
00076                      VAL_STR(_val).len,
00077                      VAL_STR(_val).s, VAL_STR(_val).s);
00078                pkg_free(VAL_STR(_val).s);
00079                VAL_STR(_val).s = NULL;
00080             }
00081             break;
00082          case DB_BLOB:
00083             if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
00084                LM_DBG("free VAL_BLOB[%d] at %p\n", col, VAL_BLOB(_val).s);
00085                pkg_free(VAL_BLOB(_val).s);
00086                VAL_BLOB(_val).s = NULL;
00087             }
00088             break;
00089          default:
00090             break;
00091       }
00092    }
00093    /* now as we freed all, set number of colums to zero again */
00094    ROW_N(_r) = 0;
00095 
00096    if (ROW_VALUES(_r)) {
00097       LM_DBG("freeing row values at %p\n", ROW_VALUES(_r));
00098       pkg_free(ROW_VALUES(_r));
00099       ROW_VALUES(_r) = NULL;
00100    }
00101    return 0;
00102 }
00103 
00104 
00105 /**
00106  * Allocate memory for row value.
00107  * \param _res result set
00108  * \param _row filled row
00109  * \return zero on success, negative on errors
00110  */
00111 inline int db_allocate_row(const db_res_t* _res, db_row_t* _row)
00112 {
00113    int len = sizeof(db_val_t) * RES_COL_N(_res);
00114    ROW_VALUES(_row) = (db_val_t*)pkg_malloc(len);
00115    if (!ROW_VALUES(_row)) {
00116       LM_ERR("no private memory left\n");
00117       return -1;
00118    }
00119    LM_DBG("allocate %d bytes for row values at %p\n", len, ROW_VALUES(_row));
00120 
00121    memset(ROW_VALUES(_row), 0, len);
00122    /* Save the number of columns in the ROW structure */
00123    ROW_N(_row) = RES_COL_N(_res);
00124 
00125    return 0;
00126 }

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