|
apt @VERSION@
|
00001 // -*- mode: cpp; mode: fold -*- 00002 // Description /*{{{*/ 00003 // $Id: mmap.h,v 1.12 2001/05/14 05:16:43 jgg Exp $ 00004 /* ###################################################################### 00005 00006 MMap Class - Provides 'real' mmap or a faked mmap using read(). 00007 00008 The purpose of this code is to provide a generic way for clients to 00009 access the mmap function. In enviroments that do not support mmap 00010 from file fd's this function will use read and normal allocated 00011 memory. 00012 00013 Writing to a public mmap will always fully comit all changes when the 00014 class is deleted. Ie it will rewrite the file, unless it is readonly 00015 00016 The DynamicMMap class is used to help the on-disk data structure 00017 generators. It provides a large allocated workspace and members 00018 to allocate space from the workspace in an effecient fashion. 00019 00020 This source is placed in the Public Domain, do with it what you will 00021 It was originally written by Jason Gunthorpe. 00022 00023 ##################################################################### */ 00024 /*}}}*/ 00025 #ifndef PKGLIB_MMAP_H 00026 #define PKGLIB_MMAP_H 00027 00028 00029 #include <string> 00030 00031 class FileFd; 00032 00033 /* This should be a 32 bit type, larger tyes use too much ram and smaller 00034 types are too small. Where ever possible 'unsigned long' should be used 00035 instead of this internal type */ 00036 typedef unsigned int map_ptrloc; 00037 00038 class MMap 00039 { 00040 protected: 00041 00042 unsigned long Flags; 00043 unsigned long long iSize; 00044 void *Base; 00045 00046 // In case mmap can not be used, we keep a dup of the file 00047 // descriptor that should have been mmaped so that we can write to 00048 // the file in Sync(). 00049 FileFd *SyncToFd; 00050 00051 bool Map(FileFd &Fd); 00052 bool Close(bool DoSync = true); 00053 00054 public: 00055 00056 enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2), 00057 UnMapped = (1<<3), Moveable = (1<<4), Fallback = (1 << 5)}; 00058 00059 // Simple accessors 00060 inline operator void *() {return Base;}; 00061 inline void *Data() {return Base;}; 00062 inline unsigned long long Size() {return iSize;}; 00063 inline void AddSize(unsigned long long const size) {iSize += size;}; 00064 inline bool validData() const { return Base != (void *)-1 && Base != 0; }; 00065 00066 // File manipulators 00067 bool Sync(); 00068 bool Sync(unsigned long Start,unsigned long Stop); 00069 00070 MMap(FileFd &F,unsigned long Flags); 00071 MMap(unsigned long Flags); 00072 virtual ~MMap(); 00073 }; 00074 00075 class DynamicMMap : public MMap 00076 { 00077 public: 00078 00079 // This is the allocation pool structure 00080 struct Pool 00081 { 00082 unsigned long ItemSize; 00083 unsigned long Start; 00084 unsigned long Count; 00085 }; 00086 00087 protected: 00088 00089 FileFd *Fd; 00090 unsigned long WorkSpace; 00091 unsigned long const GrowFactor; 00092 unsigned long const Limit; 00093 Pool *Pools; 00094 unsigned int PoolCount; 00095 00096 bool Grow(); 00097 00098 public: 00099 00100 // Allocation 00101 unsigned long RawAllocate(unsigned long long Size,unsigned long Aln = 0); 00102 unsigned long Allocate(unsigned long ItemSize); 00103 unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1); 00104 inline unsigned long WriteString(const std::string &S) {return WriteString(S.c_str(),S.length());}; 00105 void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;}; 00106 00107 DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024, 00108 unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0); 00109 DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024, 00110 unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0); 00111 virtual ~DynamicMMap(); 00112 }; 00113 00114 #endif
1.7.4