|
| 1 | +{-# LANGUAGE CApiFFI #-} |
| 2 | +{-# LANGUAGE DuplicateRecordFields #-} |
| 3 | +{-# LANGUAGE NoFieldSelectors #-} |
| 4 | +{-# LANGUAGE OverloadedRecordDot #-} |
| 5 | + |
| 6 | +module ZeroCopy where |
| 7 | + |
| 8 | +import Data.Array.Byte (ByteArray) |
| 9 | +import Data.Kind |
| 10 | +import Data.Proxy |
| 11 | +import Foreign |
| 12 | +import Foreign.C |
| 13 | +import GHC.Exts |
| 14 | +import GHC.Records qualified as GHC |
| 15 | + |
| 16 | +import HsBindgen.Runtime.ByteArray qualified as ByteArray |
| 17 | +import HsBindgen.Runtime.ConstantArray (ConstantArray) |
| 18 | +import HsBindgen.Runtime.SizedByteArray (SizedByteArray(..)) |
| 19 | + |
| 20 | +{------------------------------------------------------------------------------- |
| 21 | + Infrastructure (this would live in hs-bindgen-runtime) |
| 22 | +-------------------------------------------------------------------------------} |
| 23 | + |
| 24 | +class Storable (FieldType a field) |
| 25 | + => HasCField (a :: Type) (field :: Symbol) where |
| 26 | + type FieldType a field :: Type |
| 27 | + ptrToField :: Proxy field -> Ptr a -> Ptr (FieldType a field) |
| 28 | + |
| 29 | +pokeField :: |
| 30 | + HasCField a field |
| 31 | + => Proxy field -> Ptr a -> FieldType a field -> IO () |
| 32 | +pokeField field ptr val = poke (ptrToField field ptr) val |
| 33 | + |
| 34 | +peekField :: |
| 35 | + HasCField a field |
| 36 | + => Proxy field -> Ptr a -> IO (FieldType a field) |
| 37 | +peekField field ptr = peek (ptrToField field ptr) |
| 38 | + |
| 39 | +{------------------------------------------------------------------------------- |
| 40 | + Example (this would be generated code) |
| 41 | +-------------------------------------------------------------------------------} |
| 42 | + |
| 43 | +data Point = Point { |
| 44 | + x :: CInt |
| 45 | + , y :: CInt |
| 46 | + } |
| 47 | + deriving stock (Show, Eq) |
| 48 | + |
| 49 | +data Rect = Rect { |
| 50 | + topleft :: Point |
| 51 | + , bottomright :: Point |
| 52 | + } |
| 53 | + deriving stock (Show, Eq) |
| 54 | + |
| 55 | +instance HasCField Point "x" where |
| 56 | + type FieldType Point "x" = CInt |
| 57 | + ptrToField _ ptr = ptr `plusPtr` 0 |
| 58 | + |
| 59 | +instance HasCField Point "y" where |
| 60 | + type FieldType Point "y" = CInt |
| 61 | + ptrToField _ ptr = ptr `plusPtr` 4 |
| 62 | + |
| 63 | +instance HasCField Rect "topleft" where |
| 64 | + type FieldType Rect "topleft" = Point |
| 65 | + ptrToField _ ptr = ptr `plusPtr` 0 |
| 66 | + |
| 67 | +instance HasCField Rect "bottomright" where |
| 68 | + type FieldType Rect "bottomright" = Point |
| 69 | + ptrToField _ ptr = ptr `plusPtr` 8 |
| 70 | + |
| 71 | +{------------------------------------------------------------------------------- |
| 72 | + Storable instance |
| 73 | +-------------------------------------------------------------------------------} |
| 74 | + |
| 75 | +instance Storable Point where |
| 76 | + sizeOf _ = 8 |
| 77 | + alignment _ = 4 |
| 78 | + |
| 79 | + peek ptr = |
| 80 | + pure Point |
| 81 | + <*> peekField (Proxy @"x") ptr |
| 82 | + <*> peekField (Proxy @"y") ptr |
| 83 | + |
| 84 | + poke ptr Point{x, y} = do |
| 85 | + pokeField (Proxy @"x") ptr x |
| 86 | + pokeField (Proxy @"y") ptr y |
| 87 | + |
| 88 | +instance Storable Rect where |
| 89 | + sizeOf _ = 16 |
| 90 | + alignment _ = 4 |
| 91 | + |
| 92 | + peek ptr = |
| 93 | + pure Rect |
| 94 | + <*> peekField (Proxy @"topleft") ptr |
| 95 | + <*> peekField (Proxy @"bottomright") ptr |
| 96 | + |
| 97 | + poke ptr Rect{topleft, bottomright} = do |
| 98 | + pokeField (Proxy @"topleft") ptr topleft |
| 99 | + pokeField (Proxy @"bottomright") ptr bottomright |
| 100 | + |
| 101 | +{------------------------------------------------------------------------------- |
| 102 | + (Optional) integration with 'HasField' |
| 103 | +-------------------------------------------------------------------------------} |
| 104 | + |
| 105 | +instance (HasCField Point field, ty ~ FieldType Point field) |
| 106 | + => GHC.HasField field (Ptr Point) (Ptr ty) where |
| 107 | + getField = ptrToField (Proxy @field) |
| 108 | + |
| 109 | +instance (HasCField Rect field, ty ~ FieldType Rect field) |
| 110 | + => GHC.HasField field (Ptr Rect) (Ptr ty) where |
| 111 | + getField = ptrToField (Proxy @field) |
| 112 | + |
| 113 | +{------------------------------------------------------------------------------- |
| 114 | + Function imports are unchanged (omitting CAPI for simplicity) |
| 115 | +-------------------------------------------------------------------------------} |
| 116 | + |
| 117 | +foreign import capi safe "cbits.h show_rect" |
| 118 | + show_rect :: Ptr Rect -> IO () |
| 119 | + |
| 120 | +{------------------------------------------------------------------------------- |
| 121 | + Unions |
| 122 | +-------------------------------------------------------------------------------} |
| 123 | + |
| 124 | +newtype Point_vs_array = Point_vs_array ByteArray |
| 125 | + deriving Storable via SizedByteArray 8 4 |
| 126 | + |
| 127 | +instance HasCField Point_vs_array "as_point" where |
| 128 | + type FieldType Point_vs_array "as_point" = Point |
| 129 | + ptrToField _ = castPtr |
| 130 | + |
| 131 | +instance HasCField Point_vs_array "as_array" where |
| 132 | + type FieldType Point_vs_array "as_array" = ConstantArray 2 CInt |
| 133 | + ptrToField _ = castPtr |
| 134 | + |
| 135 | +instance (HasCField Point_vs_array field, ty ~ FieldType Point_vs_array field) |
| 136 | + => GHC.HasField field (Ptr Point_vs_array) (Ptr ty) where |
| 137 | + getField = ptrToField (Proxy @field) |
| 138 | + |
| 139 | +set_point_vs_array_as_point :: Point -> Point_vs_array |
| 140 | +set_point_vs_array_as_array :: ConstantArray 2 CInt -> Point_vs_array |
| 141 | +get_point_vs_array_as_point :: Point_vs_array -> Point |
| 142 | +get_point_vs_array_as_array :: Point_vs_array -> ConstantArray 2 CInt |
| 143 | + |
| 144 | +get_point_vs_array_as_point = ByteArray.getUnionPayload |
| 145 | +set_point_vs_array_as_point = ByteArray.setUnionPayload |
| 146 | +get_point_vs_array_as_array = ByteArray.getUnionPayload |
| 147 | +set_point_vs_array_as_array = ByteArray.setUnionPayload |
| 148 | + |
0 commit comments