mmap.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 environments 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 efficient 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. #ifndef APT_8_CLEANER_HEADERS
  23. #include <apt-pkg/fileutl.h>
  24. using std::string;
  25. #endif
  26. class FileFd;
  27. /* This should be a 32 bit type, larger tyes use too much ram and smaller
  28. types are too small. Where ever possible 'unsigned long' should be used
  29. instead of this internal type */
  30. typedef unsigned int map_ptrloc;
  31. class MMap
  32. {
  33. protected:
  34. unsigned long Flags;
  35. unsigned long long iSize;
  36. void *Base;
  37. // In case mmap can not be used, we keep a dup of the file
  38. // descriptor that should have been mmaped so that we can write to
  39. // the file in Sync().
  40. FileFd *SyncToFd;
  41. bool Map(FileFd &Fd);
  42. bool Close(bool DoSync = true);
  43. public:
  44. enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
  45. UnMapped = (1<<3), Moveable = (1<<4), Fallback = (1 << 5)};
  46. // Simple accessors
  47. inline operator void *() {return Base;};
  48. inline void *Data() {return Base;};
  49. inline unsigned long long Size() {return iSize;};
  50. inline void AddSize(unsigned long long const size) {iSize += size;};
  51. inline bool validData() const { return Base != (void *)-1 && Base != 0; };
  52. // File manipulators
  53. bool Sync();
  54. bool Sync(unsigned long Start,unsigned long Stop);
  55. MMap(FileFd &F,unsigned long Flags);
  56. MMap(unsigned long Flags);
  57. virtual ~MMap();
  58. };
  59. class DynamicMMap : public MMap
  60. {
  61. public:
  62. // This is the allocation pool structure
  63. struct Pool
  64. {
  65. unsigned long ItemSize;
  66. unsigned long Start;
  67. unsigned long Count;
  68. };
  69. protected:
  70. FileFd *Fd;
  71. unsigned long WorkSpace;
  72. unsigned long const GrowFactor;
  73. unsigned long const Limit;
  74. Pool *Pools;
  75. unsigned int PoolCount;
  76. bool Grow();
  77. public:
  78. // Allocation
  79. unsigned long RawAllocate(unsigned long long Size,unsigned long Aln = 0);
  80. unsigned long Allocate(unsigned long ItemSize);
  81. unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1);
  82. inline unsigned long WriteString(const std::string &S) {return WriteString(S.c_str(),S.length());};
  83. void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
  84. DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
  85. unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
  86. DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace = 2*1024*1024,
  87. unsigned long const &Grow = 1024*1024, unsigned long const &Limit = 0);
  88. virtual ~DynamicMMap();
  89. };
  90. #endif