mmap.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: mmap.h,v 1.8 1999/01/18 06:20:08 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. #pragma interface "apt-pkg/mmap.h"
  23. #endif
  24. #include <string>
  25. #include <apt-pkg/fileutl.h>
  26. class MMap
  27. {
  28. protected:
  29. FileFd &Fd;
  30. unsigned long Flags;
  31. unsigned long iSize;
  32. void *Base;
  33. bool Map();
  34. bool Close(bool DoClose = true,bool DoSync = true);
  35. public:
  36. enum OpenFlags {NoImmMap = (1<<0),Public = (1<<1),ReadOnly = (1<<2)};
  37. // Simple accessors
  38. inline operator void *() {return Base;};
  39. inline void *Data() {return Base;};
  40. inline unsigned long Size() {return iSize;};
  41. // File manipulators
  42. bool Sync();
  43. bool Sync(unsigned long Start,unsigned long Stop);
  44. MMap(FileFd &F,unsigned long Flags);
  45. virtual ~MMap();
  46. };
  47. class DynamicMMap : public MMap
  48. {
  49. public:
  50. // This is the allocation pool structure
  51. struct Pool
  52. {
  53. unsigned long ItemSize;
  54. unsigned long Start;
  55. unsigned long Count;
  56. };
  57. protected:
  58. unsigned long WorkSpace;
  59. Pool *Pools;
  60. unsigned int PoolCount;
  61. public:
  62. // Allocation
  63. unsigned long RawAllocate(unsigned long Size,unsigned long Aln = 0);
  64. unsigned long Allocate(unsigned long ItemSize);
  65. unsigned long WriteString(const char *String,unsigned long Len = 0);
  66. inline unsigned long WriteString(string S) {return WriteString(S.begin(),S.size());};
  67. void UsePools(Pool &P,unsigned int Count) {Pools = &P; PoolCount = Count;};
  68. DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace = 2*1024*1024);
  69. virtual ~DynamicMMap();
  70. };
  71. #endif