【问题标题】:What does '#type' mean in Haskell Foreign Function Interface?Haskell 外部函数接口中的“#type”是什么意思?
【发布时间】:2012-04-22 00:23:51
【问题描述】:

我在 Haskell sendfile 包中找到了这段代码:

http://patch-tag.com/r/mae/sendfile/snapshot/current/content/pretty/src/Network/Socket/SendFile/Linux.hsc

-- sendfile64 gives LFS support
foreign import ccall unsafe "sendfile64" c_sendfile
  :: Fd -> Fd -> Ptr (#type off64_t) -> (#type size_t) -> IO (#type ssize_t)

1) #type 是什么意思以及 2) 为什么会出现此错误,

[1 of 1] Compiling Linux.Splice     ( splice.hs, splice.o )

splice.hs:40:12: parse error on input `type'

当我自己尝试如下使用它时?:

ghc --make splice.hs

拼接.hs:

{-# LANGUAGE ForeignFunctionInterface #-}

module Linux.Splice where

import Data.Word
import System.Posix.Types

-- SPLICE

 -- fcntl.h
 -- ssize_t splice(
 --   int          fd_in,
 --   loff_t*      off_in,
 --   int          fd_out,
 --   loff_t*      off_out,
 --   size_t       len,
 --   unsigned int flags
 -- );

foreign import ccall unsafe "fnctl.h splice" c_splice
  :: Fd
  -> Ptr (#type {- < parse error -} loff_t)
  -> Fd
  -> Ptr (#type loff_t)
  -> (#type size_t)
  -> Word
  -> IO (#type ssize_t)

(使用 GHC 7.4.x)

【问题讨论】:

  • 我应该指出,这个外国进口应该被标记为unsafe。当标记为unsafe 的外部函数阻塞时,它可以阻止其他线程运行(在使用-threaded 的GHC 编译的程序中)。
  • @JoeyAdams 哦,非常感谢您指出这一点!我正在为我的代理服务器进行网络套接字的管道拼接的完美实现。我希望在 Linux 上使用它而不是读/写循环,我暂时仍会继续在其他操作系统上使用。

标签: c haskell types ffi preprocessor-directive


【解决方案1】:

正如 scdvvc 所指出的,它使用 hsc2hs 定义的 C 预处理宏来专门针对正在编译的系统定制代码。您需要使用 hsc2hs 来获取为您的代码定义的适当宏。

【讨论】:

  • 您原始问题的评论者,坦率地说,他应该得到比我更多的信任。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-31
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-08-20
相关资源
最近更新 更多