]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.h
2bf2c15408319b3693a1e6b4dcf87e82e6ddb073
[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 #include <apt-pkg/fileutl.h>
31
32 using std::string;
33
34 /* This should be a 32 bit type, larger tyes use too much ram and smaller
35 types are too small. Where ever possible 'unsigned long' should be used
36 instead of this internal type */
37 typedef unsigned int map_ptrloc;
38
39 class MMap
40 {
41 protected:
42
43 unsigned long Flags;
44 unsigned long iSize;
45 void *Base;
46
47 // In case mmap can not be used, we keep a dup of the file
48 // descriptor that should have been mmaped so that we can write to
49 // the file in Sync().
50 FileFd *SyncToFd;
51
52 bool Map(FileFd &Fd);
53 bool Close(bool DoSync = true);
54
55 public:
56
57 enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
58 UnMapped = (1<<3), Moveable = (1<<4), Fallback = (1 << 5)};
59
60 // Simple accessors
61 inline operator void *() {return Base;};
62 inline void *Data() {return Base;};
63 inline unsigned long Size() {return iSize;};
64 inline void AddSize(unsigned long const size) {iSize += size;};
65 inline bool validData() const { return Base != (void *)-1 && Base != 0; };
66
67 // File manipulators
68 bool Sync();
69 bool Sync(unsigned long Start,unsigned long Stop);
70
71 MMap(FileFd &F,unsigned long Flags);
72 MMap(unsigned long Flags);
73 virtual ~MMap();
74 };
75
76 class DynamicMMap : public MMap
77 {
78 public:
79
80 // This is the allocation pool structure
81 struct Pool
82 {
83 unsigned long ItemSize;
84 unsigned long Start;
85 unsigned long Count;
86 };
87
88 protected:
89
90 FileFd *Fd;
91 unsigned long WorkSpace;
92 unsigned long const GrowFactor;
93 unsigned long const Limit;
94 Pool *Pools;
95 unsigned int PoolCount;
96
97 bool Grow();
98
99 public:
100
101 // Allocation
102 unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0);
103 unsigned long Allocate(unsigned long ItemSize);
104 unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1);
105 inline unsigned long WriteString(const string &S) {return WriteString(S.c_str(),S.length());};
106 void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
107
108 DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
109 unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
110 DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
111 unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
112 virtual ~DynamicMMap();
113 };
114
115 #endif