iOS

Mach message结构浅谈

2019-07-12  本文已影响0人  木易林1

基础消息格式:(消息头和消息体)

typedef struct{
    mach_msg_header_t       header;
    mach_msg_body_t         body;
} mach_msg_base_t;

消息头mach_msg_header_t

typedef struct{
    mach_msg_bits_t       msgh_bits;
    mach_msg_size_t       msgh_size;
    mach_port_t           msgh_remote_port;
    mach_port_t           msgh_local_port;
    mach_port_name_t      msgh_voucher_port;
    mach_msg_id_t         msgh_id;
} mach_msg_header_t;

消息体mach_msg_body_t

typedef struct{
    mach_msg_size_t msgh_descriptor_count;
} mach_msg_body_t;

消息尾mach_msg_trailer_t

typedef struct{
    mach_msg_trailer_type_t       msgh_trailer_type;
    mach_msg_trailer_size_t       msgh_trailer_size;
} mach_msg_trailer_t;

消息描述

typedef union{
    mach_msg_port_descriptor_t            port;
    mach_msg_ool_descriptor_t             out_of_line;
    mach_msg_ool_ports_descriptor_t       ool_ports;
    mach_msg_type_descriptor_t            type;
} mach_msg_descriptor_t;

mach_msg_port_descriptor_t (结构如下):
typedef struct{
    mach_port_t                   name;
// Pad to 8 bytes everywhere except the K64 kernel where mach_port_t is 8 bytes
    mach_msg_size_t               pad1;
    unsigned int                  pad2 : 16;
    mach_msg_type_name_t          disposition : 8;
    mach_msg_descriptor_type_t    type : 8;
} mach_msg_port_descriptor_t;


mach_msg_ool_descriptor_t (结构如下):
typedef struct{
    void*                         address;
#if !defined(__LP64__)
    mach_msg_size_t               size;
#endif
    boolean_t                     deallocate: 8;
    mach_msg_copy_options_t       copy: 8;
    unsigned int                  pad1: 8;
    mach_msg_descriptor_type_t    type: 8;
#if defined(__LP64__)
    mach_msg_size_t               size;
#endif
} mach_msg_ool_descriptor_t;



mach_msg_ool_ports_descriptor_t (结构如下):
typedef struct{
    void*                         address;
#if !defined(__LP64__)
    mach_msg_size_t               count;
#endif
    boolean_t                     deallocate: 8;
    mach_msg_copy_options_t       copy: 8;
    mach_msg_type_name_t          disposition : 8;
    mach_msg_descriptor_type_t    type : 8;
#if defined(__LP64__)
    mach_msg_size_t               count;
#endif
} mach_msg_ool_ports_descriptor_t;


mach_msg_type_descriptor_t (结构如下):
typedef struct{
    natural_t                     pad1;
    mach_msg_size_t               pad2;
    unsigned int                  pad3 : 24;
    mach_msg_descriptor_type_t    type : 8;
} mach_msg_type_descriptor_t;

Mach是iOS的XNU内核中最为核心的部分,称为核心中的核心。
Mach中,所有东西都是通过自己的对象实现的。进程、线程和虚拟内存都是对象,所有的对象都有自己的属性。Mach采用消息传递的方式实现对象与对象之间的通信。Mach对象不能直接调用另一个对象,只能传递消息,源对象发送一条消息,这条消息加入到目标对象的队列中等待处理。如果产生应答,则通过另一条消息传递回去,消息遵从FIFO方式。

上一篇 下一篇

猜你喜欢

热点阅读