primitive-addr-0.1.0.3/0000755000000000000000000000000007346545000012762 5ustar0000000000000000primitive-addr-0.1.0.3/CHANGELOG.md0000644000000000000000000000042507346545000014574 0ustar0000000000000000# Revision history for primitive-addr ## 0.1.0.3 -- 2024-03-05 * Update package metadata. ## 0.1.0.2 -- 2019-07-18 * Allow building with `primitive-0.6.4.0`. ## 0.1.0.1 -- 2019-06-18 * Allow building with GHCs as old as 7.4.2. ## 0.1.0.0 -- 2019-05-17 * First version. primitive-addr-0.1.0.3/LICENSE0000644000000000000000000000276407346545000014000 0ustar0000000000000000Copyright (c) 2019, Andrew Martin All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Andrew Martin nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. primitive-addr-0.1.0.3/primitive-addr.cabal0000644000000000000000000000203407346545000016665 0ustar0000000000000000cabal-version: 3.0 name: primitive-addr version: 0.1.0.3 synopsis: Addresses to unmanaged memory description: This library provides the `Data.Primitive.Addr` module that was a part of the `primitive` library before `primitive-0.7.0.0`. homepage: https://github.com/byteverse/primitive-addr bug-reports: https://github.com/byteverse/primitive-addr/issues license: BSD-3-Clause license-file: LICENSE author: Andrew Martin maintainer: amartin@layer3com.com copyright: 2019 Andrew Martin category: Data extra-doc-files: CHANGELOG.md tested-with: GHC ==9.4.8 || ==9.6.3 || ==9.8.1 common build-settings default-language: Haskell2010 ghc-options: -Wall -Wunused-packages build-depends: base >=4.17.2.1 && <5 library import: build-settings hs-source-dirs: src exposed-modules: Data.Primitive.Addr build-depends: primitive >=0.6.4 && <0.10 source-repository head type: git location: git://github.com/byteverse/primitive-addr.git primitive-addr-0.1.0.3/src/Data/Primitive/0000755000000000000000000000000007346545000016372 5ustar0000000000000000primitive-addr-0.1.0.3/src/Data/Primitive/Addr.hs0000644000000000000000000001073407346545000017605 0ustar0000000000000000{-# LANGUAGE CPP #-} {-# LANGUAGE MagicHash #-} {-# LANGUAGE UnboxedTuples #-} {- FOURMOLU_DISABLE -} -- | Primitive operations on machine addresses. module Data.Primitive.Addr ( -- * Types Addr(..) -- * Address arithmetic , nullAddr , plusAddr , minusAddr , remAddr -- * Element access , indexOffAddr , readOffAddr , writeOffAddr -- * Block operations , copyAddr #if __GLASGOW_HASKELL__ >= 708 , copyAddrToByteArray #endif , moveAddr , setAddr -- * Conversion , addrToInt ) where {- FOURMOLU_ENABLE -} import Control.Monad.Primitive import Data.Primitive.ByteArray import Data.Primitive.Types (Prim (..)) import Numeric (showHex) import Foreign.Marshal.Utils import GHC.Exts #if __GLASGOW_HASKELL__ < 708 toBool# :: Bool -> Bool toBool# = id #else toBool# :: Int# -> Bool toBool# = isTrue# #endif -- | A machine address data Addr = Addr Addr# instance Show Addr where showsPrec _ (Addr a) = showString "0x" . showHex (fromIntegral (I# (addr2Int# a)) :: Word) instance Eq Addr where Addr a# == Addr b# = toBool# (eqAddr# a# b#) Addr a# /= Addr b# = toBool# (neAddr# a# b#) instance Ord Addr where Addr a# > Addr b# = toBool# (gtAddr# a# b#) Addr a# >= Addr b# = toBool# (geAddr# a# b#) Addr a# < Addr b# = toBool# (ltAddr# a# b#) Addr a# <= Addr b# = toBool# (leAddr# a# b#) -- | The null address nullAddr :: Addr nullAddr = Addr nullAddr# infixl 6 `plusAddr`, `minusAddr` infixl 7 `remAddr` -- | Offset an address by the given number of bytes plusAddr :: Addr -> Int -> Addr plusAddr (Addr a#) (I# i#) = Addr (plusAddr# a# i#) {- | Distance in bytes between two addresses. The result is only valid if the difference fits in an 'Int'. -} minusAddr :: Addr -> Addr -> Int minusAddr (Addr a#) (Addr b#) = I# (minusAddr# a# b#) -- | The remainder of the address and the integer. remAddr :: Addr -> Int -> Int remAddr (Addr a#) (I# i#) = I# (remAddr# a# i#) {- | Read a value from a memory position given by an address and an offset. The memory block the address refers to must be immutable. The offset is in elements of type @a@ rather than in bytes. -} indexOffAddr :: (Prim a) => Addr -> Int -> a {-# INLINE indexOffAddr #-} indexOffAddr (Addr addr#) (I# i#) = indexOffAddr# addr# i# {- | Read a value from a memory position given by an address and an offset. The offset is in elements of type @a@ rather than in bytes. -} readOffAddr :: (Prim a, PrimMonad m) => Addr -> Int -> m a {-# INLINE readOffAddr #-} readOffAddr (Addr addr#) (I# i#) = primitive (readOffAddr# addr# i#) {- | Write a value to a memory position given by an address and an offset. The offset is in elements of type @a@ rather than in bytes. -} writeOffAddr :: (Prim a, PrimMonad m) => Addr -> Int -> a -> m () {-# INLINE writeOffAddr #-} writeOffAddr (Addr addr#) (I# i#) x = primitive_ (writeOffAddr# addr# i# x) {- | Copy the given number of bytes from the second 'Addr' to the first. The areas may not overlap. -} copyAddr :: (PrimMonad m) => -- | destination address Addr -> -- | source address Addr -> -- | number of bytes Int -> m () {-# INLINE copyAddr #-} copyAddr (Addr dst#) (Addr src#) n = unsafePrimToPrim $ copyBytes (Ptr dst#) (Ptr src#) n #if __GLASGOW_HASKELL__ >= 708 -- | Copy the given number of bytes from the 'Addr' to the 'MutableByteArray'. -- The areas may not overlap. This function is only available when compiling -- with GHC 7.8 or newer. copyAddrToByteArray :: PrimMonad m => MutableByteArray (PrimState m) -- ^ destination -> Int -- ^ offset into the destination array -> Addr -- ^ source -> Int -- ^ number of bytes to copy -> m () {-# INLINE copyAddrToByteArray #-} copyAddrToByteArray (MutableByteArray marr) (I# off) (Addr addr) (I# len) = primitive_ $ copyAddrToByteArray# addr marr off len #endif {- | Copy the given number of bytes from the second 'Addr' to the first. The areas may overlap. -} moveAddr :: (PrimMonad m) => -- | destination address Addr -> -- | source address Addr -> -- | number of bytes Int -> m () {-# INLINE moveAddr #-} moveAddr (Addr dst#) (Addr src#) n = unsafePrimToPrim $ moveBytes (Ptr dst#) (Ptr src#) n {- | Fill a memory block of with the given value. The length is in elements of type @a@ rather than in bytes. -} setAddr :: (Prim a, PrimMonad m) => Addr -> Int -> a -> m () {-# INLINE setAddr #-} setAddr (Addr addr#) (I# n#) x = primitive_ (setOffAddr# addr# 0# n# x) -- | Convert an 'Addr' to an 'Int'. addrToInt :: Addr -> Int {-# INLINE addrToInt #-} addrToInt (Addr addr#) = I# (addr2Int# addr#)