* look thru a linked list for the table. if dne, create a new one * and add to the list */
| 417 | * and add to the list |
| 418 | */ |
| 419 | tbl_cache_p bdblib_get_table(database_p _db, str *_s) |
| 420 | { |
| 421 | tbl_cache_p _tbc = NULL; |
| 422 | table_p _tp = NULL; |
| 423 | |
| 424 | if(!_db || !_s || !_s->s || _s->len<=0) |
| 425 | return NULL; |
| 426 | |
| 427 | if(!_db->dbenv) |
| 428 | { |
| 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | _tbc = _db->tables; |
| 433 | while(_tbc) |
| 434 | { |
| 435 | if(_tbc->dtp) |
| 436 | { |
| 437 | |
| 438 | if(_tbc->dtp->name.len == _s->len |
| 439 | && !strncasecmp(_tbc->dtp->name.s, _s->s, _s->len )) |
| 440 | { |
| 441 | return _tbc; |
| 442 | } |
| 443 | } |
| 444 | _tbc = _tbc->next; |
| 445 | } |
| 446 | |
| 447 | _tbc = (tbl_cache_p)pkg_malloc(sizeof(tbl_cache_t)); |
| 448 | if(!_tbc) |
| 449 | return NULL; |
| 450 | |
| 451 | if(!lock_init(&_tbc->sem)) |
| 452 | { |
| 453 | pkg_free(_tbc); |
| 454 | return NULL; |
| 455 | } |
| 456 | |
| 457 | _tp = bdblib_create_table(_db, _s); |
| 458 | |
| 459 | #ifdef BDB_EXTRA_DEBUG |
| 460 | LM_DBG("table: %.*s\n", _s->len, _s->s); |
| 461 | #endif |
| 462 | |
| 463 | if(!_tp) |
| 464 | { |
| 465 | LM_ERR("failed to create table.\n"); |
| 466 | pkg_free(_tbc); |
| 467 | return NULL; |
| 468 | } |
| 469 | |
| 470 | lock_get(&_tbc->sem); |
| 471 | _tbc->dtp = _tp; |
| 472 | |
| 473 | if(_db->tables) |
| 474 | (_db->tables)->prev = _tbc; |
| 475 | |
| 476 | _tbc->next = _db->tables; |
no test coverage detected