【发布时间】:2015-02-11 16:53:56
【问题描述】:
我在 C++ 中定义了这个宏;
#define EXT_FIRST_EXTENT(__hdr__) \
((struct ext4_extent *) (((char *) (__hdr__)) + \
sizeof(struct ext4_extent_header)))
ext4_extent_header 是一个结构体;
typedef struct ext4_extent_header {
uint16_t eh_magic; /* probably will support different formats */
uint16_t eh_entries; /* number of valid entries */
uint16_t eh_max; /* capacity of store in entries */
uint16_t eh_depth; /* has tree real underlying blocks? */
uint32_t eh_generation; /* generation of the tree */
}__attribute__ ((__packed__)) EXT4_EXTENT_HEADER;
而ext4_extent也是一个struct;
typedef struct ext4_extent {
uint32_t ee_block; /* first logical block extent covers */
uint16_t ee_len; /* number of blocks covered by extent */
uint16_t ee_start_hi; /* high 16 bits of physical block */
uint32_t ee_start_lo; /* low 32 bits of physical block */
} __attribute__ ((__packed__)) EXT4_EXTENT;
这是我在 Delphi 中编写的尝试;
function Ext2Partition.EXT_First_Extent(hdr: PExt4_extent_header):PExt4_ExtEnt;
begin
Result := PExt4_ExtEnt(hdr + sizeof(ext4_extent_header));
end;
但是编译器告诉我 Operator 不适用于此操作数类型。
这是我将 c++ 结构转换为 Delphi 记录的 ext4_extent_header 和 ext4_extent;
Type
PExt4_extent_header = ^Ext4_extent_header;
Ext4_extent_header = REcord
eh_magic : Word;
eh_entries : Word;
eh_max : Word;
eh_depth : Word;
eh_generation : Cardinal;
End;
Type
PExt4_ExtEnt = ^Ext4_ExtEnt;
Ext4_ExtEnt = Record
ee_block : Cardinal;
ee_len : Word;
ee_start_hi : Word;
ee_start_low : Cardinal;
End;
谢谢!
【问题讨论】:
-
试试
Cardinal(hdr) + sizeof...。 -
由于 Pascal 是一种强类型语言,您必须将
ptr显式转换为Cardinal类型(或转换为UIntPtr类型以更方便):Result := PExt4_ExtEnt(UIntPtr(hdr) + sizeof(ext4_extent_header)); -
另外你必须使用
packed record而不是record,因为源C代码中有__packed__指令。