~alcinnz/harfbuzz-pure

ref: b7564ba8f687c05b74999b6f480f68de1b6f1d43 harfbuzz-pure/Data/Text/Glyphize/Oom.hs -rw-r--r-- 531 bytes
b7564ba8 — Adrian Cochrane Optimize Harfbuzz-Pure. 1 year, 1 month ago
                                                                                
28567186 Adrian Cochrane
2eb97269 Adrian Cochrane
28567186 Adrian Cochrane
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
module Data.Text.Glyphize.Oom where

import Control.Exception
import Foreign.Ptr (nullPtr, Ptr)

-- | Indicates that Harfbuzz has ran out of memory during a computation.
-- Should be extremely rare!
data HarfbuzzError = OutOfMemory deriving (Show)
instance Exception HarfbuzzError

throwFalse :: IO Bool -> IO ()
throwFalse cb = do
    ret <- cb
    if ret then return () else throwIO OutOfMemory

throwNull :: IO (Ptr a) -> IO (Ptr a)
throwNull cb = do
    ptr <- cb
    if ptr == nullPtr then throwIO OutOfMemory else return ptr