Lines Matching refs:bc
68 badcache_resize(dns_badcache_t *bc, isc_time_t *now);
72 dns_badcache_t *bc = NULL;
78 bc = isc_mem_get(mctx, sizeof(dns_badcache_t));
79 memset(bc, 0, sizeof(dns_badcache_t));
81 isc_mem_attach(mctx, &bc->mctx);
82 isc_rwlock_init(&bc->lock, 0, 0);
84 bc->table = isc_mem_get(bc->mctx, sizeof(*bc->table) * size);
85 bc->tlocks = isc_mem_get(bc->mctx, sizeof(isc_mutex_t) * size);
87 isc_mutex_init(&bc->tlocks[i]);
89 bc->size = bc->minsize = size;
90 memset(bc->table, 0, bc->size * sizeof(dns_bcentry_t *));
92 atomic_init(&bc->count, 0);
93 atomic_init(&bc->sweep, 0);
94 bc->magic = BADCACHE_MAGIC;
96 *bcp = bc;
102 dns_badcache_t *bc;
106 bc = *bcp;
109 dns_badcache_flush(bc);
111 bc->magic = 0;
112 isc_rwlock_destroy(&bc->lock);
113 for (i = 0; i < bc->size; i++) {
114 isc_mutex_destroy(&bc->tlocks[i]);
116 isc_mem_put(bc->mctx, bc->table, sizeof(dns_bcentry_t *) * bc->size);
117 isc_mem_put(bc->mctx, bc->tlocks, sizeof(isc_mutex_t) * bc->size);
118 isc_mem_putanddetach(&bc->mctx, bc, sizeof(dns_badcache_t));
122 badcache_resize(dns_badcache_t *bc, isc_time_t *now) {
128 RWLOCK(&bc->lock, isc_rwlocktype_write);
141 if (atomic_load_relaxed(&bc->count) > bc->size * 8) {
143 } else if (atomic_load_relaxed(&bc->count) < bc->size * 2 &&
144 bc->size > bc->minsize)
149 RWUNLOCK(&bc->lock, isc_rwlocktype_write);
154 newsize = bc->size * 2 + 1;
156 newsize = (bc->size - 1) / 2;
168 newtable = isc_mem_get(bc->mctx, sizeof(dns_bcentry_t *) * newsize);
171 newlocks = isc_mem_get(bc->mctx, sizeof(isc_mutex_t) * newsize);
174 for (i = 0; i < newsize && i < bc->size; i++) {
175 newlocks[i] = bc->tlocks[i];
178 for (i = bc->size; i < newsize; i++) {
182 for (i = newsize; i < bc->size; i++) {
183 isc_mutex_destroy(&bc->tlocks[i]);
186 for (i = 0; atomic_load_relaxed(&bc->count) > 0 && i < bc->size; i++) {
187 for (bad = bc->table[i]; bad != NULL; bad = next) {
190 isc_mem_put(bc->mctx, bad,
192 atomic_fetch_sub_relaxed(&bc->count, 1);
198 bc->table[i] = NULL;
201 isc_mem_put(bc->mctx, bc->tlocks, sizeof(isc_mutex_t) * bc->size);
202 bc->tlocks = newlocks;
204 isc_mem_put(bc->mctx, bc->table, sizeof(*bc->table) * bc->size);
205 bc->size = newsize;
206 bc->table = newtable;
208 RWUNLOCK(&bc->lock, isc_rwlocktype_write);
212 dns_badcache_add(dns_badcache_t *bc, const dns_name_t *name,
221 REQUIRE(VALID_BADCACHE(bc));
225 RWLOCK(&bc->lock, isc_rwlocktype_read);
233 hash = hashval % bc->size;
234 LOCK(&bc->tlocks[hash]);
236 for (bad = bc->table[hash]; bad != NULL; bad = next) {
247 bc->table[hash] = bad->next;
251 isc_mem_put(bc->mctx, bad,
253 atomic_fetch_sub_relaxed(&bc->count, 1);
261 bad = isc_mem_get(bc->mctx, sizeof(*bad) + name->length);
269 bad->next = bc->table[hash];
270 bc->table[hash] = bad;
271 unsigned count = atomic_fetch_add_relaxed(&bc->count, 1);
272 if ((count > bc->size * 8) ||
273 (count < bc->size * 2 && bc->size > bc->minsize))
281 UNLOCK(&bc->tlocks[hash]);
282 RWUNLOCK(&bc->lock, isc_rwlocktype_read);
284 badcache_resize(bc, &now);
289 dns_badcache_find(dns_badcache_t *bc, const dns_name_t *name,
296 REQUIRE(VALID_BADCACHE(bc));
300 RWLOCK(&bc->lock, isc_rwlocktype_read);
314 if (atomic_load_relaxed(&bc->count) == 0) {
318 hash = dns_name_hash(name, false) % bc->size;
320 LOCK(&bc->tlocks[hash]);
321 for (bad = bc->table[hash]; bad != NULL; bad = next) {
330 bc->table[hash] = bad->next;
333 isc_mem_put(bc->mctx, bad,
335 atomic_fetch_sub(&bc->count, 1);
347 UNLOCK(&bc->tlocks[hash]);
353 i = atomic_fetch_add(&bc->sweep, 1) % bc->size;
354 if (isc_mutex_trylock(&bc->tlocks[i]) == ISC_R_SUCCESS) {
355 bad = bc->table[i];
357 bc->table[i] = bad->next;
358 isc_mem_put(bc->mctx, bad,
360 atomic_fetch_sub_relaxed(&bc->count, 1);
362 UNLOCK(&bc->tlocks[i]);
365 RWUNLOCK(&bc->lock, isc_rwlocktype_read);
370 dns_badcache_flush(dns_badcache_t *bc) {
374 RWLOCK(&bc->lock, isc_rwlocktype_write);
375 REQUIRE(VALID_BADCACHE(bc));
377 for (i = 0; atomic_load_relaxed(&bc->count) > 0 && i < bc->size; i++) {
378 for (entry = bc->table[i]; entry != NULL; entry = next) {
380 isc_mem_put(bc->mctx, entry,
382 atomic_fetch_sub_relaxed(&bc->count, 1);
384 bc->table[i] = NULL;
386 RWUNLOCK(&bc->lock, isc_rwlocktype_write);
390 dns_badcache_flushname(dns_badcache_t *bc, const dns_name_t *name) {
396 REQUIRE(VALID_BADCACHE(bc));
399 RWLOCK(&bc->lock, isc_rwlocktype_read);
405 hash = dns_name_hash(name, false) % bc->size;
406 LOCK(&bc->tlocks[hash]);
408 for (bad = bc->table[hash]; bad != NULL; bad = next) {
414 bc->table[hash] = bad->next;
419 isc_mem_put(bc->mctx, bad,
421 atomic_fetch_sub_relaxed(&bc->count, 1);
426 UNLOCK(&bc->tlocks[hash]);
428 RWUNLOCK(&bc->lock, isc_rwlocktype_read);
432 dns_badcache_flushtree(dns_badcache_t *bc, const dns_name_t *name) {
439 REQUIRE(VALID_BADCACHE(bc));
446 RWLOCK(&bc->lock, isc_rwlocktype_write);
453 for (i = 0; atomic_load_relaxed(&bc->count) > 0 && i < bc->size; i++) {
455 for (bad = bc->table[i]; bad != NULL; bad = next) {
460 bc->table[i] = bad->next;
465 isc_mem_put(bc->mctx, bad,
467 atomic_fetch_sub_relaxed(&bc->count, 1);
474 RWUNLOCK(&bc->lock, isc_rwlocktype_write);
478 dns_badcache_print(dns_badcache_t *bc, const char *cachename, FILE *fp) {
486 REQUIRE(VALID_BADCACHE(bc));
494 RWLOCK(&bc->lock, isc_rwlocktype_write);
498 for (i = 0; atomic_load_relaxed(&bc->count) > 0 && i < bc->size; i++) {
500 for (bad = bc->table[i]; bad != NULL; bad = next) {
506 bc->table[i] = bad->next;
509 isc_mem_put(bc->mctx, bad,
511 atomic_fetch_sub_relaxed(&bc->count, 1);
526 RWUNLOCK(&bc->lock, isc_rwlocktype_write);