1 /**
2  * ae.sys.datamm
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.sys.datamm;
15 
16 import std.mmfile;
17 import std.typecons;
18 debug import std.stdio;
19 
20 import ae.sys.data;
21 
22 alias MmMode = MmFile.Mode;
23 
24 // ************************************************************************
25 
26 class MappedDataWrapper : DataWrapper
27 {
28 	typeof(scoped!MmFile(null)) mmFile;
29 	void[] mappedData;
30 
31 	this(string name, MmMode mode, size_t from, size_t to)
32 	{
33 		mmFile = scoped!MmFile(name, mode, 0, null);
34 		mappedData = (from || to) ? mmFile.Scoped_payload[from..(to ? to : mmFile.length)] : mmFile.Scoped_payload[];
35 
36 		debug(DATA_REFCOUNT) writefln("? -> %s: Created MappedDataWrapper", cast(void*)this);
37 	}
38 
39 	debug(DATA_REFCOUNT)
40 	~this()
41 	{
42 		writefln("? -> %s: Deleted MappedDataWrapper", cast(void*)this);
43 	}
44 
45 	override @property inout(void)[] contents() inout { return mappedData; }
46 	override @property size_t size() const { return mappedData.length; }
47 	override void setSize(size_t newSize) { assert(false, "Can't resize MappedDataWrapper"); }
48 	override @property size_t capacity() const { return mappedData.length; }
49 }
50 
51 auto mapFile(string name, MmMode mode, size_t from = 0, size_t to = 0)
52 {
53 	auto wrapper = new MappedDataWrapper(name, mode, from, to);
54 	return Data(wrapper, mode != MmMode.read);
55 }