int hash_set(struct hash *hash, uint32_t laddr, uint32_t raddr, uint16_t lport, uint16_t rport, struct timeval value) { hash_load_check(hash); if (hash_set_internal(hash->sessions, hash->sz, laddr, raddr, lport, rport, value)) { hash->count++; return 1; } return 0; }
static int hash_load_check(struct hash *hash) { if ((hash->count * 100) / hash->sz > MAX_LOAD_PERCENT) { struct session *new_sessions, *old_sessions; unsigned long nsz, i; // New container nsz = hash_newsz(hash->sz); new_sessions = malloc(nsz * sizeof(struct session)); if (!new_sessions) abort(); memset(new_sessions, 0, nsz * sizeof(struct session)); // Rehash for (i = 0; i < hash->sz; i ++) { struct session *session; for (session = hash->sessions + i; session->next; session = session->next) { hash_set_internal(new_sessions, nsz, session->laddr, session->raddr, session->lport, session->rport, session->tv); } } // Switch hash->sz = nsz; old_sessions = hash->sessions; hash->sessions = new_sessions; free(old_sessions); return 1; } return 0; }
int hash_set_N(hash_t *ht, const void *key, int klen, void *val) { return hash_set_internal(ht, key, klen, val, nalloc_N); }