On Fri, Dec 06, 2024 at 02:13:57PM -0500, Paolo Bonzini wrote:
> Date: Fri, 6 Dec 2024 14:13:57 -0500
> From: Paolo Bonzini <pbonz...@redhat.com>
> Subject: Re: [RFC 06/13] rust: add bindings for memattrs
> 
> Il ven 6 dic 2024, 09:42 Peter Maydell <peter.mayd...@linaro.org> ha
> scritto:
> 
> > On Fri, 6 Dec 2024 at 14:28, Paolo Bonzini <pbonz...@redhat.com> wrote:
> > > Yes, hence "decently" packed. But I think in both cases it's passed in
> > registers, and for 64-bit machine that shouldn't change anything.
> >
> > True. Though it does mean we go from "space to add new fields
> > without making it overflow from one register to two" to
> > "completely full and no space for expanding it".
> >
> 
> I guess it's enough to make unspecified the only non-bitfield. Then you can
> declare MEMTXATTRS_UNSPECIFIED as { unspecified: true, ..Zeroable::ZERO }
> ("start with Zeroable::ZERO and change unspecified to true"). For the rest
> it is not important to make them available in a "const".
>

Sorry I missed this comment before...

Now I have a MemTxAttrs like,

typedef struct MemTxAttrs {
    unsigned int secure:1;
    unsigned int space:2;
    unsigned int user:1;
    unsigned int memory:1;
    unsigned int requester_id:16;
    unsigned int pid:8;
    bool unspecified;
    uint8_t _reserved1;
    uint16_t _reserved2;
} MemTxAttrs;

and its binding is,

#[repr(C)]
#[repr(align(4))]
#[derive(Debug, Default, Copy, Clone)]
pub struct MemTxAttrs {
    pub _bitfield_align_1: [u16; 0],
    pub _bitfield_1: __BindgenBitfieldUnit<[u8; 4usize]>,
    pub unspecified: bool,
    pub _reserved1: u8,
    pub _reserved2: u16,
}

unfortunately, Zeroable can't be applied to __BindgenBitfieldUnit since
event its member (`storage`) is private :-(.

But there's a solution to force (and at the same time unsafely) ZERO the
entire structure in const:

 * const_zero macro: https://docs.rs/const-zero/latest/const_zero/


With const_zero, we can implement Zeroable for MemTxAttrs:

unsafe impl Zeroable for MemTxAttrs {
    const ZERO: Self = unsafe {const_zero!(MemTxAttrs)};
}

pub static MEMTXATTRS_UNSPECIFIED: MemTxAttrs = MemTxAttrs {
    unspecified: true,
    ..Zeroable::ZERO
};


So do you like this idea? If so, I can be a mover to introduce
const_zero macro.

Thanks,
Zhao


Reply via email to