-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.h
80 lines (57 loc) · 2.53 KB
/
hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (C) 2016, 2017 Alexey Khrabrov, Bogdan Simion
//
// Distributed under the terms of the GNU General Public License.
//
// This file is part of Assignment 3, CSC469, Fall 2017.
//
// This is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This file is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this file. If not, see <http://www.gnu.org/licenses/>.
#ifndef _HASH_H_
#define _HASH_H_
#include <stdbool.h>
#include <pthread.h>
#include "defs.h"
#include "dlist.h"
typedef struct _hash_entry {
dlist_entry list_entry;
char key[KEY_SIZE];
void *value;// must be != NULL
size_t value_sz;
} hash_entry;
typedef struct _hash_bucket {
dlist entries;
pthread_mutex_t lock;
} hash_bucket;
typedef struct _hash_table {
size_t size;
hash_bucket *buckets;
} hash_table;
// Initialize a hash table with given size; returns true on success
bool hash_init(hash_table *table, size_t size);
// Free resources used by a hash table
void hash_cleanup(hash_table *table);
// Lock a particular key (lock corresponding hash bucket)
void hash_lock(hash_table *table, const char key[KEY_SIZE]);
// Unlock a particular key (unlock corresponding hash bucket)
void hash_unlock(hash_table *table, const char key[KEY_SIZE]);
// Get value for a key; returns true on success; not synchronized
bool hash_get(hash_table *table, const char key[KEY_SIZE], void **value, size_t *value_sz);
// Put a new value for a key and obtain the old value (if any); returns true on success; not synchronized
bool hash_put(hash_table *table, const char key[KEY_SIZE], void *value, size_t value_sz,
void **old_value, size_t *old_value_sz);
// Remove a key and obtain the old value (if any); returns true on success; not synchronized
bool hash_remove(hash_table *table, const char key[KEY_SIZE], void **old_value, size_t *old_value_sz);
typedef void hash_iterator(const char key[KEY_SIZE], void *value, size_t value_sz, void *arg);
// Iterate through all keys, calling iterator(key, value, value_sz, arg) for each key; synchronized
void hash_iterate(hash_table *table, hash_iterator *iterator, void *arg);
#endif// _HASH_H_