mmap.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: mmap.cc,v 1.3 1999/10/24 06:53:12 jgg Exp $
  4. /* ######################################################################
  5. MMap Class - Provides 'real' mmap or a faked mmap using read().
  6. MMap cover class.
  7. Some broken versions of glibc2 (libc6) have a broken definition
  8. of mmap that accepts a char * -- all other systems (and libc5) use
  9. void *. We can't safely do anything here that would be portable, so
  10. libc6 generates warnings -- which should be errors, g++ isn't properly
  11. strict.
  12. The configure test notes that some OS's have broken private mmap's
  13. so on those OS's we can't use mmap. This means we have to use
  14. configure to test mmap and can't rely on the POSIX
  15. _POSIX_MAPPED_FILES test.
  16. ##################################################################### */
  17. /*}}}*/
  18. // Include Files /*{{{*/
  19. #ifdef __GNUG__
  20. #pragma implementation "dsync/mmap.h"
  21. #endif
  22. #define _BSD_SOURCE
  23. #include <dsync/mmap.h>
  24. #include <dsync/error.h>
  25. #include <sys/mman.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <string.h>
  30. /*}}}*/
  31. // MMap::MMap - Constructor /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* */
  34. MMap::MMap(FileFd &F,unsigned long Flags) : Flags(Flags), iSize(0),
  35. Base(0)
  36. {
  37. if ((Flags & NoImmMap) != NoImmMap)
  38. Map(F);
  39. }
  40. /*}}}*/
  41. // MMap::MMap - Constructor /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* */
  44. MMap::MMap(unsigned long Flags) : Flags(Flags), iSize(0),
  45. Base(0)
  46. {
  47. }
  48. /*}}}*/
  49. // MMap::~MMap - Destructor /*{{{*/
  50. // ---------------------------------------------------------------------
  51. /* */
  52. MMap::~MMap()
  53. {
  54. Close();
  55. }
  56. /*}}}*/
  57. // MMap::Map - Perform the mapping /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* */
  60. bool MMap::Map(FileFd &Fd)
  61. {
  62. iSize = Fd.Size();
  63. // Set the permissions.
  64. int Prot = PROT_READ;
  65. int Map = MAP_SHARED;
  66. if ((Flags & ReadOnly) != ReadOnly)
  67. Prot |= PROT_WRITE;
  68. if ((Flags & Public) != Public)
  69. Map = MAP_PRIVATE;
  70. if (iSize == 0)
  71. return _error->Error("Can't mmap an empty file");
  72. // Map it.
  73. Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0);
  74. if (Base == (void *)-1)
  75. return _error->Errno("mmap","Couldn't make mmap of %u bytes",iSize);
  76. return true;
  77. }
  78. /*}}}*/
  79. // MMap::Close - Close the map /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* */
  82. bool MMap::Close(bool DoSync)
  83. {
  84. if ((Flags & UnMapped) == UnMapped || Base == 0 || iSize == 0)
  85. return true;
  86. if (DoSync == true)
  87. Sync();
  88. if (munmap((char *)Base,iSize) != 0)
  89. _error->Warning("Unable to munmap");
  90. iSize = 0;
  91. return true;
  92. }
  93. /*}}}*/
  94. // MMap::Sync - Syncronize the map with the disk /*{{{*/
  95. // ---------------------------------------------------------------------
  96. /* This is done in syncronous mode - the docs indicate that this will
  97. not return till all IO is complete */
  98. bool MMap::Sync()
  99. {
  100. if ((Flags & UnMapped) == UnMapped)
  101. return true;
  102. #ifdef _POSIX_SYNCHRONIZED_IO
  103. if ((Flags & ReadOnly) != ReadOnly)
  104. if (msync((char *)Base,iSize,MS_SYNC) != 0)
  105. return _error->Errno("msync","Unable to write mmap");
  106. #endif
  107. return true;
  108. }
  109. /*}}}*/
  110. // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
  111. // ---------------------------------------------------------------------
  112. /* */
  113. bool MMap::Sync(unsigned long Start,unsigned long Stop)
  114. {
  115. if ((Flags & UnMapped) == UnMapped)
  116. return true;
  117. #ifdef _POSIX_SYNCHRONIZED_IO
  118. unsigned long PSize = sysconf(_SC_PAGESIZE);
  119. if ((Flags & ReadOnly) != ReadOnly)
  120. if (msync((char *)Base+(int)(Start/PSize)*PSize,Stop - Start,MS_SYNC) != 0)
  121. return _error->Errno("msync","Unable to write mmap");
  122. #endif
  123. return true;
  124. }
  125. /*}}}*/
  126. // DynamicMMap::DynamicMMap - Constructor /*{{{*/
  127. // ---------------------------------------------------------------------
  128. /* */
  129. DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) :
  130. MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(WorkSpace)
  131. {
  132. if (_error->PendingError() == true)
  133. return;
  134. unsigned long EndOfFile = Fd->Size();
  135. Fd->Seek(WorkSpace);
  136. char C = 0;
  137. Fd->Write(&C,sizeof(C));
  138. Map(F);
  139. iSize = EndOfFile;
  140. }
  141. /*}}}*/
  142. // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* This is just a fancy malloc really.. */
  145. DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) :
  146. MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace)
  147. {
  148. if (_error->PendingError() == true)
  149. return;
  150. Base = new unsigned char[WorkSpace];
  151. iSize = 0;
  152. }
  153. /*}}}*/
  154. // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
  155. // ---------------------------------------------------------------------
  156. /* We truncate the file to the size of the memory data set */
  157. DynamicMMap::~DynamicMMap()
  158. {
  159. if (Fd == 0)
  160. {
  161. delete [] (unsigned char *)Base;
  162. return;
  163. }
  164. unsigned long EndOfFile = iSize;
  165. Sync();
  166. iSize = WorkSpace;
  167. Close(false);
  168. ftruncate(Fd->Fd(),EndOfFile);
  169. Fd->Close();
  170. }
  171. /*}}}*/
  172. // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
  173. // ---------------------------------------------------------------------
  174. /* This allocates a block of memory aligned to the given size */
  175. unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln)
  176. {
  177. unsigned long Result = iSize;
  178. if (Aln != 0)
  179. Result += Aln - (iSize%Aln);
  180. iSize = Result + Size;
  181. // Just in case error check
  182. if (Result + Size > WorkSpace)
  183. {
  184. _error->Error("Dynamic MMap ran out of room");
  185. return 0;
  186. }
  187. return Result;
  188. }
  189. /*}}}*/
  190. // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
  191. // ---------------------------------------------------------------------
  192. /* This allocates an Item of size ItemSize so that it is aligned to its
  193. size in the file. */
  194. unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
  195. {
  196. // Look for a matching pool entry
  197. Pool *I;
  198. Pool *Empty = 0;
  199. for (I = Pools; I != Pools + PoolCount; I++)
  200. {
  201. if (I->ItemSize == 0)
  202. Empty = I;
  203. if (I->ItemSize == ItemSize)
  204. break;
  205. }
  206. // No pool is allocated, use an unallocated one
  207. if (I == Pools + PoolCount)
  208. {
  209. // Woops, we ran out, the calling code should allocate more.
  210. if (Empty == 0)
  211. {
  212. _error->Error("Ran out of allocation pools");
  213. return 0;
  214. }
  215. I = Empty;
  216. I->ItemSize = ItemSize;
  217. I->Count = 0;
  218. }
  219. // Out of space, allocate some more
  220. if (I->Count == 0)
  221. {
  222. I->Count = 20*1024/ItemSize;
  223. I->Start = RawAllocate(I->Count*ItemSize,ItemSize);
  224. }
  225. I->Count--;
  226. unsigned long Result = I->Start;
  227. I->Start += ItemSize;
  228. return Result/ItemSize;
  229. }
  230. /*}}}*/
  231. // DynamicMMap::WriteString - Write a string to the file /*{{{*/
  232. // ---------------------------------------------------------------------
  233. /* Strings are not aligned to anything */
  234. unsigned long DynamicMMap::WriteString(const char *String,
  235. unsigned long Len)
  236. {
  237. unsigned long Result = iSize;
  238. // Just in case error check
  239. if (Result + Len > WorkSpace)
  240. {
  241. _error->Error("Dynamic MMap ran out of room");
  242. return 0;
  243. }
  244. if (Len == (unsigned long)-1)
  245. Len = strlen(String);
  246. iSize += Len + 1;
  247. memcpy((char *)Base + Result,String,Len);
  248. ((char *)Base)[Result + Len] = 0;
  249. return Result;
  250. }
  251. /*}}}*/