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