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