]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
422d9f6e | 3 | // $Id: mmap.h,v 1.12 2001/05/14 05:16:43 jgg Exp $ |
578bfd0a AL |
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 | /*}}}*/ | |
578bfd0a AL |
25 | #ifndef PKGLIB_MMAP_H |
26 | #define PKGLIB_MMAP_H | |
27 | ||
6c139d6e | 28 | |
578bfd0a | 29 | #include <string> |
094a497d | 30 | #include <apt-pkg/fileutl.h> |
578bfd0a | 31 | |
422d9f6e AL |
32 | using std::string; |
33 | ||
349cd3b8 AL |
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 | ||
578bfd0a AL |
39 | class MMap |
40 | { | |
41 | protected: | |
42 | ||
2d11135a | 43 | unsigned long Flags; |
578bfd0a AL |
44 | unsigned long iSize; |
45 | void *Base; | |
46 | ||
2d11135a AL |
47 | bool Map(FileFd &Fd); |
48 | bool Close(bool DoSync = true); | |
578bfd0a AL |
49 | |
50 | public: | |
51 | ||
2d11135a | 52 | enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2), |
d6c4a976 | 53 | UnMapped = (1<<3), Moveable = (1<<4), Fallback = (1 << 5)}; |
578bfd0a AL |
54 | |
55 | // Simple accessors | |
56 | inline operator void *() {return Base;}; | |
57 | inline void *Data() {return Base;}; | |
58 | inline unsigned long Size() {return iSize;}; | |
59 | ||
60 | // File manipulators | |
61 | bool Sync(); | |
62 | bool Sync(unsigned long Start,unsigned long Stop); | |
63 | ||
8e06abb2 | 64 | MMap(FileFd &F,unsigned long Flags); |
2d11135a | 65 | MMap(unsigned long Flags); |
578bfd0a AL |
66 | virtual ~MMap(); |
67 | }; | |
68 | ||
69 | class DynamicMMap : public MMap | |
70 | { | |
71 | public: | |
72 | ||
73 | // This is the allocation pool structure | |
74 | struct Pool | |
75 | { | |
76 | unsigned long ItemSize; | |
77 | unsigned long Start; | |
78 | unsigned long Count; | |
79 | }; | |
80 | ||
81 | protected: | |
82 | ||
2d11135a | 83 | FileFd *Fd; |
578bfd0a | 84 | unsigned long WorkSpace; |
d6c4a976 DK |
85 | unsigned long const GrowFactor; |
86 | unsigned long const Limit; | |
578bfd0a AL |
87 | Pool *Pools; |
88 | unsigned int PoolCount; | |
f1c6a8ca DK |
89 | |
90 | bool Grow(); | |
578bfd0a AL |
91 | |
92 | public: | |
93 | ||
94 | // Allocation | |
f55a958f | 95 | unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0); |
578bfd0a | 96 | unsigned long Allocate(unsigned long ItemSize); |
6e52073f | 97 | unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1); |
171c75f1 | 98 | inline unsigned long WriteString(const string &S) {return WriteString(S.c_str(),S.length());}; |
6e52073f | 99 | void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;}; |
578bfd0a | 100 | |
d6c4a976 DK |
101 | DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024, |
102 | unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0); | |
103 | DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024, | |
104 | unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0); | |
578bfd0a AL |
105 | virtual ~DynamicMMap(); |
106 | }; | |
107 | ||
108 | #endif |