mmap.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: mmap.h,v 1.12 2001/05/14 05:16:43 jgg Exp $
  4. /* ######################################################################
  5. MMap Class - Provides 'real' mmap or a faked mmap using read().
  6. The purpose of this code is to provide a generic way for clients to
  7. access the mmap function. In enviroments that do not support mmap
  8. from file fd's this function will use read and normal allocated
  9. memory.
  10. Writing to a public mmap will always fully comit all changes when the
  11. class is deleted. Ie it will rewrite the file, unless it is readonly
  12. The DynamicMMap class is used to help the on-disk data structure
  13. generators. It provides a large allocated workspace and members
  14. to allocate space from the workspace in an effecient fashion.
  15. This source is placed in the Public Domain, do with it what you will
  16. It was originally written by Jason Gunthorpe.
  17. ##################################################################### */
  18. /*}}}*/
  19. #ifndef PKGLIB_MMAP_H
  20. #define PKGLIB_MMAP_H
  21. #include <string>
  22. #include <apt-pkg/fileutl.h>
  23. using std::string;
  24. /* This should be a 32 bit type, larger tyes use too much ram and smaller
  25. types are too small. Where ever possible 'unsigned long' should be used
  26. instead of this internal type */
  27. typedef unsigned int map_ptrloc;
  28. class MMap
  29. {
  30. protected:
  31. unsigned long Flags;
  32. unsigned long iSize;
  33. void *Base;
  34. // In case mmap can not be used, we keep a dup of the file
  35. // descriptor that should have been mmaped so that we can write to
  36. // the file in Sync().
  37. FileFd *SyncToFd;
  38. bool Map(FileFd &Fd);
  39. bool Close(bool DoSync = true);
  40. public:
  41. enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
  42. UnMapped = (1<<3), Moveable = (1<<4), Fallback = (1 << 5)};
  43. // Simple accessors
  44. inline operator void *() {return Base;};
  45. inline void *Data() {return Base;};
  46. inline unsigned long Size() {return iSize;};
  47. inline void AddSize(unsigned long const size) {iSize += size;};
  48. inline bool validData() const { return Base != (void *)-1 && Base != 0; };
  49. // File manipulators
  50. bool Sync();
  51. bool Sync(unsigned long Start,unsigned long Stop);
  52. MMap(FileFd &F,unsigned long Flags);
  53. MMap(unsigned long Flags);
  54. virtual ~MMap();
  55. };
  56. class DynamicMMap : public MMap
  57. {
  58. public:
  59. // This is the allocation pool structure
  60. struct Pool
  61. {
  62. unsigned long ItemSize;
  63. unsigned long Start;
  64. unsigned long Count;
  65. };
  66. protected:
  67. FileFd *Fd;
  68. unsigned long WorkSpace;
  69. unsigned long const GrowFactor;
  70. unsigned long const Limit;
  71. Pool *Pools;
  72. unsigned int PoolCount;
  73. bool Grow();
  74. public:
  75. // Allocation
  76. unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0);
  77. unsigned long Allocate(unsigned long ItemSize);
  78. unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1);
  79. inline unsigned long WriteString(const string &S) {return WriteString(S.c_str(),S.length());};
  80. void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
  81. DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
  82. unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
  83. DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
  84. unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
  85. virtual ~DynamicMMap();
  86. };
  87. #endif