//定义了内存映射有关的平台无关的接口
//作者:刘成彬
//适合平台:WindowsXP, WindowsCE, Linux, Android
#ifndef _MINI_SUN_PLATFORM_MEM_MAP_H_
#define _MINI_SUN_PLATFORM_MEM_MAP_H_
/////////////
//对Android平台,也认为是linux
#ifdef ANDROID
#define linux ANDROID
#endif
//包含头文件
#ifdef _WIN32
#include
#endif
#ifdef linux
#include
#include
#include
#endif
//创建文件的参数和返回值
#ifdef _WIN32
typedef HANDLE TypeRetFile;
#endif
#ifdef linux
typedef int TypeRetFile;
#endif
#ifdef _WIN32
typedef LPCTSTR TypeFileNameArg;
#endif
#ifdef linux
typedef char * TypeFileNameArg;
#endif
#ifdef _WIN32
typedef DWORD TypeAccessMode;
#endif
#ifdef linux
typedef int TypeAccessMode;
#endif
//建立内存映射函数参数类型:TypeArg
#ifdef _WIN32
typedef HANDLE TypeRetAddr;
#endif
#ifdef linux
typedef void * TypeRetAddr;
#endif
#ifdef _WIN32
typedef DWORD TypeProtArg;
#endif
#ifdef linux
typedef int TypeProtArg;
#endif
//销毁和同步内存映射函数参数类型:TypeArg
#ifdef _WIN32
typedef LPCVOID TypeAddrArg;
#endif
#ifdef linux
typedef void * TypeAddrArg;
#endif
#ifdef _WIN32
typedef DWORD TypeMemSizeArg;
#endif
#ifdef linux
typedef size_t TypeMemSizeArg;
#endif
//读方式创建文件
//(TypeRetFile)CreateFile(TypeFileNameArg n,TypeAccessMode m,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
#ifdef _WIN32
#define openRFile(h,n) (((h)=CreateFile(n,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL))!=NULL)
#endif
#ifdef linux
#define openRFile(h,n) ((h)=open(n,O_RDONLY))
#endif
//写方式创建文件
//(TypeRetFile)CreateFile(TypeFileNameArg n,TypeAccessMode m,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL);
#ifdef _WIN32
#define openWFile(h,n) (((h)=CreateFile(n,GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_SEQUENTIAL_SCAN,NULL))!=NULL)
#endif
#ifdef linux
#define openWFile(h,n) (((h)=open(n,O_RDWR)))
#endif
//读方式建立内存映射
//TypeRetAddr CreateFileMapping(TypeRetFile f,NULL,TypeProtArg p,0,TypeMemSizeArg l,NULL);
#ifdef _WIN32
#define CreateRMMap(f,l) ((CreateFileMapping(f,NULL,PAGE_READONLY,0,l,NULL)))
#endif
#ifdef linux
#define CreateRMMap(f,l) (mmap(NULL, l, PROT_READ, MAP_PRIVATE,f,0))
#endif
//写方式建立内存映射
//TypeRetAddr CreateFileMapping(TypeRetFile f,NULL,TypeProtArg p,0,TypeMemSizeArg l,NULL);
#ifdef _WIN32
#define CreateWMMap(f,l) ((CreateFileMapping(f,NULL,PAGE_READWRITE,0,l,NULL)))
#endif
#ifdef linux
#define CreateWMMap(f,l) (mmap(NULL, l, PROT_WRITE, MAP_PRIVATE,f,0))
#endif
//读映射内存数据
//TypeAddrArg MapViewOfFile(TypeRetAddr h,FILE_MAP_READ,NULL,NULL,TypeMemSizeArg l);
#ifdef _WIN32
#define ReadMMap(a,h,l) ((a)=MapViewOfFile(h,FILE_MAP_READ,NULL,NULL,l))
#endif
#ifdef linux
#define ReadMMap(a,h,l) (memcpy((unsigned char *)a,(unsigned char *)h,l))
#endif
//写映射内存数据
//TypeAddrArg MapViewOfFile(TypeRetAddr h,FILE_MAP_WRITE,NULL,NULL,TypeMemSizeArg l );
#ifdef _WIN32
#define WriteMMap(a,h,l) ((a)=MapViewOfFile(h,FILE_MAP_WRITE,NULL,NULL,l ))
#endif
#ifdef linux
#define WriteMMap(a,h,l) (memcpy((unsigned char *)a,(unsigned char *)h,l))
#endif
//销毁内存映射
//BOOL UnmapViewOfFile(TypeAddrArg a);
//int munmap(TypeAddrArg a,TypeMemSizeArg l);
#ifdef _WIN32
#define MMapDestroy(a,l) (UnmapViewOfFile(a))
#endif
#ifdef linux
#define MMapDestroy(a,l) (munmap(a,l)>0)
#endif
//同步内存映射
//BOOL FlushViewOfFile(TypeAddrArg a, TypeMemSizeArg l);
#ifdef _WIN32
#define MMapSync(a,l) (FlushViewOfFile(a,l))
#endif
#ifdef linux
#define MMapSync(a,l) (!msync(a,l,MS_SYNC))
#endif
//关闭文件句柄和返回句柄
#ifdef _WIN32
#define close(a) (CloseHandle(a))
#endif
#ifdef linux
#define close(a) (close(a))
#endif
#endif //_MINI_SUN_PLATFORM_MEM_MAP_H_