8 #define HASH_BUCKETS 512
9 #define HASH_MASK (HASH_BUCKETS - 1)
11 unsigned int file_hash_size = HASH_BUCKETS * sizeof(struct flist_head);
13 static struct flist_head *file_hash;
14 static struct fio_mutex *hash_lock;
16 static unsigned short hash(const char *name)
18 return crc16((const unsigned char *) name, strlen(name)) & HASH_MASK;
21 void remove_file_hash(struct fio_file *f)
23 fio_mutex_down(hash_lock);
25 if (f->flags & FIO_FILE_HASHED) {
26 assert(!flist_empty(&f->hash_list));
27 flist_del_init(&f->hash_list);
28 f->flags &= ~FIO_FILE_HASHED;
31 fio_mutex_up(hash_lock);
34 static struct fio_file *__lookup_file_hash(const char *name)
36 struct flist_head *bucket = &file_hash[hash(name)];
39 flist_for_each(n, bucket) {
40 struct fio_file *f = flist_entry(n, struct fio_file, hash_list);
42 if (!strcmp(f->file_name, name)) {
51 struct fio_file *lookup_file_hash(const char *name)
55 fio_mutex_down(hash_lock);
56 f = __lookup_file_hash(name);
57 fio_mutex_up(hash_lock);
61 struct fio_file *add_file_hash(struct fio_file *f)
63 struct fio_file *alias;
65 if (f->flags & FIO_FILE_HASHED)
68 INIT_FLIST_HEAD(&f->hash_list);
70 fio_mutex_down(hash_lock);
72 alias = __lookup_file_hash(f->file_name);
74 f->flags |= FIO_FILE_HASHED;
75 flist_add_tail(&f->hash_list, &file_hash[hash(f->file_name)]);
78 fio_mutex_up(hash_lock);
82 void file_hash_exit(void)
84 unsigned int i, has_entries = 0;
86 fio_mutex_down(hash_lock);
87 for (i = 0; i < HASH_BUCKETS; i++)
88 has_entries += !flist_empty(&file_hash[i]);
89 fio_mutex_up(hash_lock);
92 log_err("fio: file hash not empty on exit\n");
95 fio_mutex_remove(hash_lock);
99 void file_hash_init(void *ptr)
104 for (i = 0; i < HASH_BUCKETS; i++)
105 INIT_FLIST_HEAD(&file_hash[i]);
107 hash_lock = fio_mutex_init(1);