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