dbt_cache_p dbt_cache_get_db(str *_s) { dbt_cache_p _dcache=NULL;; if(!_dbt_cachesem || !_dbt_cachedb) { LM_ERR("dbtext cache is not initialized! Check if you loaded" " dbtext before any other module that uses it\n"); return NULL; } if(!_s || !_s->s || _s->len<=0) return NULL; LM_DBG("looking for db %.*s!\n",_s->len,_s->s); lock_get(_dbt_cachesem); _dcache = *_dbt_cachedb; while(_dcache) { if(_dcache->name.len==_s->len && !strncasecmp(_dcache->name.s, _s->s, _s->len)) { LM_DBG("db already cached!\n"); goto done; } _dcache = _dcache->next; } if(!dbt_is_database(_s)) { LM_ERR("database [%.*s] does not exists!\n", _s->len, _s->s); goto done; } LM_DBG("new db!\n"); _dcache = (dbt_cache_p)shm_malloc(sizeof(dbt_cache_t)); if(!_dcache) { LM_ERR(" no shm memory for dbt_cache_t.\n"); goto done; } memset(_dcache, 0, sizeof(dbt_cache_t)); _dcache->name.s = (char*)shm_malloc((_s->len+1)*sizeof(char)); if(!_dcache->name.s) { LM_ERR(" no shm memory for s!!\n"); shm_free(_dcache); _dcache = NULL; goto done; } memcpy(_dcache->name.s, _s->s, _s->len); _dcache->name.s[_s->len] = '\0'; _dcache->name.len = _s->len; if(*_dbt_cachedb) _dcache->next = *_dbt_cachedb; *_dbt_cachedb = _dcache; done: lock_release(_dbt_cachesem); return _dcache; }
dbt_cache_p dbt_cache_get_db(str *_s) { dbt_cache_p _dcache=NULL;; if(!_cachesem || !_cachedb) { LOG(L_ERR, "DBT:dbt_cache_get_db:dbtext cache is not initialized!\n"); return NULL; } if(!_s || !_s->s || _s->len<=0) return NULL; DBG("DBT:dbt_cache_get_db: looking for db!\n"); lock_get(_cachesem); _dcache = *_cachedb; while(_dcache) { lock_get(&_dcache->sem); if(_dcache->dbp) { if(_dcache->dbp->name.len==_s->len && !strncasecmp(_dcache->dbp->name.s, _s->s, _s->len)) { lock_release(&_dcache->sem); goto done; } } lock_release(&_dcache->sem); _dcache = _dcache->next; } if(!dbt_is_database(_s)) { LOG(L_ERR, "DBT:dbt_cache_get_db: database [%.*s] does not exists!\n", _s->len, _s->s); goto done; } DBG("DBT:dbt_cache_get_db: new db!\n"); _dcache = (dbt_cache_p)shm_malloc(sizeof(dbt_cache_t)); if(!_dcache) { LOG(L_ERR, "DBT:dbt_cache_get_db: no memory for dbt_cache_t.\n"); goto done; } _dcache->dbp = (dbt_db_p)shm_malloc(sizeof(dbt_db_t)); if(!_dcache->dbp) { LOG(L_ERR, "DBT:dbt_cache_get_db: no memory for dbt_db_t!\n"); shm_free(_dcache); goto done; } _dcache->dbp->name.s = (char*)shm_malloc(_s->len*sizeof(char)); if(!_dcache->dbp->name.s) { LOG(L_ERR, "DBT:dbt_cache_get_db: no memory for s!!\n"); shm_free(_dcache->dbp); shm_free(_dcache); _dcache = NULL; goto done; } memcpy(_dcache->dbp->name.s, _s->s, _s->len); _dcache->dbp->name.len = _s->len; _dcache->dbp->tables = NULL; if(!lock_init(&_dcache->sem)) { LOG(L_ERR, "DBT:dbt_cache_get_db: no sems!\n"); shm_free(_dcache->dbp->name.s); shm_free(_dcache->dbp); shm_free(_dcache); _dcache = NULL; goto done; } _dcache->prev = NULL; if(*_cachedb) { _dcache->next = *_cachedb; (*_cachedb)->prev = _dcache; } else _dcache->next = NULL; *_cachedb = _dcache; done: lock_release(_cachesem); return _dcache; }