10 #define HASH_BUCKETS 512
11 #define HASH_MASK (HASH_BUCKETS - 1)
13 unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
15 static struct flist_head *file_hash;
16 static struct fio_spinlock *hash_lock;
18 static unsigned short hash(const char *name)
20 return crc16((const unsigned char *) name, strlen(name)) & HASH_MASK;
23 void remove_file_hash(struct fio_file *f)
25 fio_spin_lock(hash_lock);
27 if (f->flags & FIO_FILE_HASHED) {
28 assert(!flist_empty(&f->hash_list));
29 flist_del_init(&f->hash_list);
30 f->flags &= ~FIO_FILE_HASHED;
33 fio_spin_unlock(hash_lock);
36 static struct fio_file *__lookup_file_hash(const char *name)
38 struct flist_head *bucket = &file_hash[hash(name)];
41 flist_for_each(n, bucket) {
42 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
44 if (!strcmp(f->file_name, name)) {
53 struct fio_file *lookup_file_hash(const char *name)
57 fio_spin_lock(hash_lock);
58 f = __lookup_file_hash(name);
59 fio_spin_unlock(hash_lock);
63 struct fio_file *add_file_hash(struct fio_file *f)
65 struct fio_file *alias;
67 if (f->flags & FIO_FILE_HASHED)
70 INIT_FLIST_HEAD(&f->hash_list);
72 fio_spin_lock(hash_lock);
74 alias = __lookup_file_hash(f->file_name);
76 f->flags |= FIO_FILE_HASHED;
77 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
80 fio_spin_unlock(hash_lock);
84 void file_hash_exit(void)
86 unsigned int i, has_entries = 0;
88 fio_spin_lock(hash_lock);
89 for (i = 0; i < HASH_BUCKETS; i++)
90 has_entries += !flist_empty(&file_hash[i]);
93 fio_spin_unlock(hash_lock);
96 log_err("fio: file hash not empty on exit\n");
98 fio_spinlock_remove(hash_lock);
102 void file_hash_init(void *ptr)
107 for (i = 0; i < HASH_BUCKETS; i++)
108 INIT_FLIST_HEAD(&file_hash[i]);
110 hash_lock = fio_spinlock_init();