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