apt: apt-pkg/contrib/mmap.h Source File

apt 2.9.8 commandline package manager

mmap.h
1// -*- mode: cpp; mode: fold -*-
2// SPDX-License-Identifier: GPL-2.0+
3// Description /*{{{*/
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 environments 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 commit 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 efficient fashion.
19
20 This file had this historic note, but now includes further changes
21 under the GPL-2.0+:
22
23 This source is placed in the Public Domain, do with it what you will
24 It was originally written by Jason Gunthorpe.
25
26 ##################################################################### */
27 /*}}}*/
28#ifndef PKGLIB_MMAP_H
29#define PKGLIB_MMAP_H
30
31#include <string>
32#include <limits>
33
34#include <sys/mman.h>
35
36
37class FileFd;
38
39/* This should be a 32 bit type, larger types use too much ram and smaller
40 types are too small. Where ever possible 'unsigned long' should be used
41 instead of this internal type */
42typedef unsigned int map_ptrloc;
43
44 class MMap
45{
46 protected:
47
48 unsigned long Flags;
49 unsigned long long iSize;
50 void *Base;
51
52 // In case mmap can not be used, we keep a dup of the file
53 // descriptor that should have been mmaped so that we can write to
54 // the file in Sync().
55 FileFd *SyncToFd;
56
57 bool Map(FileFd &Fd);
58 bool Close(bool DoSync = true);
59
60 public:
61
62 enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
63 UnMapped = (1<<3), Moveable = (1<<4), Fallback = (1 << 5)};
64
65 // Simple accessors
66 inline operator void *() {return Base;};
67 inline void *Data() {return Base;};
68 inline unsigned long long Size() {return iSize;};
69 inline void AddSize(unsigned long long const size) {iSize += size;};
70 inline bool validData() const { return Base != MAP_FAILED && Base != 0; };
71
72 // File manipulators
73 bool Sync();
74 bool Sync(unsigned long Start,unsigned long Stop);
75
76 MMap(FileFd &F,unsigned long Flags);
77 explicit MMap(unsigned long Flags);
78 virtual ~MMap();
79};
80
81 class DynamicMMap : public MMap
82{
83 public:
84
85 // This is the allocation pool structure
86 struct Pool
87 {
88 unsigned long ItemSize;
89 unsigned long Start;
90 unsigned long Count;
91 };
92
93 protected:
94
95 FileFd *Fd;
96 unsigned long WorkSpace;
97 unsigned long const GrowFactor;
98 unsigned long const Limit;
99 Pool *Pools;
100 unsigned int PoolCount;
101
102 bool Grow();
103
104 public:
105
106 // Allocation
107 unsigned long RawAllocate(unsigned long long Size,unsigned long Aln = 0);
108 unsigned long Allocate(unsigned long ItemSize);
109 unsigned long WriteString(const char *String,unsigned long Len = std::numeric_limits<unsigned long>::max());
110 inline unsigned long WriteString(const std::string &S) {return WriteString(S.c_str(),S.length());};
111 void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
112
113 DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
114 unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
115 DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
116 unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
117 virtual ~DynamicMMap();
118};
119
120#endif
DynamicMMap
Definition mmap.h:82
FileFd
Definition fileutl.h:41
MMap
Definition mmap.h:45
MMap::Map
bool Map(FileFd &Fd)
Definition mmap.cc:65
DynamicMMap::Pool
Definition mmap.h:87