diff options
author | Joe Crayne <joe@jerkface.net> | 2018-08-10 22:30:48 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2018-08-10 22:30:48 -0400 |
commit | f4dd948176187f5fb46a2cf0dbfbfc4c32badfa5 (patch) | |
tree | 8fe92fc8471b1ad59943b651ab243d8f67ecf2f1 /bittorrent/src/System/Torrent/FileMap.hs | |
parent | e6c64ef4e5fc0870aacff06b4903b4cfa8666478 (diff) |
Commented some unused bittorrent related code.
Diffstat (limited to 'bittorrent/src/System/Torrent/FileMap.hs')
-rw-r--r-- | bittorrent/src/System/Torrent/FileMap.hs | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/bittorrent/src/System/Torrent/FileMap.hs b/bittorrent/src/System/Torrent/FileMap.hs index 6e8d7f5a..38c475e8 100644 --- a/bittorrent/src/System/Torrent/FileMap.hs +++ b/bittorrent/src/System/Torrent/FileMap.hs | |||
@@ -39,7 +39,7 @@ import Data.Torrent | |||
39 | 39 | ||
40 | data FileEntry = FileEntry | 40 | data FileEntry = FileEntry |
41 | { filePosition :: {-# UNPACK #-} !FileOffset | 41 | { filePosition :: {-# UNPACK #-} !FileOffset |
42 | , fileBytes :: {-# UNPACK #-} !BS.ByteString | 42 | , fileBytes :: {-# UNPACK #-} !BS.ByteString -- XXX: mutable buffer (see 'writeBytes'). |
43 | } deriving (Show, Eq) | 43 | } deriving (Show, Eq) |
44 | 44 | ||
45 | type FileMap = Vector FileEntry | 45 | type FileMap = Vector FileEntry |
@@ -62,6 +62,7 @@ unmapFiles = V.mapM_ unmapEntry | |||
62 | where | 62 | where |
63 | unmapEntry (FileEntry _ (PS fptr _ _)) = finalizeForeignPtr fptr | 63 | unmapEntry (FileEntry _ (PS fptr _ _)) = finalizeForeignPtr fptr |
64 | 64 | ||
65 | -- Unsafe: FileMap 'writeBytes' will modify supplied bytestrings in place. | ||
65 | fromLazyByteString :: BL.ByteString -> FileMap | 66 | fromLazyByteString :: BL.ByteString -> FileMap |
66 | fromLazyByteString lbs = V.unfoldr f (0, lbs) | 67 | fromLazyByteString lbs = V.unfoldr f (0, lbs) |
67 | where | 68 | where |
@@ -70,6 +71,8 @@ fromLazyByteString lbs = V.unfoldr f (0, lbs) | |||
70 | where chunkSize = fromIntegral $ BS.length x | 71 | where chunkSize = fromIntegral $ BS.length x |
71 | 72 | ||
72 | -- | /O(n)/. | 73 | -- | /O(n)/. |
74 | -- | ||
75 | -- Unsafe: mutable buffers are returned without copy. | ||
73 | toLazyByteString :: FileMap -> BL.ByteString | 76 | toLazyByteString :: FileMap -> BL.ByteString |
74 | toLazyByteString = V.foldr f Empty | 77 | toLazyByteString = V.foldr f Empty |
75 | where | 78 | where |
@@ -82,6 +85,7 @@ size m | |||
82 | | FileEntry {..} <- V.unsafeLast m | 85 | | FileEntry {..} <- V.unsafeLast m |
83 | = filePosition + fromIntegral (BS.length fileBytes) | 86 | = filePosition + fromIntegral (BS.length fileBytes) |
84 | 87 | ||
88 | -- | Find the file number for a particular byte offset within a torrent. | ||
85 | bsearch :: FileOffset -> FileMap -> Maybe Int | 89 | bsearch :: FileOffset -> FileMap -> Maybe Int |
86 | bsearch x m | 90 | bsearch x m |
87 | | V.null m = Nothing | 91 | | V.null m = Nothing |
@@ -114,12 +118,17 @@ take len m | |||
114 | s = System.Torrent.FileMap.size m | 118 | s = System.Torrent.FileMap.size m |
115 | 119 | ||
116 | -- | /O(log n + m)/. Do not use this function with 'unmapFiles'. | 120 | -- | /O(log n + m)/. Do not use this function with 'unmapFiles'. |
121 | -- | ||
122 | -- The returned bytestring points directly into an area memory mapped from a | ||
123 | -- file. | ||
117 | unsafeReadBytes :: FileOffset -> FileSize -> FileMap -> BL.ByteString | 124 | unsafeReadBytes :: FileOffset -> FileSize -> FileMap -> BL.ByteString |
118 | unsafeReadBytes off s m | 125 | unsafeReadBytes off s m |
119 | | (l , m') <- System.Torrent.FileMap.drop off m | 126 | | (l , m') <- System.Torrent.FileMap.drop off m |
120 | , (m'', _ ) <- System.Torrent.FileMap.take (off + s) m' | 127 | , (m'', _ ) <- System.Torrent.FileMap.take (off + s) m' |
121 | = BL.take (fromIntegral s) $ BL.drop (fromIntegral l) $ toLazyByteString m'' | 128 | = BL.take (fromIntegral s) $ BL.drop (fromIntegral l) $ toLazyByteString m'' |
122 | 129 | ||
130 | -- The returned bytestring is copied and safe to use after the file is | ||
131 | -- unmapped. | ||
123 | readBytes :: FileOffset -> FileSize -> FileMap -> IO BL.ByteString | 132 | readBytes :: FileOffset -> FileSize -> FileMap -> IO BL.ByteString |
124 | readBytes off s m = do | 133 | readBytes off s m = do |
125 | let bs_copy = BL.copy $ unsafeReadBytes off s m | 134 | let bs_copy = BL.copy $ unsafeReadBytes off s m |
@@ -129,6 +138,8 @@ readBytes off s m = do | |||
129 | forceLBS Empty = return () | 138 | forceLBS Empty = return () |
130 | forceLBS (Chunk _ x) = forceLBS x | 139 | forceLBS (Chunk _ x) = forceLBS x |
131 | 140 | ||
141 | -- UNSAFE: Uses the first byte string as a pointer to mutable data and writes | ||
142 | -- the contents of the second bytestring there. | ||
132 | bscpy :: BL.ByteString -> BL.ByteString -> IO () | 143 | bscpy :: BL.ByteString -> BL.ByteString -> IO () |
133 | bscpy (PS _ _ 0 `Chunk` dest_rest) src = bscpy dest_rest src | 144 | bscpy (PS _ _ 0 `Chunk` dest_rest) src = bscpy dest_rest src |
134 | bscpy dest (PS _ _ 0 `Chunk` src_rest) = bscpy dest src_rest | 145 | bscpy dest (PS _ _ 0 `Chunk` src_rest) = bscpy dest src_rest |
@@ -144,8 +155,9 @@ bscpy (PS dest_fptr dest_off dest_size `Chunk` dest_rest) | |||
144 | (PS src_fptr (src_off + csize) (src_size - csize) `Chunk` src_rest) | 155 | (PS src_fptr (src_off + csize) (src_size - csize) `Chunk` src_rest) |
145 | bscpy _ _ = return () | 156 | bscpy _ _ = return () |
146 | 157 | ||
158 | -- UNSAFE: Mutates bytestring contents within the provided FileMap. | ||
147 | writeBytes :: FileOffset -> BL.ByteString -> FileMap -> IO () | 159 | writeBytes :: FileOffset -> BL.ByteString -> FileMap -> IO () |
148 | writeBytes off lbs m = bscpy dest src | 160 | writeBytes off lbs m = bscpy dest src |
149 | where | 161 | where |
150 | src = BL.take (fromIntegral (BL.length dest)) lbs | 162 | src = BL.take (fromIntegral (BL.length dest)) lbs |
151 | dest = unsafeReadBytes off (fromIntegral (BL.length lbs)) m \ No newline at end of file | 163 | dest = unsafeReadBytes off (fromIntegral (BL.length lbs)) m |