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