ssize_t與size_t的前世今生


Ssize_t 與size_t

跟蹤linux源碼得到以下宏:

 

#ifndef _SIZE_T

#define _SIZE_T

typedef __kernel_size_t         size_t;

#endif

 

#ifndef _SSIZE_T

#define _SSIZE_T

typedef __kernel_ssize_t       ssize_t;

#endif

 

在不同平台上,其具有不同的定義:

/* sparc 64 bit */

typedef unsigned long          __kernel_size_t;

typedef long                   __kernel_ssize_t;

 

/* sparc 32 bit */

typedef unsigned int           __kernel_size_t;

typedef int                    __kernel_ssize_t;

這就是兩個類型的實際定義,從字面上意思來說:

       Ssize_t = signed size_t

 

使用位置:

       在對於緩沖區大小等等非負值的長度時一般使用 size_t;

       而對於像

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

       ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);

       ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);

       ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);

可能失敗返回< 0的函數時,則使用ssize_t;

 

ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);

另外相似的一個類型: loff_t

定義如下:

       #if defined(__GNUC__)

typedef __kernel_loff_t         loff_t;

#endif

是一個long long 類型

#ifdef __GNUC__

typedef long long       __kernel_loff_t;

#endif

 

注重細節,總有一天會成功!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM