/
VirtualView.cs
367 lines (247 loc) · 10.4 KB
/
VirtualView.cs
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
VirtualView - Memory Mapped Virtual View
Copyright (c) 2013 Dror Gluska
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
(LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The terms of redistributing and/or modifying this software also
include exceptions to the LGPL that facilitate static linking.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Change log:
2013-03-15 - Initial version
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.MemoryMappedFiles;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Threading;
using FileStore.Mapper.Collections;
namespace FileStore.Mapper
{
/// <summary>
/// Memory Mapped Virtual View
/// </summary>
public class VirtualView : IDisposable
{
private VirtualViewContainer m_viewaccessor;
private long m_viewoffset;
private long m_offset;
private long m_capacity;
internal VirtualView(VirtualViewContainer accessor, long view_offset, long offset, long capacity)
{
m_viewoffset = view_offset;
m_viewaccessor = accessor;
m_offset = offset;
m_capacity = capacity;
}
public bool CanRead { get { return m_viewaccessor.Container.View.CanRead; } }
public bool CanWrite { get { return m_viewaccessor.Container.View.CanWrite; } }
public long Capacity { get { return m_capacity; } }
public void Read<T>(long position, out T structure) where T : struct
{
int size = Marshal.SizeOf(typeof(T));
if ((size + position) > m_capacity)
throw new ArgumentOutOfRangeException("Either position is invalid or structure is too big");
m_viewaccessor.Container.View.Read<T>(GetPosition(position), out structure);
}
public int ReadArray<T>(long position, T[] array, int offset, int count) where T : struct
{
return m_viewaccessor.Container.View.ReadArray<T>(GetPosition(position), array, offset, count);
}
public bool ReadBoolean(long position)
{
return m_viewaccessor.Container.View.ReadBoolean(GetPosition(position));
}
public byte ReadByte(long position)
{
return m_viewaccessor.Container.View.ReadByte(GetPosition(position));
}
public char ReadChar(long position)
{
return m_viewaccessor.Container.View.ReadChar(GetPosition(position));
}
public decimal ReadDecimal(long position)
{
return m_viewaccessor.Container.View.ReadDecimal(GetPosition(position));
}
public double ReadDouble(long position)
{
return m_viewaccessor.Container.View.ReadDouble(GetPosition(position));
}
public short ReadInt16(long position)
{
return m_viewaccessor.Container.View.ReadInt16(GetPosition(position));
}
public int ReadInt32(long position)
{
return m_viewaccessor.Container.View.ReadInt32(GetPosition(position));
}
public long ReadInt64(long position)
{
return m_viewaccessor.Container.View.ReadInt64(GetPosition(position));
}
public sbyte ReadSByte(long position)
{
return m_viewaccessor.Container.View.ReadSByte(GetPosition(position));
}
public float ReadSingle(long position)
{
return m_viewaccessor.Container.View.ReadSingle(GetPosition(position));
}
public ushort ReadUInt16(long position)
{
return m_viewaccessor.Container.View.ReadUInt16(GetPosition(position));
}
public uint ReadUInt32(long position)
{
return m_viewaccessor.Container.View.ReadUInt32(GetPosition(position));
}
public ulong ReadUInt64(long position)
{
return m_viewaccessor.Container.View.ReadUInt64(GetPosition(position));
}
public void Write(long position, bool value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, byte value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, char value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, decimal value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, double value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, float value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, int value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, long value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write<T>(long position, ref T structure) where T: struct
{
Write<T>(position, structure);
}
public void Write<T>(long position, T structure) where T : struct
{
this.m_viewaccessor.Container.View.Write<T>(GetPosition(position), ref structure);
}
public void Write(long position, sbyte value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, short value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, uint value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, ulong value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void Write(long position, ushort value)
{
m_viewaccessor.Container.View.Write(GetPosition(position), value);
}
public void WriteArray<T>(long position, T[] array, int offset, int count) where T : struct
{
m_viewaccessor.Container.View.WriteArray<T>(GetPosition(position), array, offset, count);
}
private long GetPosition(long requestedPosition)
{
return m_offset - m_viewoffset + requestedPosition;
}
/// <summary>
/// Similar to Read<T> except it first reads the struct as byte array and then copies it to the struct's memory pointer
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="position"></param>
/// <param name="structure"></param>
public void ReadStruct<T>(long position, out T structure) where T : struct
{
int size = Marshal.SizeOf(typeof(T));
byte[] readstruct = new byte[size];
if ((size + position) > m_capacity)
throw new ArgumentOutOfRangeException("Either position is invalid or structure is too big");
m_viewaccessor.Container.View.ReadArray<byte>(GetPosition(position), readstruct, 0, size);
structure = new T();
BytesToStruct<T>(readstruct, ref structure);
//m_viewaccessor.Container.View.Read<T>(GetPosition(position), out structure);
}
/// <summary>
/// Similar to Write<T> except it first copies the struct to a byte array and then writes that byte array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="position"></param>
/// <param name="structure"></param>
public void WriteStruct<T>(long position, T structure) where T : struct
{
byte[] array = StructToBytes<T>(structure);
this.m_viewaccessor.Container.View.WriteArray<byte>(GetPosition(position), array, 0, array.Length);
}
/// <summary>
/// Copy T to byte array
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
private byte[] StructToBytes<T>(T obj)
{
int structsize = Marshal.SizeOf(obj);
byte[] barray = new byte[structsize];
IntPtr structptr = Marshal.AllocHGlobal(structsize);
Marshal.StructureToPtr(obj, structptr, true);
Marshal.Copy(structptr, barray, 0, structsize);
Marshal.FreeHGlobal(structptr);
return barray;
}
/// <summary>
/// Copy byte array to T
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="bytearray"></param>
/// <param name="obj"></param>
private void BytesToStruct<T>(byte[] bytearray, ref T obj)
{
int structsize = Marshal.SizeOf(obj);
IntPtr structptr = Marshal.AllocHGlobal(structsize);
Marshal.StructureToPtr(obj, structptr, true);
Marshal.Copy(bytearray, 0, structptr, structsize);
Marshal.FreeHGlobal(structptr);
}
public void Dispose()
{
//m_viewaccessor.Container.View.Flush();
m_viewaccessor.Container.MappedViews.Remove(m_viewaccessor);
m_viewaccessor = null;
}
}
}