Ticket #7067: 0001-Add-alignment-restricted-ForeignPtr-allocation-actio.patch

File 0001-Add-alignment-restricted-ForeignPtr-allocation-actio.patch, 2.8 KB (added by nicolast, 3 years ago)

Add alignment-restricted allocation actions to GHC.ForeignPtr

  • GHC/ForeignPtr.hs

    From c1b66db257c5b9dceaabf7563b9dd66864cbc2ad Mon Sep 17 00:00:00 2001
    From: Nicolas Trangez <[email protected]>
    Date: Wed, 11 Jul 2012 15:16:11 +0200
    Subject: [PATCH 1/1] Add alignment-restricted ForeignPtr allocation actions
    
    See: #7067
    See: http://hackage.haskell.org/trac/ghc/ticket/7067
    See: http://www.haskell.org/pipermail/glasgow-haskell-users/2012-July/022579.html
    ---
     GHC/ForeignPtr.hs |   29 +++++++++++++++++++++++++++++
     1 file changed, 29 insertions(+)
    
    diff --git a/GHC/ForeignPtr.hs b/GHC/ForeignPtr.hs
    index 0efd8b8..7d7d92a 100644
    a b module GHC.ForeignPtr 
    3434        mallocPlainForeignPtr,
    3535        mallocForeignPtrBytes,
    3636        mallocPlainForeignPtrBytes,
     37        mallocForeignPtrAlignedBytes,
     38        mallocPlainForeignPtrAlignedBytes,
    3739        addForeignPtrFinalizer,
    3840        addForeignPtrFinalizerEnv,
    3941        touchForeignPtr,
    mallocForeignPtrBytes (I# size) = do 
    183185                         (MallocPtr mbarr# r) #)
    184186     }
    185187
     188-- | This function is similar to 'mallocForeignPtrBytes', except that the
     189-- size and alignment of the memory required is given explicitly as numbers of
     190-- bytes.
     191mallocForeignPtrAlignedBytes :: Int -> Int -> IO (ForeignPtr a)
     192mallocForeignPtrAlignedBytes size alignment | size < 0 =
     193  error "mallocForeignPtrAlignedBytes: size must be >= 0"
     194mallocForeignPtrAlignedBytes (I# size) (I# alignment) = do
     195  r <- newIORef (NoFinalizers, [])
     196  IO $ \s ->
     197     case newAlignedPinnedByteArray# size alignment s of { (# s', mbarr# #) ->
     198       (# s', ForeignPtr (byteArrayContents# (unsafeCoerce# mbarr#))
     199                         (MallocPtr mbarr# r) #)
     200     }
     201
    186202-- | Allocate some memory and return a 'ForeignPtr' to it.  The memory
    187203-- will be released automatically when the 'ForeignPtr' is discarded.
    188204--
    mallocPlainForeignPtrBytes (I# size) = IO $ \s -> 
    222238                         (PlainPtr mbarr#) #)
    223239     }
    224240
     241-- | This function is similar to 'mallocForeignPtrAlignedBytes', except that
     242-- the internally an optimised ForeignPtr representation with no
     243-- finalizer is used. Attempts to add a finalizer will cause an
     244-- exception to be thrown.
     245mallocPlainForeignPtrAlignedBytes :: Int -> Int -> IO (ForeignPtr a)
     246mallocPlainForeignPtrAlignedBytes size alignment | size < 0 =
     247  error "mallocPlainForeignPtrAlignedBytes: size must be >= 0"
     248mallocPlainForeignPtrAlignedBytes (I# size) (I# alignment) = IO $ \s ->
     249    case newAlignedPinnedByteArray# size alignment s of { (# s', mbarr# #) ->
     250       (# s', ForeignPtr (byteArrayContents# (unsafeCoerce# mbarr#))
     251                         (PlainPtr mbarr#) #)
     252     }
     253
    225254addForeignPtrFinalizer :: FinalizerPtr a -> ForeignPtr a -> IO ()
    226255-- ^This function adds a finalizer to the given foreign object.  The
    227256-- finalizer will run /before/ all other finalizers for the same