aboutsummaryrefslogtreecommitdiffstats
path: root/res/res_config_pgsql.c
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-04-10 02:15:47 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-04-10 02:15:47 +0000
commitb9253231be8c86053152d440d24df247cf07e739 (patch)
tree3e31456cd4a8f7980e2a339c9973702ffeea2841 /res/res_config_pgsql.c
parenta06d9b22bc8d407780bd904e4081339b16c2cbc9 (diff)
use ast_*alloc and don't create duplicated error messages.
... as stated in the coding guidelines. git-svn-id: http://svn.digium.com/svn/asterisk/trunk@18722 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'res/res_config_pgsql.c')
-rw-r--r--res/res_config_pgsql.c68
1 files changed, 25 insertions, 43 deletions
diff --git a/res/res_config_pgsql.c b/res/res_config_pgsql.c
index d7a20e8bd..85e894532 100644
--- a/res/res_config_pgsql.c
+++ b/res/res_config_pgsql.c
@@ -161,10 +161,7 @@ static struct ast_variable *realtime_pgsql(const char *database, const char *tab
ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
- fieldnames = malloc(numFields * sizeof(char *));
- if (!fieldnames) {
- /* If I can't alloc memory at this point, why bother doing anything else? */
- ast_log(LOG_WARNING, "Out of memory!\n");
+ if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
ast_mutex_unlock(&pgsql_lock);
PQclear(result);
return NULL;
@@ -223,12 +220,8 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
memset(&ra, 0, sizeof(ra));
- cfg = ast_config_new();
- if (!cfg) {
- /* If I can't alloc memory at this point, why bother doing anything else? */
- ast_log(LOG_WARNING, "Out of memory!\n");
+ if (!(cfg = ast_config_new()))
return NULL;
- }
/* Get the first parameter and first value in our list of passed paramater/value pairs */
newparam = va_arg(ap, const char *);
@@ -314,10 +307,7 @@ static struct ast_config *realtime_multi_pgsql(const char *database, const char
ast_log(LOG_DEBUG, "Postgresql RealTime: Found %d rows.\n", num_rows);
- fieldnames = malloc(numFields * sizeof(char *));
- if (!fieldnames) {
- /* If I can't alloc memory at this point, why bother doing anything else? */
- ast_log(LOG_WARNING, "Out of memory!\n");
+ if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
ast_mutex_unlock(&pgsql_lock);
PQclear(result);
return NULL;
@@ -510,10 +500,7 @@ static struct ast_config *config_pgsql(const char *database, const char *table,
ast_log(LOG_DEBUG, "Postgresql RealTime: Found %ld rows.\n", num_rows);
- fieldnames = malloc(numFields * sizeof(char *));
- if (!fieldnames) {
- /* If I can't alloc memory at this point, why bother doing anything else? */
- ast_log(LOG_WARNING, "Out of memory!\n");
+ if (!(fieldnames = ast_calloc(1, numFields * sizeof(char *)))) {
ast_mutex_unlock(&pgsql_lock);
PQclear(result);
return NULL;
@@ -751,36 +738,31 @@ static int pgsql_reconnect(const char *database)
+ strlen(dbuser)
+ strlen(dbpass)
+ strlen(my_database);
- connInfo = malloc(size);
- if (!connInfo) {
- ast_log(LOG_WARNING,
- "Postgresql RealTime: Insufficient memory to allocate Pgsql resource.\n");
+
+ if (!(connInfo = ast_malloc(size)))
return 0;
- } else {
- sprintf(connInfo, "host=%s port=%d dbname=%s user=%s password=%s",
+
+ sprintf(connInfo, "host=%s port=%d dbname=%s user=%s password=%s",
dbhost, dbport, my_database, dbuser, dbpass);
- ast_log(LOG_DEBUG, "%u connInfo=%s\n", size, connInfo);
- pgsqlConn = PQconnectdb(connInfo);
- ast_log(LOG_DEBUG, "%u connInfo=%s\n", size, connInfo);
- free(connInfo);
- connInfo = NULL;
- ast_log(LOG_DEBUG, "pgsqlConn=%p\n", pgsqlConn);
- if (pgsqlConn) {
- ast_log(LOG_DEBUG,
- "Postgresql RealTime: Successfully connected to database.\n");
- connect_time = time(NULL);
- return 1;
- } else {
- ast_log(LOG_ERROR,
- "Postgresql RealTime: Failed to connect database server %s on %s. Check debug for more info.\n",
- dbname, dbhost);
- ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n",
- PQresultErrorMessage(NULL));
- return 0;
- }
+ ast_log(LOG_DEBUG, "%u connInfo=%s\n", size, connInfo);
+ pgsqlConn = PQconnectdb(connInfo);
+ ast_log(LOG_DEBUG, "%u connInfo=%s\n", size, connInfo);
+ free(connInfo);
+ connInfo = NULL;
+ ast_log(LOG_DEBUG, "pgsqlConn=%p\n", pgsqlConn);
+ if (pgsqlConn) {
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Successfully connected to database.\n");
+ connect_time = time(NULL);
+ return 1;
+ } else {
+ ast_log(LOG_ERROR,
+ "Postgresql RealTime: Failed to connect database server %s on %s. Check debug for more info.\n",
+ dbname, dbhost);
+ ast_log(LOG_DEBUG, "Postgresql RealTime: Cannot Connect: %s\n",
+ PQresultErrorMessage(NULL));
+ return 0;
}
} else {
-
ast_log(LOG_DEBUG, "Postgresql RealTime: Everything is fine.\n");
return 1;
}