]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
6e52073f | 3 | // $Id: mmap.h,v 1.11 1999/08/02 03:07:47 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 | #ifdef __GNUG__ |
094a497d | 29 | #pragma interface "apt-pkg/mmap.h" |
6c139d6e AL |
30 | #endif |
31 | ||
578bfd0a | 32 | #include <string> |
094a497d | 33 | #include <apt-pkg/fileutl.h> |
578bfd0a | 34 | |
349cd3b8 AL |
35 | /* This should be a 32 bit type, larger tyes 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 | ||
578bfd0a AL |
40 | class MMap |
41 | { | |
42 | protected: | |
43 | ||
2d11135a | 44 | unsigned long Flags; |
578bfd0a AL |
45 | unsigned long iSize; |
46 | void *Base; | |
47 | ||
2d11135a AL |
48 | bool Map(FileFd &Fd); |
49 | bool Close(bool DoSync = true); | |
578bfd0a AL |
50 | |
51 | public: | |
52 | ||
2d11135a AL |
53 | enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2), |
54 | UnMapped = (1<<3)}; | |
578bfd0a AL |
55 | |
56 | // Simple accessors | |
57 | inline operator void *() {return Base;}; | |
58 | inline void *Data() {return Base;}; | |
59 | inline unsigned long Size() {return iSize;}; | |
60 | ||
61 | // File manipulators | |
62 | bool Sync(); | |
63 | bool Sync(unsigned long Start,unsigned long Stop); | |
64 | ||
8e06abb2 | 65 | MMap(FileFd &F,unsigned long Flags); |
2d11135a | 66 | MMap(unsigned long Flags); |
578bfd0a AL |
67 | virtual ~MMap(); |
68 | }; | |
69 | ||
70 | class DynamicMMap : public MMap | |
71 | { | |
72 | public: | |
73 | ||
74 | // This is the allocation pool structure | |
75 | struct Pool | |
76 | { | |
77 | unsigned long ItemSize; | |
78 | unsigned long Start; | |
79 | unsigned long Count; | |
80 | }; | |
81 | ||
82 | protected: | |
83 | ||
2d11135a | 84 | FileFd *Fd; |
578bfd0a AL |
85 | unsigned long WorkSpace; |
86 | Pool *Pools; | |
87 | unsigned int PoolCount; | |
88 | ||
89 | public: | |
90 | ||
91 | // Allocation | |
f55a958f | 92 | unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0); |
578bfd0a | 93 | unsigned long Allocate(unsigned long ItemSize); |
6e52073f | 94 | unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1); |
578bfd0a | 95 | inline unsigned long WriteString(string S) {return WriteString(S.begin(),S.size());}; |
6e52073f | 96 | void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;}; |
578bfd0a | 97 | |
e5eebd12 | 98 | DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace = 2*1024*1024); |
2d11135a | 99 | DynamicMMap(unsigned long Flags,unsigned long WorkSpace = 2*1024*1024); |
578bfd0a AL |
100 | virtual ~DynamicMMap(); |
101 | }; | |
102 | ||
103 | #endif |