Support for memory-only caching
[ntk/apt.git] / apt-pkg / contrib / mmap.h
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
2d11135a 3// $Id: mmap.h,v 1.9 1999/04/18 06:36:36 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#ifdef __GNUG__
094a497d 29#pragma interface "apt-pkg/mmap.h"
6c139d6e
AL
30#endif
31
578bfd0a 32#include <string>
094a497d 33#include <apt-pkg/fileutl.h>
578bfd0a
AL
34
35class MMap
36{
37 protected:
38
2d11135a 39 unsigned long Flags;
578bfd0a
AL
40 unsigned long iSize;
41 void *Base;
42
2d11135a
AL
43 bool Map(FileFd &Fd);
44 bool Close(bool DoSync = true);
578bfd0a
AL
45
46 public:
47
2d11135a
AL
48 enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
49 UnMapped = (1<<3)};
578bfd0a
AL
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
8e06abb2 60 MMap(FileFd &F,unsigned long Flags);
2d11135a 61 MMap(unsigned long Flags);
578bfd0a
AL
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
2d11135a 79 FileFd *Fd;
578bfd0a
AL
80 unsigned long WorkSpace;
81 Pool *Pools;
82 unsigned int PoolCount;
83
84 public:
85
86 // Allocation
f55a958f 87 unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0);
578bfd0a
AL
88 unsigned long Allocate(unsigned long ItemSize);
89 unsigned long WriteString(const char *String,unsigned long Len = 0);
90 inline unsigned long WriteString(string S) {return WriteString(S.begin(),S.size());};
91 void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
92
e5eebd12 93 DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace = 2*1024*1024);
2d11135a 94 DynamicMMap(unsigned long Flags,unsigned long WorkSpace = 2*1024*1024);
578bfd0a
AL
95 virtual ~DynamicMMap();
96};
97
98#endif