mmap.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: mmap.cc,v 1.22 2001/05/27 05:19:30 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. ##################################################################### */
  13. /*}}}*/
  14. // Include Files /*{{{*/
  15. #define _BSD_SOURCE
  16. #include <apt-pkg/mmap.h>
  17. #include <apt-pkg/error.h>
  18. #include <apti18n.h>
  19. #include <sys/mman.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <stdlib.h>
  24. #include <cstring>
  25. /*}}}*/
  26. // MMap::MMap - Constructor /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* */
  29. MMap::MMap(FileFd &F,unsigned long Flags) : Flags(Flags), iSize(0),
  30. Base(0)
  31. {
  32. if ((Flags & NoImmMap) != NoImmMap)
  33. Map(F);
  34. }
  35. /*}}}*/
  36. // MMap::MMap - Constructor /*{{{*/
  37. // ---------------------------------------------------------------------
  38. /* */
  39. MMap::MMap(unsigned long Flags) : Flags(Flags), iSize(0),
  40. Base(0)
  41. {
  42. }
  43. /*}}}*/
  44. // MMap::~MMap - Destructor /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* */
  47. MMap::~MMap()
  48. {
  49. Close();
  50. }
  51. /*}}}*/
  52. // MMap::Map - Perform the mapping /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* */
  55. bool MMap::Map(FileFd &Fd)
  56. {
  57. iSize = Fd.Size();
  58. // Set the permissions.
  59. int Prot = PROT_READ;
  60. int Map = MAP_SHARED;
  61. if ((Flags & ReadOnly) != ReadOnly)
  62. Prot |= PROT_WRITE;
  63. if ((Flags & Public) != Public)
  64. Map = MAP_PRIVATE;
  65. if (iSize == 0)
  66. return _error->Error(_("Can't mmap an empty file"));
  67. // Map it.
  68. Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0);
  69. if (Base == (void *)-1)
  70. return _error->Errno("mmap",_("Couldn't make mmap of %lu bytes"),iSize);
  71. return true;
  72. }
  73. /*}}}*/
  74. // MMap::Close - Close the map /*{{{*/
  75. // ---------------------------------------------------------------------
  76. /* */
  77. bool MMap::Close(bool DoSync)
  78. {
  79. if ((Flags & UnMapped) == UnMapped || Base == 0 || iSize == 0)
  80. return true;
  81. if (DoSync == true)
  82. Sync();
  83. if (munmap((char *)Base,iSize) != 0)
  84. _error->Warning("Unable to munmap");
  85. iSize = 0;
  86. Base = 0;
  87. return true;
  88. }
  89. /*}}}*/
  90. // MMap::Sync - Syncronize the map with the disk /*{{{*/
  91. // ---------------------------------------------------------------------
  92. /* This is done in syncronous mode - the docs indicate that this will
  93. not return till all IO is complete */
  94. bool MMap::Sync()
  95. {
  96. if ((Flags & UnMapped) == UnMapped)
  97. return true;
  98. #ifdef _POSIX_SYNCHRONIZED_IO
  99. if ((Flags & ReadOnly) != ReadOnly)
  100. if (msync((char *)Base,iSize,MS_SYNC) < 0)
  101. return _error->Errno("msync","Unable to write mmap");
  102. #endif
  103. return true;
  104. }
  105. /*}}}*/
  106. // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
  107. // ---------------------------------------------------------------------
  108. /* */
  109. bool MMap::Sync(unsigned long Start,unsigned long Stop)
  110. {
  111. if ((Flags & UnMapped) == UnMapped)
  112. return true;
  113. #ifdef _POSIX_SYNCHRONIZED_IO
  114. unsigned long PSize = sysconf(_SC_PAGESIZE);
  115. if ((Flags & ReadOnly) != ReadOnly)
  116. if (msync((char *)Base+(int)(Start/PSize)*PSize,Stop - Start,MS_SYNC) < 0)
  117. return _error->Errno("msync","Unable to write mmap");
  118. #endif
  119. return true;
  120. }
  121. /*}}}*/
  122. // DynamicMMap::DynamicMMap - Constructor /*{{{*/
  123. // ---------------------------------------------------------------------
  124. /* */
  125. DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long WorkSpace) :
  126. MMap(F,Flags | NoImmMap), Fd(&F), WorkSpace(WorkSpace)
  127. {
  128. if (_error->PendingError() == true)
  129. return;
  130. unsigned long EndOfFile = Fd->Size();
  131. if (EndOfFile > WorkSpace)
  132. WorkSpace = EndOfFile;
  133. else if(WorkSpace > 0)
  134. {
  135. Fd->Seek(WorkSpace - 1);
  136. char C = 0;
  137. Fd->Write(&C,sizeof(C));
  138. }
  139. Map(F);
  140. iSize = EndOfFile;
  141. }
  142. /*}}}*/
  143. // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
  144. // ---------------------------------------------------------------------
  145. /* We try here to use mmap to reserve some space - this is much more
  146. cooler than the fallback solution to simply allocate a char array
  147. and could come in handy later than we are able to grow such an mmap */
  148. DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long WorkSpace) :
  149. MMap(Flags | NoImmMap | UnMapped), Fd(0), WorkSpace(WorkSpace)
  150. {
  151. if (_error->PendingError() == true)
  152. return;
  153. #ifdef _POSIX_MAPPED_FILES
  154. // Set the permissions.
  155. int Prot = PROT_READ;
  156. int Map = MAP_PRIVATE | MAP_ANONYMOUS;
  157. if ((Flags & ReadOnly) != ReadOnly)
  158. Prot |= PROT_WRITE;
  159. if ((Flags & Public) == Public)
  160. Map = MAP_SHARED | MAP_ANONYMOUS;
  161. // use anonymous mmap() to get the memory
  162. Base = (unsigned char*) mmap(0, WorkSpace, Prot, Map, -1, 0);
  163. if(Base == MAP_FAILED)
  164. _error->Errno("DynamicMMap",_("Couldn't make mmap of %lu bytes"),WorkSpace);
  165. #else
  166. // fallback to a static allocated space
  167. Base = new unsigned char[WorkSpace];
  168. memset(Base,0,WorkSpace);
  169. #endif
  170. iSize = 0;
  171. }
  172. /*}}}*/
  173. // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
  174. // ---------------------------------------------------------------------
  175. /* We truncate the file to the size of the memory data set */
  176. DynamicMMap::~DynamicMMap()
  177. {
  178. if (Fd == 0)
  179. {
  180. #ifdef _POSIX_MAPPED_FILES
  181. munmap(Base, WorkSpace);
  182. #else
  183. delete [] (unsigned char *)Base;
  184. #endif
  185. return;
  186. }
  187. unsigned long EndOfFile = iSize;
  188. iSize = WorkSpace;
  189. Close(false);
  190. if(ftruncate(Fd->Fd(),EndOfFile) < 0)
  191. _error->Errno("ftruncate", _("Failed to truncate file"));
  192. }
  193. /*}}}*/
  194. // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
  195. // ---------------------------------------------------------------------
  196. /* This allocates a block of memory aligned to the given size */
  197. unsigned long DynamicMMap::RawAllocate(unsigned long Size,unsigned long Aln)
  198. {
  199. unsigned long Result = iSize;
  200. if (Aln != 0)
  201. Result += Aln - (iSize%Aln);
  202. iSize = Result + Size;
  203. // try to grow the buffer
  204. while(Result + Size > WorkSpace)
  205. {
  206. if(!Grow())
  207. {
  208. _error->Error(_("Dynamic MMap ran out of room. Please increase the size "
  209. "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace);
  210. return 0;
  211. }
  212. }
  213. return Result;
  214. }
  215. /*}}}*/
  216. // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
  217. // ---------------------------------------------------------------------
  218. /* This allocates an Item of size ItemSize so that it is aligned to its
  219. size in the file. */
  220. unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
  221. {
  222. // Look for a matching pool entry
  223. Pool *I;
  224. Pool *Empty = 0;
  225. for (I = Pools; I != Pools + PoolCount; I++)
  226. {
  227. if (I->ItemSize == 0)
  228. Empty = I;
  229. if (I->ItemSize == ItemSize)
  230. break;
  231. }
  232. // No pool is allocated, use an unallocated one
  233. if (I == Pools + PoolCount)
  234. {
  235. // Woops, we ran out, the calling code should allocate more.
  236. if (Empty == 0)
  237. {
  238. _error->Error("Ran out of allocation pools");
  239. return 0;
  240. }
  241. I = Empty;
  242. I->ItemSize = ItemSize;
  243. I->Count = 0;
  244. }
  245. unsigned long Result = 0;
  246. // Out of space, allocate some more
  247. if (I->Count == 0)
  248. {
  249. const unsigned long size = 20*1024;
  250. I->Count = size/ItemSize;
  251. Result = RawAllocate(size,ItemSize);
  252. // Does the allocation failed ?
  253. if (Result == 0 && _error->PendingError())
  254. return 0;
  255. I->Start = Result;
  256. }
  257. else
  258. Result = I->Start;
  259. I->Count--;
  260. I->Start += ItemSize;
  261. return Result/ItemSize;
  262. }
  263. /*}}}*/
  264. // DynamicMMap::WriteString - Write a string to the file /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* Strings are not aligned to anything */
  267. unsigned long DynamicMMap::WriteString(const char *String,
  268. unsigned long Len)
  269. {
  270. if (Len == (unsigned long)-1)
  271. Len = strlen(String);
  272. unsigned long Result = RawAllocate(Len+1,0);
  273. if (Result == 0 && _error->PendingError())
  274. return 0;
  275. memcpy((char *)Base + Result,String,Len);
  276. ((char *)Base)[Result + Len] = 0;
  277. return Result;
  278. }
  279. /*}}}*/
  280. // DynamicMMap::Grow - Grow the mmap /*{{{*/
  281. // ---------------------------------------------------------------------
  282. /* This method will try to grow the mmap we currently use. This doesn't
  283. work most of the time because we can't move the mmap around in the
  284. memory for now as this would require to adjust quite a lot of pointers
  285. but why we should not at least try to grow it before we give up? */
  286. bool DynamicMMap::Grow()
  287. {
  288. #if defined(_POSIX_MAPPED_FILES) && defined(__linux__)
  289. unsigned long newSize = WorkSpace + 1024*1024;
  290. if(Fd != 0)
  291. {
  292. Fd->Seek(newSize - 1);
  293. char C = 0;
  294. Fd->Write(&C,sizeof(C));
  295. }
  296. Base = mremap(Base, WorkSpace, newSize, 0);
  297. if(Base == MAP_FAILED)
  298. return false;
  299. WorkSpace = newSize;
  300. return true;
  301. #else
  302. return false;
  303. #endif
  304. }
  305. /*}}}*/