<commit_before>{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Anemone.Foreign.Mempool (
    Mempool
  , create
  , alloc
  , allocBytes
  , calloc
  , callocBytes
  , free
  ) where

import           Anemone.Foreign.Data

import           Foreign.C.Types ( CSize(..) )
import           Foreign.Ptr
import           Foreign.Storable ( Storable(..) )

import           P

import           System.IO (IO)

import qualified Prelude as Savage


newtype Mempool =
  Mempool (Ptr ())

foreign import ccall unsafe "anemone_mempool_create"
  create :: IO Mempool

foreign import ccall unsafe "hs_anemone_mempool_alloc"
  allocBytes :: Mempool -> CSize -> IO (Ptr a)

foreign import ccall unsafe "hs_anemone_mempool_calloc"
  callocBytes :: Mempool -> CSize -> CSize -> IO (Ptr a)

foreign import ccall unsafe "anemone_mempool_free"
  free :: Mempool -> IO ()

alloc :: forall a. Storable a => Mempool -> IO (Ptr a)
alloc mp =
  allocBytes mp (fromIntegral $ sizeOf (Savage.undefined :: a))

calloc :: forall a. Storable a => Mempool -> CSize -> IO (Ptr a)
calloc mp i =
  callocBytes mp i (fromIntegral $ sizeOf (Savage.undefined :: a))

<commit_msg>Expose Mempool constructor, but only for FFI<commit_after>{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Anemone.Foreign.Mempool (
    Mempool(..)
  , create
  , alloc
  , allocBytes
  , calloc
  , callocBytes
  , free
  ) where

import           Anemone.Foreign.Data

import           Foreign.C.Types ( CSize(..) )
import           Foreign.Ptr
import           Foreign.Storable ( Storable(..) )

import           P

import           System.IO (IO)

import qualified Prelude as Savage


-- | The Mempool constructor must be exposed to work in FFI calls.
-- However, it must not be used in any other places.
-- The only way to construct a Mempool is by calling @create@.
--
-- This infinite knot here should enforce this.
newtype Mempool =
  Mempool (Ptr Mempool)

foreign import ccall unsafe "anemone_mempool_create"
  create :: IO Mempool

foreign import ccall unsafe "hs_anemone_mempool_alloc"
  allocBytes :: Mempool -> CSize -> IO (Ptr a)

foreign import ccall unsafe "hs_anemone_mempool_calloc"
  callocBytes :: Mempool -> CSize -> CSize -> IO (Ptr a)

foreign import ccall unsafe "anemone_mempool_free"
  free :: Mempool -> IO ()

alloc :: forall a. Storable a => Mempool -> IO (Ptr a)
alloc mp =
  allocBytes mp (fromIntegral $ sizeOf (Savage.undefined :: a))

calloc :: forall a. Storable a => Mempool -> CSize -> IO (Ptr a)
calloc mp i =
  callocBytes mp i (fromIntegral $ sizeOf (Savage.undefined :: a))


{'fraction_non_alphanumeric': 0.10318047337278106, 'fraction_numerical': 0.0, 'mean_word_length': 3.0737951807228914, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3285617', 'n_tokens_mistral': 928, 'n_tokens_neox': 885, 'n_words': 280}