]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/mmap.h
* fix segfault in the pkgRecords destructor
[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#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 34
422d9f6e
AL
35using std::string;
36
349cd3b8
AL
37/* This should be a 32 bit type, larger tyes use too much ram and smaller
38 types are too small. Where ever possible 'unsigned long' should be used
39 instead of this internal type */
40typedef unsigned int map_ptrloc;
41
578bfd0a
AL
42class MMap
43{
44 protected:
45
2d11135a 46 unsigned long Flags;
578bfd0a
AL
47 unsigned long iSize;
48 void *Base;
49
2d11135a
AL
50 bool Map(FileFd &Fd);
51 bool Close(bool DoSync = true);
578bfd0a
AL
52
53 public:
54
2d11135a
AL
55 enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
56 UnMapped = (1<<3)};
578bfd0a
AL
57
58 // Simple accessors
59 inline operator void *() {return Base;};
60 inline void *Data() {return Base;};
61 inline unsigned long Size() {return iSize;};
62
63 // File manipulators
64 bool Sync();
65 bool Sync(unsigned long Start,unsigned long Stop);
66
8e06abb2 67 MMap(FileFd &F,unsigned long Flags);
2d11135a 68 MMap(unsigned long Flags);
578bfd0a
AL
69 virtual ~MMap();
70};
71
72class DynamicMMap : public MMap
73{
74 public:
75
76 // This is the allocation pool structure
77 struct Pool
78 {
79 unsigned long ItemSize;
80 unsigned long Start;
81 unsigned long Count;
82 };
83
84 protected:
85
2d11135a 86 FileFd *Fd;
578bfd0a
AL
87 unsigned long WorkSpace;
88 Pool *Pools;
89 unsigned int PoolCount;
90
91 public:
92
93 // Allocation
f55a958f 94 unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0);
578bfd0a 95 unsigned long Allocate(unsigned long ItemSize);
6e52073f 96 unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1);
171c75f1 97 inline unsigned long WriteString(const string &S) {return WriteString(S.c_str(),S.length());};
6e52073f 98 void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
578bfd0a 99
e5eebd12 100 DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace = 2*1024*1024);
2d11135a 101 DynamicMMap(unsigned long Flags,unsigned long WorkSpace = 2*1024*1024);
578bfd0a
AL
102 virtual ~DynamicMMap();
103};
104
105#endif