mmap.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #ifdef __GNUG__
  22. #endif
  23. #include <string>
  24. #include <apt-pkg/fileutl.h>
  25. using std::string;
  26. /* This should be a 32 bit type, larger tyes use too much ram and smaller
  27. types are too small. Where ever possible 'unsigned long' should be used
  28. instead of this internal type */
  29. typedef unsigned int map_ptrloc;
  30. class MMap
  31. {
  32. protected:
  33. unsigned long Flags;
  34. unsigned long iSize;
  35. void *Base;
  36. bool Map(FileFd &Fd);
  37. bool Close(bool DoSync = true);
  38. public:
  39. enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2),
  40. UnMapped = (1<<3)};
  41. // Simple accessors
  42. inline operator void *() {return Base;};
  43. inline void *Data() {return Base;};
  44. inline unsigned long Size() {return iSize;};
  45. // File manipulators
  46. bool Sync();
  47. bool Sync(unsigned long Start,unsigned long Stop);
  48. MMap(FileFd &F,unsigned long Flags);
  49. MMap(unsigned long Flags);
  50. virtual ~MMap();
  51. };
  52. class DynamicMMap : public MMap
  53. {
  54. public:
  55. // This is the allocation pool structure
  56. struct Pool
  57. {
  58. unsigned long ItemSize;
  59. unsigned long Start;
  60. unsigned long Count;
  61. };
  62. protected:
  63. FileFd *Fd;
  64. unsigned long WorkSpace;
  65. Pool *Pools;
  66. unsigned int PoolCount;
  67. public:
  68. // Allocation
  69. unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0);
  70. unsigned long Allocate(unsigned long ItemSize);
  71. unsigned long WriteString(const char *String,unsigned long Len = (unsigned long)-1);
  72. inline unsigned long WriteString(const string &S) {return WriteString(S.c_str(),S.length());};
  73. void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
  74. DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace = 2*1024*1024);
  75. DynamicMMap(unsigned long Flags,unsigned long WorkSpace = 2*1024*1024);
  76. virtual ~DynamicMMap();
  77. };
  78. #endif