forked from OpenSIPS/opensips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_row.c
96 lines (88 loc) · 2.71 KB
/
db_row.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (C) 2001-2003 FhG Fokus
* Copyright (C) 2007-2008 1&1 Internet AG
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* \file db/db_row.c
* \brief Type that represents a row in a database.
*
* This file holds a type that represents a row in a database, some convenience
* macros and a function for memory managements.
*/
#include "db_row.h"
#include <string.h>
#include "../dprint.h"
#include "../mem/mem.h"
/*
* Release memory used by row
*/
int db_free_row(db_row_t* _r)
{
int col;
db_val_t* _val;
if (!_r) {
LM_ERR("invalid parameter value\n");
return -1;
}
LM_DBG("freeing row values at %p\n", ROW_VALUES(_r));
/*
* Loop thru each columm, then check to determine if the storage pointed to
* by db_val_t structure must be freed. This is required for all data types
* which use a pointer to a buffer like DB_STRING, DB_STR and DB_BLOB and
* the database module copied them during the assignment.
* If this is not done, a memory leak will happen.
* Don't try to free the static dummy string (as indicated from the NULL
* value), as this is not valid.
*/
for (col = 0; col < ROW_N(_r); col++) {
_val = &(ROW_VALUES(_r)[col]);
switch (VAL_TYPE(_val)) {
case DB_STRING:
if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
LM_DBG("free VAL_STRING[%d] '%s' at %p\n", col,
(char *)VAL_STRING(_val),
(char *)VAL_STRING(_val));
pkg_free((char *)VAL_STRING(_val));
VAL_STRING(_val) = NULL;
}
break;
case DB_STR:
if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
LM_DBG("free VAL_STR[%d] '%.*s' at %p\n", col,
VAL_STR(_val).len,
VAL_STR(_val).s, VAL_STR(_val).s);
pkg_free(VAL_STR(_val).s);
VAL_STR(_val).s = NULL;
}
break;
case DB_BLOB:
if ( (!VAL_NULL(_val)) && VAL_FREE(_val)) {
LM_DBG("free VAL_BLOB[%d] at %p\n", col, VAL_BLOB(_val).s);
pkg_free(VAL_BLOB(_val).s);
VAL_BLOB(_val).s = NULL;
}
break;
default:
break;
}
}
pkg_free(ROW_VALUES(_r));
ROW_VALUES(_r) = NULL;
return 0;
}