db_pool.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "../dprint.h"
00031 #include "db_pool.h"
00032
00033
00034
00035 static struct pool_con* db_pool = 0;
00036
00037
00038
00039
00040
00041
00042
00043 struct pool_con* pool_get(const struct db_id* id)
00044 {
00045 struct pool_con* ptr;
00046
00047 if (!id) {
00048 LM_ERR("invalid parameter value\n");
00049 return 0;
00050 }
00051
00052 ptr = db_pool;
00053 while (ptr) {
00054 if (cmp_db_id(id, ptr->id)) {
00055 ptr->ref++;
00056 return ptr;
00057 }
00058 ptr = ptr->next;
00059 }
00060
00061 return 0;
00062 }
00063
00064
00065
00066
00067
00068 void pool_insert(struct pool_con* con)
00069 {
00070 if (!con) return;
00071
00072 con->next = db_pool;
00073 db_pool = con;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 int pool_remove(struct pool_con* con)
00088 {
00089 struct pool_con* ptr;
00090
00091 if (!con) return -2;
00092
00093 if (con->ref > 1) {
00094
00095
00096
00097 LM_DBG("connection still kept in the pool\n");
00098 con->ref--;
00099 return 0;
00100 }
00101
00102 LM_DBG("removing connection from the pool\n");
00103
00104 if (db_pool == con) {
00105 db_pool = db_pool->next;
00106 } else {
00107 ptr = db_pool;
00108 while(ptr) {
00109 if (ptr->next == con) break;
00110 ptr = ptr->next;
00111 }
00112 if (!ptr) {
00113 LM_ERR("weird, connection not found in the pool\n");
00114 return -1;
00115 } else {
00116
00117 ptr->next = con->next;
00118 }
00119 }
00120
00121 return 1;
00122 }