apt: apt-pkg/contrib/mmap.h Source File

apt 2.3.15 commandline package manager

mmap.h
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
33class 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 */
38typedef 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().
51 FileFd *SyncToFd;
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
91 FileFd *Fd;
92 unsigned long WorkSpace;
93 unsigned long const GrowFactor;
94 unsigned long const Limit;
95 Pool *Pools;
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
DynamicMMap
Definition: mmap.h:78
FileFd
Definition: fileutl.h:39
MMap
Definition: mmap.h:41
MMap::Map
bool Map(FileFd &Fd)
Definition: mmap.cc:64
DynamicMMap::Pool
Definition: mmap.h:83