public
Created

  • Download Gist
Corruption.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
{-# LANGUAGE BangPatterns, FlexibleInstances, MultiParamTypeClasses, DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies, CPP, MagicHash, UnboxedTuples #-}
{-# OPTIONS_GHC -O3 -Wall -fforce-recomp #-}
 
module Main (main) where
 
import Foreign.C.Types (CSize)
import Foreign.Storable
import Foreign.ForeignPtr
import Foreign.Marshal.Array (advancePtr, copyArray, moveArray)
 
import GHC.ForeignPtr (mallocPlainForeignPtrBytes)
import qualified GHC.ForeignPtr as FP
import GHC.Prim
import GHC.Exts
import GHC.Base
 
import Data.Word (Word8)
import qualified Data.Word as W
 
import Control.Monad.Primitive
import qualified Data.Primitive.Types as PT
import qualified Data.Primitive.ByteArray as BA
 
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Generic.Mutable as GM
import Data.Vector.Storable.Internal (getPtr, updPtr)
 
import qualified Data.Vector.Unboxed as UV
import qualified Data.Vector.Storable as SV
 
import Data.Typeable (Typeable)
 
data MVector s a = MVector {-# UNPACK #-} !Int
{-# UNPACK #-} !(ForeignPtr a)
deriving (Typeable)
 
instance Storable a => GM.MVector MVector a where
basicLength (MVector n _) = n
{-# INLINE basicLength #-}
 
basicUnsafeSlice j m (MVector _ fp) = MVector m (updPtr (`advancePtr` j) fp)
{-# INLINE basicUnsafeSlice #-}
 
-- FIXME: this relies on non-portable pointer comparisons
basicOverlaps (MVector m fp) (MVector n fq) =
between p q (q `advancePtr` n) || between q p (p `advancePtr` m)
where
between x y z = x >= y && x < z
p = getPtr fp
q = getPtr fq
{-# INLINE basicOverlaps #-}
 
basicUnsafeNew n = unsafePrimToPrim $ do
fp <- mallocVector n 16
return $ MVector n fp
{-# INLINE basicUnsafeNew #-}
{-# SPECIALIZE basicUnsafeNew ::
(Storable a, PrimMonad m) => Int -> m (MVector (PrimState m) a) #-}
 
basicUnsafeRead (MVector _ fp) i =
unsafePrimToPrim $ withForeignPtr fp (`peekElemOff` i)
{-# INLINE basicUnsafeRead #-}
 
basicUnsafeWrite (MVector _ fp) i x =
unsafePrimToPrim $ withForeignPtr fp $ \p -> pokeElemOff p i x
{-# INLINE basicUnsafeWrite #-}
 
basicUnsafeCopy (MVector n fp) (MVector _ fq) =
unsafePrimToPrim $ withForeignPtr fp $ \p ->
withForeignPtr fq $ \q ->
copyArray p q n
{-# INLINE basicUnsafeCopy #-}
 
basicUnsafeMove (MVector n fp) (MVector _ fq) =
unsafePrimToPrim $ withForeignPtr fp $ \p ->
withForeignPtr fq $ \q ->
moveArray p q n
{-# INLINE basicUnsafeMove #-}
 
 
mallocVectorMine1 :: Storable a => Int -> Int -> IO (ForeignPtr a)
mallocVectorMine1 = doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> Int -> IO (ForeignPtr b)
doMalloc b !l !align = do
!ba <- BA.newAlignedPinnedByteArray bytes align
let !(PT.Addr addr) = BA.mutableByteArrayContents ba
!ptr = Ptr addr
 
newForeignPtr_ ptr
where
bytes :: Int
!bytes = sizeOf b * l
{-# INLINE bytes #-}
{-# INLINE doMalloc #-}
{-# SPECIALIZE doMalloc :: W.Word8 -> Int -> Int -> IO (ForeignPtr W.Word8) #-}
{-# INLINE mallocVectorMine1 #-}
{-# SPECIALIZE mallocVectorMine1 :: Int -> Int -> IO (ForeignPtr W.Word8) #-}
 
mallocVectorMine2 :: Storable a => Int -> Int -> IO (ForeignPtr a)
mallocVectorMine2 = doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> Int -> IO (ForeignPtr b)
doMalloc b !(I# len) !(I# align) = IO $ \s0 ->
case newAlignedPinnedByteArray# bytes align s0 of { (# s1, mbarr# #) ->
let ptr = Ptr (byteArrayContents# (unsafeCoerce# mbarr#)) in
let (IO f) = newForeignPtr_ ptr in
case f s1 of { (# s2, fp# #) -> (# s2, fp# #) } }
where
!(I# size) = sizeOf b
{-# INLINE size #-}
!bytes = len *# size
{-# INLINE bytes #-}
{-# INLINE doMalloc #-}
{-# SPECIALIZE doMalloc :: W.Word8 -> Int -> Int -> IO (ForeignPtr W.Word8) #-}
{-# INLINE mallocVectorMine2 #-}
{-# SPECIALIZE mallocVectorMine2 :: Int -> Int -> IO (ForeignPtr W.Word8) #-}
mallocVectorOrig :: Storable a => Int -> Int -> IO (ForeignPtr a)
mallocVectorOrig =
doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> Int -> IO (ForeignPtr b)
doMalloc dummy size _ = mallocPlainForeignPtrBytes (size * sizeOf dummy)
{-# INLINE mallocVectorOrig #-}
 
#ifdef MALLOC_MINE3
mallocVectorMine3 :: Storable a => Int -> Int -> IO (ForeignPtr a)
mallocVectorMine3 =
doMalloc undefined
where
doMalloc :: Storable b => b -> Int -> Int -> IO (ForeignPtr b)
doMalloc dummy size align =
FP.mallocPlainForeignPtrAlignedBytes (size * sizeOf dummy) align
{-# INLINE doMalloc #-}
{-# SPECIALIZE doMalloc :: W.Word8 -> Int -> Int -> IO (ForeignPtr W.Word8) #-}
{-# INLINE mallocVectorMine3 #-}
{-# SPECIALIZE mallocVectorMine2 :: Int -> Int -> IO (ForeignPtr W.Word8) #-}
#endif
 
mallocVector :: Storable a => Int -> Int -> IO (ForeignPtr a)
#ifdef MALLOC_ORIG
mallocVector = mallocVectorOrig
#elif defined MALLOC_MINE1
mallocVector = mallocVectorMine1
#elif defined MALLOC_MINE2
mallocVector = mallocVectorMine2
#elif defined MALLOC_MINE3
mallocVector = mallocVectorMine3
#else
# error No malloc define
#endif
{-# INLINE mallocVector #-}
{-# SPECIALIZE mallocVector :: Int -> Int -> IO (ForeignPtr W.Word8) #-}
 
 
 
data Vector a = Vector {-# UNPACK #-} !Int
{-# UNPACK #-} !(ForeignPtr a)
deriving (Typeable)
 
type instance G.Mutable Vector = MVector
 
 
instance Storable a => G.Vector Vector a where
basicUnsafeFreeze (MVector n fp) = return $ Vector n fp
{-# INLINE basicUnsafeFreeze #-}
 
basicUnsafeThaw (Vector n fp) = return $ MVector n fp
{-# INLINE basicUnsafeThaw #-}
 
basicLength (Vector n _) = n
{-# INLINE basicLength #-}
 
basicUnsafeSlice i n (Vector _ fp) = Vector n (updPtr (`advancePtr` i) fp)
{-# INLINE basicUnsafeSlice #-}
 
basicUnsafeIndexM (Vector _ fp) i = return
. unsafeInlineIO
$ withForeignPtr fp $ \p ->
peekElemOff p i
{-# INLINE basicUnsafeIndexM #-}
 
basicUnsafeCopy (MVector n fp) (Vector _ fq) =
unsafePrimToPrim $ withForeignPtr fp $ \p ->
withForeignPtr fq $ \q ->
copyArray p q n
{-# INLINE basicUnsafeCopy #-}
 
elemseq _ = seq
{-# INLINE elemseq #-}
 
fromListN :: Storable a => Int -> [a] -> Vector a
fromListN = G.fromListN
{-# INLINE fromListN #-}
 
toList :: Storable a => Vector a -> [a]
toList = G.toList
{-# INLINE toList #-}
 
 
main :: IO ()
main = loop 0
where
loop n | n > 8192 = putStrLn "OK!"
| otherwise = do
print n
 
let uv = gen UV.fromListN n
sv = gen SV.fromListN n
mv = gen fromListN n :: Vector Word8
 
putStrLn $ "uv: " ++ show (UV.toList uv)
putStrLn $ "mv: " ++ show (toList mv)
 
if UV.toList uv /= (take n $ cycle [0 :: Word8 .. 255])
then print "Oops"
else return ()
 
if SV.toList sv /= (take n $ cycle [0 :: Word8 .. 255])
then print "Oop 2"
else return ()
 
if toList mv /= (take n $ cycle [0 :: Word8 .. 255])
then do
print "Oops3"
print $ zip3 [(0 :: Int) ..] (cycle [(0 :: Word8) .. 255]) (toList mv)
print $ head $ dropWhile (\(_, b, c) -> b == c) $ zip3 [(0 :: Int) ..] (cycle [(0 :: Word8) .. 255]) (toList mv)
else return ()
 
if (UV.toList uv /= toList mv)
then do
putStrLn $ "Fail at " ++ show n
putStrLn $ "mv: " ++ show (toList mv)
else loop (n + 16)
 
gen :: (Int -> [Word8] -> v Word8) -> Int -> v Word8
gen f n = a
where
!a = f n $ cycle [0 :: Word8 .. 255]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.