Dsync merge
[ntk/apt.git] / apt-pkg / contrib / mmap.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: mmap.h,v 1.8 1999/01/18 06:20:08 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 #ifdef __GNUG__
29 #pragma interface "apt-pkg/mmap.h"
30 #endif
31
32 #include <string>
33 #include <apt-pkg/fileutl.h>
34
35 class MMap
36 {
37 protected:
38
39 FileFd &Fd;
40 unsigned long Flags;
41 unsigned long iSize;
42 void *Base;
43
44 bool Map();
45 bool Close(bool DoClose = true,bool DoSync = true);
46
47 public:
48
49 enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2)};
50
51 // Simple accessors
52 inline operator void *() {return Base;};
53 inline void *Data() {return Base;};
54 inline unsigned long Size() {return iSize;};
55
56 // File manipulators
57 bool Sync();
58 bool Sync(unsigned long Start,unsigned long Stop);
59
60 MMap(FileFd &F,unsigned long Flags);
61 virtual ~MMap();
62 };
63
64 class DynamicMMap : public MMap
65 {
66 public:
67
68 // This is the allocation pool structure
69 struct Pool
70 {
71 unsigned long ItemSize;
72 unsigned long Start;
73 unsigned long Count;
74 };
75
76 protected:
77
78 unsigned long WorkSpace;
79 Pool *Pools;
80 unsigned int PoolCount;
81
82 public:
83
84 // Allocation
85 unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0);
86 unsigned long Allocate(unsigned long ItemSize);
87 unsigned long WriteString(const char *String,unsigned long Len = 0);
88 inline unsigned long WriteString(string S) {return WriteString(S.begin(),S.size());};
89 void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
90
91 DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace = 2*1024*1024);
92 virtual ~DynamicMMap();
93 };
94
95 #endif