mmap.h 2.5 KB

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