apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

mmap.h
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  MMap Class - Provides 'real' mmap or a faked mmap using read().
6 
7  The purpose of this code is to provide a generic way for clients to
8  access the mmap function. In environments that do not support mmap
9  from file fd's this function will use read and normal allocated
10  memory.
11 
12  Writing to a public mmap will always fully commit all changes when the
13  class is deleted. Ie it will rewrite the file, unless it is readonly
14 
15  The DynamicMMap class is used to help the on-disk data structure
16  generators. It provides a large allocated workspace and members
17  to allocate space from the workspace in an efficient fashion.
18 
19  This source is placed in the Public Domain, do with it what you will
20  It was originally written by Jason Gunthorpe.
21 
22  ##################################################################### */
23  /*}}}*/
24 #ifndef PKGLIB_MMAP_H
25 #define PKGLIB_MMAP_H
26 
27 #include <string>
28 #include <limits>
29 
30 #include <sys/mman.h>
31 
32 
33 class FileFd;
34 
35 /* This should be a 32 bit type, larger types use too much ram and smaller
36  types are too small. Where ever possible 'unsigned long' should be used
37  instead of this internal type */
38 typedef unsigned int map_ptrloc;
39 
40 class MMap
41 {
42  protected:
43 
44  unsigned long Flags;
45  unsigned long long iSize;
46  void *Base;
47 
48  // In case mmap can not be used, we keep a dup of the file
49  // descriptor that should have been mmaped so that we can write to
50  // the file in Sync().
52 
53  bool Map(FileFd &Fd);
54  bool Close(bool DoSync = true);
55 
56  public:
57 
58  enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
59  UnMapped = (1<<3), Moveable = (1<<4), Fallback = (1 << 5)};
60 
61  // Simple accessors
62  inline operator void *() {return Base;};
63  inline void *Data() {return Base;};
64  inline unsigned long long Size() {return iSize;};
65  inline void AddSize(unsigned long long const size) {iSize += size;};
66  inline bool validData() const { return Base != MAP_FAILED && Base != 0; };
67 
68  // File manipulators
69  bool Sync();
70  bool Sync(unsigned long Start,unsigned long Stop);
71 
72  MMap(FileFd &F,unsigned long Flags);
73  explicit MMap(unsigned long Flags);
74  virtual ~MMap();
75 };
76 
77 class DynamicMMap : public MMap
78 {
79  public:
80 
81  // This is the allocation pool structure
82  struct Pool
83  {
84  unsigned long ItemSize;
85  unsigned long Start;
86  unsigned long Count;
87  };
88 
89  protected:
90 
92  unsigned long WorkSpace;
93  unsigned long const GrowFactor;
94  unsigned long const Limit;
96  unsigned int PoolCount;
97 
98  bool Grow();
99 
100  public:
101 
102  // Allocation
103  unsigned long RawAllocate(unsigned long long Size,unsigned long Aln = 0);
104  unsigned long Allocate(unsigned long ItemSize);
105  unsigned long WriteString(const char *String,unsigned long Len = std::numeric_limits<unsigned long>::max());
106  inline unsigned long WriteString(const std::string &S) {return WriteString(S.c_str(),S.length());};
107  void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
108 
109  DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
110  unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
111  DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
112  unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
113  virtual ~DynamicMMap();
114 };
115 
116 #endif
unsigned long WriteString(const std::string &S)
Definition: mmap.h:106
bool Grow()
Definition: mmap.cc:455
unsigned long WorkSpace
Definition: mmap.h:92
FileFd * Fd
Definition: mmap.h:91
unsigned int PoolCount
Definition: mmap.h:96
unsigned long const GrowFactor
Definition: mmap.h:93
unsigned long RawAllocate(unsigned long long Size, unsigned long Aln=0)
Definition: mmap.cc:325
unsigned long const Limit
Definition: mmap.h:94
virtual ~DynamicMMap()
Definition: mmap.cc:301
unsigned long WriteString(const char *String, unsigned long Len=std::numeric_limits< unsigned long >::max())
Definition: mmap.cc:414
unsigned long Allocate(unsigned long ItemSize)
Definition: mmap.cc:350
DynamicMMap(FileFd &F, unsigned long Flags, unsigned long const &WorkSpace=2 *1024 *1024, unsigned long const &Grow=1024 *1024, unsigned long const &Limit=0)
Definition: mmap.cc:216
Pool * Pools
Definition: mmap.h:95
void UsePools(Pool &P, unsigned int Count)
Definition: mmap.h:107
Definition: fileutl.h:39
Definition: mmap.h:41
bool Sync()
Definition: mmap.cc:161
void AddSize(unsigned long long const size)
Definition: mmap.h:65
virtual ~MMap()
Definition: mmap.cc:56
unsigned long Flags
Definition: mmap.h:44
bool Close(bool DoSync=true)
Definition: mmap.cc:132
bool validData() const
Definition: mmap.h:66
FileFd * SyncToFd
Definition: mmap.h:51
unsigned long long iSize
Definition: mmap.h:45
OpenFlags
Definition: mmap.h:58
@ NoImmMap
Definition: mmap.h:58
@ Public
Definition: mmap.h:58
@ Fallback
Definition: mmap.h:59
@ Moveable
Definition: mmap.h:59
@ UnMapped
Definition: mmap.h:59
@ ReadOnly
Definition: mmap.h:58
void * Data()
Definition: mmap.h:63
bool Map(FileFd &Fd)
Definition: mmap.cc:64
void * Base
Definition: mmap.h:46
MMap(FileFd &F, unsigned long Flags)
Definition: mmap.cc:38
unsigned long long Size()
Definition: mmap.h:64
unsigned int map_ptrloc
Definition: mmap.h:33
unsigned long Start
Definition: mmap.h:85
unsigned long Count
Definition: mmap.h:86
unsigned long ItemSize
Definition: mmap.h:84