Skip to content

Commit bd48858

Browse files
authored
Merge pull request #146 from lukasz-golebiewski/use-foreign-concurrent
Use Foreign.Concurrent.newForeignPtr where applicable
2 parents 5f970eb + 1441fb8 commit bd48858

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

src/Kafka/Internal/RdKafka.chs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ import qualified Data.Text as Text
88
import Control.Monad (liftM)
99
import Data.Int (Int32, Int64)
1010
import Data.Word (Word8)
11+
import Foreign.Concurrent (newForeignPtr)
1112
import Foreign.Marshal.Alloc (alloca, allocaBytes)
1213
import Foreign.Marshal.Array (peekArray, allocaArray)
1314
import Foreign.Storable (Storable(..))
1415
import Foreign.Ptr (Ptr, FunPtr, castPtr, nullPtr)
15-
import Foreign.ForeignPtr (FinalizerPtr, addForeignPtrFinalizer, withForeignPtr, newForeignPtr, newForeignPtr_)
16+
import Foreign.ForeignPtr (FinalizerPtr, addForeignPtrFinalizer, newForeignPtr_, withForeignPtr)
1617
import Foreign.C.Error (Errno(..), getErrno)
1718
import Foreign.C.String (CString, newCString, withCAString, peekCAString, peekCAStringLen, peekCString)
1819
import Foreign.C.Types (CFile, CInt(..), CSize, CChar)
@@ -267,12 +268,14 @@ instance Storable RdKafkaMetadataT where
267268
{`Int'} -> `RdKafkaTopicPartitionListTPtr' #}
268269

269270
foreign import ccall unsafe "rdkafka.h &rd_kafka_topic_partition_list_destroy"
270-
rdKafkaTopicPartitionListDestroy :: FinalizerPtr RdKafkaTopicPartitionListT
271+
rdKafkaTopicPartitionListDestroyF :: FinalizerPtr RdKafkaTopicPartitionListT
272+
foreign import ccall unsafe "rdkafka.h rd_kafka_topic_partition_list_destroy"
273+
rdKafkaTopicPartitionListDestroy :: Ptr RdKafkaTopicPartitionListT -> IO ()
271274

272275
newRdKafkaTopicPartitionListT :: Int -> IO RdKafkaTopicPartitionListTPtr
273276
newRdKafkaTopicPartitionListT size = do
274277
ret <- rdKafkaTopicPartitionListNew size
275-
addForeignPtrFinalizer rdKafkaTopicPartitionListDestroy ret
278+
addForeignPtrFinalizer rdKafkaTopicPartitionListDestroyF ret
276279
return ret
277280

278281
{# fun rd_kafka_topic_partition_list_add as ^
@@ -287,7 +290,7 @@ newRdKafkaTopicPartitionListT size = do
287290
copyRdKafkaTopicPartitionList :: RdKafkaTopicPartitionListTPtr -> IO RdKafkaTopicPartitionListTPtr
288291
copyRdKafkaTopicPartitionList pl = do
289292
cp <- rdKafkaTopicPartitionListCopy pl
290-
addForeignPtrFinalizer rdKafkaTopicPartitionListDestroy cp
293+
addForeignPtrFinalizer rdKafkaTopicPartitionListDestroyF cp
291294
return cp
292295

293296
{# fun rd_kafka_topic_partition_list_set_offset as ^
@@ -560,7 +563,7 @@ rdKafkaConsumeBatchQueue :: RdKafkaQueueTPtr -> Int -> Int -> IO [RdKafkaMessage
560563
rdKafkaConsumeBatchQueue qptr timeout batchSize = do
561564
allocaArray batchSize $ \pArr -> do
562565
rSize <- rdKafkaConsumeBatchQueue' qptr timeout pArr (fromIntegral batchSize)
563-
peekArray (fromIntegral rSize) pArr >>= traverse newForeignPtr_
566+
peekArray (fromIntegral rSize) pArr >>= traverse (flip newForeignPtr (return ()))
564567

565568
-------------------------------------------------------------------------------------------------
566569
---- High-level KafkaConsumer
@@ -582,7 +585,7 @@ rdKafkaSubscription k = do
582585
(err, sub) <- rdKafkaSubscription' k
583586
case err of
584587
RdKafkaRespErrNoError ->
585-
Right <$> newForeignPtr rdKafkaTopicPartitionListDestroy sub
588+
Right <$> newForeignPtr sub (rdKafkaTopicPartitionListDestroy sub)
586589
e -> return (Left e)
587590

588591
{#fun rd_kafka_consumer_poll as ^
@@ -614,7 +617,7 @@ rdKafkaAssignment k = do
614617
(err, ass) <- rdKafkaAssignment' k
615618
case err of
616619
RdKafkaRespErrNoError ->
617-
Right <$> newForeignPtr rdKafkaTopicPartitionListDestroy ass
620+
Right <$> newForeignPtr ass (rdKafkaTopicPartitionListDestroy ass)
618621
e -> return (Left e)
619622

620623
{#fun rd_kafka_commit as ^
@@ -721,7 +724,10 @@ instance Storable RdKafkaGroupListT where
721724
-> `RdKafkaRespErrT' cIntToEnum #}
722725

723726
foreign import ccall "rdkafka.h &rd_kafka_group_list_destroy"
724-
rdKafkaGroupListDestroy :: FinalizerPtr RdKafkaGroupListT
727+
rdKafkaGroupListDestroyF :: FinalizerPtr RdKafkaGroupListT
728+
729+
foreign import ccall "rdkafka.h rd_kafka_group_list_destroy"
730+
rdKafkaGroupListDestroy :: Ptr RdKafkaGroupListT -> IO ()
725731

726732
rdKafkaListGroups :: RdKafkaTPtr -> Maybe String -> Int -> IO (Either RdKafkaRespErrT RdKafkaGroupListTPtr)
727733
rdKafkaListGroups k g t = case g of
@@ -731,7 +737,7 @@ rdKafkaListGroups k g t = case g of
731737
listGroups grp = do
732738
(err, res) <- rdKafkaListGroups' k grp t
733739
case err of
734-
RdKafkaRespErrNoError -> Right <$> newForeignPtr rdKafkaGroupListDestroy res
740+
RdKafkaRespErrNoError -> Right <$> newForeignPtr res (rdKafkaGroupListDestroy res)
735741
e -> return $ Left e
736742
-------------------------------------------------------------------------------------------------
737743

@@ -910,15 +916,15 @@ rdKafkaConsumeStop topicPtr partition = do
910916
alloca- `Ptr RdKafkaMetadataT' peekPtr*, `Int'}
911917
-> `RdKafkaRespErrT' cIntToEnum #}
912918

913-
foreign import ccall unsafe "rdkafka.h &rd_kafka_metadata_destroy"
914-
rdKafkaMetadataDestroy :: FinalizerPtr RdKafkaMetadataT
919+
foreign import ccall unsafe "rdkafka.h rd_kafka_metadata_destroy"
920+
rdKafkaMetadataDestroy :: Ptr RdKafkaMetadataT -> IO ()
915921

916922
rdKafkaMetadata :: RdKafkaTPtr -> Bool -> Maybe RdKafkaTopicTPtr -> Int -> IO (Either RdKafkaRespErrT RdKafkaMetadataTPtr)
917923
rdKafkaMetadata k allTopics mt timeout = do
918924
tptr <- maybe (newForeignPtr_ nullPtr) pure mt
919925
(err, res) <- rdKafkaMetadata' k allTopics tptr timeout
920926
case err of
921-
RdKafkaRespErrNoError -> Right <$> newForeignPtr rdKafkaMetadataDestroy res
927+
RdKafkaRespErrNoError -> Right <$> newForeignPtr res (rdKafkaMetadataDestroy res)
922928
e -> return (Left e)
923929

924930
{#fun rd_kafka_poll as ^

0 commit comments

Comments
 (0)