-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
UnsafeTable.java
222 lines (193 loc) · 7.23 KB
/
UnsafeTable.java
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.collection.primitive.hopscotch;
import org.neo4j.unsafe.impl.internal.dragons.UnsafeUtil;
public abstract class UnsafeTable<VALUE> extends PowerOfTwoQuantizedTable<VALUE>
{
private final int bytesPerKey;
private final int bytesPerEntry;
private final long dataSize;
// address which should be free when closing
private final long allocatedAddress;
// address which should be used to access the table, the address where the table actually starts at
private final long address;
protected final VALUE valueMarker;
protected UnsafeTable( int capacity, int bytesPerKey, VALUE valueMarker )
{
super( capacity, 32 );
UnsafeUtil.assertHasUnsafe();
this.bytesPerKey = bytesPerKey;
this.bytesPerEntry = 4+bytesPerKey;
this.valueMarker = valueMarker;
this.dataSize = (long)this.capacity*bytesPerEntry;
// Below is a piece of code which ensures that allocated memory is aligned to 4-byte boundary
// if memory system requires aligned memory access. The reason we pick 4-byte boundary is that
// it's the lowest common denominator and the size of our hop-bits field for every entry.
// So even for a table which would only deal with, say longs (8-byte), it would still need to
// read and write 4-byte hop-bits fields. Therefore this table can, if required to, read anything
// bigger than 4-byte fields as multiple 4-byte fields. This way it can play well with aligned
// memory access requirements.
assert bytesPerEntry % Integer.BYTES == 0 : "Bytes per entry needs to be divisible by 4, this constraint " +
"is checked because on memory systems requiring aligned memory access this would otherwise break.";
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
this.allocatedAddress = this.address = UnsafeUtil.allocateMemory( dataSize );
}
else
{
// There's an assertion above also verifying this, but it's only an actual problem if our memory system
// requires aligned access, which seems to be the case right here and now.
if ( (bytesPerEntry % Integer.BYTES) != 0 )
{
throw new IllegalArgumentException( "Memory system requires aligned memory access and " +
getClass().getSimpleName() + " was designed to cope with this requirement by " +
"being able to accessing data in 4-byte chunks, if needed to. " +
"Although this table tried to be constructed with bytesPerKey:" + bytesPerKey +
" yielding a bytesPerEntry:" + bytesPerEntry + ", which isn't 4-byte aligned." );
}
this.allocatedAddress = UnsafeUtil.allocateMemory( dataSize + Integer.BYTES - 1 );
this.address = UnsafeUtil.alignedMemory( allocatedAddress, Integer.BYTES );
}
clearMemory();
}
@Override
public void clear()
{
if ( !isEmpty() )
{
clearMemory();
}
super.clear();
}
private void clearMemory()
{
UnsafeUtil.setMemory( address, dataSize, (byte)-1 );
}
@Override
public long key( int index )
{
return internalKey( keyAddress( index ) );
}
protected abstract long internalKey( long keyAddress );
@Override
public VALUE value( int index )
{
return valueMarker;
}
@Override
public void put( int index, long key, VALUE value )
{
internalPut( keyAddress( index ), key, value );
size++;
}
protected abstract void internalPut( long keyAddress, long key, VALUE value );
@Override
public VALUE putValue( int index, VALUE value )
{
return value;
}
@Override
public long move( int fromIndex, int toIndex )
{
long adr = keyAddress( fromIndex );
long key = internalKey( adr );
VALUE value = internalRemove( adr );
internalPut( keyAddress( toIndex ), key, value );
return key;
}
@Override
public VALUE remove( int index )
{
VALUE value = internalRemove( keyAddress( index ) );
size--;
return value;
}
protected VALUE internalRemove( long keyAddress )
{
UnsafeUtil.setMemory( keyAddress, bytesPerKey, (byte)-1 );
return valueMarker;
}
@Override
public long hopBits( int index )
{
return ~(UnsafeUtil.getInt( hopBitsAddress( index ) ) | 0xFFFFFFFF00000000L);
}
@Override
public void putHopBit( int index, int hd )
{
long adr = hopBitsAddress( index );
int hopBits = UnsafeUtil.getInt( adr );
hopBits &= ~(1 << hd);
UnsafeUtil.putInt( adr, hopBits );
}
@Override
public void moveHopBit( int index, int hd, int delta )
{
long adr = hopBitsAddress( index );
int hopBits = UnsafeUtil.getInt( adr );
hopBits ^= (1 << hd) | (1 << (hd+delta));
UnsafeUtil.putInt( adr, hopBits );
}
protected long keyAddress( int index )
{
return address + (index*((long) bytesPerEntry)) + 4;
}
protected long hopBitsAddress( int index )
{
return address + (index*((long) bytesPerEntry));
}
@Override
public void removeHopBit( int index, int hd )
{
long adr = hopBitsAddress( index );
int hopBits = UnsafeUtil.getInt( adr );
hopBits |= (1 << hd);
UnsafeUtil.putInt( adr, hopBits );
}
@Override
public void close()
{
UnsafeUtil.free( allocatedAddress );
}
protected static void alignmentSafePutLongAsTwoInts( long address, long value )
{
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
UnsafeUtil.putLong( address, value );
}
else
{
// See javadoc in constructor as to why we do this
UnsafeUtil.putInt( address, (int) value );
UnsafeUtil.putInt( address + Integer.BYTES, (int) (value >>> Integer.SIZE) );
}
}
protected static long alignmentSafeGetLongAsTwoInts( long address )
{
if ( UnsafeUtil.allowUnalignedMemoryAccess )
{
return UnsafeUtil.getLong( address );
}
// See javadoc in constructor as to why we do this
long lsb = UnsafeUtil.getInt( address ) & 0xFFFFFFFFL;
long msb = UnsafeUtil.getInt( address + Integer.BYTES ) & 0xFFFFFFFFL;
return lsb | (msb << Integer.SIZE);
}
}