2016-09-24 22 views
0

使用從apple's open source repo最新消息透露我已經衍化爲「STAT」結構的結構如下(以去語法):爲什麼64位OSX上stat的st_size字段偏移96並且可以計算出來?

type timespec struct { 
    tv_sec  int32 
    tv_nsec  uint32 
} 

type stat64 struct { 
    st_dev  int32   /* [XSI] ID of device containing file */ 
    st_mode  uint16   /* [XSI] Mode of file (see below) */ 
    st_nlink uint16   /* [XSI] Number of hard links */ 
    st_ino  uint64   /* [XSI] File serial number */ 
    st_uid  uint32   /* [XSI] User ID of the file */ 
    st_gid  uint32   /* [XSI] Group ID of the file */ 
    st_rdev  int32   /* [XSI] Device ID */ 

    st_atimespec  timespec /* time of last access */ 
    st_mtimespec  timespec /* time of last data modification */ 
    st_ctimespec  timespec /* time of last status change */ 
    st_birthtimespec timespec /* time of file creation(birth) */ 

    st_size  int64   /* [XSI] file size, in bytes */ 
    st_blocks int64   /* [XSI] blocks allocated for file */ 
    st_blksize int32   /* [XSI] optimal blocksize for I/O */ 
    st_flags uint32   /* user defined flags for file */ 
    st_gen  uint32   /* file generation number */ 
    st_lspare int32   /* RESERVED: DO NOT USE! */ 
    st_qspare [2]int64  /* RESERVED: DO NOT USE! */ 
} 

但在實踐中證明st_size有96個字節,而不是60的偏移如上所示。這種差異的原因是什麼?如何從原始源代碼中看到這一點?

回答

1

在OS X上,struct timespec的兩個字段都是long,它在通常的LP64約定中爲64位。因此,sizeof(struct timespec) == 16(您可以自己檢查),它在64位邊界上對齊,爲st_size提供96的偏移量。

相關問題