withAddrValIO :: VSpace -> Address -> (MDB_val -> IO a) -> IO a
withAddrValIO vc addr action =
    alloca $ \ pAddr ->
    poke pAddr addr >>
    let vAddr = MDB_val { mv_data = castPtr pAddr
                        , mv_size = fromIntegral (sizeOf addr)
                        }
    in
    withRdOnlyTxn vc $ \ txn ->
    mdb_get' txn (vcache_db_memory vc) vAddr >>= \case
        Just vData -> action vData -- found data in database (ideal)
        Nothing -> -- since not in the database, try the allocator
            let ff = Map.lookup addr . alloc_list in
            readMVar (vcache_memory vc) >>= \ memory ->
            let ac = mem_alloc memory in
            case allocFrameSearch ff ac of
                Just _data -> withByteStringVal _data action -- found data in allocator
                Nothing -> fail $ "VCache: address " ++ show addr ++ " is undefined!"

withAddrValIO :: VSpace -> Address -> (MDB_val -> IO a) -> IO a
withAddrValIO vc addr action =
    alloca $ \ pAddr ->
    poke pAddr addr >>
    let vAddr = MDB_val { mv_data = castPtr pAddr
                        , mv_size = fromIntegral (sizeOf addr)
                        }
    in
    withRdOnlyTxn vc $ \ txn ->
    mdb_get' txn (vcache_db_memory vc) vAddr >>= \case
        Just vData -> action vData -- found data in database (ideal)
        Nothing -> -- since not in the database, try the allocator
            let ff = Map.lookup addr . alloc_list in
            readMVar (vcache_memory vc) >>= \ memory ->
            let ac = mem_alloc memory in
            case allocFrameSearch ff ac of
                Just _data -> withByteStringVal _data action -- found data in allocator
                Nothing -> fail $ "VCache: address " ++ show addr ++ " is undefined!"

withAddrValIO vc addr action =
    alloca $ \ pAddr ->
    poke pAddr addr >>
    let vAddr = MDB_val { mv_data = castPtr pAddr
                        , mv_size = fromIntegral (sizeOf addr)
                        }
    in
    withRdOnlyTxn vc $ \ txn ->
    mdb_get' txn (vcache_db_memory vc) vAddr >>= \case
        Just vData -> action vData -- found data in database (ideal)
        Nothing -> -- since not in the database, try the allocator
            let ff = Map.lookup addr . alloc_list in
            readMVar (vcache_memory vc) >>= \ memory ->
            let ac = mem_alloc memory in
            case allocFrameSearch ff ac of
                Just _data -> withByteStringVal _data action -- found data in allocator
                Nothing -> fail $ "VCache: address " ++ show addr ++ " is undefined!"
