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