apt @VERSION@

mmap.h

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 #include <apt-pkg/fileutl.h>
00031 
00032 using std::string;
00033 
00034 /* This should be a 32 bit type, larger tyes use too much ram and smaller
00035    types are too small. Where ever possible 'unsigned long' should be used
00036    instead of this internal type */
00037 typedef unsigned int map_ptrloc;
00038 
00039 class MMap
00040 {
00041    protected:
00042    
00043    unsigned long Flags;
00044    unsigned long iSize;
00045    void *Base;
00046 
00047    // In case mmap can not be used, we keep a dup of the file
00048    // descriptor that should have been mmaped so that we can write to
00049    // the file in Sync().
00050    FileFd *SyncToFd;
00051 
00052    bool Map(FileFd &Fd);
00053    bool Close(bool DoSync = true);
00054    
00055    public:
00056 
00057    enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
00058                    UnMapped = (1<<3), Moveable = (1<<4), Fallback = (1 << 5)};
00059       
00060    // Simple accessors
00061    inline operator void *() {return Base;};
00062    inline void *Data() {return Base;}; 
00063    inline unsigned long Size() {return iSize;};
00064    inline void AddSize(unsigned long const size) {iSize += size;};
00065    inline bool validData() const { return Base != (void *)-1 && Base != 0; };
00066    
00067    // File manipulators
00068    bool Sync();
00069    bool Sync(unsigned long Start,unsigned long Stop);
00070    
00071    MMap(FileFd &F,unsigned long Flags);
00072    MMap(unsigned long Flags);
00073    virtual ~MMap();
00074 };
00075 
00076 class DynamicMMap : public MMap
00077 {
00078    public:
00079    
00080    // This is the allocation pool structure
00081    struct Pool
00082    {
00083       unsigned long ItemSize;
00084       unsigned long Start;
00085       unsigned long Count;
00086    };
00087    
00088    protected:
00089    
00090    FileFd *Fd;
00091    unsigned long WorkSpace;
00092    unsigned long const GrowFactor;
00093    unsigned long const Limit;
00094    Pool *Pools;
00095    unsigned int PoolCount;
00096 
00097    bool Grow();
00098    
00099    public:
00100 
00101    // Allocation
00102    unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0);
00103    unsigned long Allocate(unsigned long ItemSize);
00104    unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1);
00105    inline unsigned long WriteString(const string &S) {return WriteString(S.c_str(),S.length());};
00106    void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
00107    
00108    DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
00109                unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
00110    DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
00111                unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
00112    virtual ~DynamicMMap();
00113 };
00114 
00115 #endif