-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashtable.h
190 lines (134 loc) · 4.79 KB
/
hashtable.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#ifndef _HASHTABLE_H_
#define _HASHTABLE_H_
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
#include "table-params.h"
#include "bits.h"
#include "opt.h"
#include "util.h"
//////////////////////////////////////////////////////////////////////
//arbitrary constants
#define EQUALS 1
#define NOT_EQUALS 0
//return values for checking table. Returned by lookupQuery
#define not_in -3 //item is not in (used in query/delete)
#define in -1 //item is already in
#define unknown -2 //unkown keep looking
#define copy 0x1 //sets 1s bit of ent ptr for resizing
#define getCopy(X) lowBitsGet((void*)(X))
#define setCopy(X) lowBitsSet_atomic((void**)(&(X)), lowBitsGetPtr((void*)(X)), copy)
#define set_return(X, Y) ((node *)(((uint64_t)get_ptr(X)) | (Y)))
#define getNodePtr(X) (getPtr((void*)(X)))
#define getCounter(X) (highBitsGet((void*)(X))&(counter_bits_mask))
#define decrCounter(X) (highBitsSetDECR((void**)(&(X))))
#define incrCounter(X) (highBitsSetINCR((void**)(&(X))))
#define subCounter(X, Y) (highBitsSetSUB((void**)(&(X)), (Y)))
#define setCounter(X, Y) (highBitsSetMASK((void**)(&(X)), (Y), counter_bits_mask))
#define getNH(X, Y) ((highBitsGet((void*)(X)))>>(counter_bits+(Y)))
#define setNH(X, Y) (highBitsSet((void**)(&(X)), (Y)))
//table config is necessary
//////////////////////////////////////////////////////////////////////
//config for type hashtable will use
typedef struct frame_node{
pthread_t key;
void * val; //this is a frame_data_t ** defined in debug
}frame_node;
//define node and key type
typedef pthread_t key_type;
typedef struct frame_node node;
uint16_t genTag(pthread_t t);
//nothing for ints
#define hashTag(X) (genTag(X))
//compare key comparison
#define compare_keys(X, Y) ((X)==(Y))
#define getKey(X) (((node *)getNodePtr(X))->key)
#define getVal(X) ((node *)(getNodePtr(X))->thread_frames)
#define getKeyLen(X) sizeof(pthread_t)
//hash function for int (key is passed as a ptr)
#define hashFun(X, Y) murmur3_32((const uint8_t*)(&(X)), sizeof(pthread_t), (Y));
#define getKeyTag(X) genTag(X)
//////////////////////////////////////////////////////////////////////
//Config for hash function table will use
uint32_t murmur3_32(const uint8_t* key, size_t len, uint32_t seed);
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//table structs
//a sub table (this should be hidden)
typedef struct subTable {
node ** innerTable; //rows (table itself)
//for keeping track of when all items from min sub table have been moved
uint32_t * threadCopy;
uint32_t tableSize; //size
uint32_t logTableSize;
} subTable;
// head of cache: this is the main hahstable
typedef struct hashTable{
subTable ** tableArray; //array of tables
uint64_t start;
uint64_t end; //current max index (max exclusive)
const uint32_t seeds[DEFAULT_HASH_ATTEMPTS];
} hashTable; //worth it to aligned alloc this
//////////////////////////////////////////////////////////////////////
#ifdef cache
#define incr(X, Y, Z) (X)[((Y)<<log_int_ca)]+=(Z)
#define decr(X, Y, Z) (X)[((Y)<<log_int_ca)]-=(Z)
#else
#define incr(X, Y, Z) (X)[(Y)]+=(Z)
#define decr(X, Y, Z) (X)[(Y)]-=(Z)
#endif //cache
//wrapper fro resizing node, will call add resize if necessary and
//possible increment head's table index
static void
resize_node(hashTable * head, subTable * ht, uint32_t slot, uint32_t tid);
//lookup function in insertTrial to check a given inner table
static uint32_t
lookup(hashTable * head, subTable * ht, node * n,uint32_t s, uint32_t tid
#ifdef lazy_resize
, uint32_t doCopy, uint32_t start
#endif //lazy_resize
);
//lookup for queries
static uint32_t
lookupQuery(subTable * ht, key_type key, uint32_t s);
//addnode resize we have different set of conditions that we can
// optimize around i.e should never find self deleted, dont need to
// recompute tag, and later will remove need for rehashing
uint32_t
addNode_resize(hashTable * head, uint32_t start, node * n, uint32_t tid, uint16_t tag
#ifdef next_hash
, uint32_t from_slot
#endif //next_hash
);
//////////////////////////////////////////////////////////////////////
//general api start
// return 1 if inserted, 0 if already there
node *
addNode(hashTable * head,
node * n,
uint32_t tid);
// see if node is in the table
node *
findNode(hashTable * table,
key_type key,
uint32_t tid);
//free all nodes. returns num nodes in table
double
freeAll(hashTable * table,
uint32_t verbose,
uint32_t full);
// initialize a new main hashtable
hashTable *
initTable();
uint64_t
getStart(hashTable * head);
//////////////////////////////////////////////////////////////////////
//general api end
#endif