]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/mmap.h
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: mmap.h,v 1.12 2001/05/14 05:16:43 jgg Exp $
4 /* ######################################################################
6 MMap Class - Provides 'real' mmap or a faked mmap using read().
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
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
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.
20 This source is placed in the Public Domain, do with it what you will
21 It was originally written by Jason Gunthorpe.
23 ##################################################################### */
31 #ifndef APT_8_CLEANER_HEADERS
32 #include <apt-pkg/fileutl.h>
38 /* This should be a 32 bit type, larger tyes use too much ram and smaller
39 types are too small. Where ever possible 'unsigned long' should be used
40 instead of this internal type */
41 typedef unsigned int map_ptrloc
;
48 unsigned long long iSize
;
51 // In case mmap can not be used, we keep a dup of the file
52 // descriptor that should have been mmaped so that we can write to
53 // the file in Sync().
57 bool Close(bool DoSync
= true);
61 enum OpenFlags
{Public
= (1<<1),ReadOnly
= (1<<2),
62 UnMapped
= (1<<3), Moveable
= (1<<4), Fallback
= (1 << 5)};
65 inline operator void *() {return Base
;};
66 inline void *Data() {return Base
;};
67 inline unsigned long long Size() {return iSize
;};
68 inline void AddSize(unsigned long long const size
) {iSize
+= size
;};
69 inline bool validData() const { return Base
!= (void *)-1 && Base
!= 0; };
73 bool Sync(unsigned long Start
,unsigned long Stop
);
75 MMap(FileFd
&F
,unsigned long Flags
);
76 MMap(unsigned long Flags
);
80 class DynamicMMap
: public MMap
84 // This is the allocation pool structure
87 unsigned long ItemSize
;
95 unsigned long WorkSpace
;
96 unsigned long const GrowFactor
;
97 unsigned long const Limit
;
99 unsigned int PoolCount
;
106 unsigned long RawAllocate(unsigned long long Size
,unsigned long Aln
= 0);
107 unsigned long Allocate(unsigned long ItemSize
);
108 unsigned long WriteString(const char *String
,unsigned long Len
= (unsigned long)-1);
109 inline unsigned long WriteString(const std::string
&S
) {return WriteString(S
.c_str(),S
.length());};
110 void UsePools(Pool
&P
,unsigned int Count
) {Pools
= &P
; PoolCount
= Count
;};
112 DynamicMMap(FileFd
&F
,unsigned long Flags
,unsigned long const &WorkSpace
= 2*1024*1024,
113 unsigned long const &Grow
= 1024*1024, unsigned long const &Limit
= 0);
114 DynamicMMap(unsigned long Flags
,unsigned long const &WorkSpace
= 2*1024*1024,
115 unsigned long const &Grow
= 1024*1024, unsigned long const &Limit
= 0);
116 virtual ~DynamicMMap();