/****************************************************************************
* gmmap.h :
* header file for platform-independent memory mapping.
*
* Change Log :
* June 16, 1998 Initial version, Liujian Qian
*
* $Id: gmmap.h,v 1.9 1999/02/15 21:50:58 qian Exp $
***************************************************************************/
#ifndef _G_MMAP_H_
#define _G_MMAP_H_
#include <unistd.h>
#if defined(_SUNOS_) || defined(_IRIX_) || defined(_LINUX_)
#include <sys/types.h>
#include <sys/mman.h>
#endif
// linux specific stuff.
#ifdef _LINUX_
#define madvise(addr,len,advice)
#endif
/**
* portable memory mapping class.
*
*/
class GMMap
{
bool mapped; //true if the object is currently mapped.
int flag;
int protection;
int fd; //file desc.
off_t offset; //offset into the file
off_t last_offset;
size_t map_len; //length of the mmaped portion of the file
size_t last_map_len;
char file[256]; //path for the file to be mapped.
void* start_hint; //hint where in memory should be mmap'ed. 0
void* map_region; //start of actual map'ed memory address
void* last_map_region;
public:
/**
* default constructor. It needs the name of the file to
* be mapped. The constructor does not acutally open the file and
* map it into memory. Use the mehtod map for this purpose.
*
* @param file is the file to be mapped.
*/
GMMap(const char* file);
/**
* destructor
* unmap and close the file if still mapped.
*/
~GMMap();
/**
* map the portion [offset, offset+len] of 'file' to memory.
* If the file is already mapped, it will be unmap()'ed first
* before re-mapping it.
* returns 1 if succeed; otherwise 0.
*
* @param ps is the protection string similar to the access flag
* of the open(). It can be "r", "w", "x" and the
* combinations.
*/
int map(off_t off, size_t len, const char* ps=0);
/**
* map the full length of the file. This is equivalent to
* map(0, file_length, ps).
*/
int mapAll(const char* ps=0);
/**
* extend the file and mmap the file again.
* If off and len are both negative then simply
* mmap() the whole extended file. This is useful
* when the file needs to grow in size while being
* mapped in memory.
*
* @param size_inc is the size to extend the file.
*/
int extendMap(int size_inc, long off=-1, long len=-1);
/**
* unmap the file and release the mapped region.
* returns 1 if success; otherwise 0.
*/
int unmap();
/**
* gives Operating system some advice about the mapped memory usage.
*
* @param advice is the advice to be given to the VM system.
* possible advices are:
* MADV_NORMAL (0x0)
* MADV_RNADOM (0x1)
* MADV_SEQUENTIAl (0x2)
* MADV_WILLNEED (0x3)
* MADV_DONTNEED (0x4)
* return 1 if success, otherwise 0.
*/
int advise(int advice);
/**
* set the flag for mapping.
* the flag affects only the next mmap() calls.
* Default is "s" for SHARED mapping.
* Other settings are:
* "p" for PRIVATE,
* "f" for FIXED,
* "n" for NORESEVE.
*/
void setFlag(const char* fs );
/**
* set the protection for mapping.
* the flag affects only the next mmap() call.
* Default is PROT_READ|PROT_WRITE.
*/
void setProtection(const char* ps);
/**
* return the pointer to the start position of mmaped memory.
*/
void* ptr() ;
/**
* return the length of the current mmaped region.
* access beyond the ptr()+len() is illeagl.
*/
int len() const;
/**
* return the file descriptor
*/
int fileDesc() const {return fd;}
};
#endif
Documentation generated by lqian@lqian-sun on Wed Jul 14 09:36:10 EDT 1999