2012-04-09 93 views
5

我發現這段代碼在哈斯克爾sendfile包:'#type'在Haskell外部函數接口中的含義是什麼?

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 

splice.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)

+5

http://www.haskell.org/ghc/docs/latest/html/users_guide/hsc2hs.html – sdcvvc 2012-04-09 16:39:12

+2

我應該指出,這個外國進口應該不**標記爲「不安全」。當一個外部函數標記爲'unsafe'時,它可以阻止其他線程運行(在用''thread''編譯的GHC程序中)。 – 2012-04-09 17:46:32

+0

@JoeyAdams哦,非常感謝你指出了這一點!我正在研究我的代理服務器的管道拼接網絡套接字的優化實現。我希望在Linux上使用而不是讀/寫循環我仍然會繼續在其他操作系統上使用。 – 2012-04-09 18:46:26

回答

5

正如scdvvc指出的那樣,這種使用由hsc2hs定義的C預處理宏特別定製代碼到系統它被編譯上。您需要使用hsc2hs來獲取爲您的代碼定義的適當的宏。

+0

哇感謝您的快速回答:) – 2012-04-09 16:42:20

+0

順便說一句,誰/什麼是'scdvvc'? – 2012-04-09 16:51:51

+2

你原來的問題上的評論者,坦率地說,值得我比我更多的信用。 ;) – 2012-04-09 16:53:52

相關問題