/*
CLASS {NATIVE_Memory_mapping|_CSharp}, NATIVE :

NATIVE: "using System.IO.MemoryMappedFiles;" .
NATIVE: "using System.IO;" .
NATIVE: "using System.Runtime.InteropServices;" .

NATIVE: "///COPY NATIVE_Memory_mapping.cs" .


*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;

using AL4;

namespace AL4
{
    public class NATIVE_Memory_mapping
    {
        
        public static List<NATIVE_Memory_mapping> All_mmaps 
        		= new List<NATIVE_Memory_mapping>();
        public string Filename;
        public bool Can_write;
        public int Max_size;
        public FileStream fs;
        public MemoryMappedFile mmf;
        public MemoryMappedViewAccessor Mapped;
        public int Mapped_from;
        public int Mapped_size;
        
        public void Map(int from, int size) {
        	if (Mapped_from==from && Mapped_size==size) return;
        	Mapped_from = from;
        	Mapped_size = size;
        	Mapped = null;
        	if (Mapped_from < 0 || Mapped_size <= 0) return;
        	Mapped = mmf.CreateViewAccessor(from, size);
        }
        
        public void Unmap() {
        	if (Mapped == null) return;
        	Mapped = null;
        	Mapped_from = -1;
        	Mapped_size = 0;
        }
        
        /*
        
FUNCTION Create_memory_mapping(
STR File|_path, BOOL Can_w|rite, INT Max|imum_size) ==> INT,
	NATIVE : @
            "int h = NATIVE_Memory_mapping.All_mmaps.LastIndexOf(null);"
            "if (h <= 0)"
            "{"
            "   h = NATIVE_Memory_mapping.All_mmaps.Count();"
            "   NATIVE_Memory_mapping.All_mmaps.Add(null);"
            "}"
            "NATIVE_Memory_mapping mmap = new NATIVE_Memory_mapping();"
            "NATIVE_Memory_mapping.All_mmaps[h] = mmap;"
            "mmap.Filename = X_File;"    
            "mmap.Can_write = X_Can_w;"    
            "mmap.Max_size = (int)X_Max;" 
            ""
            "FileAccess fa = FileAccess.Read;"
            "if (X_Can_w) fa = FileAccess.ReadWrite;"   
            "mmap.fs = new FileStream("
			"   	X_File, "
			"		FileMode.OpenOrCreate, fa, "
			"		FileShare.ReadWrite | FileShare.Delete);"
			"int lenfs = (int)mmap.fs.Length;"     
			""     
            "MemoryMappedFileAccess mmfa = MemoryMappedFileAccess.Read;"
            "if (X_Can_w) mmfa = MemoryMappedFileAccess.ReadWrite;"     
            "mmap.mmf = MemoryMappedFile.CreateFromFile("
            "	mmap.fs, null//mapName"
            "	, lenfs, mmfa, "
            "	null//memoryMappedFileSecurity"
            "	, HandleInheritability.None, true);"     
            "RESULT = h;" .
            
FUNCTION Close_memory_mapping(INT Handle|_mapping),
	NATIVE :
	DEBUG:<< "###Close_memory_mapping"#NL;
	NATIVE @
    "NATIVE_Memory_mapping mmap = "
	"	NATIVE_Memory_mapping.All_mmaps[(int)X_Handle];"
	"mmap.Unmap();"
	"mmap.mmf.Dispose();"     
	"mmap.mmf = null;"     
	"mmap.fs = null;"     
	"NATIVE_Memory_mapping.All_mmaps[(int)X_Handle] = null;" 
	.     

        CONST STR :
            NATIVE_Memory_mapping_import = @
            "using System.IO;"
            //"using System.Threading;"
            
			NATIVE_All_Memory_mappings = ""
			
			NATIVE_Memory_mapping_size = @
			"NATIVE_Memory_mapping mmap = "
			"	NATIVE_Memory_mapping.All_mmaps[(int)X_handle];"
			"FileInfo fi = new FileInfo(mmap.Filename);"
			"RESULT = (int)fi.Length;"     
            
            NATIVE_Memory_mapping_get = @
            "NATIVE_Memory_mapping mmap = "
			"	NATIVE_Memory_mapping.All_mmaps[(int)X_handle];"
			"mmap.Map((int)X_offset, (int)X_dest.Count);"
			"RESULT = 0;"     
			"if (mmap.Mapped != null) {"
			"	byte[] buf = new byte[X_dest.Count];"  
			"	try {"     
			"	    mmap.Mapped.ReadArray<byte>(0, buf, 0, X_dest.Count);"     
			"	    for (int i=0; i < X_dest.Count; i++) {"
			"		    X_dest.set_byte(i, buf[i]);"
			"		    RESULT += 1;"     
			"	    }"     
			"	}"
			"	catch (Exception) { }"     
			"}"

            NATIVE_Memory_mapping_put = @
            "NATIVE_Memory_mapping mmap = "
			"	NATIVE_Memory_mapping.All_mmaps[(int)X_handle];"
			"mmap.Map((int)X_offset, X_source.Count);"   
			"if (mmap.Mapped != null) {"
			"	byte[] buf = new byte[X_source.Count];"     
			"	for (int i=0; i < X_source.Count; i++) {"
			"		buf[i] = X_source.get_byte(i);"     
			"	}"     
			"	try {"
			"		mmap.Mapped.WriteArray<byte>(0, buf, 0, X_source.Count);"
			"		RESULT = X_source.Count;"     
			"	}"  
			"	catch (Exception) {"
			"		RESULT = 0;"     
			"	}"     
			"}"     

            .
            
            
        */


    }
}
/*
END
*/

