Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Hercules.CNix.Memory
Description
Memory management utilities.
Synopsis
- class Delete a where
- withDelete :: Delete a => IO (Ptr a) -> (Ptr a -> IO b) -> IO b
- class Finalizer a where
- finalizer :: FinalizerPtr a
- toForeignPtr :: Finalizer a => Ptr a -> IO (ForeignPtr a)
- forNonNull :: Applicative m => Ptr a -> (Ptr a -> m b) -> m (Maybe b)
- traverseNonNull :: Applicative m => (Ptr a -> m b) -> Ptr a -> m (Maybe b)
Free after use
Types whose memory / resources can be freed in a consistent way.
Instances
Delete CStdString Source # | |
Defined in Hercules.CNix.Std.String | |
Delete DerivationInputsIterator Source # | |
Defined in Hercules.CNix.Store | |
Delete DerivationOutputsIterator Source # | |
Defined in Hercules.CNix.Store | |
Delete StringPairs Source # | |
Defined in Hercules.CNix.Store | |
Delete StringPairsIterator Source # | |
Defined in Hercules.CNix.Store | |
Delete Strings Source # | |
Delete (Ref NixStore) Source # | |
withDelete :: Delete a => IO (Ptr a) -> (Ptr a -> IO b) -> IO b Source #
Obtain a pointer to a resource and run an action with it.
Free on GC
class Finalizer a where Source #
Like Delete
, but the design of finalizers favors that we implement it
by means of a function pointer instead of a Haskell function. That way, it
can be run during GC, without the need for a separate thread and such.
NOTE: This should always return a CAF, to avoid repeated allocation, initialization, etc.
Example:
instance Finalizer CStdString where finalizer = finalize finalize :: FinalizerPtr CStdString {-# NOINLINE finalize #-} finalize = unsafePerformIO [C.exp| void (*)(std::string *) { [](std::string *v) { delete v; } } |]
Methods
finalizer :: FinalizerPtr a Source #
Instances
Finalizer CStdString Source # | |
Defined in Hercules.CNix.Std.String Methods | |
Finalizer Derivation Source # | |
Defined in Hercules.CNix.Store Methods | |
Finalizer NixStorePath Source # | |
Defined in Hercules.CNix.Store Methods | |
Finalizer NixStorePathWithOutputs Source # | |
Defined in Hercules.CNix.Store Methods | |
Finalizer SecretKey Source # | |
Defined in Hercules.CNix.Store Methods | |
Finalizer Strings Source # | |
Defined in Hercules.CNix.Store Methods | |
Finalizer (Ref ValidPathInfo) Source # | |
Defined in Hercules.CNix.Store Methods |
toForeignPtr :: Finalizer a => Ptr a -> IO (ForeignPtr a) Source #
Construct a ForeignPtr
using finalizer
.
This takes ownership of the pointer, so it must only be called once per pointer.
Nullable pointers
forNonNull :: Applicative m => Ptr a -> (Ptr a -> m b) -> m (Maybe b) Source #
Run an action with a pointer, if it is not nullPtr
.
Same as flip
traverseNonNull
.
traverseNonNull :: Applicative m => (Ptr a -> m b) -> Ptr a -> m (Maybe b) Source #
Turn an action on pointer into an action that returns Nothing
iff the pointer is nullPtr
.
Same as flip
forNonNull
.